0

I'm trying to save in memory (FRAM in this case) DOUBLE value, i try the same method with use of UNION for save FLOAT value, but don't go over the 2nd number after dot, why? Where I'm wrong? thank's for help .

 //---------------------------------------------------------

  union Scomp_double  // si utilizza union x double
  {
    double temp;
    char byte_s[8];
  } S_double;

  //---------------------------------------------------------

double mioDouble = 1234567.12345678;

//-------------------------DOUBLE--------------------------------

  Serial.println("DOUBLE");
  byte d0 = 0;  // contengono i byte del float
  byte d1 = 0;
  byte d2 = 0;
  byte d3 = 0;
  byte d4 = 0;
  byte d5 = 0;
  byte d6 = 0;
  byte d7 = 0;


  S_double.temp = mioDouble;
  d0 = S_double.byte_s[0];
  d1 = S_double.byte_s[1];
  d2 = S_double.byte_s[2];
  d3 = S_double.byte_s[3];
  d4 = S_double.byte_s[4];
  d5 = S_double.byte_s[5];
  d6 = S_double.byte_s[6];
  d7 = S_double.byte_s[7];

  Serial.println(d0);
  Serial.println(d1);
  Serial.println(d2);
  Serial.println(d3);
  Serial.println(d4);
  Serial.println(d5);
  Serial.println(d6);
  Serial.println(d7);

  fram.writeEnable(true);
  fram.write8(11, d0);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(12, d1);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(13, d2);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(14, d3);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(15, d4);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(16, d5);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(17, d6);
  fram.writeEnable(false);
  fram.writeEnable(true);
  fram.write8(18, d7);
  fram.writeEnable(false);

  S_double.byte_s[0] = fram.read8(11); // si ricompone il double
  S_double.byte_s[1] = fram.read8(12);
  S_double.byte_s[2] = fram.read8(13);
  S_double.byte_s[3] = fram.read8(14);
  S_double.byte_s[4] = fram.read8(15);
  S_double.byte_s[5] = fram.read8(16);
  S_double.byte_s[6] = fram.read8(17);
  S_double.byte_s[7] = fram.read8(18);

  double masterDouble = S_double.temp;
  Serial.println(masterDouble);
  //-------------------------DOUBLE-------------------------------

expected result 1234567.12345678 , result taken 1234567,12

4
  • 2
    double on Arduino is 4 bytes. The same as a float.
    – gre_gor
    Feb 7, 2017 at 14:58
  • no way for have 4 digit after dot ,nice :) tnks for reply Feb 7, 2017 at 15:07
  • Why not simple use "fram.write(&aFloatVar, sizeof(aFloatVar));"?? Feb 7, 2017 at 15:19
  • and what for read? tnks Feb 7, 2017 at 15:21

2 Answers 2

1

how suggest in the comments, and on Arduino reference DOUBLE website

only on arduino 2 float it's 8 byte.

1
  • 1
    If you mean on the Due, yes, but readers who don't speak Italian will not automatically map the actual name of the product "Arduino Due" to "Arduino 2" and understand that you are talking about an ARM-based board rather than a traditional AVR-based one where float and double are 4-byte types. Feb 7, 2017 at 16:27
1

The problem here is the serial output, which by default only outputs 2 decimal places. Solution: Serial.println(masterDouble,8);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.