View previous topic :: View next topic |
Author |
Message |
Helen
Joined: 15 Mar 2018 Posts: 9
|
Posted: Wed Apr 18, 2018 12:10 pm Post subject: floating number problem with write |
|
|
Again, another little problem and I hope I'm not embarassing myself here...
Here is the simple code:
real*8 :: 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!) |
|
Back to top |
|
|
Helen
Joined: 15 Mar 2018 Posts: 9
|
Posted: Wed Apr 18, 2018 12:15 pm Post subject: cracked it! |
|
|
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! |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1886
|
Posted: Wed Apr 18, 2018 1:06 pm Post subject: |
|
|
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. |
|
Back to top |
|
|
|