[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

How the computer identify an NaN?

fl

8/16/2011 3:19:00 AM

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;
}

..............
18 Answers

Ben Bacarisse

8/16/2011 3:34:00 AM

0

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.

fl

8/16/2011 4:45:00 AM

0

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?

Thanks,

robertwessel2@yahoo.com

8/16/2011 5:46:00 AM

0

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).

Dave \Crash\ Dummy

8/16/2011 6:51:00 AM

0


"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

bert

8/16/2011 9:01:00 AM

0

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.
--

Dave \Crash\ Dummy

8/16/2011 11:05:00 AM

0


"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?

Dave \Crash\ Dummy

8/16/2011 11:22:00 AM

0

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

jt

8/16/2011 11:29:00 AM

0

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://t...

jt

8/16/2011 11:32:00 AM

0

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://t...

Dave \Crash\ Dummy

8/16/2011 12:08:00 PM

0


"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!