Strange behaviours with stringWithFormat float

2019-07-07 02:29发布

问题:

(lldb) po [NSString stringWithFormat:@"%.1f", 0.01]
(id) $21 = 0x003a2560 19991592471028323832250853378750414848.0
(lldb) po [NSString stringWithFormat:@"%.1f", 0.1]
(id) $22 = 0x0de92240 -0.0

Does anyone understand the behaviour here? I'm running on device.

回答1:

It's a bug in lldb. If you try the same thing in gdb, it works properly. I suspect lldb is only passing the low 32 bits of the argument. The IEEE representation of 0.01 and of the number it's printing are these:

47ae147b3778df69 = 19991592471028323832250853378750414848.00
3f847ae147ae147b = 0.01

Notice that the low 32 bits of 0.01 match the high 32 bits of the other number.

The bug also happens with printf:

(lldb) expr (void)printf("%.1f\n", 0.01)
19991592257096858016910903319197646848.0
<no result>

It doesn't happen with +[NSNumber numberWithDouble:]:

(lldb) po [NSNumber numberWithDouble:0.01]
(id) $3 = 0x0fe81390 0.01

So I suspect the bug is in lldb's handling of variadic functions.

You can open a bug report at the LLVM bugzilla and/or at Apple's bug reporter (aka rdar).