Project

General

Profile

Actions

Bug #5266

closed

BigDecimal#sqrt で、大きな数の平方根の、要求する精度が低いある範囲のとき、結果がおかしい

Added by metanest (Makoto Kishimoto) over 12 years ago. Updated over 10 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.4dev (2011-07-18 trunk 32577) [x86_64-freebsd8.2]
Backport:
[ruby-dev:44450]

Description

BigDecimal#sqrt で、大きい数(10**n でだいたい n > 100 あたり
から顕著)の平方根を求める時、sqrt の引数(桁数で要求する、精度の下限)
がだいたい 20 ~ 0.55 * n の時、期待するような結果が出ません。

以下具体例を示して説明します。

$ irb19
irb(main):001:0> RUBY_DESCRIPTION
=> "ruby 1.9.4dev (2011-07-18 trunk 32577) [x86_64-freebsd8.2]"
irb(main):002:0> require "bigdecimal"
=> true
irb(main):003:0> BigDecimal("2" + "0" * 100).sqrt(55)
=> #<BigDecimal:801370a88,'0.1414213562 3730925960 0758552551 27E51',36(81)>
irb(main):004:0> BigDecimal("2" + "0" * 100).sqrt(56)
=> #<BigDecimal:8013720b8,'0.1414213562 3730950488 0168872420 9698078569 6718753769 4807317668 1337583355E51',81(90)>

101 桁の数の平方根で、55 桁の精度を要求しても、36 桁の結果しか
帰って来ず、56 桁の結果の値と比較するとわかりますが、20桁程度の精度しか
ありません。

irb(main):005:0> BigDecimal("2" + "0" * 200).sqrt(109)
=> #<BigDecimal:80137ba00,'0.1414213562 3730950488 0168872420 9698078570 3148413402 613295E101',63(135)>
irb(main):006:0> BigDecimal("2" + "0" * 200).sqrt(110)
=> #<BigDecimal:80137d0a8,'0.1414213562 3730950488 0168872420 9698078569 6718753769 4807317667 9737990732 4784621071 8501120828 2516704398 7185098437 5959938108 258992945E101',144(144)>

201 桁の数だと、要求する精度が 109 桁と 110 桁の間に境界があり、
また異常のある結果の精度も倍程度になります。

Updated by mrkn (Kenta Murata) over 12 years ago

  • Assignee set to mrkn (Kenta Murata)
Actions #2

Updated by shyouhei (Shyouhei Urabe) about 12 years ago

  • Status changed from Open to Assigned

Updated by mame (Yusuke Endoh) about 11 years ago

  • Target version changed from 2.0.0 to 2.1.0
Actions #4

Updated by mrkn (Kenta Murata) over 10 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r43764.
Makoto, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • ext/bigdecimal/bigdecimal.c (BigDecimal_sqrt): Fix the precision of
    the result BigDecimal of sqrt.
    [Bug #5266] [ruby-dev:44450]

  • test/bigdecimal/test_bigdecimal.rb: add tests for the above changes.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0