Search code examples
debugginggdbfortranfortran77

Fortran variable has different value


I have a FORTRAN program, where I assign PI=3.14159265

when I run the program, my numbers could out slightly off. So i recompiled with debug flags set, and started stepping through with gdb. A few lines in, I notice PI is not what i set it to?

(gdb) s

57 PI=3.14159265

(gdb) s

58 TWOPI= 2* PI

(gdb) print pi

$1 = 3.1415927410125732

What is going on here? I would understand if PI was a system value, but if it is, why is the value wrong?? how should i fix this?


Solution

  • It is printing the best binary approximation to the value of pi you entered; I get exactly the same answer in C using a float. You can get more accuracy by going to double precision, but this is as close as you can get to your value using binary with 32 bits and the usual representation of floating point numbers.