Project

General

Profile

Bug #17264

BigDecimal exponentiation cannot be used with #** method

Added by karatedog (Földes László) about 1 month ago. Updated 6 days ago.

Status:
Assigned
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
[ruby-core:<unknown>]

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) 6 days ago

  • Assignee set to mrkn (Kenta Murata)
  • Status changed from Open to Assigned

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.

Also available in: Atom PDF