- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I am Gaurav & just started using fortran. I tried to run following code for which output should be 10.5 but instead I am getting 16.5. Why is it so? please tell
PROGRAM try
REAL (KIND=8):: x,y,z
x=0.5;y=0.25434678E9;z=0.25434677E9
w=x+y-z
PRINT*,w
END PROGRAM try
Regards,
Gaurav K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This has nothing to do with Fortran, but rather with the finite precision of floating-point numbers on computers. You are simply exceeding the limits of the internal representation Compare it to working with 0.33333 and 1/3 in mathematics with paper and pencil. The literal numbers are in _single precision_. That is a rather crucial part of Fortran: the right-hand side is not influenced by the left-hand side. You probably want to use double precision numbers instead:
PROGRAM try
REAL (KIND=8):: x,y,z
x=0.5;y=0.25434678E9_8;z=0.25434677E9_8
w=x+y-z
PRINT*,w
END PROGRAM try
Note the "_8" which is an example of specifying the kind to a literal number. You can also use "d9" instead of "e9" to get double precision.
Regards,
Arjen
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This has nothing to do with Fortran, but rather with the finite precision of floating-point numbers on computers. You are simply exceeding the limits of the internal representation Compare it to working with 0.33333 and 1/3 in mathematics with paper and pencil. The literal numbers are in _single precision_. That is a rather crucial part of Fortran: the right-hand side is not influenced by the left-hand side. You probably want to use double precision numbers instead:
PROGRAM try
REAL (KIND=8):: x,y,z
x=0.5;y=0.25434678E9_8;z=0.25434677E9_8
w=x+y-z
PRINT*,w
END PROGRAM try
Note the "_8" which is an example of specifying the kind to a literal number. You can also use "d9" instead of "e9" to get double precision.
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ifort and gfortran also work with the more modern real64:
PROGRAM try
use iso_fortran_env
REAL (KIND=real64):: x,y,z
x=0.5;y=0.25434678E9_real64;z=0.25434677E9_real64
w=x+y-z
PRINT*,w
END PROGRAM try
If you intend to play with the precision limits, you need parentheses:
w=x+(y-z)
and, in principle, you need to tell ifort to adhere to Fortran standard:
ifort -standard-semantics gk.f90
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
and as W is not specified it would be a default real which unless you have set some specific compiler options will be kind=4. It is best to always have IMPLICIT NONE command in the code after the PROGRAM and any USE statements. This forces you to declare everything which avoids some unexpected things and will find typing/spelling errors in variable names.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page