3 ms·
> How do float32 and float64 satisfy constraints.Ordered? Well, the simple answer is that constraints.Ordered is a type set which lists them: https://pkg.go.d
by Merovius 4y ago
> How do float32 and float64 satisfy constraints.Ordered?
Well, the simple answer is that constraints.Ordered is a type set which lists them:
https://pkg.go.dev/golang.org/x/exp/constraints#Ordered https://pkg.go.dev/golang.org/x/exp/constraints#Ordered
The other answer is of course, that float32 and float64 have a < operator, so every type listed in constraints.Ordered does, so you can use the < operator on a type parameter constrained by constraints.Orderded.
The real question you seem to be asking though, is "how does Go support < on float32 and float64 if the IEEE-754 standard says that NaN are not comparable".
I can't comprehensively answer that, because I don't know that standard well enough. But for == the answer is that == is supported on floats and x == y is always false if either is NaN. Empirically, the same seems to be true for NaN <= NaN: https://go.dev/play/p/hB9CnrzpAVq https://go.dev/play/p/hB9CnrzpAVq
In any case, Wikipedia claims that IEEE-754 defines in fact an ordering, which is the one Go is going to use use: https://en.wikipedia.org/wiki/IEEE_754#Total-ordering_predicate https://en.wikipedia.org/wiki/IEEE_754#Total-ordering_predic...