Skip to content

py/formatfloat: Use pow() to generate base values. #8981

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

Closed
wants to merge 0 commits into from

Conversation

dpwe
Copy link
Contributor

@dpwe dpwe commented Jul 29, 2022

formatfloat needs to calculate values of 10^X to convert numbers into decimal strings. Formerly, this was done efficiently by multiplying combinations of 10^(2^Y) read from a lookup table. However, this led to small differences from the exact values of 10^X that were calculated by parsenum, which could lead to unexpected results for expressions like print(1e23). These were addressed in #8905 by loosening the comparison bounds around exact values of 10^X, which led to slightly aggressive rounding up for some values.

This patch instead ensures alignment between 10^X values in both parsing and formatting by using parsenum's pow(10, e) idiom in formatfloat. This eliminates the unexpected rounding-up, and also means we can drop the lookup table. It is, however, presumably slower to compute.

It also introduces direct calculation of the power-of-10 exponent from the power-of-2 exponent in the floating-point representation, eliminating much of the search. It still checks a couple of values in case there are edge cases in this calculation.

Note that this patch incorporates trailing-zero mitigation for parsenum which exists as a separate PR #8980; without this modification, some tests including trailing zeros were failing under this change.

Signed-off-by: Dan Ellis dan.ellis@gmail.com

@dpwe dpwe force-pushed the format_float_with_pow branch 5 times, most recently from d378b2e to bcfc6f6 Compare July 29, 2022 16:26
@dpwe dpwe closed this Jul 29, 2022
@dpwe dpwe force-pushed the format_float_with_pow branch from bcfc6f6 to 963e599 Compare July 29, 2022 16:27
tannewt added a commit to tannewt/circuitpython that referenced this pull request Feb 29, 2024
Print output muddies the serial output. Setting warnings to crash
could actual raise an exception.

Fixes micropython#8981
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant