Project

General

Profile

Actions

Bug #8401

closed

BigDecimal.new("2").power(1e20) is zero.

Added by akr (Akira Tanaka) almost 11 years ago. Updated over 10 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.1.0dev (2013-05-13 trunk 40697) [x86_64-linux]
[ruby-dev:47340]

Description

ふと見つけたのですが、BigDecimal.new("2").power(1e20) が 0.0 になります。

% ./ruby -v -rbigdecimal -e 'puts BigDecimal.new("2").power(1e20)'
ruby 2.1.0dev (2013-05-13 trunk 40697) [x86_64-linux]
0.0

以下のように、power の引数が
1e1 や 1e2 なら値が出てきて、
1e10 ならエラーになるのに、
1e20 になるとまたエラーじゃなくなって 0.0 になっちゃうのは
変ではないでしょうか。

% ./ruby -rbigdecimal -e 'puts BigDecimal.new("2").power(1e1)'
0.1024E4
% ./ruby -rbigdecimal -e 'puts BigDecimal.new("2").power(1e2)'
0.1267650600228229401496703205376E31
% ./ruby -rbigdecimal -e 'puts BigDecimal.new("2").power(1e10)'
-e:1:in power': integer 10000000000 too big to convert to int' (RangeError)
from -e:1:in `'

Updated by mrkn (Kenta Murata) almost 11 years ago

  • Category set to ext
  • Assignee set to mrkn (Kenta Murata)

1.9.3p392 では -Infinity になるから、2.0.0p0 で導入されたバグですねぇ。

Updated by akr (Akira Tanaka) almost 11 years ago

2013/5/14 mrkn (Kenta Murata) :

1.9.3p392 では -Infinity になるから、2.0.0p0 で導入されたバグですねぇ。

こっちの Debian GNU/Linux では 1.9.3p392 でも 0.0 ですねぇ。
そちらの環境は書いてありませんが、環境依存でしょうか。

% ruby-1.9.3p392 -v -rbigdecimal -e 'puts BigDecimal.new("2").power(1e20)'
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
0.0

あと、2**(1e20) が負になるのはおかしいと思うので、-Infinity はそれはそれで
変だと思います。

[田中 哲][たなか あきら][Tanaka Akira]

Updated by mrkn (Kenta Murata) over 10 years ago

  • Status changed from Open to Closed

It is corrected in 1.2.3.

$ ruby -ve 'gem "bigdecimal", "1.2.3"; require "bigdecimal"; puts BigDecimal(2).power(1e20)'
ruby 1.9.3p484 (2013-11-22) [x86_64-darwin12.5.0]
Infinity

$ ruby -ve 'gem "bigdecimal", "1.2.3"; require "bigdecimal"; puts BigDecimal(2).power(1e20)'
ruby 2.0.0p353 (2013-11-22) [x86_64-darwin12.5.0]
Infinity

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0