Hello,
i am having difficulty in 32 bit multiplication.the formula i want to implement includes 32 bit multiplication.My code is as follows please help me how to solve this..
void Velocity()
{
float c =1498;
float d = 0.0188;
float l = 0.032;
int V;
float DeltaTOF1 = 789;
V = (DeltaTOF1* pow(c,2))/(2*(d+l));
}
for this am getting 0 as ans and am not getting exact output for pow(c,2)
Regards,
Rashmi H M
"int" is 16bit, you need to:
unsigned long V = round(DeltaTOF1* pow(c,2))/(2*(d+l));
If velocity can be negative, please remove "unsigned".
This is a ridiculous waste of processor cycles:
pow(c,2)
use:
c*c
You're going to have a problem with the result because in the example you gave, the answer is 1.7426e10 which is a 34 bit number. You can't cram that into an unsigned long. You will either have to store the result as a 64-bit integer or leave the result as floating point.
How are you going to use V? Your Velocity function is declared void and therefore can't have a return value and V is declared local to the Velocity function and can't be used outside it anyway.
Pete
@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()
{
}