double store to eeprom

I work at eclipse.I want to convert a double to char array to store to EEPROM.i found an answer to the problem.But the number i get back is not exactly the same. The code is:

#include "Arduino.h"
#include "EEPROM.h"

char finalArray[8];
double final;
double d;

int main(void)
{
    init();
    setup();

    for (;;)
        loop();

    return 0;
}


void setup() {
    Serial.begin(115200);
    d = 958.254;
    char* byteArray = reinterpret_cast<char*>(&d);
    for(int i=0;i<8;i++){
        EEPROM.write(i,byteArray[i]);
    }
    for(int i=0;i<8;i++){
            finalArray[i]=EEPROM.read(i);
    }

}

void loop() {
    final = *reinterpret_cast<double*>(finalArray);
    Serial.println(final,5);
    delay(1000);
} 0 down vote favorite
	

I work at arduino with eclipse.I want to convert a double to char array to store to EEPROM. From another question i found an answer to the problem.I changed it to my need.But the number i get back is not exactly the same. The code is:

#include "Arduino.h"
#include "EEPROM.h"

char finalArray[8];
double final;
double d;

int main(void)
{
    init();
    setup();

    for (;;)
        loop();

    return 0;
}


void setup() {
    Serial.begin(115200);
    d = 958.254;
    char* byteArray = reinterpret_cast<char*>(&d);
    for(int i=0;i<8;i++){
        EEPROM.write(i,byteArray[i]);
    }
    for(int i=0;i<8;i++){
            finalArray[i]=EEPROM.read(i);
    }

}

void loop() {
    final = *reinterpret_cast<double*>(finalArray);
    Serial.println(final,5);
    delay(1000);
}

At the terminal i get value of 958.25402 instead of 958.25400. And if i ask for 9 decimal digits at the print instead of 5 i get the number 958.254028320. What is the solution?

At the terminal i get value of 958.25402 instead of 958.25400. And if i ask for 9 decimal digits at the print instead of 5 i get the number 958.254028320. What is the solution?

To what? What is the problem?

As I see it, you seem to think that the value actually stored in the float by the assignment operator is EXACTLY 958.25400. It is not.

Note that the Arduino only has a 32 bit float, no 64 bit double. Double is defines as float so they are both 32 bit.
Storing 8 bytes makes no sense as the “double” is only 4 bytes in size.

A 32 bit float (IEEE754) has only 23 bits = ~7 significant digits, that’s why you see 958.25402 (it has 8 digits)

Ok i understood that.I know that arduino works with doubles like float.The basic question is what happens in a calculation does it give the correct number when i use the variable or does it give the wrong ? How am i sure that it will give the correct.From what you read i suppose the problem is not at the number but at the Serial.print

The basic question is what happens in a calculation does it give the correct number when i use the variable or does it give the wrong ?

If you use it correctly, the calculation will be performed correctly. If not, it won't. Show an example of what you are talking about.

How am i sure that it will give the correct.

How is anyone sure of anything?

From what you read i suppose the problem is not at the number but at the Serial.print

No, the problem seems to be that you don't understand that Serial.print() only prints an approximation of the value stored, and that the value stored is only an approximation, to 6 or 7 digits, of the actual value.

And before you ask, no, you can't do anything to get the actual value.