Question

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?

Was it helpful?

Solution

let (=~) x1 x2 = abs(x1 - x2) < tolerance
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top