Silverfrost Forums

Welcome to our forums

floating number problem with write

18 Apr 2018 11:10 #21873

Again, another little problem and I hope I'm not embarassing myself here...

Here is the simple code:

real8 :: a do i = 1, 2000 if (i>1) then a= 3 + ((i-1)((4-3)/1999)) end if
write(,) a end do

results show a long list of 3.00000000000 values which are not (as expected) getting any bigger. I'd expect them to grow by 1/1999 = 0.00050025 each iteration but my WRITE just shows a long list of 3s.

This one has to be stupid, so go easy on me! (and HUGE thanks!)

18 Apr 2018 11:15 #21875

my problem was to refer to a as an integer... i had a=3 and in my equation i used the value 3.

Instead, I just changed that to 3.0 everywhere, and I'm good!

18 Apr 2018 12:06 #21876

Note (or don't overlook) the fact that the integer expression ((4-3)/1999) evaluates to zero, so subsequent multiplication by (i-1) will also yield zero.

In Fortran, the type of the variable being assigned a value can be different from the type of the expression that yields the value. There are rules for how to evaluate mixed type expressions and how to assign a value of one type to a variable of another type. Informing yourself about these rules, or at least knowing where to look them up, will be useful.

Please login to reply.