• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to pretty print floating numbers (without loosing precision)?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to pretty print floating numbers (without loosing precision)?


  • Subject: Re: How to pretty print floating numbers (without loosing precision)?
  • From: Richard Charles <email@hidden>
  • Date: Mon, 13 Oct 2014 09:16:30 -0600

A float has 7.22 decimal digits of precision. A double has 15.95 decimal digits of precision.

http://en.wikipedia.org/wiki/IEEE_floating_point

The format specification Apple uses for [NSNumber descriptionWithLocale:] with a data type of double is %0.16g. Refer to Apple documentation.

[NSNumberFormatter localizedStringFromNumber:numberStyle:] is most likely doing something similar to %0.16g internally.

The extra precision added by using a format specification %.22g is meaningless for an IEEE double.

Richard Charles

On Oct 11, 2014, at 2:04 AM, Gerriet M. Denkmann <email@hidden> wrote:

>
> UInt64 sum = 16494631536958187520UL;
> double doubleSum = sum;
> NSString *strSum = 	[ NSNumberFormatter	localizedStringFromNumber:	@(doubleSum)
> 										numberStyle: 				NSNumberFormatterDecimalStyle
> 					];
> NSLog(@"%s DecimalStyle (of double): %@; UInt64: %llu, double: %.22g",__FUNCTION__, strSum, sum, doubleSum );
>
> This prints:
> DecimalStyle (of double): 16,494,631,536,958,200,000; UInt64: 16494631536958187520, double: 16494631536958187520
>
> Note: converting to double does NOT loose any digits. But NSNumberFormatter does. Why?
>
> I also tried:
> 	NSNumberFormatter *nf = [ [ NSNumberFormatter alloc ] init ];
> 	[ nf setNumberStyle: NSNumberFormatterDecimalStyle ];
> 	[ nf setUsesSignificantDigits: YES ];
> 	[ nf setMaximumSignificantDigits: 22 ];
> 	NSString *str2Sum = [ nf stringFromNumber: @(doubleSum) ];
> But the result is the same.
>
> 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


References: 
 >How to pretty print floating numbers (without loosing precision)? (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Re: NSPopover tear-off problem
  • Next by Date: Code signing problem using shared framework in Yosemite-SOLVED
  • Previous by thread: Re: How to pretty print floating numbers (without loosing precision)?
  • Next by thread: Kerning Pairs
  • Index(es):
    • Date
    • Thread