【问题标题】:lldb and C code give different results for pow()lldb 和 C 代码为 pow() 给出不同的结果
【发布时间】:2017-07-23 17:29:54
【问题描述】:

我有一个变量Npart,它是一个int,并初始化为64。下面是我的代码(test.c):

#include <math.h>
#include <stdio.h>

int Npart, N;

int main(){

Npart = 64;

N = (int) (pow(Npart/1., (1.0/3.0)));
printf("%d %d\n",Npart, N);

return 0;
};

打印出64 3,可能是由于数值精度问题。我编译如下:

gcc -g3 test.c -o test.x

如果我尝试使用lldb 进行调试,我尝试计算该值并在命令提示符中打印它,会发生以下情况:

$ lldb ./test.x
(lldb) target create "./test.x"
Current executable set to './test.x' (x86_64).
(lldb) breakpoint set --file test.c --line 1
Breakpoint 1: where = test.x`main + 44 at test.c:8, address = 0x0000000100000f0c
(lldb) r
Process 20532 launched: './test.x' (x86_64)
Process 20532 stopped
* thread #1: tid = 0x5279e0, 0x0000000100000f0c test.x`main + 44 at test.c:8, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100000f0c test.x`main + 44 at test.c:8
   5    
   6    int main(){
   7    
-> 8    Npart = 64;
   9    
   10   N = (int) (pow(Npart/1., (1.0/3.0)));
   11   printf("%d %d\n",Npart, N);
(lldb) n
Process 20532 stopped
* thread #1: tid = 0x5279e0, 0x0000000100000f12 test.x`main + 50 at test.c:10, queue = 'com.apple.main-thread', stop reason = step over
frame #0: 0x0000000100000f12 test.x`main + 50 at test.c:10
   7    
   8    Npart = 64;
   9    
-> 10   N = (int) (pow(Npart/1., (1.0/3.0)));
   11   printf("%d %d\n",Npart, N);
   12   
   13   return 0;
(lldb) n
Process 20532 stopped
* thread #1: tid = 0x5279e0, 0x0000000100000f4a test.x`main + 106 at test.c:11, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x0000000100000f4a test.x`main + 106 at test.c:11
   8    Npart = 64;
   9    
   10   N = (int) (pow(Npart/1., (1.0/3.0)));
-> 11   printf("%d %d\n",Npart, N);
   12   
   13   return 0;
   14   };
(lldb) print Npart
(int) $0 = 64
(lldb) print (int)(pow(Npart/1.,(1.0/3.0)))
warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
(int) $1 = 0
(lldb) print (int)(pow(64,1.0/3.0))
(int) $2 = 0

为什么lldb 给出不同的结果?

编辑:澄清了问题并提供了一个可验证的最小示例。

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建最小、完整和可验证的示例。
  • @Olaf 请查看更新后的问题。感谢您抽出宝贵时间解释投票失败的原因。
  • 首先,你正在铺设n,这是最不明智的做法(使用round/nearbyint/rint/whatever)
  • 第二,我有一种模糊的感觉,lldb 不知道pow 返回一个double,它认为它返回一个int。请打印没有任何强制转换的返回值!
  • 您通过转换为int 来铺平N,这会截断小数部分。为什么不使用round()lround()

标签: c debugging int lldb


【解决方案1】:

您的代码计算出 64 的立方根,应该是 4。

C 代码通过 flooring 将返回值转换为整数。 pow 通常以某种泰勒多项式或类似形式实现 - 这往往在数值上不准确。您计算机上的结果似乎略小于 4.0,当转换为 int 时,它会被 截断 - 解决方案是首先使用 lround

N = lround(pow(Npart/1., (1.0/3.0)));

至于lldb,关键是文字:

error: 'pow' has unknown return type; cast the call to its declared return type

即它知道函数的返回类型——因此是原型。 pow 被声明为

double pow(double x, double y);

但由于lldb 对返回类型的唯一提示是您提供的演员表,lldb 认为原型是

int pow(int x, double y);

这将导致未定义的行为 - 实际上,lldb 认为返回值应该是来自 EAX 寄存器的int,因此打印了0,但实际返回值是某个浮点数/SIMD 寄存器。同样,由于参数的类型也不知道,所以您不能传入int

因此我猜你会在调试器中得到正确的值

print (double)(pow(64.0, 1.0/3.0))

【讨论】:

  • 为了减少调试信息的大小,大多数编译器只在定义函数的 dylib 的调试信息中发出函数原型。正如 Antti 正确指出的那样,您没有 libC 的调试信息,因此 lldb 不会知道 pow 的签名。但是如果你在 macOS 上,你可以发出命令:expr -l objc -- @import Darwin 这将导致 lldb 构建 Darwin 模块 - 包括所有标准 C 库定义,并将其加载到表达式解析器中。这将为 lldb 提供所有标准 C 函数的原型,然后您可以在不强制转换的情况下调用它们。
  • @JimIngham 可能值得单独回答!我不是 Mac 用户:/
猜你喜欢
  • 1970-01-01
  • 2013-12-25
  • 2021-08-23
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
相关资源
最近更新 更多