Bug #17264
closedBigDecimal exponentiation cannot be used with #** method
Description
As stated in Bug #17214 (https://bugs.ruby-lang.org/issues/17214) when exponentiating a BigDdecimal number even when using small numbers, a precision argument must be passed to the operation or the operation will return wrong result, 2222 and 3.5 not being 'huge' numbers:
(BigDecimal("2222",10000) ** BigDecimal("3.5",10000)).to_i
# => 517135311000
However the #** method cannot be passed a precision argument and as seen above it will return wrong values for small numbers even if the BigDecimal numbers themselves have a large precision argument.
Therefore this operation can only be valid if used with the #power method and provided with a larger precision argument:
BigDecimal(2222).power(3.5, 15).to_i #=> 517135308457
My suggestion is the #** method and #power method should work the same way or the #** method retired.
Updated by jeremyevans0 (Jeremy Evans) about 4 years ago
- Status changed from Open to Assigned
- Assignee set to mrkn (Kenta Murata)
karatedog (Földes László) wrote:
My suggestion is the #** method and #power method should work the same way or the #** method retired.
The methods actually do operate the same way. #**
is basically:
def **(other)
power(other)
end
So the issue is basically the precision for power
when given one argument is not high enough, at least for fractional powers. I've submitted a pull request to increase the precision, but the downside is it makes the power calculations much slower: https://github.com/ruby/bigdecimal/pull/171. It is up to @mrkn (Kenta Murata) whether the benefits of increasing the precision are worth the performance decrease.
Updated by Anonymous almost 4 years ago
- Status changed from Assigned to Closed
Applied in changeset git|a86c147579745859ea064ec22b2901a7ac7e4abf.
Import bigdecimal 2.0.2 (#3905)
-
remove duplicated include
-
Make BigDecimal#round with argument < 1 return Integer
Fixes [Bug #12780]
- Use a higher default precision for BigDecimal#power and #**
When a fractional power is given, increase the precision if the
precision isn't specified via power's second argument:
Float: increase by 15 (rough number of decimal precision in float)
BigDecimal: increase by adding similar precision modifier as done to
calculate the base precision.
Rational: double the precision, since a BigDecimal is created, but
the created BigDecimal uses the same precision.
Increasing the precision for these power calculations has the obvious
tradeoff of making the calculations slower.
Fixes Ruby Bug #17264
-
Use DBLE_FIG for a Float value
-
Version 2.0.1
Co-authored-by: pavel pavel.rosicky@easy.cz
Co-authored-by: Jeremy Evans code@jeremyevans.net