[swift-users] Different behaviour when casting Float and Double to NSNumber

Jens Alfke jens at mooseyard.com
Wed Oct 5 12:26:50 CDT 2016


> On Oct 5, 2016, at 2:30 AM, Lars-Jørgen Kristiansen via swift-users <swift-users at swift.org> wrote:
> 
> // Also noticed this:
> "\(floatNumber)" // "1e+07"
> "\(doubleNumer)" // "10000000"

Numbers around 10 million are too large to be represented exactly by a 32-bit float — the mantissa is 24 bits, including sign, so its range is ±8.3 million. (The specific number 10,000,000 does come out exactly, though, since it’s a multiple of 128.) 

So even if Float.description did use non-scientific notation for numbers at this scale, they wouldn’t be accurate. In fact the implementor of the .description method may have decided intentionally to switch to scientific notation at this scale so that the number of significant figures can be limited to the available precision.

—Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161005/5c9e9309/attachment.html>


More information about the swift-users mailing list