AnsweredAssumed Answered

Unsigned Long to Double - value is truncated?

Question asked by Jonathan Scott on Apr 15, 2015
Latest reply on May 1, 2015 by Jonathan Scott

I am trying to read a double-precision floating point number from Flash into RAM.  It is correctly stored in flash as:


0x3e3ce7b697e010B6 = 6.7299999999999997139836383094E-9


Since Processor Expert provides methods for reading (at most) Long data types, I am loading 4 bytes at a time using:

     double myDbl;

     unsigned long temp0, temp1;

     IFsh1_TAddress Addr = FLASH_LOC;                 //this is where the 8-byte double is correctly stored as 0x3e3ce7b6 97e010B6

     IFsh1_GetLongFlash(Addr, &temp0);                    //here temp0 is correctly populated with 0x3E3CE7B6

     IFsh1_GetLongFlash(Addr+4, &temp1);                //here temp1 is correctly populated with 0x97E010B6

     myDbl = ((unsigned long long)(temp0)<<32) | (unsigned long long)(temp1)      //adding/ORing produces the same result


But, no matter how i cast or combine these variables in RAM, the last byte of the fractional part of myDbl is truncated.  I wind up with:


myDbl = 0x3e3ce7b697e01000 = 4.4847141003722793e+018 (according to the debugger)


I can manually edit the fraction in the debugger, but it won't let me change "00" to "B6".  It's almost like it thinks that would be out of range for a double, which it's not.  I've tried casting temp0, temp1, and their combination as long, ulong, long long, and unsigned long long.  I've tried adding, ORing, and anything else i could think of. 


The result is the same even if I hard code it as myDbl = (double)(0x3E3CE7B697E010B6);


This is the FRDM-KL02Z board, and I'm using KDS 2.0.0 with the Cross ARM GNU Assembler and Cross ARM C Compiler and GDB PEMicro (OpenSDA) debugger.  Optimizations are set to none and Endianness is Little.


Casting issue?  Compiler issue?  Can anyone help???


Thank you in advance,