[swift-dev] Rationalizing FloatingPoint conformance to Equatable

Tino Heth 2th at gmx.de
Thu Nov 2 03:21:13 CDT 2017


> Am 01.11.2017 um 17:51 schrieb Greg Titus via swift-dev <swift-dev at swift.org>:
> 
> The common (and correct!) wisdom in _any_ programming language that uses IEEE floating point is that checking equality of two floating point values is almost always a terrible idea. Usually what you want in any real world code is to check for a difference less than some epsilon value, which depends upon context. There are just too many issues with values that aren’t exactly representable, rounding errors during computations, et cetera, for perfectly normal floats even if you completely left aside equality rules for NaN.

Quite much what I was about to write about the topic:
I guess the most common difference between a programmer with a graduation and a autodidact is that one was told that „==" shouldn’t be used with floats, whereas the other had to learn the hard way.

So, I think it Swift could be really innovative by protecting people from the trouble of float-equallity — and although that definitely belongs more to evolution:
I see two possibilities here, and both would rely on generic value parameters.
One is creating a Float type with a parametrized epsilon (or maximal relative error), the other is using an parametrizing an integer type with a scaling factor.

Both options are already possible, but as the needed precision depends on the use case, it imho doesn’t make sense to create a type with a fixed parameter in the stdlib.

- Tino
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20171102/6178dc98/attachment.html>


More information about the swift-dev mailing list