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?