Project

General

Profile

Actions

Bug #6986

closed

Inconsistent result of BigDecimal#power

Added by phasis68 (Heesob Park) over 11 years ago. Updated over 10 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.0.0dev (2012-09-05 trunk 36913) [i386-mswin32_100]
Backport:
[ruby-core:47444]

Description

I found BigDecimal#power returns incorrect result for a large value.

C:\tmp>ruby -rbigdecimal -e "p BigDecimal('1e-10').power(1e7)"
#BigDecimal:a2bdb8,'0.1E-99999999',9(100000026)

C:\tmp>ruby -rbigdecimal -e "p BigDecimal('1e-10').power(1e8)"
-e:1:in power': failed to allocate memory (NoMemoryError) from -e:1:in '

C:\tmp>ruby -rbigdecimal -e "p BigDecimal('1e-10').power(1e10)"
#BigDecimal:f7cb3c,'0.0',9(18)

C:\tmp>ruby -rbigdecimal -e "p BigDecimal('1e-10').power(1e19)"
#BigDecimal:f7cb3c,'Infinity',9(18)

Updated by shyouhei (Shyouhei Urabe) over 11 years ago

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

Updated by mrkn (Kenta Murata) over 11 years ago

I cannot reproduce NoMemoryError on my environment.
However I think the following behaviors for the large parameters may be inconsistent, so I will attempt to fix these behavior.

Unfortunately I may not be able to fix NoMemoryError because I don't have the environment of mswin32.
Please submit your patch for fixing it.

$ ruby-head -v
ruby 2.0.0dev (2012-09-06 trunk 36915) [x86_64-darwin11.4.0]
$
$ ruby-head -rbigdecimal -e 'p BigDecimal("1e-10").power(1e7)'
#BigDecimal:7fbd51033f98,'0.1E-99999999',9(100000026)
$
$ ruby-head -rbigdecimal -e 'p BigDecimal("1e-10").power(1e8)'
#BigDecimal:7f8401033f90,'0.1E-999999999',9(1000000026)
$
$ ruby-head -rbigdecimal -e 'p BigDecimal("1e-10").power(1e10)'
-e:1:in power': integer 10000000000 too big to convert to int' (RangeError)
from -e:1:in `'
$
$ ruby-head -rbigdecimal -e 'p BigDecimal("1e-10").power(1e19)'
#BigDecimal:7fe595004998,'0.0',9(18)

Updated by phasis68 (Heesob Park) over 11 years ago

Could you try "p BigDecimal('1e-10').power(1e50)" ?
On Windows, the parameter higher than 1e19 returns Infinity instead of 0.0.

Here is a patch of NoMemoryError on Windows.

diff --git a/bigdecimal.c b/bigdecimal.c.new
index e798781..976503b 100644
--- a/bigdecimal.c
+++ b/bigdecimal.c.new
@@ -2184,7 +2184,7 @@ retry:
if (exp != NULL) {
return rmpd_power_by_big_decimal(x, exp, n);
}

  • else if (TYPE(vexp) == T_BIGNUM) {
  • else if (TYPE(vexp) == T_BIGNUM || FIX2INT(vexp)>=10000000) {
    VALUE abs_value = BigDecimal_abs(self);
    if (is_one(abs_value)) {
    return ToValue(VpCreateRbObject(n, "1"));

Updated by usa (Usaku NAKAMURA) over 11 years ago

Hello,

In message "[ruby-core:47446] [ruby-trunk - Bug #6986] Inconsistent result of BigDecimal#power"
on Sep.06,2012 14:27:38, wrote:

I cannot reproduce NoMemoryError on my environment.
However I think the following behaviors for the large parameters may be inconsistent, so I will attempt to fix these behavior.

Unfortunately I may not be able to fix NoMemoryError because I don't have the environment of mswin32.
Please submit your patch for fixing it.

Generally, you should fix platform independent problem first.
If some platform dependent problem remains after it, I'll fix it.

Regards,

U.Nakamura

Updated by mrkn (Kenta Murata) over 10 years ago

  • Status changed from Assigned to Closed

I think it is fixed in bigdecimal version 1.2.3.

Updated by phasis68 (Heesob Park) over 10 years ago

There is still NoMemoryError on Windows with trunk.

C:\Users\phasis>ruby -v -rbigdecimal -e "p BigDecimal('1e-10').power(1e8)"
ruby 2.1.0dev (2013-11-28 trunk 43886) [i386-mingw32]
-e:1:in power': failed to allocate memory (NoMemoryError) from -e:1:in '

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0