summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--ext/bigdecimal/lib/bigdecimal/math.rb20
-rw-r--r--test/bigdecimal/test_bigmath.rb3
3 files changed, 13 insertions, 15 deletions
diff --git a/ChangeLog b/ChangeLog
index ff359dc1e3..0315699ef2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/ext/bigdecimal/lib/bigdecimal/math.rb b/ext/bigdecimal/lib/bigdecimal/math.rb
index d1e8dbe326..eeffde4e68 100644
--- a/ext/bigdecimal/lib/bigdecimal/math.rb
+++ b/ext/bigdecimal/lib/bigdecimal/math.rb
@@ -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
diff --git a/test/bigdecimal/test_bigmath.rb b/test/bigdecimal/test_bigmath.rb
index 7801e6045b..2870664bcf 100644
--- a/test/bigdecimal/test_bigmath.rb
+++ b/test/bigdecimal/test_bigmath.rb
@@ -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