-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Pseudo floating point numbers using integer data types #30
Comments
I understand the desire for this on AVRs, but modifying the I do like the idea however. If we were to create a new modifier specifically for this library that does not conflict with other implementations I would support it. What about |
I agree that modifying the Doesn't matter much what letter is chosen as the specifier for this "integer with decimal exponent" "decimal integer" or "pseudo float", but reusing the relevant modifiers from the |
The following solutions may be appropriate:
Application examples:
Comment: |
I think I found a possible bug:
Adding the initial |
Also, with warnings enabled I get:
Chaning it to |
The "bug" may be a simple integer overflow. Here is the full sketch. Compare the output after commenting out line #12. |
Now I understand your problem! You probably did not fix the PrintExtension.h! |
Aha... that would make sense! |
Sorry for errors in ltostrqf(). The original version does not process correctly numbers in the target format range of -0.9999999 to +0.999999. |
Proposed feature
I propose, that the
printf
precision
option be made to also work with pseudo floating point numbers, represented by an integer type with an implicit (fixed) decimal exponent.Example:
The value
3.14
could be represented (with two decimals precision) by an integer value of314
(assuming an implicit decimal exponent of -2):314 * 10^(-2) = 3.14
Note, that
precision = -exponent
Current printf formatting design.
Proposed behavior
Using the standard "f" specifier, with an implicit float conversion:
int_value * 10^(-precision)
It may be a good idea, to even support negative precision specifiers (positive decimal exponents):
For backwards compatibility, it might be better to use a new "integer with decimal exponent" specifier, rather than repurposing the "f" specifier with an integer argument.
Motivation
On memory and CPU resource restricted MCUs like the AVRs used on the Arduino, it is common practice to avoid using
float
s to represent floating point values, like temperature, but instead use an integer data type, like an int, representing tenth of degrees. So a temperature of 45.6 would be represented by an integer value of 456, with an implicit decimal exponent (10^-2). Similarly, a monetary amount, e.g. $234.56 can be represented by an integer value, representing the number of cents: 23456, with an implicit decimal exponent (10^-2)Such an "external/fixed 10^n exponent" implementation have several advantages over using native floating point data types:
The text was updated successfully, but these errors were encountered: