我有一个针对 iOS SDK 6.1 的通用 iOS 应用程序,编译器设置为Apple LLVM compiler 4.2。当我在我的代码中放置一个断点并运行以下命令时,我得到了奇怪的结果sin(int)
。
作为参考,sin(70)
= 0.7739
(70 以弧度为单位)。
(lldb) p (double)sin(70)
(double) $0 = -0.912706376367676 // initial value
(lldb) p (double)sin(1.0)
(double) $1 = 0.841470984807897 // reset the value sin(int) will return
(lldb) p (double)sin(70)
(double) $2 = 0.841470984807905 // returned same as sin(1.0)
(lldb) p (double)sin(70.0)
(double) $3 = 0.773890681557889 // reset the value sin(int) will return
(lldb) p (double)sin(70)
(double) $4 = 0.773890681558519
(lldb) p (double)sin((float)60)
(double) $5 = -0.304810621102217 // casting works the same as appending a ".0"
(lldb) p (double)sin(70)
(double) $6 = -0.30481062110269
(lldb) p (double)sin(1)
(double) $7 = -0.304810621102223 // every sin(int) behaves the same way
观察:
- 调试会话中的第一个值
sin(int)
始终是-0.912706376367676
。 sin(int)
将始终返回与上次执行时返回的值相同的值sin(float)
。- 如果我
p
用po
, 或expr
(例如 expr (double)sin(70)) 替换,我会得到完全相同的结果。
为什么调试器会这样?
这是否意味着每次调用函数时都应该对每个参数进行类型转换?
NSLog 的一些更有趣的行为:
(lldb) expr (void)NSLog(@"%f", (float)sin(70))
0.000000 // new initial value
(lldb) expr (void)NSLog(@"%f", (float)sin(70.0))
0.773891
(lldb) expr (void)NSLog(@"%f", (float)sin(70))
0.000000 // does not return the previous sin(float) value
(lldb) p (double)sin(70)
(double) $0 = 1.48539705402154e-312 // sin(int) affected by sin(float) differently
(lldb) p (double)sin(70.0)
(double) $1 = 0.773890681557889
(lldb) expr (void)NSLog(@"%f", (float)sin(70))
0.000000 // not affected by sin(float)