Implement pow/pow_with_context (exponentiation with integer parameter)#148
Merged
akubera merged 6 commits intoakubera:feature/powfrom Oct 12, 2025
Merged
Implement pow/pow_with_context (exponentiation with integer parameter)#148akubera merged 6 commits intoakubera:feature/powfrom
akubera merged 6 commits intoakubera:feature/powfrom
Conversation
b43dcf2 to
046bb72
Compare
Contributor
Author
|
Fixed the CI, updated margin logic to add multiplications/10 as extra margin (not that I'm fully convinced by the argument in #147...) Still some rare errors with this logic. |
Add a function to raise a BigDecimal to an integer power. The number of digits would blow up if we kept the exact value, so we use the precision provided in Context parameter (or the default value). We need to keep additional precision during intermediate computations though, as each multiplication may lose up to 2 digits. For tests, we compare against values that we can obtain from Wolfram Alpha, and test a few cases with 50 or 500 precision context, to check that the precision loss compensation work we do works correctly. For now, only support positive exponent values. Fixes akubera#147.
Supporting negative exponents is fairly straightforward, we just need to be careful to start with enough precision when doing the initial invertion.
For unclear reasons related to rounding, we still sometimes get the last digit wrong. Adding a little bit of extra margin fixes the problem (or at least, makes it extremely unlikely to happen).
It does appear adding an additional digit per ten multiplications might be enough. At least this seems to be enough to not be able to find any counter example within reasonable time.
Directly build stdlib::num::NonZeroU64 with new instead of using try_into, stub out println! in tests. Also avoid using ilog2 and div_ceil.
drinkcat
added a commit
to drinkcat/coreutils
that referenced
this pull request
May 29, 2025
This is the latest version in akubera/bigdecimal-rs#148 , but the discussion sort of stalled, this is really complicated math, but this new function is a little bit better than the original.
drinkcat
added a commit
to drinkcat/coreutils
that referenced
this pull request
May 29, 2025
This is the latest version in akubera/bigdecimal-rs#148 , but the discussion sort of stalled, this is really complicated math, but this new function is a little bit better than the original (at least I hope so).
drinkcat
added a commit
to drinkcat/coreutils
that referenced
this pull request
May 29, 2025
This is the latest version in akubera/bigdecimal-rs#148 , but the discussion sort of stalled, this is really complicated math, but this new function is a little bit better than the original (at least I hope so).
drinkcat
added a commit
to drinkcat/coreutils
that referenced
this pull request
May 29, 2025
This is the latest version in akubera/bigdecimal-rs#148 , but the discussion sort of stalled, this is really complicated math, but this new function is a little bit better than the original (at least I hope so).
Noticed while pushing the code to coreutils.
Owner
|
Finally released in 0.4.9. Thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
pow: Fix nonstd build, and build with older versions
Directly build stdlib::num::NonZeroU64 with new instead of using
try_into, stub out println! in tests.
Also avoid using ilog2 and div_ceil.
pow: More analytical thinking about additional margin
It does appear adding an additional digit per ten multiplications
might be enough. At least this seems to be enough to not be
able to find any counter example within reasonable time.
pow: Add a little bit of additional margin
For unclear reasons related to rounding, we still sometimes get
the last digit wrong. Adding a little bit of extra margin fixes
the problem (or at least, makes it extremely unlikely to happen).
pow: Allow negative exponents
Supporting negative exponents is fairly straightforward, we
just need to be careful to start with enough precision when doing
the initial invertion.
pow: Add pow/pow_with_context function
Add a function to raise a BigDecimal to an integer power.
The number of digits would blow up if we kept the exact value, so
we use the precision provided in Context parameter (or the default
value).
We need to keep additional precision during intermediate
computations though, as each multiplication may lose up to 2 digits.
For tests, we compare against values that we can obtain from
Wolfram Alpha, and test a few cases with 50 or 500 precision context,
to check that the precision loss compensation work we do works
correctly.
For now, only support positive exponent values.
Fixes #147.