Skip to content

Commit

Permalink
Clean up mathematics
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Oct 26, 2024
1 parent 7460b77 commit d4a143c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions Hypercube.Client/Graphics/Viewports/Camera2D.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Hypercube.Mathematics;
using Hypercube.Mathematics.Matrices;
using Hypercube.Mathematics.Quaternions;
using Hypercube.Mathematics.Transforms;
using Hypercube.Mathematics.Vectors;

Expand Down
17 changes: 9 additions & 8 deletions Hypercube.Mathematics/Extensions/FloatingPointEqualsExtension.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
namespace Hypercube.Mathematics.Extensions;
using JetBrains.Annotations;

namespace Hypercube.Mathematics.Extensions;

[PublicAPI]
public static class FloatingPointEqualsExtension
{
public static bool AboutEquals(this double a, double b, double tolerance = 1E-15d)
public static bool AboutEquals(this float a, float b, float tolerance = 1E-15f)
{
var epsilon = System.Math.Max(System.Math.Abs(a), System.Math.Abs(b)) * 1E-15d;
return System.Math.Abs(a - b) <= epsilon;
return HyperMath.AboutEquals(a, b, tolerance);
}
public static bool AboutEquals(this float a, float b, float tolerance = 1E-15f)

public static bool AboutEquals(this double a, double b, double tolerance = 1E-15d)
{
var epsilon = System.Math.Max(System.Math.Abs(a), System.Math.Abs(b)) * tolerance ;
return System.Math.Abs(a - b) <= epsilon;
return HyperMath.AboutEquals(a, b, tolerance);
}
}
14 changes: 13 additions & 1 deletion Hypercube.Mathematics/HyperMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ public static class HyperMath

public const float RadiansToDegreesF = 180 / PIf;
public const float DegreesToRadiansF = PIf / 180;


public static bool AboutEquals(float a, float b, float tolerance = 1E-15f)
{
var epsilon = Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance ;
return Math.Abs(a - b) <= epsilon;
}

public static bool AboutEquals(double a, double b, double tolerance = 1E-15d)
{
var epsilon = Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance;
return Math.Abs(a - b) <= epsilon;
}

public static byte MoveTowards(byte current, byte target, byte distance)
{
return current < target ?
Expand Down
1 change: 1 addition & 0 deletions Hypercube.Mathematics/Matrices/Matrix4X4.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Hypercube.Mathematics.Quaternions;
using Hypercube.Mathematics.Shapes;
using Hypercube.Mathematics.Transforms;
using Hypercube.Mathematics.Vectors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Hypercube.Mathematics.Vectors;
using JetBrains.Annotations;

namespace Hypercube.Mathematics;
namespace Hypercube.Mathematics.Quaternions;

[StructLayout(LayoutKind.Sequential)]
[PublicAPI, StructLayout(LayoutKind.Sequential)]
public readonly struct Quaternion : IEquatable<Quaternion>
{
private const float SingularityThreshold = 0.4999995f;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using Hypercube.Mathematics.Quaternions;
using Hypercube.Mathematics.Vectors;

namespace Hypercube.Mathematics.Transforms;
Expand Down
1 change: 1 addition & 0 deletions Hypercube.Mathematics/Transforms/Transform3.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hypercube.Mathematics.Matrices;
using Hypercube.Mathematics.Quaternions;
using Hypercube.Mathematics.Vectors;

namespace Hypercube.Mathematics.Transforms;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hypercube.Mathematics;
using Hypercube.Mathematics.Quaternions;
using Hypercube.Mathematics.Vectors;

namespace Hypercube.UnitTests.Mathematics.Quaternions;
Expand Down

0 comments on commit d4a143c

Please sign in to comment.