【发布时间】:2011-07-04 14:25:15
【问题描述】:
3 的平方根,由 Wolfram Alpha 估计:
1.7320508075688772935274463415058723669428052538103806280558...
当我在 C 中执行 sqrt(3) 时,它的计算结果为 0。为什么?
EDIT4:这是在 GDB 中重现此问题的方法。创建test.c如下:
#include <stdio.h>
#include <math.h>
int main()
{
printf("sqrt(3): %f\n", sqrt(3));
return 0;
}
编译:
gcc -O0 -g -Wall -pedantic -ansi -lm -o test test.c
运行调试器:
gdb test
在控制台输入:
(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
我的 GDB 版本是GNU gdb (GDB) SUSE (7.1-3.12)。
【问题讨论】:
-
向我们展示您用于获取此结果的代码。
-
你包括
<math.h>吗? -
是的,我确实包含了
math.h并添加了适当的-lrt标志。 -
显然,Wolfram Alpha 是错误的。 *耸耸肩*
-
也许您的调试器没有正确评估表达式。