A floating point question, wierd problem
Author Message A floating point question, wierd problem

Hello there,
I have encounter a wierd problem with floating point numbers. Given
this simple code:
program test
implicit none

real(4)::z,PI2,pi,pie

pi=3.141592653589793238462643d0
pie=3.141592653589793238462643
write(*,fmt='(2E20.10)') pi,pie
PI2=2.0*pi
z=PI2-1e-7
print *,"modulo(z,PI2)=",modulo(z,PI2)
if(z<PI2) print *,"z<PI2"
if(z==PI2) print *,"z=PI2"
if(z>PI2) print *,"z>PI2"
end program

You can compile and run it. I run it on a x86 linux machine with absoft
f90 compiler.
When the "write(*,fmt='(2E20.10)') pi,pie" is there, I get the result
"z=PI2"
While when I comment out the "write" line, result shows "z<PI2"
This is kind of showing randomness of the same code.
Could some one explain this to me?

I have some naive explanation and don't know if it makes sense.
When there is no "write" statment, the compiler does not force pi to be
single precision as I delcared in the code, thus procducing "z<PI2".
When I do the "writing", pi is now forced to be single precision, thus
resulting a "z=PI". When pi is replace with pie, which in statement is
directly given a single precision number, no matter whether there is
"write" or not, result is always "z=PI2".

Does this make any sense?
Thank you very much.

Tue, 22 Jul 2008 02:02:51 GMT  A floating point question, wierd problem
1.e-7 is smaller than the "machine Epsilon
(basically the smallest number that  when added to 1.0 is greater than 1.0.
Sngle precision Epsilon (assuming Intel 32-bit chip) is about: 1.192093E-07
Thus anything smaller than that (like 1.E-7) when added to 1.0 (or 3.14)

Skip Knoble

-|Hello there,
-|I have encounter a wierd problem with floating point numbers. Given
-|this simple code:
-|program test
-|implicit none
-|
-|real(4)::z,PI2,pi,pie
-|
-|pi=3.141592653589793238462643d0
-|pie=3.141592653589793238462643
-|write(*,fmt='(2E20.10)') pi,pie
-|PI2=2.0*pi
-|z=PI2-1e-7
-|print *,"modulo(z,PI2)=",modulo(z,PI2)
-|if(z<PI2) print *,"z<PI2"
-|if(z==PI2) print *,"z=PI2"
-|if(z>PI2) print *,"z>PI2"
-|end program
-|
-|You can compile and run it. I run it on a x86 linux machine with absoft
-|f90 compiler.
-|When the "write(*,fmt='(2E20.10)') pi,pie" is there, I get the result
-|"z=PI2"
-|While when I comment out the "write" line, result shows "z<PI2"
-|This is kind of showing randomness of the same code.
-|Could some one explain this to me?
-|
-|I have some naive explanation and don't know if it makes sense.
-|When there is no "write" statment, the compiler does not force pi to be
-|single precision as I delcared in the code, thus procducing "z<PI2".
-|When I do the "writing", pi is now forced to be single precision, thus
-|resulting a "z=PI". When pi is replace with pie, which in statement is
-|directly given a single precision number, no matter whether there is
-|"write" or not, result is always "z=PI2".
-|
-|Does this make any sense?
-|Thank you very much.

Tue, 22 Jul 2008 02:30:34 GMT  A floating point question, wierd problem

Quote:
> 1.e-7 is smaller than the "machine Epsilon
> (basically the smallest number that  when added to 1.0 is greater than 1.0.
> Sngle precision Epsilon (assuming Intel 32-bit chip) is about: 1.192093E-07
> Thus anything smaller than that (like 1.E-7) when added to 1.0 (or 3.14)

Which doesn't explain why it sometimes does have an effect.

Some processors keep single precision data in double precision
registers longer than they should.  The write forces it to
store the value in the variable and reload it later.

-- glen

Tue, 22 Jul 2008 02:54:26 GMT  A floating point question, wierd problem
Glen:  You are correct. For example the XLF option -qfloat=nomaf
that tries to "turn off" floaating point inconsistencies.

-|> 1.e-7 is smaller than the "machine Epsilon
-|> (basically the smallest number that  when added to 1.0 is greater than 1.0.
-|> Sngle precision Epsilon (assuming Intel 32-bit chip) is about: 1.192093E-07
-|> Thus anything smaller than that (like 1.E-7) when added to 1.0 (or 3.14)
-|
-|Which doesn't explain why it sometimes does have an effect.
-|
-|Some processors keep single precision data in double precision
-|registers longer than they should.  The write forces it to
-|store the value in the variable and reload it later.
-|
-|-- glen

Tue, 22 Jul 2008 03:02:49 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages