NEWBEE: About reading uninitialized number in EEPROM section.

Hello,
I write a software that read and write number in the EEPROM section.

The first operation read all number.

The second operation check for every number (int or float) that the value is in the correct range.

But, when before the software write in the EEProm, the value are not initialised.
So, if the software read uninitialised number, it can be all but not a number.
I would like to check this case, because in this case I can check if the value is lower than xx or greather than yy.
How can I test if there is a number in the assigned value ?

Best regards,

Thierry

There is always a number in every location. The default value for EEPROM is 255 0xFF

so you could check on that.

Hello,
Thanks for your reply,

But I reed integer and float.

If I print some value with Serial.print, sometime I can read : NAN
In this case, I can't check if the value is in the range with
If (value < xx or value > yy).
So I have to add a check if the value is a number or not (NAN -> not a number).

I reed the structure with the small header in attachment: EEPROMAnything.h
Many thanks for your help.

Thierry

EEPROMAnything.h (535 Bytes)

if you read a float (that's four bytes long) and the EEPROM is not written yet you will read all 1 bits.
A float is a sign, mantissa and exponent so

sign = 1
mantissa = 11111111111111111111
exponent = 1111111
(I might have missed 1)

this is defined as NaN == Not a Number - e.g. one divide by zero gives NaN or tan(PI/2)

details see - IEEE 754 - Wikipedia -

Hello,
Thanks again,

I understand your reply but...

I read the all the structure with the macro:

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          *p++ = EEPROM.read(ee++);
    return i;
}
#endif

After that, I will have a structure like:
int config.a;
int config.b;
float config.c
etc.

How can I test for example if config.a is a normal number or a NAN ?

Many thanks for your explanation.

Best regards

Vorms:
How can I test for example if config.a is a normal number or a NAN ?

if (config.c == 0xFFFFFFFFFFFFFFFF)

Many thanks !

if (config.c == 0xFFFFFFFFFFFFFFFF) is for a float

what is it for an integer ?

Best regard and many thanks for your help

Thierry

an integer can never be NaN, all possible bit combinations represent numbers.

for a float it is different, there exist two functions you should know that they exist

EEPROM_readAnything(addr, &config);

if ( isnan(config.c) );
{
  Serial.println("Eeprom not initialized?");
}
else if (isInf(config.c) > 0)
{
  Serial.println("extreme positive");
}
else if (isinf(config.c) < 0 )
{
  Serial.println("more negative than this does not exist");
}

more interesting functions see - avr-libc: Modules -

Arrch:

Vorms:
How can I test for example if config.a is a normal number or a NAN ?

if (config.c == 0xFFFFFFFFFFFFFFFF)

Sorry but that does not work

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");

  float f = log(-1);  // you can't take the logarithm of a negative number in R

  Serial.println(f,7);
  Serial.println(f == 0xFFFFFFFF);
  Serial.println(isnan(f));
}

void loop() 
{
}

output:

Start
nan
0
1

QED

if (config.c == 0xFFFFFFFFFFFFFFFF)
  Serial.println(f == 0xFFFFFFFF);

?

At least eliminate possible sources of error and match the parameters of the test.

so your comparing 32 bit float with a 64 bit long ?
OK, I'll adjust the test, fair enough

Serial.println(f == 0xFFFFFFFFFFFFFFFF);
results in
"sketch_sep19b:20: error: integer constant is too large for 'long' type"

so at least some type modifier is missing...

Many thanks for your help !

isnan is definitively a nice macro !

Best regards

Thierry

Whoops. That's what I get for testing in the wrong environment.