visual studio 2010 - Precision not respected -
i use visual studio (2010 sp1) fortran imsl (2011) , can't right precision reals:
program prova use, intrinsic :: iso_fortran_env implicit none integer, parameter :: ikind=selected_real_kind(p=8, r=99) real(kind=ikind) :: a=0.79 real(real64) :: b=0.79 real(kind=16) :: c=0.79 real(8) :: d=0.79 print *, print *, b print *, c print *, d end program prova
give me same result: 0.790000021457672
(one more precision, 1 less precision every number different assigned one: 0.79)
why willingness not respected?
how can set all reals needed precision?
nb: problem has nothing "limited nature of computer", roundoff numbers , similar. problem regards type/kind of variable in fortran.
you setting variables have desired precision, assign constants default precision. try:
program prova use, intrinsic :: iso_fortran_env implicit none integer, parameter :: ikind=selected_real_kind(p=8, r=99) real(kind=ikind) :: a=0.79_ikind real(real64) :: b=0.79_real64 real(kind=16) :: c=0.79_16 real(8) :: d=0.79_8 print *, print *, b print *, c print *, d end program prova
this set constants in correct precision, since provide 2 significant digits, result still rounded nearest representable floating point number (in base 2). 0.79
can represented in decimal system (base 10), not in base 2. hence deviations. should read wikipedia artical on floating-point numbers and, of course, what every computer scientist should know floating-point arithmetic.
this results in
0.79000000000000004 0.79000000000000004 0.790000000000000000000000000000000031 0.79000000000000004
on machine.
Comments
Post a Comment