Pie*_*ter 16 c eclipse math gdb
由Wolfram Alpha估算的3的平方根:
1.7320508075688772935274463415058723669428052538103806280558...
Run Code Online (Sandbox Code Playgroud)
当我sqrt(3)在C中时,它的计算结果为0.为什么?
编辑4:这是你如何在GDB中重现这个问题.创建test.c如下:
#include <stdio.h>
#include <math.h>
int main()
{
printf("sqrt(3): %f\n", sqrt(3));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:
gcc -O0 -g -Wall -pedantic -ansi -lm -o test test.c
Run Code Online (Sandbox Code Playgroud)
运行调试器:
gdb test
Run Code Online (Sandbox Code Playgroud)
在控制台输入:
(gdb) break test.c:6
Breakpoint 1 at 0x400578: file test.c, line 6.
(gdb) r
Starting program: /home/pdedecker/Desktop/test
Breakpoint 1, main () at test.c:6
6 printf("sqrt(3): %f\n", sqrt(3));
(gdb) print sqrt(3)
$1 = 0
(gdb) s
sqrt(3): 1.732051
Run Code Online (Sandbox Code Playgroud)
我的GDB版本是GNU gdb (GDB) SUSE (7.1-3.12).
Emp*_*ian 17
问题不在于缺少的函数声明(由于你确实包含了这个声明,因此不会丢失<math.h>).
问题是缺少sqrt您实际使用的调试信息.没有那个调试信息,GDB不知道要传递给哪个参数类型sqrt(),以及它返回什么.
您可以通过安装libc-debuginfo软件包在许多Linux发行版上获得所需的调试信息.这是我在这样一个系统上看到的:
gdb -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) b main
Breakpoint 1 at 0x400558: file t.c, line 6.
(gdb) r
Breakpoint 1, main () at t.c:6
6 printf("sqrt(3): %f\n", sqrt(3));
(gdb) p sqrt
$1 = {<text variable, no debug info>} 0x7ffff7b7fb50 <__sqrt>
Run Code Online (Sandbox Code Playgroud)
注意:"没有调试信息"
(gdb) p sqrt(3)
$2 = 0
(gdb) p sqrt(3.0)
$3 = 0
Run Code Online (Sandbox Code Playgroud)
注意:符合您的行为.什么sqrt功能做有调试信息?
(gdb) info func sqrt
All functions matching regular expression "sqrt":
File ../sysdeps/x86_64/fpu/e_sqrt.c:
double __ieee754_sqrt(double);
File s_csqrt.c:
complex double __csqrt(complex double);
File ../sysdeps/x86_64/fpu/e_sqrtf.c:
float __ieee754_sqrtf(float);
File w_sqrtf.c:
float __sqrtf(float);
File s_csqrtf.c:
complex float __csqrtf(complex float);
File ../sysdeps/i386/fpu/e_sqrtl.c:
long double __ieee754_sqrtl(long double);
File w_sqrtl.c:
long double __sqrtl(long double);
File s_csqrtl.c:
complex long double __csqrtl(complex long double);
File ../sysdeps/ieee754/dbl-64/mpsqrt.c:
void __mpsqrt(mp_no *, mp_no *, int);
File w_sqrt.c:
double __sqrt(double);
(gdb) p __sqrt
$4 = {double (double)} 0x7ffff7b7fb50 <__sqrt>
Run Code Online (Sandbox Code Playgroud)
注意:__sqrt与地址相同sqrt,但GDB知道它的类型!
(gdb) p __sqrt(3)
$5 = 1.7320508075688772
(gdb) p __sqrt(3.0)
$6 = 1.7320508075688772
Run Code Online (Sandbox Code Playgroud)
可以合理地说这是GDB中的一个错误.随意在GDB bugzilla中创建一个.
我预测你没做 #include <math.h>
如果没有函数声明,C将默认函数的返回值int.浮点数可能会返回0,具体取决于int的大小.C也不知道如何转换函数参数.它将默认将参数传递为它碰巧的类型.如果传递一个整数,sqrt()它将不会转换为double,但该sqrt()函数会将位模式解释为double.
| 归档时间: |
|
| 查看次数: |
3666 次 |
| 最近记录: |