0

I am trying to get series of numbers starting from zero by increasing it by 0.1 by coding it in Fortran 90. But I am not getting exact values. I read a lot of answers that it is floating point error. I found this answer which was originally written in python and was facing the same issue as mine. Error in generating a set of decimal point numbers with a particular common difference in python I tried to implement this but no help. Edit: I have understood the reason for this behaviour through this: Is floating point math broken? But couldn't find the solution to solve this in fortran as it is solved in python in given answer. The code is as follows:

program float_trial

    implicit none
    integer :: t = 0
    integer :: dt = 1
    integer :: a_sec = 10
    integer :: timemax = 12
    double precision:: exact_t
    if ( t<timemax ) then
        do
            print*, exact_t
            t = t+ dt
            exact_t = dble(t)/a_sec

            if(t>=timemax) then
                exit
            endif    
        enddo
    endif

end program float_trial

the output:

0.0000000000000000     
  0.10000000000000001     
  0.20000000000000001
  0.29999999999999999
  0.40000000000000002
  0.50000000000000000
  0.59999999999999998
  0.69999999999999996
  0.80000000000000004
  0.90000000000000002
   1.0000000000000000
   1.1000000000000001

My expected output is : 0.0, 0.1, 0.2 ......... How can we resolve this in Fortran?

hustler
  • 1
  • 2
  • Floating point numbers have a limited precision. You can increase it by using a different kind https://stackoverflow.com/questions/838310/fortran-90-kind-parameter You can just print a certain number of decimals with explicit format like `(f3.1)` or similar. – Vladimir F Героям слава May 15 '22 at 10:25
  • Instead of a simple `print *,exact_t` use a print with format specifier, for example `print 1000,exact_t` and `1000 format(1f6.4)` – Alex Sveshnikov May 15 '22 at 10:27
  • @AlexSveshnikov I want the numerical value also to be stored in `exact_t` as 0.1, 0.2 etc. as it would be used in further calculations as well. Any other suggestions for this? – hustler May 15 '22 at 10:29
  • @VladimirFГероямслава I want the numerical value also to be stored in `exact_t `as 0.1, 0.2 etc. as it would be used in further calculations as well. I just used the print statement to check if values are getting properly stored or not. – hustler May 15 '22 at 10:32
  • 1
    Can you store 3^(-1) = 1/3 as an exact decimal number? No. It doesn't matter how many digits of 0.333333... you will store you cannot store infinitely many of them, so there always be difference between the exact 1/3 and its decimal representation. Similarly, you cannot store 10^(-1) =0.1 as a binary number. It doesn't matter how many binary digits (bits) you use, there always be a difference from the exact 0.1. – Alex Sveshnikov May 15 '22 at 10:37
  • @hustler Numbers like `0.1` **cannot** be exactly represented in binary. Please read the duplicate link. – Vladimir F Героям слава May 15 '22 at 10:37
  • @VladimirFГероямслава That is why I tried adding the numbers in integer format and then dividing it buy 10 to get in the form of 0.1, 0.2.... as given in tagged link which deals with the same thing in python language. But following that I am not able to do it in fortran? Is it possible to do it in fortran? – hustler May 15 '22 at 10:41
  • 1
    There are special decimal numeric types. They are however not normally natively supported in common Fortran compilers. You would have to work with special libraries - all your math would have to be done in such libraries. Or just work with integers. That means completely in integers. Whatever you will do, you cannot get exact 0.1 in the native binary `real`. It simply cannot exactly represent this value. – Vladimir F Героям слава May 15 '22 at 10:42
  • "But couldn't find the solution to solve this in fortran as it is solved in python in given answer"—that question is language agnostic, and it has _80_ answers, not just one. Have you looked at all of them? Have you searched for relevant terms you learned in that answer along with fortran to see what might be out there? – ChrisGPT was on strike May 15 '22 at 15:25
  • Note exact_t is un-initiallized, so the result of the first print statement is indeterminant – Cocofalco May 16 '22 at 01:23

0 Answers0