let (=~) x1 x2 = abs(x1 - x2) < tolerance
Define an equality operator for floats with tolerance: (~=)
-
20-07-2023 - |
Pergunta
I currently have the function:
let equal x1 x2 = abs(x1 - x2) < tolerance
to check for equality with some tolerance since we are checking floats so they won't be exactly equal.
How can I make this a binary infix operator on floats instead, as in x ~= y?
I tried:
type System.Double with
static member (~-) (this, that: System.Double) =
equal this that
but then it says that I extension methods cannot provide operator overload
I tried then to inherit from System.Double and do the same:
type double() =
inherit System.Double()
static member (==) (this, that: double) =
equal this that
but it complains that I cannot inherit from a "sealed" type
How would you then define a type double with this operator overload that acts like a normal System.Double ("float" in F#) but with this operator overload method?
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow