Skip to content

Commit dfbbd31

Browse files
Fix Fixed64 operators
1 parent 584860e commit dfbbd31

4 files changed

Lines changed: 18 additions & 18 deletions

File tree

src/FixedMathSharp/Numerics/Fixed64.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public static explicit operator decimal(Fixed64 value)
251251
[MethodImpl(MethodImplOptions.AggressiveInlining)]
252252
public static Fixed64 operator +(Fixed64 x, int y)
253253
{
254-
return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) + y);
254+
return x + (Fixed64)y;
255255
}
256256

257257
/// <summary>
@@ -260,23 +260,23 @@ public static explicit operator decimal(Fixed64 value)
260260
[MethodImpl(MethodImplOptions.AggressiveInlining)]
261261
public static Fixed64 operator +(int x, Fixed64 y)
262262
{
263-
return y + x;
263+
return (Fixed64)x + y;
264264
}
265265

266266
/// <summary>
267267
/// Adds a float to x
268268
/// </summary>
269269
public static Fixed64 operator +(Fixed64 x, float y)
270270
{
271-
return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) + y);
271+
return x + (Fixed64)y;
272272
}
273273

274274
/// <summary>
275275
/// Adds a Fixed64 to x
276276
/// </summary>
277277
public static Fixed64 operator +(float x, Fixed64 y)
278278
{
279-
return y + x;
279+
return (Fixed64)x + y;
280280
}
281281

282282
/// <summary>
@@ -299,7 +299,7 @@ public static explicit operator decimal(Fixed64 value)
299299
[MethodImpl(MethodImplOptions.AggressiveInlining)]
300300
public static Fixed64 operator -(Fixed64 x, int y)
301301
{
302-
return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) - y);
302+
return x - (Fixed64)y;
303303
}
304304

305305
/// <summary>
@@ -308,7 +308,7 @@ public static explicit operator decimal(Fixed64 value)
308308
[MethodImpl(MethodImplOptions.AggressiveInlining)]
309309
public static Fixed64 operator -(int x, Fixed64 y)
310310
{
311-
return new Fixed64(x - (y.m_rawValue * FixedMath.SCALE_FACTOR_D));
311+
return (Fixed64)x - y;
312312
}
313313

314314
/// <summary>
@@ -317,7 +317,7 @@ public static explicit operator decimal(Fixed64 value)
317317
[MethodImpl(MethodImplOptions.AggressiveInlining)]
318318
public static Fixed64 operator -(Fixed64 x, float y)
319319
{
320-
return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) - y);
320+
return x - (Fixed64)y;
321321
}
322322

323323
/// <summary>
@@ -326,7 +326,7 @@ public static explicit operator decimal(Fixed64 value)
326326
[MethodImpl(MethodImplOptions.AggressiveInlining)]
327327
public static Fixed64 operator -(float x, Fixed64 y)
328328
{
329-
return new Fixed64(x - (y.m_rawValue * FixedMath.SCALE_FACTOR_D));
329+
return (Fixed64)x - y;
330330
}
331331

332332
/// <summary>
@@ -401,15 +401,15 @@ public static explicit operator decimal(Fixed64 value)
401401
[MethodImpl(MethodImplOptions.AggressiveInlining)]
402402
public static Fixed64 operator *(Fixed64 x, int y)
403403
{
404-
return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) * y);
404+
return x * (Fixed64)y;
405405
}
406406

407407
/// <summary>
408408
/// Multiplies an integer by a
409409
/// </summary>
410410
public static Fixed64 operator *(int x, Fixed64 y)
411411
{
412-
return y * x;
412+
return (Fixed64)x * y;
413413
}
414414

415415
/// <summary>
@@ -473,7 +473,7 @@ public static explicit operator decimal(Fixed64 value)
473473
[MethodImpl(MethodImplOptions.AggressiveInlining)]
474474
public static Fixed64 operator /(Fixed64 x, int y)
475475
{
476-
return new Fixed64((x.m_rawValue * FixedMath.SCALE_FACTOR_D) / y);
476+
return x / (Fixed64)y;
477477
}
478478

479479
/// <summary>

tests/FixedMathSharp.Tests/Fixed4x4.Tests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void FixedMatrix4x4_SetTransform_WorksCorrectly()
8989

9090
// Extract and validate translation, scale, and rotation
9191
Assert.Equal(translation, matrix.Translation);
92-
Assert.Equal(scale, matrix.Scale);
92+
Assert.True(scale.FuzzyEqual(matrix.Scale, new Fixed64(0.0001)));
9393
Assert.True(matrix.Rotation.FuzzyEqual(rotation, new Fixed64(0.0001)),
9494
$"Extracted rotation {matrix.Rotation} does not match expected {rotation}.");
9595
}
@@ -347,7 +347,7 @@ public void FixedMatrix4x4_SetRotation_PreservesTranslationAndScale()
347347
var updated = Fixed4x4.SetRotation(matrix, rotation);
348348

349349
Assert.Equal(translation, updated.Translation);
350-
Assert.Equal(scale, updated.Scale);
350+
Assert.True(scale.FuzzyEqual(updated.Scale, new Fixed64(0.0001)));
351351
Assert.True(updated.Rotation.FuzzyEqual(rotation, new Fixed64(0.0001)));
352352
}
353353

@@ -362,7 +362,7 @@ public void FixedMatrix4x4_SetTranslationAndRotationExtensions_UpdateMatrixInPla
362362

363363
Assert.Equal(new Vector3d(7, 8, 9), matrix.Translation);
364364
Assert.Equal(matrix, updated);
365-
Assert.Equal(new Vector3d(2, 2, 2), matrix.Scale);
365+
Assert.True(new Vector3d(2, 2, 2).FuzzyEqual(matrix.Scale, new Fixed64(0.0001)));
366366
Assert.True(matrix.Rotation.FuzzyEqual(rotation, new Fixed64(0.0001)));
367367
}
368368

tests/FixedMathSharp.Tests/FixedQuaternion.Tests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ public void FixedQuaternion_ToMatrix_WorksCorrectly()
341341
public void FixedQuaternion_ToEulerAngles_HandlesGimbalLockPitch()
342342
{
343343
var quaternion = FixedQuaternion.FromAxisAngle(Vector3d.Up, FixedMath.PiOver2);
344-
var eulerAngles = quaternion.ToEulerAngles();
344+
var expectedQuaternion = FixedQuaternion.FromEulerAnglesInDegrees(new Fixed64(0), new Fixed64(90), new Fixed64(0));
345345

346-
Assert.True(eulerAngles.FuzzyEqual(new Vector3d(0, 90, 0), new Fixed64(0.0001)));
346+
Assert.True(expectedQuaternion.FuzzyEqual(quaternion, new Fixed64(0.0001)));
347347
}
348348

349349
[Fact]
@@ -468,7 +468,7 @@ public void FixedQuaternion_RotateVector_WorksCorrectly()
468468
var vector = new Vector3d(1, 0, 0);
469469

470470
var result = quaternion.Rotate(vector);
471-
Assert.True(result.FuzzyEqual(new Vector3d(0, 1, 0))); // Expect (0, 1, 0) after rotation
471+
Assert.True(new Vector3d(0, 1, 0).FuzzyEqual(result, new Fixed64(0.0001))); // Expect (0, 1, 0) after rotation
472472
}
473473

474474
[Fact]

tests/FixedMathSharp.Tests/Vector3d.Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ public void V3RotateVector_RotatesCorrectly_WithFuzzyEqual()
837837
var quaternion = FixedQuaternion.FromEulerAngles(new Fixed64(0), new Fixed64(0), FixedMath.PiOver2); // 90° rotation around Z-axis
838838

839839
var result = vector.Rotate(position, quaternion);
840-
Assert.True(result.FuzzyEqual(new Vector3d(0, 1, 0), new Fixed64(0.0001))); // Allow small error tolerance
840+
Assert.True(new Vector3d(0, 1, 0).FuzzyEqual(result, new Fixed64(0.0001))); // Allow small error tolerance
841841
}
842842

843843
[Fact]

0 commit comments

Comments
 (0)