[swift-dev] Rationalizing FloatingPoint conformance to Equatable

Stephen Canon scanon at apple.com
Mon Oct 23 11:25:01 CDT 2017

> On Oct 23, 2017, at 1:47 AM, Brent Royal-Gordon via swift-dev <swift-dev at swift.org> wrote:
>> On Oct 21, 2017, at 6:27 PM, Xiaodi Wu via swift-dev <swift-dev at swift.org> wrote:
>> Steve can describe the exact number of additional machine instructions on each architecture, but from my cursory reading the performance cost is not good and there's little cleverness to be had.
> I'm not sure why you think there's no cleverness to be had. For instance, suppose we loosen our requirement a bit: We require the `==` operator to be reflexive, but we allow `BinaryFloatingPoint.==` to treat NaNs with different payloads as different values. That very nearly allows us to use integer equality for floating-point comparison; we only need a +0/-0 special case:

This way leads to madness: a set S that contains a NaN, but S.contains(T.nan) is false. You’re going to run into worse problems when you get to Decimal; is 1e2 == 100e0? They have totally different encodings.

If we’re going to define another notion of floating-point equality in order to satisfy the usual mathematical axioms, it should treat all NaNs as equal, and all encodings of any non-zero value as equal. Whether it treats +0 and -0 as the same or different is the only really question from my perspective (I come down *very* weakly on the side of “they’re different”).

– Steve

More information about the swift-dev mailing list