Silverfrost Forums

Welcome to our forums

floaty problem (beginner)

17 Apr 2018 9:40 #21858

must be a stupid question but... i have a variable a = 1.5 i add to it 0.00125 and then a shows in a write as 1.50124999997

what am i doing wrong?

is there a different declaration for fixed floats?

17 Apr 2018 9:55 #21859

try the following code: real8 a8 real4 a4

      a8 = 1.5
      a8 = a8 + .00125
      write (*,*) 'a8 =',a8

      a4 = 1.5
      a4 = a4 + .00125
      write (*,*) 'a4 =',a4      

      a8 = .00125
      write (*,*) 'a8 =',a8
      a8 = .00125d0
      write (*,*) 'a8 =',a8
      end

The problem is 1.5 = 1.5d0 ( the 8 byte representation of the 2 constants is the same) but .00125 is not the same as .00125d0 ( although .125 is the same as .125d0 )

17 Apr 2018 9:54 #21868

i declared my a as real*4 and all was going well but... on the 1924th (or so) iteration of adding 0.00125 to 1.5...

when i would expect the value of 3.90255 to go to 3.90380 it actually jumps to 3.90381

this is really quite interesting... any suggestions from my wizards greatly appreciated!

17 Apr 2018 10:48 #21869

You have an interesting adventure ahead of you. Try the following calculation using ftn95 /lgo, then ftn95 /64 /lgo, and on a pocket calculator if you have one:

program tst
real :: x=4.0, y=3.0
print *,(x/y-1)*y-1
end

After observing and trying to explain the results, please read this article: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

18 Apr 2018 10:50 #21872

Things won't get much better with integers, just different. Try this, for example:

      PROGRAM INT
      INTEGER*1 K
      K = 0
      DO 10 I=1,260
      K = K + 1
      WRITE(*,*) I, K
  10  CONTINUE
      END

Or (for a much longer loop) with INTEGER2 or INTEGER4.

Eddie

18 Apr 2018 11:12 #21874

Did a more accurate calculation on the iteration (instead of adding to previous, possibly wonky, value, I RE-calculated).

Thanks for this guys!

18 Apr 2018 4:44 #21878

Managing the propagation of rounding errors is a skill that can be picked up over time. It helps to build up your own library of useful information, books, websites etc.

Here is a good site that considers floating point.

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

In the mid 80s I was fortunate to own a workstation running RISC-OS that was designed to be programmed. It had a C and Fortran 77 compiler. It also had a large 8x8 card as a 'floating point co processor'. Nowadays this is just built into the chips. However, I still have the manual that came with that card (not the machine or card now sadly) and it contains a lot of useful diagrams that I still consult today.

What a shame proper manuals have disappeared from computer stuff we all purchase. All we get these days is a getting started card with scribbled cartoons.

18 Apr 2018 4:52 #21879

Thanks for this... yes, I'll ride the learning curve like a trooper. Checked out that page (same one given earlier I think... great minds think alike!)

Please login to reply.