How the computer identify an NaN?

F

fl

Hi,

I encounter NaN often in Matlab. Recently, I use Matlab to generate
some code for an embedded project. I get the following source code
from Matlab below dot line.

I don't understand the line:
c_y = (b_u != b_u);

Could you explain it to me? More specific, what result can get
from: (b_u != b_u);?

How does it relate to NaN?


Thanks a lot.


BTW, real_T and boolean_T just like int and bool in C I think.






.................................
real_T b_u0;
real_T b_u;
boolean_T c_y;

b_u0 = (real_T)Idata;
b_u = b_u0;
c_y = (b_u != b_u);
if (c_y) {
d_y = rtNaN;
} else {
d_y = b_u0 * b_u0;
}

..............
 
B

Ben Bacarisse

fl said:
I encounter NaN often in Matlab. Recently, I use Matlab to generate
some code for an embedded project. I get the following source code
from Matlab below dot line.

I don't understand the line:
c_y = (b_u != b_u);

Could you explain it to me? More specific, what result can get
from: (b_u != b_u);?

The != operator always gives 0 or 1 depending on the result of the
comparison. It's a not-equals comparison so it is asking if b_u is not
equal to itself. A NaNs won't compare equal to anything -- even another
NaN, so b_u != b_u is a way to ask if b_u is a NaN.

<snip>
 
F

fl

The != operator always gives 0 or 1 depending on the result of the
comparison.  It's a not-equals comparison so it is asking if b_u is not
equal to itself.  A NaNs won't compare equal to anything -- even another
NaN, so b_u != b_u is a way to ask if b_u is a NaN.

<snip>

If NaN won't equal to anything, any number will not equal to itself
either. I still do not understand b_u != b_u;

ex:
b_u = 3;
(b_u!=b_u) ==> 1, right?

Thanks,
 
H

Heinrich Wolf

....
If NaN won't equal to anything, any number will not equal to itself
either. I still do not understand b_u != b_u;

ex:
b_u = 3;
(b_u!=b_u) ==> 1, right?

Thanks,

Have you tried?
I doubt that.

Heiner
 
B

bert

...
If NaN won't equal to anything, any number will not equal to itself
either. I still do not understand b_u != b_u;

ex:
b_u = 3;
(b_u!=b_u) ==> 1, right?

Thanks,

Have you tried?
I doubt that.

Heiner

You should listen more carefully when
people who have a complete understanding
of some topic try to explain it to you.

Where 'b' is a real, (b != b) is 1 or 0,
depending strictly on whether the current
value of 'b' is a NaN, or is not a NaN.
--
 
H

Heinrich Wolf

....
You should listen more carefully when
people who have a complete understanding
of some topic try to explain it to you.

Where 'b' is a real, (b != b) is 1 or 0,
depending strictly on whether the current
value of 'b' is a NaN, or is not a NaN.
--

pardon?
I noticed that b_u is a real which results in 3.0 by assignment;
Why should (3.0 != 3.0) result in 1?
 
H

Heinrich Wolf

Asking NaN != NaN looks reasonable to me.
But asking 3 != 3 looks like trolling.
 
J

Jens Thoms Toerring

Heinrich Wolf said:
pardon?
I noticed that b_u is a real which results in 3.0 by assignment;
Why should (3.0 != 3.0) result in 1?

You snipped the most relevant part of your own post, i.e.

and the statement made by "bert" was that

b != b

evaluates to 1 if b is not-NaN (e.g. for b set to 3) but to 0
if b is NaN (always assuming that IEEE floating point represen-
tation is used).

The claim you made (and then snipped in your reply) is wrong
(that's not a question of logic but due to the way things are
required by the IEEE standard for floating point handling) and
that's what the "Have you tried" was refering to.

Regards, Jens
 
J

Jens Thoms Toerring

You snipped the most relevant part of your own post, i.e.
and the statement made by "bert" was that
evaluates to 1 if b is not-NaN (e.g. for b set to 3) but to 0
if b is NaN (always assuming that IEEE floating point represen-
tation is used).

Sorry, got that just the wrong way round

For b = 3 b != b results in 0
For b = NaN b != b results in 1

Regards, Jens
 
B

Ben Bacarisse

This thread has been screwed up. When Heinrich Wolf first posted he did
not correctly quote the post he was replying to. I think several
replies have confused who said what after that.

This was in reply to Heinrich who said only "Have you tried it?" though
it looked like he repeated fl's confusion.
You snipped the most relevant part of your own post, i.e.

This was not said by Heinrich but by fl.

<snip>
 
J

James Kuyper

On 08/16/2011 12:45 AM, fl wrote:
....
If NaN won't equal to anything, any number will not equal to itself
either. ...

That does not follow. There's no connection between those two clauses
that I can see. The first one is true and the second one is false, so
whatever connection you see between those two clauses is invalid.
... I still do not understand b_u != b_u;

ex:
b_u = 3;
(b_u!=b_u) ==> 1, right?

No. This is a special rule for NaN's; 3 is not a NaN, so the rule
doesn't apply. Some examples that might clarify this:

3.0 != 3.0; // False
3.0 != 5.0; // True
5.0 != 5.0; // False
NaN != NaN; // True
3.0 != Nan; // True
NaN != 5.0; // True

The special rule, that a NaN doesn't compare equal to anything, applies
to all of the last three statements. It doesn't apply to any of the
first three, because none of the first three statements involve a NaN.
 
S

Stephen Sprunk

If NaN won't equal to anything, any number will not equal to itself
either.

Incorrect.

A number is always equal to itself. However, a NaN is Not a Number, and
the rules are different from numbers: it never compares equal to itself.
I still do not understand b_u != b_u;

ex:
b_u = 3;
(b_u!=b_u) ==> 1, right?

Since 3 is a number, (3 != 3) is false. That condition would only be
true for NaNs, which are not numbers.

S
 
A

Angel

Asking NaN != NaN looks reasonable to me.
But asking 3 != 3 looks like trolling.

It's an explaination of how logical expressions involving real numbers
(which may be NaN) are handled. In pseudo-C:

bool double_equals(const double a, const double b)
{
if (isnan(a) || isnan(b))
return false;

[...]
}

In other words, any comparison between two real numbers (less than,
equals, greater than) will always yield false if either operant is NaN.

Because of that, the expression "a != a" always returns true if a is
NaN, and false if it is not. And thus, "a != a" is a way to test if a
is NaN.

Yes, it mystified me too at first, but once it was explained it made
sense. ^^
 
J

Joe Pfeiffer

fl said:
If NaN won't equal to anything, any number will not equal to itself
either. I still do not understand b_u != b_u;

ex:
b_u = 3;
(b_u!=b_u) ==> 1, right?

No, b_u!=b_u ==> 0, since they are indeed equal.

Do you understand that NaN is an abbreviation for "Not a Number"? NaN is
a specific bit pattern (actually, a pattern from a set defined by your
floating point representation) that is recognized by the floating point
number unit as meaning "this is not a number", and gets treated
specially.

So for instance, if you add anything to it, the result is also Not a
Number. And if you compare it to anything, the result is !=. So for
any valid number b_u, b_u==b_u. But for NaN, NaN!= NaN.

I'll bet you'd get better Matlab-specific advice in a Matlab
newsgroup...
 
K

Keith Thompson

Joe Pfeiffer said:
I'll bet you'd get better Matlab-specific advice in a Matlab
newsgroup...

Like comp.soft-sys.matlab (I don't know how active it is).
 
J

Jens Thoms Toerring

Ben Bacarisse said:
(e-mail address removed) (Jens Thoms Toerring) writes:
This thread has been screwed up. When Heinrich Wolf first posted he did
not correctly quote the post he was replying to. I think several
replies have confused who said what after that.
This was in reply to Heinrich who said only "Have you tried it?" though
it looked like he repeated fl's confusion.
This was not said by Heinrich but by fl.

Aoory and my apologies to Heinrich Wolf. Hadn't read the
attribution lines carefully enough...

Best regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,952
Messages
2,570,116
Members
46,705
Latest member
BufordPala

Latest Threads

Top