Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Why different result for 1/10.0

우왕희_WooWangHee
699 Views

Real(4) :: B
Real(8) :: C
Real(16) :: D

B=1/10.0
C=1/10.0
D=1/10.0

Result....

B=0.1000000

C=0.100000001490116

D=0.100000001490116119384765625000000

0 Kudos
1 Solution
tropfen
New Contributor I
699 Views
Hello,

your statments are solved from right to left.

step one dividing a integer constant with a real constant. (both by default kind 4)

step two assigning to your variable

To get different results you have to change your constants

[bash]real*4 :: rb
real*8 :: rc
real*16:: rd

rb=1/10.0
rc=1/10.0D0
rd=1/10.0Q0

write(6,'(1f40.38)')rb
write(6,'(1f40.38)')rc
write(6,'(1f40.38)')rd[/bash]
In line 6 the constant is changed to real*8
in line 7 to real*18

Frank

View solution in original post

0 Kudos
4 Replies
Arjen_Markus
Honored Contributor II
699 Views
A value like 1/10 can not be represented exactly as a finite binary fraction, just like 1/3 can not
be represented as an exact finite decimal fraction.

You get the best approximation that is possible with single-precision, double- and quadruple-precision.
If you convert the number back to a decimal representation, there simply has to be a slight bias.

Regards,

Arjen
0 Kudos
tropfen
New Contributor I
700 Views
Hello,

your statments are solved from right to left.

step one dividing a integer constant with a real constant. (both by default kind 4)

step two assigning to your variable

To get different results you have to change your constants

[bash]real*4 :: rb
real*8 :: rc
real*16:: rd

rb=1/10.0
rc=1/10.0D0
rd=1/10.0Q0

write(6,'(1f40.38)')rb
write(6,'(1f40.38)')rc
write(6,'(1f40.38)')rd[/bash]
In line 6 the constant is changed to real*8
in line 7 to real*18

Frank
0 Kudos
Arjen_Markus
Honored Contributor II
699 Views
Argh, missed that "obvious" bit. Actually, it is not from right to left, but the right-hand side is
evaluated without regard to the left-hand side. So, indeed: 1/10.0 is evaluated using single-precision
as that is the highest precision in the two operands and then it is converted to the precision of the
left-hand side.

Regards,

Arjen
0 Kudos
mecej4
Honored Contributor III
699 Views
Repeat using 8.0 or 16.0 in place of 10.0 and you will probably find the answer yourself.
0 Kudos
Reply