GDB math.h中使用函数时,给出了奇怪的输出 [英] gdb gives strange output when using math.h functions
问题描述
可能重复:结果
为什么GDB评估的sqrt(3)0?
Ç新手在这里。必须有一个显而易见的解释,为什么要使用math.h中的函数在线时GDB给人奇怪的输出。例如,下面的晶圆厂
函数应该采取的绝对值,并返回一个double。
C newbie here. There must be an obvious explanation why gdb gives strange outputs when trying to use math.h functions in-line. For example, the fabs
function below is supposed to take the absolute value, and return a double.
(gdb) p cos(2*3.141/4)
$13 = 1073291460
(gdb) p fabs(-3)
$14 = 0
(gdb) p fabs(3)
$15 = 0
(gdb) p fabs(3.333)
$16 = 1
(gdb) p (double) fabs(-3.234)
$17 = 1
(gdb) p (double) fabs((double)-3.234)
$18 = 1
(gdb) p ((double(*)(double))fabs)(-3)
$19 = 682945
在code,我使用了包括math.h中,实际code能够正确执行,虽然同code放置在网上广发行产生奇怪的结果。我可以忽视它,但它似乎是一个很好的学习机会。
The code I'm using has included math.h, and the actual code appears to execute correctly, although the same code placed in-line in gdb produces strange results. I could ignore it, but it seems a good learning opportunity.
推荐答案
(编号:<一href=\"http://lists.gnu.org/archive/html/gdb/2009-12/msg00004.html\">http://lists.gnu.org/archive/html/gdb/2009-12/msg00004.html)
GDB
缺少 COS
函数的调试信息,因此假定它是一个 INT COS(...)
功能,所以值不正确返回(尤其是在x86的寄存器来存储浮点回报,整数返回不同)。
gdb
is missing the debug information of the cos
function, and therefore assume it is an int cos(...)
function, so the values are not returned correctly (esp. on x86 as the registers to store floating point return and integer return are different).
这可以通过指定类型工作围绕:
This could be worked around by specifying the type:
(gdb) p ((double(*)(double))cos) (1.0)
$18 = 0.54030230586813977
这篇关于GDB math.h中使用函数时,给出了奇怪的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!