Simple question about Fortran
Followings are fortran programming.
real(8) :: a=0.78
I think a should be "0.7800000000...".
But there is some difference with exact a.
I would like to know what the problem is...
If you write a floating point number in Fortran, it assumes that the number has single precision. If you assign this number to a double precision value, the additional digits are filled up with random values. So you should add an underscore and the desired precision the each number which is directly coded in your program. For example:
! this is equivalent to a = 0.78_4
real(8) :: a = 0.78
! this is better
real(8) :: b = 0.78_8
end program test
The small difference in the last number is due to fact that not all decimal fractions may be described by IEEE floating point number exactly. There's always a relative difference of approx 2**(-52) which relates to the number of bits of the mantisse.
In order to handle the precisions in Fortran in a flexible way, I suggest defining a parameter for each precision in use, i.e.
integer, parameter :: rp = 8
real(kind=rp) :: a = 0.78_rp
end program test2
Thank-you for your answer.
|All times are GMT -4. The time now is 15:03.|