【问题标题】:Strange behaviours with stringWithFormat floatstringWithFormat 浮动的奇怪行为
【发布时间】:2012-07-07 04:18:47
【问题描述】:
(lldb) po [NSString stringWithFormat:@"%.1f", 0.01]
(id) $21 = 0x003a2560 19991592471028323832250853378750414848.0
(lldb) po [NSString stringWithFormat:@"%.1f", 0.1]
(id) $22 = 0x0de92240 -0.0
有人理解这里的行为吗?我正在设备上运行。
【问题讨论】:
标签:
ios
floating-point
nsstring
lldb
【解决方案1】:
这是lldb 中的一个错误。如果您在gdb 中尝试相同的操作,它会正常工作。我怀疑lldb 只传递了参数的低 32 位。 IEEE 表示的 0.01 及其打印的数字如下:
47ae147b3778df69 = 19991592471028323832250853378750414848.00
3f847ae147ae147b = 0.01
请注意,0.01 的低 32 位与其他数字的高 32 位匹配。
这个错误也发生在printf:
(lldb) expr (void)printf("%.1f\n", 0.01)
19991592257096858016910903319197646848.0
<no result>
+[NSNumber numberWithDouble:] 不会发生这种情况:
(lldb) po [NSNumber numberWithDouble:0.01]
(id) $3 = 0x0fe81390 0.01
所以我怀疑这个错误出在lldb 对可变参数函数的处理中。
您可以在the LLVM bugzilla 和/或Apple's bug reporter (aka rdar) 打开错误报告。