Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify floating point precision in GDB

I am debugging a C++ application and when I display a double-precision floating point number in GDB I get a result like:.035449094393

How do I display more decimal places? Ideally I would be able to specificy the precision and get a result like: .0354490943927692

Basically, I am attempting to find the reason for a very minor difference between 2 variables. If I use printf with a format specifier like %1.20f I can see the difference in the variables but not using GDB.

like image 474
Absolute Zero Avatar asked Sep 19 '18 11:09

Absolute Zero


2 Answers

By default, GDB's p/f <variable> has limited precision.

You can use printf to show more decimal places:

(gdb) printf "%1.20f\n", <variable>

However, it is very likely that at this point you will start running into the limitations of your data type (there may be rounding errors and other small deviations from what you might expect as a value).

like image 171
hlt Avatar answered Sep 25 '22 18:09

hlt


You can do call printf("%.10f\n", d) in gdb and it will print it in stdout with the desired format.

like image 34
Maxim Egorushkin Avatar answered Sep 22 '22 18:09

Maxim Egorushkin