Epyon
Well-known member
I have a decimal value of 1607154700 represented as a 64 bit unsigned data type. I can access this value one byte at a time. On the Teensy, I want to reconstruct these bytes into the original 64 bit data type and then do some math on it and print it human readable before storing it. However, my knowledge of how to handle these data types fails me. If I look up threads on the forum they are often from before 2015, and that year the native double types apparently changed from 64 bit to 32 bit.
Below is my test sketch. data[] is the array of bytes I receive and that I have to combine to a 64 bit integer. multiplier is a floating point variable to multiply the integer with, result stores the result and is printed and saved to SD. All these variables are dynamic, in my main sketch data[] and multiplier can change every loop.
I also added tmp to check if the byte-to-int conversion yields the same result.
This is my output:
Combining the bytes works. If i print them as scientific notation, it also appears to work. Printing them as decimals yields strange results. If I perform some math on it, even the scientific notation doesn't work anymore.
How can I print the value human readable (1607154700) and perform math on them correctly? I don't care for speed (I only calculate the result every few seconds) but I need the precision.
This sketch runs on a T3.2 with Arduino 1.6.11 and TD 1.3b3.
Below is my test sketch. data[] is the array of bytes I receive and that I have to combine to a 64 bit integer. multiplier is a floating point variable to multiply the integer with, result stores the result and is printed and saved to SD. All these variables are dynamic, in my main sketch data[] and multiplier can change every loop.
I also added tmp to check if the byte-to-int conversion yields the same result.
Code:
uint64_t tmp64u;
byte data[] = {0x41, 0xA3, 0x29, 0x02, 0x8F, 0x45, 0xF2, 0xD8};
uint64_t tmp = 0x41A329028F45F2D8;
float multiplier = 0.1L;
float result = 0.0L;
void setup() {
Serial.begin(9600);
delay(3000);
Serial.println("Start");
for (int q = 0; q < 8; q++) { //copy and shift the bytes into a temp var
tmp64u |= data[q];
if (q < 7) {
tmp64u = tmp64u << 8;
}
}
Serial.printf("%llu,%lli,%lle\n", tmp64u, tmp64u, tmp64u);
Serial.printf("%llu,%lli,%lle\n", tmp, tmp, tmp);
result = tmp64u * multiplier;
Serial.printf("%llu,%lli,%lle\n", result, result, result);
result = tmp * multiplier;
Serial.printf("%llu,%lli,%lle\n", result, result, result);
}
void loop() {
// put your main code here, to run repeatedly:
}
This is my output:
Code:
Start
4729669124639552216,4729669124639552216,1.607274e+08
4729669124639552216,4729669124639552216,1.607274e+08
4871277705657581568,4871277705657581568,4.729669e+17
4871277705657581568,4871277705657581568,4.729669e+17
How can I print the value human readable (1607154700) and perform math on them correctly? I don't care for speed (I only calculate the result every few seconds) but I need the precision.
This sketch runs on a T3.2 with Arduino 1.6.11 and TD 1.3b3.
Last edited: