문제

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?

도움이 되었습니까?

해결책

let (=~) x1 x2 = abs(x1 - x2) < tolerance
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top