accurate float int printing #5766
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is to fix issue #4212 with the minimum change possible.
It has to store a copy of the input float value before it gets divided out by powers of 10, but it runs slightly faster with this benchmark:
This is because the implementation from @dhylands https://github.com/dhylands/format-float does a floating point multiplication (by ten) for each digit, and my fix deals with the integer part of the number using only integer arithmetic.
I think there is scope for an overall faster implementation just using int32 arithmetic, but one would have to start by decoding the bit layout of the float into its mantissa and exponent, rather than interacting with value using normal arithmetic functions that do not depend on its encoding -- but since MicroPython is already bit packing and unpacking these values, this wouldn't make it any more dependent.
One advantage of any float32 format conversion is it's possible to exhaustively test every single combination of bits (there's only 4 billion of them) in just a few hours.