Gdb gives strange output when using math.h functions

Possible duplicate:
Why does gdb evaluate sqrt (3) to 0?

C newbie here. There should be an obvious explanation of why gdb gives strange outputs when trying to use the math.h functions in a string. For example, the fabs function below should take an absolute value and return 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 

The code I use includes math.h, and the actual code looks correct, although the same code placed on a line in gdb produces strange results. I could ignore this, but it seems like a good opportunity to study.

+7
source share
1 answer

(Link: http://lists.gnu.org/archive/html/gdb/2009-12/msg00004.html )

gdb lacks information about debugging the cos function, and therefore it is assumed that it is an int cos(...) function, so the values โ€‹โ€‹are not returned correctly (for example, on x86 as registers for storing floating point returns and the integer return is different).

This can be circumvented by specifying a type:

 (gdb) p ((double(*)(double))cos) (1.0) $18 = 0.54030230586813977 
+8
source

All Articles