c - The value of the output for my double variable is not correct on the Arduino Uno -
i use code test if output displayed correctly when print double variable:
double t = (0.8*0.8*(10^5)*599*(10^-6)*1388.888889)/(287*(25+273)*14.7*3*(10^-3)*4); serial.println(t);
but output on serial port 4.03 instead of 3.52
i using arduino uno , arduino c language.
what problems code?
how solve problem
in c,c++,javascript,java:
number 10^5 has represented in program 1e5
and number 10^-6 1e-6
the use of '^' forces program perform bit wise operation 10^5 = (10)^(5)
see: https://msdn.microsoft.com/en-us/library/3akey979.aspx
solution
change
double t = (0.8*0.8*(10^5)*599*(10^-6)*1388.888889)/(287*(25+273)*14.7*3*(10^-3)*4);
to:
double t = (0.8*0.8*(1e5)*599*(1e-6)*1388.888889)/(287*(25+273)*14.7*3*(1e-3)*4);
now correct result:
t = 3.5292104651726235
the int on arduino has limited range: 32767 -32768. long 2147483647 -2147483648
avoid mixing int , double avoid rounding error.
use double or can use cast:
double d = (double)(0.8*0.8*(1e5)*(double)599*(1e-6)*1388.888889)/(((double)287)*((double)(25+273))*((14.7*((double)3)))*(1e-3)*(double)4);
note on arduino
serial.print(1.23456); //gives "1.23"
to more numbers convert double variable string , print string.
the example:
double num = (double)(0.8*0.8*(1e5)*(double)599*(1e-6)*1388.888889)/(((double)287)*((double)(25+273))*((14.7*((double)3)))*(1e-3)*(double)4); char output[50]; snprintf(output, 50, "%f", num); serial.print(output); // should give @ least 3.529210
Comments
Post a Comment