CFD Online Logo CFD Online URL
Home > Forums > Main CFD Forum

Simple question about Fortran

Register Blogs Members List Search Today's Posts Mark Forums Read

LinkBack Thread Tools Display Modes
Old   March 5, 2012, 04:03
Default Simple question about Fortran
New Member
Join Date: Jan 2012
Posts: 8
Rep Power: 7
effort8 is on a distinguished road
Sponsored Links
Followings are fortran programming.

implicit none

real(8) :: a=0.78

print *,a


---> a=0.779999971389

I think a should be "0.7800000000...".
But there is some difference with exact a.

I would like to know what the problem is...

effort8 is offline   Reply With Quote
Sponsored Links

Old   March 5, 2012, 04:16
New Member
Join Date: Mar 2012
Posts: 8
Rep Power: 7
Fabian82 is on a distinguished road
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:

program test
implicit none

! 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.

program test2
implicit none
integer, parameter :: rp = 8

real(kind=rp) :: a = 0.78_rp

end program test2
Fabian82 is offline   Reply With Quote

Old   March 5, 2012, 06:11
Default Thank-you
New Member
Join Date: Jan 2012
Posts: 8
Rep Power: 7
effort8 is on a distinguished road
Thank-you for your answer.
effort8 is offline   Reply With Quote


Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Question - or not? Florian2 Main CFD Forum 10 November 17, 2011 17:48
SIMPLE Algo question hornig Main CFD Forum 0 November 10, 2011 17:17
Very simple question Vashishth Patel CFX 10 November 8, 2011 05:26
Comparison between C/C++ and Fortran? rick Main CFD Forum 45 September 6, 2011 00:52
Simple CFD question for those in the know judderod Main CFD Forum 8 August 5, 2011 11:05

Sponsored Links

All times are GMT -4. The time now is 16:15.