@Danois90 unsigned long V = round(DeltaTOF1* pow(c,2))/(2*(d+l));
An 'unsigned long int' variable can hold a maximum value of: 232-1 = 4294967295 (10-digit)
The OP's expression will be evaluated to:
(integer)(78914981498)/(2*(0.0188+0.0320) = 17426369645 (11-digit)
Therefore, the expression should be evaluated using binary32 floating point format; binary32 format can hold maximum 3.4028235E+38.
//------------------------------------------------------------------------------------------------
@el_supremo You will either have to store the result as a 64-bit integer or leave the result as floating point.
float c =1498;
float d = 0.0188;
float l = 0.032;
//int V;
float DeltaTOF1 = 789;
float V = (float)(DeltaTOF1* pow(c,2))/(2*(d+l)); //V = 17426369645.66929 by hand calculation
uint64_t VX = (uint64_t)V;
Is the above expression for VX correct? If so, shall I get VX = 17426369645 = 0x040EB14C6D? Because Serial.print() does not support data type more than 32-bit, I have to go through union mechanism to print the value of VX.
The following codes show the value of VX as (hex)00 00 00 04 0E B1 48 00= (dec) 17426368512. the error is about 6x10-6%. The decimal value could be easily found using modulus and division operations on the value of myData.res.
float c =1498.0;
float d = 0.0188;
float l = 0.032;
// int V;
float DeltaTOF1 = 789.0;
void setup()
{
Serial.begin(9600);
union {
byte dataArray[8];
uint64_t res;
} myData;
float V = (float)(DeltaTOF1* pow(c,2))/(2.0*(d+l)); //17426369645.66929;
uint64_t VX = (uint64_t)V;
myData.res = (uint64_t)VX;
for (int i =0; i<8; i++)
{
byte x1 = myData.dataArray[i];
x1 = x1>>4;
if(x1 <=9)
{
Serial.write(x1+0x30);
}
else
{
Serial.write(x1+0x37);
}
//--------------------
x1 = myData.dataArray[i];
x1 = x1 & 0x0F;
if(x1 <=9)
{
Serial.write(x1+0x30);
}
else
{
Serial.write(x1+0x37);
}
Serial.println();
}
}
void loop()
{
}