[swift-evolution] Add max/min to floating point types

Stephen Canon scanon at apple.com
Fri Jun 10 17:51:55 CDT 2016

On Jun 10, 2016, at 12:38 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:

> On Fri, Jun 10, 2016 at 1:24 PM, Darren Mo via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Today, one can get max/min by doing:
> let max = Float.greatestFiniteMagnitude
> let min = -Float.greatestFiniteMagnitude
> On review of the proposal for the new FloatingPoint, I too commented on the lack of `max` and `min`. You've pointed out the issue with infinity. But also, FLT_MIN (from your local friendly C universe and available in Swift, obviously) is actually the smallest representable positive value, so `Float.min` is of ambiguous meaning. It was therefore decided not to use those words `max` and `min`.

It’s worth noting that this issue has been pretty extensively discussed both on- and off-list.  Although convenience is good, the objections to the ambiguity of `.max` and `.min` would be *very* hard to overcome:

– They’re not actually the maximum and minimum values of the type.  In particular, that `max(Float.infinity, .max)` wouldn’t be `Float.max` is pretty seriously confusing.

– The proposed `.min` doesn't align with the meaning of the "float-min-thing” in most other major languages:

	In C, FLT_MIN is the smallest positive normal
	In C++, std::numeric_limits<float>::min() is the smallest positive normal
	In Python, sys.float_info.min is the smallest positive normal
	In C#, .minValue is documented as “the smallest possible value”, but is actually the value you want, rather than the documented –infinity.
	In Java, MIN_VALUE is the smallest positive value (including subnormals)
	In Ruby, MIN is the smallest positive normal
	In R, double.xmin is the smallest positive normal
	In MATLAB, realmin is the smallest positive normal
	Actually, Rust is the only language I know of where `MIN` is the value you want *and* correctly documented as such.

All that’s not to say that Swift can’t do this, but there’s a lot of opportunity for confusion on this point, and having a very explicit name isn’t really a bad thing.

– There is also some concern that having `.min` and `.max` with the same names as on Integer types would lead people to try to use them the same way in code, which generally isn’t going to work the way users expect.

– Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160610/285e2cf2/attachment.html>

More information about the swift-evolution mailing list