-1

(lldb) po [@(70.033) stringValue]
70.033
(lldb) po [@(80.138) stringValue]
80.13800000000001
(lldb) po [@(70.138) stringValue]
70.13800000000001
(lldb) po [@(100.01) stringValue]
100.01
(lldb) po [@(90.03) stringValue]
90.03
(lldb) po [@(90.01) stringValue]
90.01000000000001
(lldb) po [@(900.01) stringValue]
900.01
(lldb) po [@(100.01) stringValue]
100.01
(lldb) po [@(80.01) stringValue]
80.01000000000001

Is this a bug form the NSNumber? Or a mistake with my Mac?

Gereon
  • 17,258
  • 4
  • 42
  • 73
  • 2
    Is *what* a bug or a mistake? The output you show seems to be a series of numbers printed in the `lldb` debugger, so it's hard to understand which strings you do want and which you don't. – Caleb Jan 16 '19 at 06:47
  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Gereon Jan 16 '19 at 07:10
  • I don't think so, and I also want to confirm that : Is @(90.01) == [NSNumber numberWithFloat:90.01]; ? here is code : { NSNumber *num = [NSNumber numberWithFloat:90.01]; // NSNumber *num = @(90.01); NSString *str = [num stringValue]; NSLog(@"str - %@", str); str - 90.01 } { // NSNumber *num = [NSNumber numberWithFloat:90.01]; NSNumber *num = @(90.01); NSString *str = [num stringValue]; NSLog(@"str - %@", str); str - 90.01000000000001 <- that I don't want } – kulishi_jin Jan 16 '19 at 07:32
  • "90.01000000000001 <- that I don't want" - please read the answer I linked to, it explains why this happens and why your expectation is flawed. If you want to avoid these kinds of things, use `NSDecimalNumber`. – Gereon Jan 16 '19 at 08:28

1 Answers1

0

No, it is not a bug. It is just the standard issue with the internal representation of numbers. So (in Swift):

var a = 0.15 + 0.15
var b = 0.1 + 0.2
print (a == b) // can be false!
print (a >= b) // can also be false!

var c = 80.138
print(c) // 80.138
print (c - 80.0 - 0.138) // 5.218048215738236e-15

There are many people observing this "strang" behaviour, in almost any programming language. Just check some web sites like stackoverflow.com:

strange output in comparison of float with float literal

or, with nice images:

https://bitbashing.io/comparing-floats.html

Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34