summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-09-20 15:07:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-09-20 15:07:38 +0000
commitcd4f59bc5e32c71fcef679ebdbd750c7d2ed5f5e (patch)
tree1f13e1e640ac8b543a7e6c958731b8bbf6942932 /test
parent970ec9c02d683b86ca90dc6e63f3c1b4d8226960 (diff)
* ext/bigdecimal/lib/bigdecimal/math.rb (sin, cos, atan, exp, log):
improved precision and performance. based on a patch from Makoto Yamashita in [ruby-core:25600] and [ruby-core:25602]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25013 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/bigdecimal/test_bigmath.rb77
1 files changed, 77 insertions, 0 deletions
diff --git a/test/bigdecimal/test_bigmath.rb b/test/bigdecimal/test_bigmath.rb
new file mode 100644
index 0000000000..7801e6045b
--- /dev/null
+++ b/test/bigdecimal/test_bigmath.rb
@@ -0,0 +1,77 @@
+require "test/unit"
+require "bigdecimal"
+require "bigdecimal/math"
+
+class TestBigMath < Test::Unit::TestCase
+ include BigMath
+ N = 20
+ PINF = BigDecimal("+Infinity")
+ MINF = BigDecimal("-Infinity")
+ NAN = BigDecimal("NaN")
+
+ def test_const
+ assert_in_delta(Math::PI, PI(N))
+ assert_in_delta(Math::E, E(N))
+ end
+
+ def test_sqrt
+ assert_in_delta(2**0.5, sqrt(BigDecimal("2"), N))
+ assert_equal(10, sqrt(BigDecimal("100"), N))
+ assert_equal(0.0, sqrt(BigDecimal("0"), N))
+ 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))
+ end
+
+ def test_sin
+ assert_in_delta(0.0, sin(BigDecimal("0.0"), N))
+ assert_in_delta(Math.sqrt(2.0) / 2, sin(PI(N) / 4, N))
+ assert_in_delta(1.0, sin(PI(N) / 2, N))
+ assert_in_delta(0.0, sin(PI(N) * 2, N))
+ assert_in_delta(0.0, sin(PI(N), N))
+ assert_in_delta(-1.0, sin(PI(N) / -2, N))
+ assert_in_delta(0.0, sin(PI(N) * -2, N))
+ assert_in_delta(0.0, sin(-PI(N), N))
+ assert_in_delta(0.0, sin(PI(N) * 21, N))
+ assert_in_delta(0.0, sin(PI(N) * 30, N))
+ assert_in_delta(-1.0, sin(PI(N) * BigDecimal("301.5"), N))
+ end
+
+ def test_cos
+ assert_in_delta(1.0, cos(BigDecimal("0.0"), N))
+ assert_in_delta(Math.sqrt(2.0) / 2, cos(PI(N) / 4, N))
+ assert_in_delta(0.0, cos(PI(N) / 2, N))
+ assert_in_delta(1.0, cos(PI(N) * 2, N))
+ assert_in_delta(-1.0, cos(PI(N), N))
+ assert_in_delta(0.0, cos(PI(N) / -2, N))
+ assert_in_delta(1.0, cos(PI(N) * -2, N))
+ assert_in_delta(-1.0, cos(-PI(N), N))
+ assert_in_delta(-1.0, cos(PI(N) * 21, N))
+ assert_in_delta(1.0, cos(PI(N) * 30, N))
+ assert_in_delta(0.0, cos(PI(N) * BigDecimal("301.5"), N))
+ end
+
+ def test_atan
+ 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))
+ end
+
+ def test_exp
+ assert_in_epsilon(Math::E, exp(BigDecimal("1"), N))
+ assert_in_epsilon(Math.exp(N), exp(BigDecimal("20"), N))
+ assert_in_epsilon(Math.exp(40), exp(BigDecimal("40"), N))
+ assert_in_epsilon(Math.exp(-N), exp(BigDecimal("-20"), N))
+ assert_in_epsilon(Math.exp(-40), exp(BigDecimal("-40"), N))
+ end
+
+ def test_log
+ assert_in_delta(0.0, log(BigDecimal("1"), N))
+ assert_in_delta(1.0, log(E(N), N))
+ assert_in_delta(Math.log(2.0), log(BigDecimal("2"), N))
+ assert_in_delta(2.0, log(E(N)*E(N), N))
+ assert_in_delta(Math.log(42.0), log(BigDecimal("42"), N))
+ assert_in_delta(Math.log(1e-42), log(BigDecimal("1e-42"), N))
+ end
+end