奇怪的行为与stringWithFormat浮法 [英] Strange behaviours with stringWithFormat float
问题描述
(lldb)po [NSString stringWithFormat:@%。1f,0.01]
(id)$ 21 = 0x003a2560 19991592471028323832250853378750414848.0
(lldb)po [NSString stringWithFormat:@%。1f,0.1]
(id)$ 22 = 0x0de92240 -0.0
<有人知道这里的行为吗?我在设备上运行。
这是 lldb
。如果你在 gdb
中尝试同样的事情,它可以正常工作。我怀疑 lldb
只传递参数的低32位。 0.01和它打印的数字的IEEE表示是:
47ae147b3778df69 = 19991592471028323832250853378750414848.00
3f847ae147ae147b = 0.01
请注意,0.01的低32位与另一个数的高32位匹配。 b
$ b
这个错误也发生在 printf
:
(lldb)expr(void)printf(%。1f \ n,0.01)
19991592257096858016910903319197646848.0
< no result>
+ [NSNumber numberWithDouble:] $
$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
所以我怀疑这个bug是在 lldb
你可以在 > LLVM bugzilla 和/或苹果bug记者(又名rdar)。
(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.
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).
这篇关于奇怪的行为与stringWithFormat浮法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!