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;
}
..............
|
|
0
|
|
|
|
Reply
|
rxjwg98 (311)
|
8/16/2011 3:19:29 AM |
|
fl <rxjwg98@gmail.com> writes:
> 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>
--
Ben.
|
|
0
|
|
|
|
Reply
|
ben.usenet (6515)
|
8/16/2011 3:33:50 AM
|
|
On 15 ao=FBt, 23:33, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> fl <rxjw...@gmail.com> writes:
> > 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:
> > =A0 =A0 =A0 =A0c_y =3D (b_u !=3D b_u);
>
> > Could you explain it to me? More specific, =A0 =A0 =A0 =A0what result c=
an get
> > from: (b_u !=3D b_u);?
>
> The !=3D operator always gives 0 or 1 depending on the result of the
> comparison. =A0It's a not-equals comparison so it is asking if b_u is not
> equal to itself. =A0A NaNs won't compare equal to anything -- even anothe=
r
> NaN, so b_u !=3D b_u is a way to ask if b_u is a NaN.
>
> <snip>
> --
> Ben.
If NaN won't equal to anything, any number will not equal to itself
either. I still do not understand b_u !=3D b_u;
ex:
b_u =3D 3;
(b_u!=3Db_u) =3D=3D> 1, right?
Thanks,
|
|
0
|
|
|
|
Reply
|
rxjwg98 (311)
|
8/16/2011 4:45:19 AM
|
|
On Mon, 15 Aug 2011 21:45:19 -0700 (PDT), fl <rxjwg98@gmail.com>
wrote:
>On 15 ao�t, 23:33, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>> fl <rxjw...@gmail.com> writes:
>> > 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>
>> --
>> Ben.
>
>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?
IEEE NaNs are special in a lot of ways. As Ben said, a NaN does not
compare equal to anything, even another NaN. An IEEE FP comparison is
*not* a direct comparison of the bit patterns of the two operands. A
NaN compared to anything else (including another NaN) also has no
order (IOW, "NaN > 3", "NaN == 3" and "NaN < 3" are *all* false, while
"NaN != 3" is true).
The "if (f != f)" trick works on many implementations using IEEE math
(all that I'm aware of), but it would probably be safer to use the
isnan() function provided by many implementations (and required by C99
- where it's a macro).
|
|
0
|
|
|
|
Reply
|
robertwessel2 (1339)
|
8/16/2011 5:45:51 AM
|
|
"fl" <rxjwg98@gmail.com> schrieb im Newsbeitrag
news:9d47f418-5533-414f-b9da-15385ce9c442@c19g2000yqe.googlegroups.com...
....
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
|
|
0
|
|
|
|
Reply
|
invalid171 (6559)
|
8/16/2011 6:51:01 AM
|
|
On Tuesday, August 16, 2011 7:51:01 AM UTC+1, Heinrich Wolf wrote:
> "fl" <rxj...@gmail.com> schrieb im Newsbeitrag
> ...
> 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.
--
|
|
0
|
|
|
|
Reply
|
bert.hutchings (56)
|
8/16/2011 9:01:13 AM
|
|
"bert" <bert.hutchings@btinternet.com> schrieb im Newsbeitrag
news:0e6e63f1-e63e-4e52-9db5-ec1807044878@glegroupsg2000goo.googlegroups.com...
....
>> 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.
> --
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?
|
|
0
|
|
|
|
Reply
|
invalid171 (6559)
|
8/16/2011 11:04:47 AM
|
|
Asking NaN != NaN looks reasonable to me.
But asking 3 != 3 looks like trolling.
|
|
0
|
|
|
|
Reply
|
invalid171 (6559)
|
8/16/2011 11:22:15 AM
|
|
Heinrich Wolf <invalid@invalid.invalid> wrote:
> "bert" <bert.hutchings@btinternet.com> schrieb im Newsbeitrag
> news:0e6e63f1-e63e-4e52-9db5-ec1807044878@glegroupsg2000goo.googlegroups.com...
> ...
> >> ex:
> >> b_u = 3;
> >> (b_u!=b_u) ==> 1, right?
> >>
> >> Have you tried?
> >> I doubt that.
> >>
> > 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?
You snipped the most relevant part of your own post, i.e.
>>> If NaN won't equal to anything, any number will not equal to itself
>>> either. I still do not understand b_u != b_u;
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
0
|
|
|
|
Reply
|
jt68 (1134)
|
8/16/2011 11:28:40 AM
|
|
Jens Thoms Toerring <jt@toerring.de> wrote:
> Heinrich Wolf <invalid@invalid.invalid> wrote:
> > "bert" <bert.hutchings@btinternet.com> schrieb im Newsbeitrag
> > news:0e6e63f1-e63e-4e52-9db5-ec1807044878@glegroupsg2000goo.googlegroups.com...
> > ...
> > >> ex:
> > >> b_u = 3;
> > >> (b_u!=b_u) ==> 1, right?
> > >>
> > >> Have you tried?
> > >> I doubt that.
> > >>
> > > 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?
> You snipped the most relevant part of your own post, i.e.
> >>> If NaN won't equal to anything, any number will not equal to itself
> >>> either. I still do not understand b_u != b_u;
> 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).
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
0
|
|
|
|
Reply
|
jt68 (1134)
|
8/16/2011 11:31:31 AM
|
|
"Jens Thoms Toerring" <jt@toerring.de> schrieb im Newsbeitrag
news:9av2ojF9heU4@mid.uni-berlin.de...
....
> Sorry, got that just the wrong way round
....
I don't mind.
You're welcome!
|
|
0
|
|
|
|
Reply
|
invalid171 (6559)
|
8/16/2011 12:08:09 PM
|
|
"Heinrich Wolf" <invalid@invalid.invalid> schrieb im Newsbeitrag
news:j2ditd$t3d$1@news.m-online.net...
....
>> You should listen more carefully when
>> people who have a complete understanding
>> of some topic try to explain it to you.
....
I am sorry and I apologize. Too short sleep this night ...
|
|
0
|
|
|
|
Reply
|
invalid171 (6559)
|
8/16/2011 12:17:09 PM
|
|
jt@toerring.de (Jens Thoms Toerring) writes:
> Heinrich Wolf <invalid@invalid.invalid> wrote:
>
>> "bert" <bert.hutchings@btinternet.com> schrieb im Newsbeitrag
>> news:0e6e63f1-e63e-4e52-9db5-ec1807044878@glegroupsg2000goo.googlegroups.com...
>> ...
>> >> ex:
>> >> b_u = 3;
>> >> (b_u!=b_u) ==> 1, right?
>> >>
>> >> Have you tried?
>> >> I doubt that.
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.
>> > You should listen more carefully when
>> > people who have a complete understanding
>> > of some topic try to explain it to you.
This was in reply to Heinrich who said only "Have you tried it?" though
it looked like he repeated fl's confusion.
>> > 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?
>
> You snipped the most relevant part of your own post, i.e.
>>>> If NaN won't equal to anything, any number will not equal to itself
>>>> either. I still do not understand b_u != b_u;
This was not said by Heinrich but by fl.
<snip>
--
Ben.
|
|
0
|
|
|
|
Reply
|
ben.usenet (6515)
|
8/16/2011 12:37:21 PM
|
|
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.
--
James Kuyper
|
|
0
|
|
|
|
Reply
|
jameskuyper (5171)
|
8/16/2011 12:42:58 PM
|
|
On 15-Aug-11 23:45, fl wrote:
> 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
--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
|
|
0
|
|
|
|
Reply
|
stephen (1128)
|
8/16/2011 2:25:52 PM
|
|
On 2011-08-16, Heinrich Wolf <invalid@invalid.invalid> wrote:
> 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. ^^
--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c
|
|
0
|
|
|
|
Reply
|
news104 (780)
|
8/16/2011 3:17:08 PM
|
|
fl <rxjwg98@gmail.com> writes:
> On 15 août, 23:33, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>> fl <rxjw...@gmail.com> writes:
>> > 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>
>> --
>> Ben.
>
> 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...
|
|
0
|
|
|
|
Reply
|
pfeiffer (616)
|
8/16/2011 6:37:41 PM
|
|
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
[...]
> 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).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21481)
|
8/16/2011 7:26:21 PM
|
|
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> jt@toerring.de (Jens Thoms Toerring) writes:
> > Heinrich Wolf <invalid@invalid.invalid> wrote:
> >
> >> "bert" <bert.hutchings@btinternet.com> schrieb im Newsbeitrag
> >> news:0e6e63f1-e63e-4e52-9db5-ec1807044878@glegroupsg2000goo.googlegroups.com...
> >> ...
> >> >> ex:
> >> >> b_u = 3;
> >> >> (b_u!=b_u) ==> 1, right?
> >> >>
> >> >> Have you tried?
> >> >> I doubt that.
> 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.
> >> > You should listen more carefully when
> >> > people who have a complete understanding
> >> > of some topic try to explain it to you.
> This was in reply to Heinrich who said only "Have you tried it?" though
> it looked like he repeated fl's confusion.
> >> > 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?
> >
> > You snipped the most relevant part of your own post, i.e.
> >>>> If NaN won't equal to anything, any number will not equal to itself
> >>>> either. I still do not understand b_u != b_u;
> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
0
|
|
|
|
Reply
|
jt68 (1134)
|
8/16/2011 9:13:17 PM
|
|
|
18 Replies
29 Views
(page loaded in 0.269 seconds)
|