Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question about Fortran 1

Status
Not open for further replies.

Emmo2010

Programmer
Jun 23, 2010
2
GB
I use Lahey Fortran 95 version 5.6 in my programming. Unfortunately, I do not know why I get Inaccurate result when I run my program. For example, after running the following code:

PROGRAM TEST
IMPLICIT NONE
REAL:: A
DO A=1.0,3.0,0.2
WRITE (*,*) A
END DO
END PROGRAM TEST

I get the following result:

1.00000000
1.20000005
1.40000010
1.60000014
1.80000019
2.00000024
2.20000029
2.40000033
2.60000038
2.80000043
3.00000048
 
That's because "REAL" declares single precision. To use double precision, try declaring

"double precision :: A"

instead of

"real :: A"

I wouldn't expect the above code to work, though, since -- as far as I know -- DO loops need to be done with integers. I suspect that it's some sort of feature of Lahey to accept real as well.

I don't have Lahey, so I can't test this. If you still get the problem, you might need to use

"DO A=1.0d0,3.0d0,0.2d0"

The "d0" ensures that it will use double precision. Again, this is as far as I know -- the really knowledgeable users on this forum may disagree.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Ah, I just saw that g95 has the -freal-loops option to enable real loops. With that option, as I suspected, the "d0" was necessary.

If you compile this code:

[CODE/]PROGRAM TEST
IMPLICIT NONE
DOUBLE PRECISION :: A
DO A=1.0d0,3.0d0,0.2d0
WRITE (*,*) A
END DO
END PROGRAM TEST
[/CODE]

and run it, you get:

Code:
1.
1.2
1.4
1.5999999999999999
1.7999999999999998
1.9999999999999998
2.1999999999999997
2.4
2.6
2.8000000000000003
3.0000000000000004

Those tiny errors are just that: tiny. So tiny that they won't have any noticeable impact on your calculations. If you used some sort of format for the print (other than the free format you've specified) that used less than 15 decimal places, you wouldn't even see it the error.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Thank you for your valuable contributions. I used double precision and I got exact result:

1.00000000000000000
1.20000000000000000
.
.
.
3.00000000000000000

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top