Searching \ for '[PIC] Detecting NaN with C-30 on 24FJ128GA006' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Detecting NaN with C-30 on 24FJ128GA006'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Detecting NaN with C-30 on 24FJ128GA006'
2008\10\25@130135 by Piclist Vanzo

flavicon
face
Hello to all.

I have a structure like this:

typedef union tagFLOAT
{
   float data;
   unsigned char byte[4];
}FLOAT_I2C;


I use it to write and read floats to an external eeprom via I2C.
Everthing is fine if I already have data on the eeprom. But with a new
one it reads NaN (not a number) and I don't
have no idea of how to detect it.
Would like to check on setup and put in all 0x0000.

// LONGITUDINE
FLOAT_I2C _long;     // variabile che contiene la LONGITIDINE
void ReadLONG(void)
{
   LowAdd = 0x04;  
   HighAdd = 0x00;
     _long.data = HDReadI2CFloat(ControlByte, HighAdd, LowAdd);

_long.data  returns NaN on a new eeprom. _long.byte to are unpredectible.

Thanks
Stefano Vanzo

2008\10\25@131549 by sergio masci

flavicon
face


On Sat, 25 Oct 2008, Piclist Vanzo wrote:

> I use it to write and read floats to an external eeprom via I2C.
> Everthing is fine if I already have data on the eeprom. But with a new
> one it reads NaN (not a number) and I don't
> have no idea of how to detect it.
> Would like to check on setup and put in all 0x0000.

I haven't used C30 but you might find something like "isnan" in math.h.

so you would do something like:

       _long.data = HDReadI2CFloat(ControlByte, HighAdd, LowAdd);

       if (isnan(_long.data))
       {
               _long.data = 0;
       }

if there is no "isnan" you could instead try:

       if (_long.data != _long.data)

Regards
Sergio Masci

2008\10\25@133948 by PICS

flavicon
face
Grazie Segio
There is no isnan in C30
I will try the other way and let you know

Regards
Stefano Vanzo


sergio masci ha scritto:
{Quote hidden}

2008\10\25@141400 by Peter

picon face
sergio masci <smplx <at> allotrope.net> writes:
> I haven't used C30 but you might find something like "isnan" in math.h.

isnan should be a macro that is equivalent to checking all sizeof(x) bytes of x
for equality with 0xff.

Peter


2008\10\25@142026 by Peter

picon face
see if you have _isnan or fpclassify in float.h or math.h (or any other header)
you can also use an integer marker in the eeprom to mark whether it was
initialized. it will be 0xff with a fresh eeprom, then set it to 0xa5 or such
after initialization.

Peter

2008\10\25@143153 by PICS

flavicon
face
Using:

if (_long.data != _long.data)

worked perfectly

I did not find either a MACRO nor a function named "isnan" in all the librarys

Regards
Stefano Vazno



Peter ha scritto:
{Quote hidden}

2008\10\25@150839 by sergio masci

flavicon
face


On Sat, 25 Oct 2008, Peter wrote:

> sergio masci <smplx <at> allotrope.net> writes:
> > I haven't used C30 but you might find something like "isnan" in math.h.
>
> isnan should be a macro that is equivalent to checking all sizeof(x) bytes of x
> for equality with 0xff.

I think you meen the exponent == all ones with the remaining bits
indicating the type of Nan

Regards
Sergio Masci

2008\10\25@190353 by peter green

flavicon
face

> isnan should be a macro that is equivalent to checking all sizeof(x) bytes of x
> for equality with 0xff.
>  
I don't think so. I just checked IEEE 754 floating point spec and for
both single and double precision it says that any number with the
exponent field all ones and the mantissa not equal to zero is a NaN.

2008\10\25@224324 by Matthew Miller

flavicon
face
On Sat, Oct 25, 2008 at 08:30:24PM +0200, PICS wrote:
> Using:
>
> if (_long.data != _long.data)
>
> worked perfectly
>
> I did not find either a MACRO nor a function named "isnan" in all the librarys

How could this work? Wouldn't "_long.data != _long.data" always be false?

Matthew

2008\10\25@233339 by Xiaofan Chen
face picon face
On Sun, Oct 26, 2008 at 10:39 AM, Matthew Miller <spam_OUTnamiller2TakeThisOuTspamnaxs.net> wrote:
> On Sat, Oct 25, 2008 at 08:30:24PM +0200, PICS wrote:
>> Using:
>>
>> if (_long.data != _long.data)
>>
>> worked perfectly

Nice trick!

>> I did not find either a MACRO nor a function named "isnan" in all the librarys
>
> How could this work? Wouldn't "_long.data != _long.data" always be false?
>

http://en.wikipedia.org/wiki/NaN
"A NaN does not compare equal to any floating-point number or NaN,
even if the latter has an identical representation."

Xiaofan

2008\10\26@083011 by Matthew Miller

flavicon
face
On Sun, Oct 26, 2008 at 11:33:35AM +0800, Xiaofan Chen wrote:
>
> http://en.wikipedia.org/wiki/NaN
> "A NaN does not compare equal to any floating-point number or NaN,
> even if the latter has an identical representation."

Thanks for the info Xiaofan!

2008\10\27@054257 by Vitaliy

flavicon
face
Xiaofan Chen wrote:
>> On Sat, Oct 25, 2008 at 08:30:24PM +0200, PICS wrote:
>>> Using:
>>>
>>> if (_long.data != _long.data)
>>>
>>> worked perfectly
>
> Nice trick!

Yeah, I didn't know you could do that.

This is a good example where it makes sense to wrap confusing code in a
macro, so you could say:

if ( IsNotANumber(_long.data) )
   // do stuff

Instead of trying to explain in a comment why you're comparing a variable to
itself.

Vitaliy


'[PIC] Detecting NaN with C-30 on 24FJ128GA006'
2008\11\01@081920 by Peter
picon face
peter green <plugwash <at> p10link.net> writes:

> > isnan should be a macro that is equivalent to checking all sizeof(x) bytes of
> > for equality with 0xff.

> I don't think so. I just checked IEEE 754 floating point spec and for

For an uninitialized eprom or eeprom one would read all 1s which suits both the
definition of the error and the way I suggested to check for it (I was assuming
that there would be no other way to check). I did not know that one could use
the inequality to self test for this. I also do not see why it would work. Once
compiled there is no cast involved and the way I see it comparing 0xffffffff
with 0xffffffff should yield true, not false. If the comparison actually
performs a floating point substraction and fail, then I would understand, but
the expected error would be NaN and not inequality!

Peter



More... (looser matching)
- Last day of these posts
- In 2008 , 2009 only
- Today
- New search...