Skip to content

Commit 3eef289

Browse files
committed
feat(trigonometry): add InvertedPI and switch angle conversions to fixed-point math
1 parent 8f94777 commit 3eef289

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

AGENTS.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
- Serialization compatibility is intentional and now uses MemoryPack:
2929
- MemoryPack attributes on serializable structs (for example `[MemoryPackable]`, `[MemoryPackInclude]`) are the source of truth for serialized layouts.
3030
- Tests use MemoryPack-based roundtrips (and `System.Text.Json` where appropriate) instead of legacy `MessagePack`/`BinaryFormatter` serializers.
31-
- `ThreadLocalRandom` is marked `[Obsolete]`; new deterministic RNG work should prefer `DeterministicRandom` and `DeterministicRandom.FromWorldFeature(...)` in `src/FixedMathSharp/Utility/DeterministicRandom.cs`.
3231

3332
## Testing patterns to mirror
3433

src/FixedMathSharp/Core/FixedTrigonometry.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,30 @@ public static partial class FixedMath
2121
};
2222

2323
// Trigonometric and logarithmic constants
24-
internal const double PI_D = 3.14159265358979323846;
25-
public static readonly Fixed64 PI = (Fixed64)PI_D;
26-
public static readonly Fixed64 TwoPI = PI * 2;
27-
public static readonly Fixed64 PiOver2 = PI / 2;
28-
public static readonly Fixed64 PiOver3 = PI / 3;
29-
public static readonly Fixed64 PiOver4 = PI / 4;
30-
public static readonly Fixed64 PiOver6 = PI / 6;
31-
public static readonly Fixed64 Ln2 = (Fixed64)0.6931471805599453; // Natural logarithm of 2
24+
public static readonly Fixed64 PI = (Fixed64)3.14159265358979323846;
3225

26+
public static readonly Fixed64 TwoPI = PI * Fixed64.Two;
27+
public static readonly Fixed64 PiOver2 = PI / new Fixed64(2);
28+
public static readonly Fixed64 PiOver3 = PI / new Fixed64(3);
29+
public static readonly Fixed64 PiOver4 = PI / new Fixed64(4);
30+
public static readonly Fixed64 PiOver6 = PI / new Fixed64(6);
31+
public static readonly Fixed64 InvertedPI = Fixed64.One / PI;
32+
33+
public static readonly Fixed64 OneEighty = new Fixed64(180);
34+
35+
/// <summary>
36+
/// Degrees to radians conversion factor (π / 180)
37+
/// </summary>
38+
public static readonly Fixed64 Deg2Rad = PI / OneEighty;
39+
/// <summary>
40+
/// Radians to degrees conversion factor (180 / π)
41+
/// </summary>
42+
public static readonly Fixed64 Rad2Deg = OneEighty / PI;
43+
44+
public static readonly Fixed64 Ln2 = new Fixed64(0.6931471805599453); // Natural logarithm of 2
3345
public static readonly Fixed64 LOG_2_MAX = new Fixed64(63L * ONE_L);
3446
public static readonly Fixed64 LOG_2_MIN = new Fixed64(-64L * ONE_L);
3547

36-
internal const double DEG2RAD_D = 0.01745329251994329576; // π / 180
37-
public static readonly Fixed64 Deg2Rad = (Fixed64)DEG2RAD_D; // Degrees to radians conversion factor
38-
internal const double RAD2DEG_D = 57.2957795130823208767; // 180 / π
39-
public static readonly Fixed64 Rad2Deg = (Fixed64)RAD2DEG_D; // Radians to degrees conversion factor
40-
4148
// Asin Padé approximations
4249
private static readonly Fixed64 PADE_A1 = (Fixed64)0.183320102;
4350
private static readonly Fixed64 PADE_A2 = (Fixed64)0.0218804099;
@@ -249,25 +256,19 @@ public static Fixed64 Sqrt(Fixed64 x)
249256
/// <summary>
250257
/// Converts a value in radians to degrees.
251258
/// </summary>
252-
/// <remarks>
253-
/// Uses double precision to avoid precision loss
254-
/// </remarks>
255259
[MethodImpl(MethodImplOptions.AggressiveInlining)]
256260
public static Fixed64 RadToDeg(Fixed64 rad)
257261
{
258-
return new Fixed64((double)rad * RAD2DEG_D);
262+
return (rad * OneEighty) / PI;
259263
}
260264

261265
/// <summary>
262266
/// Converts a value in degrees to radians.
263267
/// </summary>
264-
/// <remarks>
265-
/// Uses double precision to avoid precision loss
266-
/// </remarks>
267268
[MethodImpl(MethodImplOptions.AggressiveInlining)]
268269
public static Fixed64 DegToRad(Fixed64 deg)
269270
{
270-
return new Fixed64((double)deg * DEG2RAD_D);
271+
return (deg * PI) / OneEighty;
271272
}
272273

273274
/// <summary>
@@ -379,7 +380,7 @@ public static Fixed64 Tan(Fixed64 x)
379380
for (int i = start; i >= 1; i -= 2)
380381
{
381382
denominator = (Fixed64)i - (x2 / denominator);
382-
if ((denominator - prevDenominator).Abs() < Fixed64.Precision)
383+
if ((denominator - prevDenominator).Abs() < Fixed64.Epsilon)
383384
break;
384385
prevDenominator = denominator;
385386
}
@@ -485,7 +486,7 @@ public static Fixed64 Atan(Fixed64 z)
485486
{
486487
term *= zSq;
487488
Fixed64 nextTerm = term / i;
488-
if (nextTerm.Abs() < Fixed64.Precision)
489+
if (nextTerm.Abs() < Fixed64.Epsilon)
489490
break;
490491

491492
result += nextTerm * sign;

0 commit comments

Comments
 (0)