* ext/bigdecimal/lib/bigdecimal/math.rb (atan): atan(Infinity) is

PI/2.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25063 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2009-09-23 17:21:26 +00:00
parent 1ef3ac4738
commit 39ea1c8c7c
3 changed files with 13 additions and 15 deletions

View File

@ -1,4 +1,7 @@
Thu Sep 24 02:08:35 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
Thu Sep 24 02:21:23 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/bigdecimal/lib/bigdecimal/math.rb (atan): atan(Infinity) is
PI/2.
* ext/bigdecimal/lib/bigdecimal/math.rb (atan): reduce loop with
the formula of the double corner. based on a patch from

View File

@ -117,22 +117,16 @@ module BigMath
# Computes the arctangent of x to the specified number of digits of precision.
#
# If x is infinite or NaN, returns NaN.
# If x is NaN, returns NaN.
def atan(x, prec)
raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
return BigDecimal("NaN") if x.infinite? || x.nan?
return BigDecimal("NaN") if x.nan?
pi = PI(prec)
if neg = x < 0
x = -x
end
if x.round(prec) == 1
return pi / (neg ? -4 : 4)
elsif inv = x > 1
x = 1 / x
end
if dbl = x > 0.5
x = (-1 + sqrt(1 + x**2, prec))/x
end
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 = (-1 + sqrt(1 + x**2, prec))/x if dbl = x > 0.5
n = prec + BigDecimal.double_fig
y = x
d = y

View File

@ -21,7 +21,7 @@ class TestBigMath < Test::Unit::TestCase
assert_equal(0.0, sqrt(BigDecimal("-0"), N))
assert_raise(FloatDomainError) {sqrt(BigDecimal("-1.0"), N)}
assert_raise(FloatDomainError) {sqrt(NAN, N)}
assert_equal(PINF, sqrt(PINF, N))
assert_raise(FloatDomainError) {sqrt(PINF, N)}
end
def test_sin
@ -56,6 +56,7 @@ class TestBigMath < Test::Unit::TestCase
assert_equal(0.0, atan(BigDecimal("0.0"), N))
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))
end
def test_exp