Skip to content

Commit

Permalink
[FSharpMath] Add approximateEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Aug 19, 2024
1 parent e95ecf7 commit 0ac497a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Aardvark.Base.FSharp/Math/Math.fs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ module FSharpMath =
let inline isNegInfAux (_ : ^Z) (_ : ^Y) (x : ^T) =
((^Z or ^Y or ^T) : (static member IsNegativeInfinity : ^T -> bool) x)

let inline approximateEqualsAux (_: ^Z) (epsilon: ^U) (x: ^T) (y: ^T) =
((^Z or ^T) : (static member ApproximateEquals : ^T * ^T * ^U -> bool) (x, y, epsilon))

/// Resolves to the zero value for any scalar or vector type.
[<GeneralizableValue>]
let inline zero< ^T when ^T : (static member Zero : ^T) > : ^T =
Expand Down Expand Up @@ -387,6 +390,10 @@ module FSharpMath =
let inline isFinite (x : ^T) =
(x |> isInfinity |> not) && (x |> isNaN |> not)

/// Returns whether the distance between x and y is not more than epsilon.
let inline approximateEquals epsilon x y =
approximateEqualsAux Unchecked.defaultof<Fun> epsilon x y

[<CompilerMessage("testing purposes", 1337, IsHidden = true)>]
module ``Math compiler tests 😀😁`` =
type MyCustomNumericTypeExtensionTestTypeForInternalTesting() =
Expand Down Expand Up @@ -898,4 +905,12 @@ module FSharpMath =
let a : bool = isNegativeInfinity C3d.Black
let a : bool = isNegativeInfinity V3d.Zero
let a : bool = isNegativeInfinity M33d.Zero
()
()

let approximateEqualsWorking() =
let a : bool = approximateEquals 2 42 43
let a : bool = approximateEquals 1.0 42.0 43.0
let a : bool = approximateEquals 1 V2i.Zero V2i.One
let a : bool = approximateEquals 1 M44i.Zero M44i.Identity
let a : bool = approximateEquals 1.0 ComplexD.Zero ComplexD.One
()

0 comments on commit 0ac497a

Please sign in to comment.