Path: news.mathworks.com!not-for-mail
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$1@fred.mathworks.com>
References: <gpaejq$heu$1@fred.mathworks.com> <gpagqo$b9e$1@fred.mathworks.com> <gpah4d$183$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1236848644 14507 172.30.248.38 (12 Mar 2009 09:04:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 12 Mar 2009 09:04:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:524262

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpah4d$183$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gpagqo$b9e$1@fred.mathworks.com>...
> > ......
> >   ......  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 =
   ffffffffffffffff

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

ans =
fff8000000000000

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 =
   8000000000000000
>> num2hex(-0)
ans =
0000000000000000

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