When is a Nan not a Nan?
Author Message When is a Nan not a Nan?

When I run the following code on an SGI machine I get the output
as indicated:

PROGRAM BITEST
REAL X

X = B'11111111111111111111111111111111'
WRITE(*,*) X

END

Output:

0.0000000E+00

However, according to the IEEE 754 standard, I was expecting
to get NaN.  Does anyone know if there is a reason for this behavior?

Fri, 22 Aug 1997 14:49:05 GMT  When is a Nan not a Nan?

Quote:

>    When I run the following code on an SGI machine I get the output
> as indicated:

>       PROGRAM BITEST
>       REAL X

>       X = B'11111111111111111111111111111111'
>       WRITE(*,*) X

>       END

> Output:

>    0.0000000E+00

>    However, according to the IEEE 754 standard, I was expecting
> to get NaN.  Does anyone know if there is a reason for this behavior?

Since you are assigning an integer constant to a real variable, it might
be reasonable to assume that the type conversion may yield something other
than the expected bit pattern.  I've found that EQUIVALENCE works well to
set specific bit patterns in floating-point variables.  Something like:

PROGRAM BITEST
REAL X
EQUIVALENCE (X, I)

I = B'11111111111111111111111111111111'
WRITE(*,*) X

END

--
"Where a calculator on the ENIAC is equipped with 18,000 vacuum tubes and
weighs 30 tons, computers in the future may have only 1,000 vacuum tubes
and perhaps weigh 1 1/2 tons."   --Popular Mechanics, March 1949

Sat, 23 Aug 1997 00:26:28 GMT  When is a Nan not a Nan?

Quote:

>Since you are assigning an integer constant to a real variable, it might
>be reasonable to assume that the type conversion may yield something other
>than the expected bit pattern.  I've found that EQUIVALENCE works well to
>set specific bit patterns in floating-point variables.  Something like:

>      PROGRAM BITEST
>      REAL X
>      EQUIVALENCE (X, I)

>      I = B'11111111111111111111111111111111'
>      WRITE(*,*) X

>      END

I followed your suggestion and wrote the following program:

PROGRAM BITEST
REAL X, Y
EQUIVALENCE (X, I)

I = B'11111111111111111111111111111111'
Y = B'11111111111111111111111111111111'
WRITE(*,*) X
WRITE(*,*) Y

END

For which I got the following output:

nan
0.0000000E+00

However, I decided to check the assembly code.  I am including the following
fragment:

#   2        REAL X, Y
#   3        EQUIVALENCE (X, I)
#   4
#   5        I = B'11111111111111111111111111111111'
li      \$24, -1
sw      \$24, .bitest_.0
.loc    2 6
#   6        Y = B'11111111111111111111111111111111'
li.s    \$f4, nan
s.s     \$f4, 44(\$sp)
.loc    2 7

So even though Y is set to nan in the assemble code, it is still
equal to 0.0 on output.
I still find this behaviour curious.

Ed Lang

Sat, 30 Aug 1997 01:26:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages