Simpletext print function and floats

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.

Comments

  • edited 2016-12-12 - 17:40:28
    Thanks for reporting this jcook, and especially for narrowing down the culprint. (I decided to leave that typo in.) The Simple Libraries, including simpletext, are scheduled to get some maintenance this month, early Jan at the latest. So, I added this one to the list:

    https://github.com/parallaxinc/Simple-Libraries/issues/56

    If memory serves, I added f2s and s2f to simpletext because atof, strtof, strtod32 and others all had program size penalties that made them difficult to fit certain apps into the Propeller chip's 32 KB.

    P.S. If you're using libraries that are older than the ones that come with SimpleIDE 1.0.1/2, please let me know. Thanks again.
Sign In or Register to comment.