Thread Subject: NaN's in C (C++) interfacing to engine

Subject: NaN's in C (C++) interfacing to engine

From: Fred Ma

Date: 4 Mar, 2003 05:45:50

Message: 1 of 13

Hello,

Some of my vectors are deliberately set to NaN
because I want to distinguish those array positions
holding valid data versus no data. That's fine in
matlab, but now I'm connecting to a matlab engine
with my C++ program (gcc on solaris). Is there a
way to refer to these NaN values, and to test the
mxArray for them?

Thanks.

Fred

Subject: NaN's in C (C++) interfacing to engine

From: Randy Poe

Date: 4 Mar, 2003 11:25:51

Message: 2 of 13

Fred Ma wrote:
> Hello,
>
> Some of my vectors are deliberately set to NaN
> because I want to distinguish those array positions
> holding valid data versus no data. That's fine in
> matlab, but now I'm connecting to a matlab engine
> with my C++ program (gcc on solaris). Is there a
> way to refer to these NaN values, and to test the
> mxArray for them?

Can't you use the isnan() call within the engine?

NaN is defined within the C and C++ standards, so within
your code you could use something like this:

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.15

It's equivalent to isnan(). It relies on the weird property
(defined by the language standards?) that NaN is not equal
to itself.

This article is from the FAQ for alt.comp.lang.learn.c-c++.

         - Randy

Subject: NaN's in C (C++) interfacing to engine

From: Fred Ma

Date: 4 Mar, 2003 12:32:26

Message: 3 of 13

Fred Ma wrote:

> Hello,
>
> Some of my vectors are deliberately set to NaN
> because I want to distinguish those array positions
> holding valid data versus no data. That's fine in
> matlab, but now I'm connecting to a matlab engine
> with my C++ program (gcc on solaris). Is there a
> way to refer to these NaN values, and to test the
> mxArray for them?
>
> Thanks.
>
> Fred

Actually, I'm running into problems in that front too
(see posting below). I can't get gcc (3.2) to meet
the standard sanity checks for ability to handle NaN.
I was hoping to find some other way.

If the ability to handle NaN's in the compiler depends on
the build, I'm out of luck in that direction I tried 3 or 4 times
to build gcc, each taking a better portion of a day, and
much more than that on the first try, including
installing all utilities required to test it. It failed alot
of the tests. And since I'm not sysadmin, I was trying
to build my own personal copy. In the end, I kindly
asked the sysadmin to download and install the
solaris binaries.

Fred

-------- Original Message --------
Subject: Using NaNs: _GNU_SOURCE and <cmath>
Date: Tue, 04 Mar 2003 05:24:40 -0500
From: Fred Ma <fma@doe.carleton.ca>
Organization: Bell Sympatico
Newsgroups: gnu.gcc.help,gnu.g++.help

Hello,

According to the documentation for glibc, I can
use Not-a-Number (NaN) if I define _GNU_SOURCE
prior to including any files, and include math.h. Since
I'm using g++, I included <cmath> instead. However,
NaN's are still not defined. I should be able to test
for their recognition by testing the preprocessor
macro NAN:

    #ifdef NAN
    #warning "NAN is defined"
    #endif

The documentation says this is applicable on platforms
for which IEEE NaN is defined. I tried this on both
Win2K/SP3 (Intel Pentium III) running cygwin's bash,
as well as solaris 8. I'm using the most recent gcc.

I have been assuming that glibc is part of gcc. Is this
the case? If so, is would it still work if I include cmath
instead of math.h, as above? Would anyone know if
the two platforms I tried support IEEE NaNs?

Fred

Subject: NaN's in C (C++) interfacing to engine

From: Fred Ma

Date: 4 Mar, 2003 14:00:29

Message: 4 of 13

Fred Ma wrote:

> Fred Ma wrote:
>
> > Hello,
> >
> > Some of my vectors are deliberately set to NaN
> > because I want to distinguish those array positions
> > holding valid data versus no data. That's fine in
> > matlab, but now I'm connecting to a matlab engine
> > with my C++ program (gcc on solaris). Is there a
> > way to refer to these NaN values, and to test the
> > mxArray for them?
> >
> > Thanks.
> >
> > Fred
>
> Actually, I'm running into problems in that front too
> (see posting below). I can't get gcc (3.2) to meet
> the standard sanity checks for ability to handle NaN.
> I was hoping to find some other way.
>
> If the ability to handle NaN's in the compiler depends on
> the build, I'm out of luck in that direction I tried 3 or 4 times
> to build gcc, each taking a better portion of a day, and
> much more than that on the first try, including
> installing all utilities required to test it. It failed alot
> of the tests. And since I'm not sysadmin, I was trying
> to build my own personal copy. In the end, I kindly
> asked the sysadmin to download and install the
> solaris binaries.
>
> Fred
>
> -------- Original Message --------
> Subject: Using NaNs: _GNU_SOURCE and <cmath>
> Date: Tue, 04 Mar 2003 05:24:40 -0500
> From: Fred Ma <fma@doe.carleton.ca>
> Organization: Bell Sympatico
> Newsgroups: gnu.gcc.help,gnu.g++.help
>
> Hello,
>
> According to the documentation for glibc, I can
> use Not-a-Number (NaN) if I define _GNU_SOURCE
> prior to including any files, and include math.h. Since
> I'm using g++, I included <cmath> instead. However,
> NaN's are still not defined. I should be able to test
> for their recognition by testing the preprocessor
> macro NAN:
>
> #ifdef NAN
> #warning "NAN is defined"
> #endif
>
> The documentation says this is applicable on platforms
> for which IEEE NaN is defined. I tried this on both
> Win2K/SP3 (Intel Pentium III) running cygwin's bash,
> as well as solaris 8. I'm using the most recent gcc.
>
> I have been assuming that glibc is part of gcc. Is this
> the case? If so, is would it still work if I include cmath
> instead of math.h, as above? Would anyone know if
> the two platforms I tried support IEEE NaNs?
>
> Fred

I forgot to add that yes, I can handle NaNs if I confine
myself to the matlab engine's workspace, but it's
processing that data in the c++ world that I'm trying I'm
trying to find a way to do.

Fred

Subject: NaN's in C (C++) interfacing to engine

From: Cheryl Jones

Date: 5 Mar, 2003 11:21:51

Message: 5 of 13

I don't have a problem with gcc on solaris. isnan is defined. To
generate a nan I use:

inf = ldexp(1.0, 1024);
mynan = inf*0;


Fred Ma wrote:

> Hello,
>
> Some of my vectors are deliberately set to NaN
> because I want to distinguish those array positions
> holding valid data versus no data. That's fine in
> matlab, but now I'm connecting to a matlab engine
> with my C++ program (gcc on solaris). Is there a
> way to refer to these NaN values, and to test the
> mxArray for them?
>
> Thanks.
>
> Fred
>
>

Subject: NaN's in C (C++) interfacing to engine

From: Temu Gautama

Date: 5 Mar, 2003 11:29:23

Message: 6 of 13

Hi,


I could be wrong, but if x is NaN, wouldn't it be possible to do
something along the lines of:


if (x!=x)
printf ("NaN detected\n");


???


Temu


Fred Ma wrote:
>
>
> Hello,
>
> Some of my vectors are deliberately set to NaN
> because I want to distinguish those array positions
> holding valid data versus no data. That's fine in
> matlab, but now I'm connecting to a matlab engine
> with my C++ program (gcc on solaris). Is there a
> way to refer to these NaN values, and to test the
> mxArray for them?
>
> Thanks.
>
> Fred
>
>

Subject: NaN's in C (C++) interfacing to engine

From: Fred Ma

Date: 5 Mar, 2003 15:52:24

Message: 7 of 13

Temu Gautama wrote:

> Hi,
>
> I could be wrong, but if x is NaN, wouldn't it be possible to do
> something along the lines of:
>
> if (x!=x)
> printf ("NaN detected\n");
>
> ???
>
> Temu

Thank you Cheryl and Temu.

I confirmed the ability to test for Nans using the simple
code attached below. Since isnan and isinf are nonstandard
functions, they don't show up in the standard C textbooks,
so it's luck as to which one is supported on which system,
and guesswork as to the name of any functions like
isnan and isinf. For example, the glibc test that I posted
earlier this thread doesn't tell me whether I'm using glibc,
or whether I'm relaying on gcc/gnu libraries, or sun/solaris
libraries, or both of the latter. The test code below runs on
cygwin (gcc), but gcc on our solaris 8 apparently doesn't have
isinf.

Fred

-------- Original Message --------
Subject: Re: NaN's in C (C++) interfacing to engine
Date: Wed, 05 Mar 2003 11:21:51 -0500
From: Cheryl Jones <cherylj@mathworks.com>
Organization: The MathWorks, Inc.
To: Fred Ma <fma@doe.carleton.ca>
Newsgroups: comp.soft-sys.matlab
References: <3E6483DE.BF9AF598@doe.carleton.ca>

I don't have a problem with gcc on solaris. isnan is defined. To
generate a nan I use:

inf = ldexp(1.0, 1024);
mynan = inf*0;
=================================================
$$ cat Nan.cpp

     #include<cstdlib>
     #include<cmath>
     #include<cstdio>

     int main(){

        double OKdouble=exp(9.0);
        printf("OKdouble=exp(9.0)=%g\n",OKdouble);
        if(OKdouble!=OKdouble) printf("OKdouble!=OKdouble.\n");
        else if(OKdouble==OKdouble)
     printf("OKdouble==OKdouble.\n");
        else printf("OKdouble??OKdouble.\n");
        printf("isnan(OKdouble)=%i\n",isnan(OKdouble));
        printf("isinf(OKdouble)=%i\n",isinf(OKdouble));

        double MyInf=exp(99999.0);
        printf("MyInf=exp(99999.0)=%g\n",MyInf);
        if(MyInf!=MyInf) printf("MyInf!=MyInf.\n");
        else if(MyInf==MyInf) printf("MyInf==MyInf.\n");
        else printf("MyInf??MyInf.\n");
        printf("isnan(MyInf)=%i\n",isnan(MyInf));
        printf("isinf(MyInf)=%i\n",isinf(MyInf));

        double MyNan=0.0*MyInf;
        printf("MyNan=0.0*MyInf=%g\n",MyNan);
        if(MyNan!=MyNan) printf("MyNan!=MyNan.\n");
        else if(MyNan==MyNan) printf("MyNan==MyNan.\n");
        else printf("MyNan??MyNan.\n");
        printf("isnan(MyNan)=%i\n",isnan(MyNan));
        printf("isinf(MyNan)=%i\n",isinf(MyNan));

     };

$$ g++ Nan.cpp
$$ a <======== (a.out on unix systems)

     OKdouble=exp(9.0)=8103.08
     OKdouble==OKdouble.
     isnan(OKdouble)=0
     isinf(OKdouble)=0
     MyInf=exp(99999.0)=Inf
     MyInf==MyInf.
     isnan(MyInf)=0
     isinf(MyInf)=1
     MyNan=0.0*MyInf=NaN
     MyNan!=MyNan.
     isnan(MyNan)=1
     isinf(MyNan)=0

Subject: NaN's in C (C++) interfacing to engine

From: Cheryl Jones

Date: 6 Mar, 2003 11:11:09

Message: 8 of 13

You are correct in saying that each platform is different. In general
there are three routines:

isnan
isinf
finite

If you have 2 of these routines, you can deduce the remaining one ie. on
Solaris, if its not a nan and its not finite, its an inf.

Remember there are two infs, +inf and -inf.
NaN is a family of numbers.

Fred Ma wrote:

> Temu Gautama wrote:
>
>
>>Hi,
>>
>>I could be wrong, but if x is NaN, wouldn't it be possible to do
>>something along the lines of:
>>
>>if (x!=x)
>> printf ("NaN detected\n");
>>
>>???
>>
>>Temu
>>
>
> Thank you Cheryl and Temu.
>
> I confirmed the ability to test for Nans using the simple
> code attached below. Since isnan and isinf are nonstandard
> functions, they don't show up in the standard C textbooks,
> so it's luck as to which one is supported on which system,
> and guesswork as to the name of any functions like
> isnan and isinf. For example, the glibc test that I posted
> earlier this thread doesn't tell me whether I'm using glibc,
> or whether I'm relaying on gcc/gnu libraries, or sun/solaris
> libraries, or both of the latter. The test code below runs on
> cygwin (gcc), but gcc on our solaris 8 apparently doesn't have
> isinf.
>
> Fred
>
> -------- Original Message --------
> Subject: Re: NaN's in C (C++) interfacing to engine
> Date: Wed, 05 Mar 2003 11:21:51 -0500
> From: Cheryl Jones <cherylj@mathworks.com>
> Organization: The MathWorks, Inc.
> To: Fred Ma <fma@doe.carleton.ca>
> Newsgroups: comp.soft-sys.matlab
> References: <3E6483DE.BF9AF598@doe.carleton.ca>
>
> I don't have a problem with gcc on solaris. isnan is defined. To
> generate a nan I use:
>
> inf = ldexp(1.0, 1024);
> mynan = inf*0;
> =================================================
> $$ cat Nan.cpp
>
> #include<cstdlib>
> #include<cmath>
> #include<cstdio>
>
> int main(){
>
> double OKdouble=exp(9.0);
> printf("OKdouble=exp(9.0)=%g\n",OKdouble);
> if(OKdouble!=OKdouble) printf("OKdouble!=OKdouble.\n");
> else if(OKdouble==OKdouble)
> printf("OKdouble==OKdouble.\n");
> else printf("OKdouble??OKdouble.\n");
> printf("isnan(OKdouble)=%i\n",isnan(OKdouble));
> printf("isinf(OKdouble)=%i\n",isinf(OKdouble));
>
> double MyInf=exp(99999.0);
> printf("MyInf=exp(99999.0)=%g\n",MyInf);
> if(MyInf!=MyInf) printf("MyInf!=MyInf.\n");
> else if(MyInf==MyInf) printf("MyInf==MyInf.\n");
> else printf("MyInf??MyInf.\n");
> printf("isnan(MyInf)=%i\n",isnan(MyInf));
> printf("isinf(MyInf)=%i\n",isinf(MyInf));
>
> double MyNan=0.0*MyInf;
> printf("MyNan=0.0*MyInf=%g\n",MyNan);
> if(MyNan!=MyNan) printf("MyNan!=MyNan.\n");
> else if(MyNan==MyNan) printf("MyNan==MyNan.\n");
> else printf("MyNan??MyNan.\n");
> printf("isnan(MyNan)=%i\n",isnan(MyNan));
> printf("isinf(MyNan)=%i\n",isinf(MyNan));
>
> };
>
> $$ g++ Nan.cpp
> $$ a <======== (a.out on unix systems)
>
> OKdouble=exp(9.0)=8103.08
> OKdouble==OKdouble.
> isnan(OKdouble)=0
> isinf(OKdouble)=0
> MyInf=exp(99999.0)=Inf
> MyInf==MyInf.
> isnan(MyInf)=0
> isinf(MyInf)=1
> MyNan=0.0*MyInf=NaN
> MyNan!=MyNan.
> isnan(MyNan)=1
> isinf(MyNan)=0
>
>

Subject: NaN's in C (C++) interfacing to engine

From: Fred Ma

Date: 6 Mar, 2003 11:35:00

Message: 9 of 13

Cheryl Jones wrote:

> You are correct in saying that each platform is different. In general
> there are three routines:
>
> isnan
> isinf
> finite
>
> If you have 2 of these routines, you can deduce the remaining one ie. on
> Solaris, if its not a nan and its not finite, its an inf.
>
> Remember there are two infs, +inf and -inf.
> NaN is a family of numbers.

Thanks, Cheryl. That should be handy reference knowledge
for the future. I've also changed the program so that I don't
rely on NaNs. The portability problem is disturbing.

Fred

Subject: NaN's in C (C++) interfacing to engine

From: Duane Bozarth

Date: 6 Mar, 2003 11:13:24

Message: 10 of 13



Cheryl Jones wrote:
>
> .... NaN is a family of numbers.

Isn't that a family of <non>numbers??? :)

Subject: NaN's in C (C++) interfacing to engine

From: Fred Ma

Date: 7 Mar, 2003 23:06:08

Message: 11 of 13

Duane Bozarth wrote:

> Cheryl Jones wrote:
> >
> > .... NaN is a family of numbers.
>
> Isn't that a family of <non>numbers??? :)

Yes, exactly. The Brady Bunch is normal
numbers. NaNs are the Adams family.
Think of the parallels. Ifin-It. Cousin-It.

F

Subject: NaN's in C (C++) interfacing to engine

From: Cheryl Jones

Date: 10 Mar, 2003 11:33:51

Message: 12 of 13

A NaN, an Inf and a Finite number are talking in a bar.

The finite number says:

I am who I am (a==a)

The NaN says:

I cannot say who I am but I am not nothing! (a != <anything>)

The Inf says:

I am who I am (a==a)
but I have my highs and lows (+inf, -inf)


Fred Ma wrote:

> Duane Bozarth wrote:
>
>
>>Cheryl Jones wrote:
>>
>>>.... NaN is a family of numbers.
>>>
>>Isn't that a family of <non>numbers??? :)
>>
>
> Yes, exactly. The Brady Bunch is normal
> numbers. NaNs are the Adams family.
> Think of the parallels. Ifin-It. Cousin-It.
>
> F
>
>

Subject: NaN's in C (C++) interfacing to engine

From: fma@doe.carleton.ca (f)

Date: 13 Mar, 2003 20:13:20

Message: 13 of 13

Cheryl Jones <cherylj@mathworks.com> wrote in message news:<3E6CBE6F.8050002@mathworks.com>...
> A NaN, an Inf and a Finite number are talking in a bar.
>
> The finite number says:
>
> I am who I am (a==a)
>
> The NaN says:
>
> I cannot say who I am but I am not nothing! (a != <anything>)
>
> The Inf says:
>
> I am who I am (a==a)
> but I have my highs and lows (+inf, -inf)


GGgggrRRRrrooooOOooAaaaAAANNnnnnn....

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com