-2

When I'm doing an interating sum over a double precision value:

    REAL*8 :: a
    INTEGER :: p

    a = 0.0d0
    DO p = 1,10
    a = a + 1d-04   
    END DO

I get the following answer:

    p =1 - a = 1.000000000000000D-004
    p =2 - a = 2.000000000000000D-004
    p =3 - a = 3.000000000000000D-004
    p =4 - a = 4.000000000000000D-004
    p =5 - a = 5.000000000000000D-004
    p =6 - a = 6.000000000000001D-004

Why in interation p=6 I get the number 1 in the end of the variable 'a'?

dcalmeida
  • 393
  • 7
  • 18

1 Answers1

0

In Fortran a double (or real) is a floating point number (e.g. see https://en.wikipedia.org/wiki/Floating_point ). Floating point numbers can not represent every decimal number correctly and rounding errors occur.