This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: A questions about NaN and Infinity in GCC
- From: Michael Veksler <VEKSLER at il dot ibm dot com>
- To: Yao Qi <qiyaoltc at cn dot ibm dot com>
- Cc: gcc <gcc at gcc dot gnu dot org>
- Date: Tue, 6 Sep 2005 09:25:14 +0300
- Subject: 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