| | | 1 | | using System; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | |
| | | 4 | | namespace FixedMathSharp |
| | | 5 | | { |
| | | 6 | | public static class Fixed64Extensions |
| | | 7 | | { |
| | | 8 | | #region Fixed64 Operations |
| | | 9 | | |
| | | 10 | | /// <inheritdoc cref="Fixed64.Sign(Fixed64)" /> |
| | | 11 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 12 | | public static int Sign(this Fixed64 value) |
| | 6 | 13 | | { |
| | 6 | 14 | | return Fixed64.Sign(value); |
| | 6 | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc cref="Fixed64.IsInteger(Fixed64)" /> |
| | | 18 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 19 | | public static bool IsInteger(this Fixed64 value) |
| | 6 | 20 | | { |
| | 6 | 21 | | return Fixed64.IsInteger(value); |
| | 6 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc cref="FixedMath.Squared(Fixed64)" /> |
| | | 25 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 26 | | public static Fixed64 Squared(this Fixed64 value) |
| | 1 | 27 | | { |
| | 1 | 28 | | return FixedMath.Squared(value); |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc cref="FixedMath.Round(Fixed64, MidpointRounding)" /> |
| | | 32 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 33 | | public static Fixed64 Round(this Fixed64 value, MidpointRounding mode = MidpointRounding.ToEven) |
| | 3 | 34 | | { |
| | 3 | 35 | | return FixedMath.Round(value, mode); |
| | 3 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc cref="FixedMath.RoundToPrecision(Fixed64, int, MidpointRounding)" /> |
| | | 39 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 40 | | public static Fixed64 RoundToPrecision(this Fixed64 value, int places, MidpointRounding mode = MidpointRounding. |
| | 1 | 41 | | { |
| | 1 | 42 | | return FixedMath.RoundToPrecision(value, places, mode); |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc cref="FixedMath.ClampOne(Fixed64)" /> |
| | | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 47 | | public static Fixed64 ClampOne(this Fixed64 f1) |
| | 6 | 48 | | { |
| | 6 | 49 | | return FixedMath.ClampOne(f1); |
| | 6 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc cref="FixedMath.Clamp01(Fixed64)" /> |
| | | 53 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 54 | | public static Fixed64 Clamp01(this Fixed64 f1) |
| | 1 | 55 | | { |
| | 1 | 56 | | return FixedMath.Clamp01(f1); |
| | 1 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc cref="FixedMath.Abs(Fixed64)" /> |
| | | 60 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 61 | | public static Fixed64 Abs(this Fixed64 value) |
| | 1134 | 62 | | { |
| | 1134 | 63 | | return FixedMath.Abs(value); |
| | 1134 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Checks if the absolute value of x is less than y. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="x">The value to compare.</param> |
| | | 70 | | /// <param name="y">The comparison threshold.</param> |
| | | 71 | | /// <returns>True if |x| < y; otherwise false.</returns> |
| | | 72 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 73 | | public static bool AbsLessThan(this Fixed64 x, Fixed64 y) |
| | 1 | 74 | | { |
| | 1 | 75 | | return Abs(x) < y; |
| | 1 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <inheritdoc cref="FixedMath.FastAdd(Fixed64, Fixed64)" /> |
| | | 79 | | public static Fixed64 FastAdd(this Fixed64 a, Fixed64 b) |
| | 1 | 80 | | { |
| | 1 | 81 | | return FixedMath.FastAdd(a, b); |
| | 1 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc cref="FixedMath.FastSub(Fixed64, Fixed64)" /> |
| | | 85 | | public static Fixed64 FastSub(this Fixed64 a, Fixed64 b) |
| | 1 | 86 | | { |
| | 1 | 87 | | return FixedMath.FastSub(a, b); |
| | 1 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc cref="FixedMath.FastMul(Fixed64, Fixed64)" /> |
| | | 91 | | public static Fixed64 FastMul(this Fixed64 a, Fixed64 b) |
| | 1 | 92 | | { |
| | 1 | 93 | | return FixedMath.FastMul(a, b); |
| | 1 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <inheritdoc cref="FixedMath.FastMod(Fixed64, Fixed64)" /> |
| | | 97 | | public static Fixed64 FastMod(this Fixed64 a, Fixed64 b) |
| | 1 | 98 | | { |
| | 1 | 99 | | return FixedMath.FastMod(a, b); |
| | 1 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc cref="FixedMath.Floor(Fixed64)" /> |
| | | 103 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 104 | | public static Fixed64 Floor(this Fixed64 value) |
| | 30 | 105 | | { |
| | 30 | 106 | | return FixedMath.Floor(value); |
| | 30 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <inheritdoc cref="FixedMath.Ceiling(Fixed64)" /> |
| | | 110 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 111 | | public static Fixed64 Ceiling(this Fixed64 value) |
| | 1 | 112 | | { |
| | 1 | 113 | | return FixedMath.Ceiling(value); |
| | 1 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Rounds the Fixed64 value to the nearest integer. |
| | | 118 | | /// </summary> |
| | | 119 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 120 | | public static int RoundToInt(this Fixed64 x) |
| | 6 | 121 | | { |
| | 6 | 122 | | return (int)FixedMath.Round(x); |
| | 6 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Rounds up the Fixed64 value to the nearest integer. |
| | | 127 | | /// </summary> |
| | | 128 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 129 | | public static int CeilToInt(this Fixed64 x) |
| | 1 | 130 | | { |
| | 1 | 131 | | return (int)FixedMath.Ceiling(x); |
| | 1 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Rounds down the Fixed64 value to the nearest integer. |
| | | 136 | | /// </summary> |
| | | 137 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 138 | | public static int FloorToInt(this Fixed64 x) |
| | 1 | 139 | | { |
| | 1 | 140 | | return (int)Floor(x); |
| | 1 | 141 | | } |
| | | 142 | | |
| | | 143 | | #endregion |
| | | 144 | | |
| | | 145 | | #region Conversion |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Converts the Fixed64 value to a string formatted to 2 decimal places. |
| | | 149 | | /// </summary> |
| | | 150 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 151 | | public static string ToFormattedString(this Fixed64 f1) |
| | 1 | 152 | | { |
| | 1 | 153 | | return f1.ToPreciseFloat().ToString("0.##"); |
| | 1 | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Converts the Fixed64 value to a double with specified decimal precision. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="f1">The Fixed64 value to convert.</param> |
| | | 160 | | /// <param name="precision">The number of decimal places to round to.</param> |
| | | 161 | | /// <returns>The formatted double value.</returns> |
| | | 162 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 163 | | public static double ToFormattedDouble(this Fixed64 f1, int precision = 2) |
| | 326 | 164 | | { |
| | 326 | 165 | | return Math.Round((double)f1, precision, MidpointRounding.AwayFromZero); |
| | 326 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Converts the Fixed64 value to a float with 2 decimal points of precision. |
| | | 170 | | /// </summary> |
| | | 171 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 172 | | public static float ToFormattedFloat(this Fixed64 f1) |
| | 2 | 173 | | { |
| | 2 | 174 | | return (float)ToFormattedDouble(f1); |
| | 2 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Converts the Fixed64 value to a precise float representation (without rounding). |
| | | 179 | | /// </summary> |
| | | 180 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 181 | | public static float ToPreciseFloat(this Fixed64 f1) |
| | 7 | 182 | | { |
| | 7 | 183 | | return (float)(double)f1; |
| | 7 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Converts the angle in degrees to radians. |
| | | 188 | | /// </summary> |
| | | 189 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 190 | | public static Fixed64 ToRadians(this Fixed64 angleInDegrees) |
| | 2 | 191 | | { |
| | 2 | 192 | | return FixedMath.DegToRad(angleInDegrees); |
| | 2 | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Converts the angle in radians to degree. |
| | | 197 | | /// </summary> |
| | | 198 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 199 | | public static Fixed64 ToDegree(this Fixed64 angleInRadians) |
| | 1 | 200 | | { |
| | 1 | 201 | | return FixedMath.RadToDeg(angleInRadians); |
| | 1 | 202 | | } |
| | | 203 | | |
| | | 204 | | #endregion |
| | | 205 | | |
| | | 206 | | #region Equality |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Checks if the value is greater than epsilon (positive or negative). |
| | | 210 | | /// Useful for determining if a value is effectively non-zero with a given precision. |
| | | 211 | | /// </summary> |
| | | 212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 213 | | public static bool MoreThanEpsilon(this Fixed64 d) |
| | 2 | 214 | | { |
| | 2 | 215 | | return d.Abs() > Fixed64.Epsilon; |
| | 2 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Checks if the value is less than epsilon (i.e., effectively zero). |
| | | 220 | | /// Useful for determining if a value is close enough to zero with a given precision. |
| | | 221 | | /// </summary> |
| | | 222 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 223 | | public static bool LessThanEpsilon(this Fixed64 d) |
| | 2 | 224 | | { |
| | 2 | 225 | | return d.Abs() < Fixed64.Epsilon; |
| | 2 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Helper method to compare individual vector components for approximate equality, allowing a fractional differ |
| | | 230 | | /// Handles zero components by only using the allowed percentage difference. |
| | | 231 | | /// </summary> |
| | | 232 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 233 | | public static bool FuzzyComponentEqual(this Fixed64 a, Fixed64 b, Fixed64 percentage) |
| | 254 | 234 | | { |
| | 254 | 235 | | var diff = (a - b).Abs(); |
| | 254 | 236 | | var allowedErr = a.Abs() * percentage; |
| | | 237 | | // Compare directly to percentage if a is zero |
| | | 238 | | // Otherwise, use percentage of a's magnitude |
| | 254 | 239 | | return a == Fixed64.Zero ? diff <= percentage : diff <= allowedErr; |
| | 254 | 240 | | } |
| | | 241 | | |
| | | 242 | | #endregion |
| | | 243 | | } |
| | | 244 | | } |