Skip to content

Implement pow/pow_with_context (exponentiation with integer parameter)#148

Merged
akubera merged 6 commits intoakubera:feature/powfrom
drinkcat:pow
Oct 12, 2025
Merged

Implement pow/pow_with_context (exponentiation with integer parameter)#148
akubera merged 6 commits intoakubera:feature/powfrom
drinkcat:pow

Conversation

@drinkcat
Copy link
Contributor

@drinkcat drinkcat commented Apr 10, 2025

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.

@drinkcat drinkcat force-pushed the pow branch 4 times, most recently from b43dcf2 to 046bb72 Compare April 16, 2025 14:48
@drinkcat
Copy link
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.

drinkcat added 5 commits May 29, 2025 11:46
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.
@akubera akubera changed the base branch from trunk to feature/pow October 12, 2025 16:22
@akubera akubera merged commit 8e17cd4 into akubera:feature/pow Oct 12, 2025
11 of 13 checks passed
@akubera
Copy link
Owner

akubera commented Oct 19, 2025

Finally released in 0.4.9. Thanks.

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.

Raise to the power of an _integer_

2 participants