Is Fortran unable to do the addition between 865398.78 and -865398.78? Why the answer is -0.03?

前端 未结 2 827
醉梦人生
醉梦人生 2020-12-04 02:42

In the code below I am adding together 865398.78 and -865398.78. I expect to get 0, but instead I get -0.03.

\"

2条回答
  •  醉酒成梦
    2020-12-04 03:29

    The number 865398.78 is represented in single precision in your code. Single precision can handle about 7 significant digits, while your number has 8. You can make it double precision by writing

    x=x+865398.78_8
    

提交回复
热议问题