cocoa - I've discovered what I think is a bug with NSDecimalNumber

前端 未结 2 1009
挽巷
挽巷 2020-12-17 03:23

Here is a simple code that shows what I think is a bug when dealing with double numbers...

double wtf = 36.76662445068359375000;
id xxx = [NSDecimalNumber nu         


        
2条回答
  •  盖世英雄少女心
    2020-12-17 04:00

    Trying to go beyond roughly 16 digits of decimal precision with a double is generally not recommended. Frankly, I'm surprised that you are able to represent this double (with 19 significant digits) in a way that maintains that precision when logging it out. You may even get different behavior on the iPhone itself, which maps a long double type to just plain double (your Mac may be handling this as a long double behind the scenes).

    The rounding you are seeing may be happening at the binary level (see here for more on this), so you won't be seeing the decimal rounding that you are expecting.

    It is for these reasons that you will want to work entirely with NSDecimalNumbers or NSDecimals from start to finish if you need this kind of high-precision math. To do this, do not convert to and from floating point types, but instead use NSStrings directly to populate and export the numbers (or store them as NSDecimalNumbers in Core Data).

    For example, you could work around the above problems with the following code:

    id xxx = [NSDecimalNumber decimalNumberWithString:@"36.76662445068359375000"];
    

    NSDecimalNumbers (and their C struct equivalent NSDecimal) can handle up to 38 significant digits.

提交回复
热议问题