Closed RobTillaart closed 3 years ago
If there was another way to do negative numbers e.g. also set the last decimal . or the : in the middle, but non of those are common as far as I know.
if only positive numbers need to be supported one could do
int displayFixedPoint(float f, uint8_t pos)
{
if (f < 0) return UNDERFLOW_ERROR;
if ((pos == 1) && (f >= 10)) return OVERFLOW_ERROR;
if ((pos == 2) && (f >= 100)) return OVERFLOW_ERROR;
if ((pos == 3) && (f >= 1000)) return OVERFLOW_ERROR;
// data OK -> display it.
....
return OK;
}
However the user needs to verify the return value explicitly every time..
What are the ranges needed?
I need applications to get more clear requirements.
A car battery monitor = 00.0 - 13.6 Volt (or even up to 50V) - leaves one position for (A = Alarm, E=error, F = Fail)
A temperature sensor
A humidity sensor Percentage
Closed as displayFixedPointX() makes these possible.
displayFixedPoint(float f, uint8_t pos) Display a float in a fixed point float format e.g. always one decimal.
This improves the readability of the numbers as it is less error prone than reading with a floating point position. Special care has to be taken for negative numbers as these would shift the position of the point by one place... how bad is this? Keeping the point in the same spot would mean that one must keep one position free for sign, so only 3 digit positions are available. implying only #.## or ##.# or ### pattern is possible