Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

Conversion problems; single to double precision

verboomziggo_nl
Beginner
525 Views
I have a problem with the conversion from single to double precision.
With the following statements and results:
real a
real*8 b

a=3. shows in Watch: 3.000000
b=3.d0 shows 3.00000000000000

and

b=a shows 3.00000000000000 sofar so good

BUT

a=3.1 shows 3.100000

b=a shows 3.09999990463257
and
b=2.d0*b shows 6.199999980926514

so the wrong bits are kept and used.

I got the same (!) results with the CVF compiler
a reason to try the Intel compiler.

Question: what am I doing wrong or mis-understanding?

0 Kudos
2 Replies
Steven_L_Intel1
Employee
525 Views

Read the three-part article "The Perils of Real Numbers", starting here. The correct bits are being used, but you are missing the difference between binary fractions and decimal fractions.
0 Kudos
jimdempseyatthecove
Honored Contributor III
525 Views
Quoting - verboomziggo.nl
I have a problem with the conversion from single to double precision.
With the following statements and results:
real a
real*8 b

a=3. shows in Watch: 3.000000
b=3.d0 shows 3.00000000000000

and

b=a shows 3.00000000000000 sofar so good

BUT

a=3.1 shows 3.100000

b=a shows 3.09999990463257
and
b=2.d0*b shows 6.199999980926514

so the wrong bits are kept and used.

I got the same (!) results with the CVF compiler
a reason to try the Intel compiler.

Question: what am I doing wrong or mis-understanding?


0.1 is an infinately repeating binary fraction and therefor cannot be precisely represented in neither real*4 nor real*8. Therefore 3.1 cannot be precisely represented.

b=a shows 3.09999990463257
and
b=2.d0*b shows 6.199999980926514

try

b=3.1D0
b=2.D0*b

Jim Dempsey

0 Kudos
Reply