|
[Sponsors] |
September 29, 2016, 16:09 |
precision in fortran 77
|
#1 |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 279
Rep Power: 12 |
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. |
|
September 29, 2016, 16:40 |
|
#2 |
Senior Member
Holger Dietrich
Join Date: Apr 2011
Location: Germany
Posts: 174
Rep Power: 15 |
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? |
|
September 29, 2016, 16:48 |
|
#3 | |
Senior Member
mohammad
Join Date: Sep 2015
Posts: 279
Rep Power: 12 |
Quote:
|
||
September 29, 2016, 17:07 |
|
#4 |
Senior Member
Holger Dietrich
Join Date: Apr 2011
Location: Germany
Posts: 174
Rep Power: 15 |
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 |
|
September 30, 2016, 19:23 |
|
#5 |
Senior Member
adrin
Join Date: Mar 2009
Posts: 115
Rep Power: 17 |
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 |
|
September 30, 2016, 21:11 |
|
#6 |
Super Moderator
Alex
Join Date: Jun 2012
Location: Germany
Posts: 3,427
Rep Power: 49 |
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.
|
|
September 30, 2016, 21:21 |
|
#7 |
Senior Member
adrin
Join Date: Mar 2009
Posts: 115
Rep Power: 17 |
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 |
|
September 30, 2016, 21:22 |
|
#8 |
Senior Member
adrin
Join Date: Mar 2009
Posts: 115
Rep Power: 17 |
Clarification: my "well said" comment was to Alex (flotus1)
adrin |
|
|
|
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 15:51 |
Fortran quad precision | truffaldino | Main CFD Forum | 4 | September 19, 2012 10:26 |
RE Double precision in Fortran | gonski | Main CFD Forum | 0 | May 6, 2009 22:06 |
Double precision & User Fortran | Martijn | CFX | 3 | April 4, 2009 06:43 |
what's wrong about my code for 2d burgers equation | morxio | Main CFD Forum | 3 | April 27, 2007 11:38 |