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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with understanding precision 1

Status
Not open for further replies.

bmccollo

Technical User
Jan 14, 2014
2
0
0
US
I have a limited background in scientific computing. I'm writing a program for EM wave scattering, trying to replicate work done in python. I'm having problems with round-off (I believe) and am hoping someone can explain what is going on. So I have

integer, parameter :: wp = selected_real_kind( 15 )
real (wp) :: epsilon_1,epsilon_2, f
epsilon_1=1.0
epsilon_2=2.05
f=100.0*1.0*10**9
write(*,*) "epsilon_1=",epsilon_1,"epsilon_2=",epsilon_2,"f=",f

So, I want high precision, I'll be making several computations. When I run the code, it writes

epsilon_1= 1.0000000000000000 epsilon_2= 2.0499999523162842 f= 99999997952.000000

Why is epsilon_2 and f rounded the way they are rather than being stored as 2.0500000000000000 etc? I have several variables I'm working with, and they're all giving similar results, I'm worried that there will be lots of error due to this. I tried changing to epsilon_2=2.05_wp and got 2.0499999999999998 but this is still not what I'd expect.

Can someone please tell me what's happening here? Your help is appreciated.

Additionally, is there much benefit to working in fortran rather than python?
 
It has nothing to do with rounding. You only display the floating point numbers without specifying the format. If you want to display them in other way then use the format.
 
For example to display epsilon_2 with 2 decimal places use:
Code:
write(*,'(f5.2)') 'epsilon_2 = ',epsilon_2
 
I slightly disagree Mikrom : you also have a rounding problem. You should write :

Code:
epsilon_1=1.0_wp ! not really necessary for 1.
epsilon_2=2.05_wp 
f=100._wp*1*10**9

François Jacq
 
At some point, I had the exact same question.

The answer is not about how many decimal places you choose to print, instead, it is about the fact that, as humans with 10 fingers, we naturally chose base 10 for us, yet, for our computers, we have chosen base 2.

Different number systems are able to represent different set of numbers exactly and others not...sure, having a finite set of bits makes things worse.

To be sure, every base-2 number can be represented exactly in base-10; but the reverse is not true...there are quite a few base-10 numbers that simply cannot be represented exactly in base-2.

These following two links cover some discussion: Link1, Link2

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top