forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

floating number problem with write

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Helen



Joined: 15 Mar 2018
Posts: 9

PostPosted: Wed Apr 18, 2018 12:10 pm    Post subject: floating number problem with write Reply with quote

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
View user's profile Send private message
Helen



Joined: 15 Mar 2018
Posts: 9

PostPosted: Wed Apr 18, 2018 12:15 pm    Post subject: cracked it! Reply with quote

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
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1884

PostPosted: Wed Apr 18, 2018 1:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group