This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: A questions about NaN and Infinity in GCC





Yao Qi wrote on 06/09/2005 08:48:16:

> 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.  */
[removed similar stuff]
> 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!

You missed one part of IEEE754. NaN is never equal to any value, including
itself.
You can test for NaN by doing the following (seemingly) absurd test:
       3 int main()
       4 {
       5   double res;
       5   /* INF - INF => NaN.  */
       6   res=__builtin_inf() - __builtin_inf();
       7   if (res == res)
       8     printf("error 1\n"); /* A NaN is always different from
itself*/

You can also use isnan(__buiting_inf()-__builtin_inf())

  Michael


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]