[Bug #3267] BigDecimal/mathでatan()に1.08を入れると戻り値の有効桁数が足りない

Bug #3267:
BigDecimal/mathe$B$Ge(Batan()e$B$Ke(B1.08e$B$rF~$l$k$HLa$jCM$NM-8z7e?t$,B-$j$J$$e(B
http://redmine.ruby-lang.org/issues/show/3267

e$B5/I<<Te(B: _ wanabe
e$B%9%F!<%?%9e(B: Open, e$BM%@hEYe(B: Normal
e$BC4Ev<Te(B: Kenta M.
ruby -v: ruby 1.9.2dev (2010-05-04 trunk 27600) [i386-mingw32]

2ch.net e$B>e$GJs9p$,$"$C$?$N$GE>:$7$^$9!#e(B

Rubye$B$K$D$$$Fe(B Part 40
http://pc12.2ch.net/test/read.cgi/tech/1272248179/121-122

Rubye$B$N%H%i%s%/$Ne(BBigDecimal/math
atan()e$B$Ke(B1.08e$B$rF~$l$k$HLa$jCM$NM-8z7e?t$,B-$j$J$$e(B

return pi.div(neg ? -2 : 2, prec) if x.infinite?
return pi / (neg ? -4 : 4) if x.round(prec) == 1

  • x = 1 / x if inv = x > 1
    x = (-1 + sqrt(1 + x**2, prec))/x if dbl = x > 0.5

irb(main):004:0> BigDecimal(1)/BigDecimal(1.08)
=> #<BigDecimal:9940ce4,0.9259259259 25926E0,16(24)>
irb(main):005:0> BigDecimal(1).div(BigDecimal(1.08), 30)
=> #<BigDecimal:9957468,0.9259259259 2592592592 5925925926E0,32(44)>
irb(main):006:0>

e$B$3$3$G7e?t$,B-$j$J$/$J$k$N$M!#e(B
e$BJ,Jl$HJ,;R$NM-8z7e?t$,>/$J$$$H$-$K!=PNO$NM-8z7e?t$N;XDj$,L5$$>l9g$O!e(B
e$BE,Ev$K$A$g$s$.$k$N$O!8@8l;EMME*$K$O!@52r$G$$j$^$7$g$&!#e(B
1/3e$B$,Mh$?$i!%a%b%j$rA4It;H$$@Z$j$^$7$?!$G$OHa$7$$!#e(B
e$B$O$$!e(B
x = BigDecimal(1).div(x, prec)
e$B$G!$J$*$j$^$9!#e(B
y = tan(atan(x, prec), prec)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/$Ne(BBigDecimal/math
atan()e$B$Ke(B1.08e$B$rF~$l$k$HLa$jCM$NM-8z7e?t$,B-$j$J$$e(B

return pi.div(neg ? -2 : 2, prec) if x.infinite?
return pi / (neg ? -4 : 4) if x.round(prec) == 1

  • x = 1 / x if inv = x > 1
    x = (-1 + sqrt(1 + x**2, prec))/x if dbl = x > 0.5

(snip)

e$B$O$$!e(B
x = BigDecimal(1).div(x, prec)
e$B$G!$J$*$j$^$9!#e(B

e$B$3$l$K4X$7$F$O!"0J2<$N=$@5$r%3%_%C%H$7$^$9!#e(B
e$B%P%0$@$H;W$&$N$Ge(B 1.9.2 e$B$N%V%i%s%A$K$b%^!<%8$7$^$9!#e(B

diff --git a/ext/bigdecimal/lib/bigdecimal/math.rb
b/ext/bigdecimal/lib/bigdecimal/math.rb
index eeffde407efcbe 100644
a/ext/bigdecimal/lib/bigdecimal/math.rb
+++ b/ext/bigdecimal/lib/bigdecimal/math.rb
@@ -125,7 +125,7 @@ module BigMath
x = -x if neg = x < 0
return pi.div(neg ? -2 : 2, prec) if x.infinite?
return pi / (neg ? -4 : 4) if x.round(prec) == 1

  • x = 1 / x if inv = x > 1
  • x = BigDecimal(1).div(x, prec) if inv = x > 1
    x = (-1 + sqrt(1 + x**2, prec))/x if dbl = x > 0.5
    n = prec + BigDecimal.double_fig
    y = x
    diff --git a/test/bigdecimal/test_bigmath.rb
    b/test/bigdecimal/test_bigmath.rb
    index fbeb062453c47e 100644
    a/test/bigdecimal/test_bigmath.rb
    +++ b/test/bigdecimal/test_bigmath.rb
    @@ -57,6 +57,8 @@ class TestBigMath < Test::Unit::TestCase
    assert_in_delta(Math::PI/4, atan(BigDecimal(1.0), N))
    assert_in_delta(Math::PI/6, atan(sqrt(BigDecimal(3.0), N) / 3,
    N))
    assert_in_delta(Math::PI/2, atan(PINF, N))

assert_equal(BigDecimal(0.823840753418636291769355073102514088959345624027952954058347023122539489),

  •             atan(BigDecimal("1.08"), 72).round(72), 
    

[ruby-dev:41257])
end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)$Ne(B Feature e$B%A%1%C%H$KJ,N%$7!"e(B
1.9.3 e$B$G2~A1$9$k$3$H$rL;X$7$^$9!#e(B


Kenta M.
OpenPGP FP = FA26 35D7 4F98 3498 0810 E0D5 F213 966F E9EB 0BCC

e$BK$r=q$-$^$7$?e(B!!
e$B!Xe(BRuby e$B5U0z$-%l%7%T!Ye(B

E-mail: [email protected]
twitter: http://twitter.com/mrkn/
blog:

e$B%A%1%C%He(B #3267 e$B$,99?7$5$l$^$7$?!#e(B (by Kenta M.)

e$B%9%F!<%?%9e(B Opene$B$+$ie(BClosede$B$KJQ99e(B
e$B?JD=e(B % 0e$B$+$ie(B100e$B$KJQ99e(B

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


http://redmine.ruby-lang.org/issues/show/3267