为什么GDB评估的sqrt(3)为0? [英] Why does gdb evaluate sqrt(3) to 0?

查看:154
本文介绍了为什么GDB评估的sqrt(3)为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

3的平方根,如由钨阿尔法估计:

The square root of 3, as estimated by Wolfram Alpha:

1.7320508075688772935274463415058723669428052538103806280558...

当我这样做的sqrt(3)在C,它的计算结果为0。为什么?

When I do sqrt(3) in C, it evaluates to 0. Why?

EDIT4 :这里是如何在GDB重现此问题。创建 test.c以如下:

EDIT4: here's how you can reproduce this issue in GDB. Create test.c as follows:

#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

在控制台输入以下内容:

Enter this at console:

(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)

推荐答案

的问题不是缺少函数声明(这是不缺的,因为你没有包括&LT;文件math.h&GT; )。

The problem is not the missing function declaration (which isn't missing, since you did include <math.h>).

问题是缺少对开方调试信息与实际使用。如果没有调试信息,GDB没有线索传递什么参数类型为的sqrt(),以及它返回。

The problem is missing debug info for the sqrt you are actually using. Without that debug info, GDB has no clue what parameter type to pass to sqrt(), and what it returns.

您可以通过安装的libc-debuginfo软包得到很多Linux发行版所需的调试信息。以下是我看到这样一个系统:

You can get the required debug info on many Linux distributions by installing libc-debuginfo package. Here is what I see on such a system:

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>

注:没有调试信息

Note: "no debug info"

(gdb) p sqrt(3)
$2 = 0
(gdb) p sqrt(3.0)
$3 = 0

请注意:符合你的行为。
什么开方功能的有调试信息?

Note: matches your behavior. What sqrt functions do have debug info?

(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>

请注意:<!code> __开方是在同一地址为开方,但GDB知道它的类型

Note: __sqrt is at the same address as sqrt, but GDB knows its type!

(gdb) p __sqrt(3)
$5 = 1.7320508075688772
(gdb) p __sqrt(3.0)
$6 = 1.7320508075688772

一个可以合理认为这是GDB的错误。随意创建一个在 GDB的Bugzilla

这篇关于为什么GDB评估的sqrt(3)为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆