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

Lars-Jørgen Kristiansen larsjk.84 at gmail.com
Wed Oct 5 13:36:20 CDT 2016


Hmm, I actually thought about this being the reason but couldn't remember the limits.

And thought that i would receive a overflows when stored into 'Float' error...?

> 5. okt. 2016 kl. 19.26 skrev Jens Alfke <jens at mooseyard.com>:
> 
> 
>> On Oct 5, 2016, at 2:30 AM, Lars-Jørgen Kristiansen via swift-users <swift-users at swift.org <mailto: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/4f8737c5/attachment.html>


More information about the swift-users mailing list