lldb 和 C 代码对 pow() 给出了不同的结果



我有一个变量,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 %dn",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 %dn",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 %dn",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 %dn",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给出不同的结果?

编辑:澄清了这个问题,并提供了一个最低限度的可验证的例子。

您的代码计算 64 的立方根,应为 4。

C 代码通过地板将返回值转换为整数。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))

最新更新