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

Multiplying by 0.01 is not equivalent to dividing by 100.

merckx
Beginner
1,170 Views
In Compaq Visual Fortran 6.6 (but perhaps also in other Fortran compiler)
the reals b and c defined as
b = a * 0.01
and
c = a / 100.
are different for some values of the real a
The difference is less than the availableaccuracy on a real.
Could someone give me an explanation for this ?
Christian
0 Kudos
3 Replies
greldak
Beginner
1,170 Views
0.01 cannot be defined exactly in binary which is what the computer uses so any calculations based on it will be subject to the error inherrant in this conversion.
0 Kudos
merckx
Beginner
1,170 Views
Ok... obviously
Thanks
Christian
0 Kudos
Intel_C_Intel
Employee
1,170 Views
Hello.

We have very good experience with the accuracy of using DOUBLE PRECISION throughout the entire code and also to enable SSE2 calculations. To do this, please compile your code this way:

ifort /fpconstant /QxN (or QxP if you have Pentium4 Prescott)

The /fpconstant option increases the precision of single precision constants to double precision and the /QxN (or /QxP) enables the SSE2 instruction set for accurate (and potentially packed) calculation of 64 bit double precision numbers.

To be even more careful (maybe the compiler options change in the future?), please reqrite your code this way:

double precision a, b, c
b = a * 0.01D0
c = a / 100.0D0

Now the results should not deviate that much I think.

Lars Petter
0 Kudos
Reply