0

When I run the code below I get an output of 6378136.5 instead of 6378136.3

PROGRAM test

implicit none
real*8 radius

radius = 6378136.3

print*,radius

END

I have read this other link (Precision problems of real numbers in Fortran) but it doesn't explain how to fix this problem.

Community
  • 1
  • 1

1 Answers1

1

The reason this is happening is not because the variable you are using lacks precision, but because you initialized the value using a single precision number.

Take a look at this answer for a good explanation, and an elegant solution to your problem for any larger programs.

If you just want to solve it quickly, then you only have to change one line:

radius = 6378136.3d0

Though this will still give you a value of 6378136.2999999998 because of floating point precision.

Community
  • 1
  • 1
Addison
  • 7,322
  • 2
  • 39
  • 55