CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

precision in fortran 77

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 2 Post By adrin
  • 1 Post By flotus1

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 29, 2016, 15:09
Default precision in fortran 77
  #1
Senior Member
 
mohammad
Join Date: Sep 2015
Posts: 274
Rep Power: 11
mostanad is on a distinguished road
hello everybody.
i am calculating a geometric series in fortran 77. in fact i have the totall length of series,it's number of statements an stretching factor. my unknown parameter is first number. then i finding the other numbers and find the total length again.all of my parameters are defined by COMMON blocks. after obtaining the first number and finding another numbers in series, i find that my real total length is 0.4999999 but the total from series is 0.4999998 and this difference make for me errors. is any body say me how converge my calculated length of series to real one in fortran 77? thank a lot.
mostanad is offline   Reply With Quote

Old   September 29, 2016, 15:40
Default
  #2
Senior Member
 
DarylMusashi's Avatar
 
Holger Dietrich
Join Date: Apr 2011
Location: Germany
Posts: 174
Rep Power: 15
DarylMusashi is on a distinguished road
Hello mostanad,

maybe it helps to define all dependend variables as double precision (if you dont do it already):

IMPLICIT NONE
REAL*8 :: DOUBLE_PRECISION_VARIABLE

Make sure you alsways take the "D0" at the end when you set values to this variable.

DOUBLE_PRECISION_VARIALBE = 3.1415926D0


If this does not help maybe you can share the source code with the community?
DarylMusashi is offline   Reply With Quote

Old   September 29, 2016, 15:48
Default
  #3
Senior Member
 
mohammad
Join Date: Sep 2015
Posts: 274
Rep Power: 11
mostanad is on a distinguished road
Quote:
Originally Posted by darylmusashi View Post
hello mostanad,

maybe it helps to define all dependend variables as double precision (if you dont do it already):

Implicit none
real*8 :: Double_precision_variable

make sure you alsways take the "d0" at the end when you set values to this variable.

Double_precision_varialbe = 3.1415926d0


if this does not help maybe you can share the source code with the community?
thanks. But as i said i am defining parameters in common blocks in fortran 77. So any of parameters don't have specific parameter type such as integer,.... . Is any command that can change type of parameter to double precision in code lines? For example 1. Change one to a float parameter.
mostanad is offline   Reply With Quote

Old   September 29, 2016, 16:07
Default
  #4
Senior Member
 
DarylMusashi's Avatar
 
Holger Dietrich
Join Date: Apr 2011
Location: Germany
Posts: 174
Rep Power: 15
DarylMusashi is on a distinguished road
Isn*t it possible to define a variable and set it to a parameter after that? Of course take note of the "D0" at the end again.

REAL*8 = PI,VARIABLE_2, RESULT
PARAMETER(PI=3.141592653589793D0,VARIABLE_2=1.2339 2D0)


Secondly a good advise might be not to mix real and integer operations. This might a problem in your case, too. Good example:

RESULT=PI*VARIABLE_2/18.0D0 <---18 is a double precision number

Bad example

RESULT=PI*VARIABLE_2/18 <--- 18 is an integer number
DarylMusashi is offline   Reply With Quote

Old   September 30, 2016, 18:23
Default
  #5
Senior Member
 
adrin
Join Date: Mar 2009
Posts: 115
Rep Power: 17
adrin is on a distinguished road
Using COMMON blocks does not prevent you from assigning the variable type. Based on the little that you said I'm guessing you are not using "implicit none", but using fortran's nuance that variables starting with letters other than I-through-N are real. This is generally considered poor programming practice.

Anyway, considering the above you have two options for setting real*8:

1) You can set all your real variables as real*8 at compile time. All compilers have an option to automatically convert real to real*8 at compile/run time.

2) You can specify real*8 in the code for only those variables that you want to specify as double. For example:

real*8 dbl_var
COMMON /mixed_vars/ int_var, real_var, dbl_var

adrin
sbaffini and FMDenaro like this.
adrin is offline   Reply With Quote

Old   September 30, 2016, 20:11
Default
  #6
Super Moderator
 
flotus1's Avatar
 
Alex
Join Date: Jun 2012
Location: Germany
Posts: 3,401
Rep Power: 47
flotus1 has a spectacular aura aboutflotus1 has a spectacular aura about
Adding to what has already been said: Using higher precision variables is not a magic bullet to avoid round-off or cancellation errors. Without a suitable algorithm you can end up with errors even larger than in your example despite using real8 variables.
sbaffini likes this.
flotus1 is offline   Reply With Quote

Old   September 30, 2016, 20:21
Default
  #7
Senior Member
 
adrin
Join Date: Mar 2009
Posts: 115
Rep Power: 17
adrin is on a distinguished road
Well said! I didn't bother with this component because the original request/message is not easy to comprehend and does not provide any information of value to make recommendations.

To me, if the implementation were correct there would/should be no practical difference between 0.4999999 and 0.4999998. But, again, there was not sufficient information to make any recommendation; in this case, double precision would be the easiest way out (hopefully!)

adrin
adrin is offline   Reply With Quote

Old   September 30, 2016, 20:22
Default
  #8
Senior Member
 
adrin
Join Date: Mar 2009
Posts: 115
Rep Power: 17
adrin is on a distinguished road
Clarification: my "well said" comment was to Alex (flotus1)

adrin
adrin is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CFD Code Conversion from Intel Fortran to GNU Fortran pitto Main CFD Forum 4 August 4, 2016 14:51
Fortran quad precision truffaldino Main CFD Forum 4 September 19, 2012 09:26
RE Double precision in Fortran gonski Main CFD Forum 0 May 6, 2009 21:06
Double precision & User Fortran Martijn CFX 3 April 4, 2009 05:43
what's wrong about my code for 2d burgers equation morxio Main CFD Forum 3 April 27, 2007 10:38


All times are GMT -4. The time now is 12:54.