The simpletext print function seems to have problems when printing floating point numbers.
I discovered this when converting a temperature measured with the DS18B20 temperature sensor to Fahrenheit. The sensor outputs the temperature in units of 1/16 degree Celsius and I am displaying the temperature in Fahrenheit with a precision of 0.1°. When the sensor returns a value of 0xff3c (-12.25°C) I expect the displayed value to be either 9.9 or 10.0 since the actual temperature is 9.95°F. Instead my display shows 0.0°F. I experimented with printing the conversion result with different number of digits after the decimal point.
When using the default number of digits the output is okay:
print("%f\n", 32+(9*(-196))/80.0);
results in displaying: 9.950001
When using one digit after the decimal point the problem is evident:
print("%.1f\n", 32+(9*(-196))/80.0);
results in displaying: 0.0
When using two digits after the decimal point the output is okay:
print("%.2f\n", 32+(9*(-196))/80.0);
results in displaying: 9.95
I looked at the source code for the simpletext library and I think the issue is in the float2string function. It has some code to determine the number of digits to print that isn't working.
From: http://forums.parallax.com/discussion/165609
(Thanks jcook!)
The simpletext print function seems to have problems when printing floating point numbers.
I discovered this when converting a temperature measured with the DS18B20 temperature sensor to Fahrenheit. The sensor outputs the temperature in units of 1/16 degree Celsius and I am displaying the temperature in Fahrenheit with a precision of 0.1°. When the sensor returns a value of 0xff3c (-12.25°C) I expect the displayed value to be either 9.9 or 10.0 since the actual temperature is 9.95°F. Instead my display shows 0.0°F. I experimented with printing the conversion result with different number of digits after the decimal point.
When using the default number of digits the output is okay: print("%f\n", 32+(9*(-196))/80.0); results in displaying: 9.950001
When using one digit after the decimal point the problem is evident: print("%.1f\n", 32+(9*(-196))/80.0); results in displaying: 0.0
When using two digits after the decimal point the output is okay: print("%.2f\n", 32+(9*(-196))/80.0); results in displaying: 9.95
I looked at the source code for the simpletext library and I think the issue is in the float2string function. It has some code to determine the number of digits to print that isn't working.