bigdecimal-rs
bigdecimal-rs copied to clipboard
Implement pow/pow_with_context (exponentiation with integer parameter)
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.