Re: How to pretty print floating numbers (without loosing precision)?
Re: How to pretty print floating numbers (without loosing precision)?
- Subject: Re: How to pretty print floating numbers (without loosing precision)?
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sun, 12 Oct 2014 00:22:14 +0700
On 11 Oct 2014, at 20:47, Scott Ribe <email@hidden> wrote:
> On Oct 11, 2014, at 7:36 AM, Scott Ribe <email@hidden> wrote:
>
>> On Oct 11, 2014, at 2:04 AM, Gerriet M. Denkmann <email@hidden> wrote:
>>
>>> Note: converting to double does NOT loose any digits.
>>
>> Well, it has to. Not sure how you're getting that output, but a double has 52 bits for the mantissa, so the largest integer it can represent without losing precision is about 4.5x10^15.
>
> Oops. Forgot the implicit MSB 1 in normalized doubles, so I guess max is ~9X10^15.
All integers up to 9007199254740992 are exact representable as double (which is probably ~9X10^15 as you mentioned).
The next representable integer is then 9007199254740994.
And of course the difference between representable numbers increases as the numbers get bigger.
> Anyway, your number is well into the range where it lose binary digits, so it would just be pure (very unlikely) luck if that specific integer happened to be one that is precisely representable as a double.
Yes, this number is well into the range where not every integer is representable as double. But some are. And this number was chosen to be one of those.
But still: "%.22g" prints the exact number (although not very readable) whereas NSNumberFormatter looses digits.
Just using sprintf and then adding the thousands-separators will give a better result.
Which means: the ICU algorithms are not very good with large numbers.
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden