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