From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: NaN2: Another NaN, an NaN alternative, two types of NaN?
Date: Thu, 12 Mar 2009 09:04:04 +0000 (UTC)
Organization: Boeing
Lines: 46
Message-ID: <gpaj64$e5b$>
References: <gpaejq$heu$> <gpagqo$b9e$> <gpah4d$183$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1236848644 14507 (12 Mar 2009 09:04:04 GMT)
NNTP-Posting-Date: Thu, 12 Mar 2009 09:04:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: comp.soft-sys.matlab:524262

"Roger Stafford" <> wrote in message <gpah4d$183$>...
> "Roger Stafford" <> wrote in message <gpagqo$b9e$>...
> > ......
> >   ......  However, you do have the problem of recognizing the difference afterwards.  At the moment I cannot think of a way to do it.
> > ......
> ... except of course by using 'format hex'.
> Roger Stafford

Timing is everything ... I was just looking into this topic because of some negative zero behavior I ran into in Fortran and was looking to see how MATLAB handled it, and the NaN patterns as well.

Interestingly, format hex will reveal the NaN pattern difference, but num2hex will not. For example, this simple mex routine produces a NaN pattern with all bits set:

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    unsigned long *ulp;
    plhs[0] = mxCreateDoubleScalar(0.0);
    ulp = mxGetData(plhs[0]);
    *ulp++ = 0xFFFFFFFFu;
    *ulp = 0xFFFFFFFFu;

When you look at the results of this mex function with format hex, you get:

ans =

But when you do a num2hex on this same value, you get:

ans =

So MATLAB does some type of value conversion for the num2hex function and always produces the same default NaN pattern for display regardless of the actual underlying NaN pattern, whereas the format hex just does a hex conversion of the raw bits apparently. The same type of behavior can be seen for negative zero. You can see that MATLAB will produce it using format hex, but a normal display will not reveal it, nor will a num2hex call:

>> -0
ans =
>> num2hex(-0)
ans =

I am currently working on a suite of functions to handle sign bits directly and different NaN patterns ... getting, setting, comparing bit patterns directly, etc. Will probably upload it to the FEX sometime this weekend so should be available sometime early next week.

James Tursa