A questions about NaN and Infinity in GCC

Yao Qi qiyaoltc@cn.ibm.com
Tue Sep 6 05:53:00 GMT 2005


I am work on a floating points test according to IEEE754 and IEEE754 specifies
that seven invalid arithmetic operations shall deliver a NaN unless they are
trapped:
sqrt(Negative), 0*Infinity, 0.0/0.0, Infinity/Infinity, REMAINDER(Anything,0.0),
REMAINDER(Infinity, Anything), Infinity - Infinity.

I write a small case to verify part of this, and I list the source code here for
your reference,
       1 #include<stdio.h>
       2
       3 int main()
       4 {
       5   /* INF - INF => NaN.  */
       6   if ((__builtin_inf() - __builtin_inf()) !=  __builtin_nan(""))
       7     printf("error 1\n");
       8   /* 0 * INF => NaN.  */
       9   if ((0*__builtin_inf()) !=  __builtin_nan(""))
      10     printf("error 2\n");
      11   /* INF/INF => NaN.  */
      12   if ((__builtin_inf()/__builtin_inf()) !=  __builtin_nan(""))
      13     printf("error 3\n");
      14   /* INF + INF => INF.  */
      15   if ((__builtin_inf() + __builtin_inf()) != __builtin_inf())
      16     printf("error 4\n");
      17   if (0.0/0.0 != __builtin_nan(""))
      18     printf("error 5\n");
      19
      20   return 0;
      21 }

And I compile it like this:
[qiyao@ qiyao]$ gcc -o nan-3 nan-3.c
And run it,
[qiyao@ qiyao]$ ./nan-3
error 1
error 2
error 3
error 5

It seems that the result do not obey IEEE754.  I am not so sure about it, Anyone
could confirm or deny this?  Thanks in advance!
-- 
Regards, Yao

Yao Qi



More information about the Gcc mailing list