summaryrefslogtreecommitdiff
path: root/test/ruby/test_math.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_math.rb')
-rw-r--r--test/ruby/test_math.rb264
1 files changed, 190 insertions, 74 deletions
diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb
index 3895eec7f5..e134600cc4 100644
--- a/test/ruby/test_math.rb
+++ b/test/ruby/test_math.rb
@@ -1,30 +1,44 @@
+# frozen_string_literal: false
require 'test/unit'
class TestMath < Test::Unit::TestCase
def assert_infinity(a, *rest)
rest = ["not infinity: #{a.inspect}"] if rest.empty?
- assert(!a.finite?, *rest)
+ assert_predicate(a, :infinite?, *rest)
+ assert_predicate(a, :positive?, *rest)
end
def assert_nan(a, *rest)
rest = ["not nan: #{a.inspect}"] if rest.empty?
- assert(a.nan?, *rest)
+ assert_predicate(a, :nan?, *rest)
end
- def check(a, b)
+ def assert_float(a, b)
err = [Float::EPSILON * 4, [a.abs, b.abs].max * Float::EPSILON * 256].max
assert_in_delta(a, b, err)
end
+ alias check assert_float
+
+ def assert_float_and_int(exp_ary, act_ary)
+ flo_exp, int_exp, flo_act, int_act = *exp_ary, *act_ary
+ assert_float(flo_exp, flo_act)
+ assert_equal(int_exp, int_act)
+ end
def test_atan2
check(+0.0, Math.atan2(+0.0, +0.0))
check(-0.0, Math.atan2(-0.0, +0.0))
check(+Math::PI, Math.atan2(+0.0, -0.0))
check(-Math::PI, Math.atan2(-0.0, -0.0))
- assert_raise(Math::DomainError) { Math.atan2(Float::INFINITY, Float::INFINITY) }
- assert_raise(Math::DomainError) { Math.atan2(Float::INFINITY, -Float::INFINITY) }
- assert_raise(Math::DomainError) { Math.atan2(-Float::INFINITY, Float::INFINITY) }
- assert_raise(Math::DomainError) { Math.atan2(-Float::INFINITY, -Float::INFINITY) }
+
+ inf = Float::INFINITY
+ expected = 3.0 * Math::PI / 4.0
+ assert_nothing_raised { check(+expected, Math.atan2(+inf, -inf)) }
+ assert_nothing_raised { check(-expected, Math.atan2(-inf, -inf)) }
+ expected = Math::PI / 4.0
+ assert_nothing_raised { check(+expected, Math.atan2(+inf, +inf)) }
+ assert_nothing_raised { check(-expected, Math.atan2(-inf, +inf)) }
+
check(0, Math.atan2(0, 1))
check(Math::PI / 4, Math.atan2(1, 1))
check(Math::PI / 2, Math.atan2(1, 0))
@@ -36,6 +50,7 @@ class TestMath < Test::Unit::TestCase
check(0.0, Math.cos(2 * Math::PI / 4))
check(-1.0, Math.cos(4 * Math::PI / 4))
check(0.0, Math.cos(6 * Math::PI / 4))
+ check(0.5403023058681398, Math.cos(1))
end
def test_sin
@@ -49,9 +64,9 @@ class TestMath < Test::Unit::TestCase
def test_tan
check(0.0, Math.tan(0 * Math::PI / 4))
check(1.0, Math.tan(1 * Math::PI / 4))
- assert(Math.tan(2 * Math::PI / 4).abs > 1024)
+ assert_operator(Math.tan(2 * Math::PI / 4).abs, :>, 1024)
check(0.0, Math.tan(4 * Math::PI / 4))
- assert(Math.tan(6 * Math::PI / 4).abs > 1024)
+ assert_operator(Math.tan(6 * Math::PI / 4).abs, :>, 1024)
end
def test_acos
@@ -59,9 +74,9 @@ class TestMath < Test::Unit::TestCase
check(1 * Math::PI / 4, Math.acos( 1.0 / Math.sqrt(2)))
check(2 * Math::PI / 4, Math.acos( 0.0))
check(4 * Math::PI / 4, Math.acos(-1.0))
- assert_raise(Math::DomainError) { Math.acos(+1.0 + Float::EPSILON) }
- assert_raise(Math::DomainError) { Math.acos(-1.0 - Float::EPSILON) }
- assert_raise(Math::DomainError) { Math.acos(2.0) }
+ assert_raise_with_message(Math::DomainError, /\bacos\b/) { Math.acos(+1.0 + Float::EPSILON) }
+ assert_raise_with_message(Math::DomainError, /\bacos\b/) { Math.acos(-1.0 - Float::EPSILON) }
+ assert_raise_with_message(Math::DomainError, /\bacos\b/) { Math.acos(2.0) }
end
def test_asin
@@ -69,9 +84,9 @@ class TestMath < Test::Unit::TestCase
check( 1 * Math::PI / 4, Math.asin( 1.0 / Math.sqrt(2)))
check( 2 * Math::PI / 4, Math.asin( 1.0))
check(-2 * Math::PI / 4, Math.asin(-1.0))
- assert_raise(Math::DomainError) { Math.asin(+1.0 + Float::EPSILON) }
- assert_raise(Math::DomainError) { Math.asin(-1.0 - Float::EPSILON) }
- assert_raise(Math::DomainError) { Math.asin(2.0) }
+ assert_raise_with_message(Math::DomainError, /\basin\b/) { Math.asin(+1.0 + Float::EPSILON) }
+ assert_raise_with_message(Math::DomainError, /\basin\b/) { Math.asin(-1.0 - Float::EPSILON) }
+ assert_raise_with_message(Math::DomainError, /\basin\b/) { Math.asin(2.0) }
end
def test_atan
@@ -97,14 +112,16 @@ class TestMath < Test::Unit::TestCase
check(Math.sinh(0) / Math.cosh(0), Math.tanh(0))
check(Math.sinh(1) / Math.cosh(1), Math.tanh(1))
check(Math.sinh(2) / Math.cosh(2), Math.tanh(2))
+ check(+1.0, Math.tanh(+1000.0))
+ check(-1.0, Math.tanh(-1000.0))
end
def test_acosh
check(0, Math.acosh(1))
check(1, Math.acosh((Math::E ** 1 + Math::E ** -1) / 2))
check(2, Math.acosh((Math::E ** 2 + Math::E ** -2) / 2))
- assert_raise(Math::DomainError) { Math.acosh(1.0 - Float::EPSILON) }
- assert_raise(Math::DomainError) { Math.acosh(0) }
+ assert_raise_with_message(Math::DomainError, /\bacosh\b/) { Math.acosh(1.0 - Float::EPSILON) }
+ assert_raise_with_message(Math::DomainError, /\bacosh\b/) { Math.acosh(0) }
end
def test_asinh
@@ -119,8 +136,8 @@ class TestMath < Test::Unit::TestCase
check(2, Math.atanh(Math.sinh(2) / Math.cosh(2)))
assert_nothing_raised { assert_infinity(Math.atanh(1)) }
assert_nothing_raised { assert_infinity(-Math.atanh(-1)) }
- assert_raise(Math::DomainError) { Math.atanh(+1.0 + Float::EPSILON) }
- assert_raise(Math::DomainError) { Math.atanh(-1.0 - Float::EPSILON) }
+ assert_raise_with_message(Math::DomainError, /\batanh\b/) { Math.atanh(+1.0 + Float::EPSILON) }
+ assert_raise_with_message(Math::DomainError, /\batanh\b/) { Math.atanh(-1.0 - Float::EPSILON) }
end
def test_exp
@@ -130,59 +147,110 @@ class TestMath < Test::Unit::TestCase
check(Math::E ** 2, Math.exp(2))
end
+ def test_expm1
+ check(0, Math.expm1(0))
+ check(Math.sqrt(Math::E) - 1, Math.expm1(0.5))
+ check(Math::E - 1, Math.expm1(1))
+ check(Math::E ** 2 - 1, Math.expm1(2))
+ end
+
def test_log
check(0, Math.log(1))
check(1, Math.log(Math::E))
check(0, Math.log(1, 10))
check(1, Math.log(10, 10))
check(2, Math.log(100, 10))
- assert_equal(1.0/0, Math.log(1.0/0))
+ check(Math.log(2.0 ** 64), Math.log(1 << 64))
+ check(Math.log(2) * 1024.0, Math.log(2 ** 1024))
+ assert_nothing_raised { assert_infinity(Math.log(1.0/0)) }
assert_nothing_raised { assert_infinity(-Math.log(+0.0)) }
assert_nothing_raised { assert_infinity(-Math.log(-0.0)) }
- assert_raise(Math::DomainError) { Math.log(-1.0) }
+ assert_raise_with_message(Math::DomainError, /\blog\b/) { Math.log(-1.0) }
+ assert_raise_with_message(Math::DomainError, /\blog\b/) { Math.log(-Float::EPSILON) }
assert_raise(TypeError) { Math.log(1,nil) }
+ assert_raise_with_message(Math::DomainError, /\blog\b/, '[ruby-core:62309] [ruby-Bug #9797]') { Math.log(1.0, -1.0) }
+ assert_raise_with_message(Math::DomainError, /\blog\b/) { Math.log(1.0, -Float::EPSILON) }
+ assert_nothing_raised { assert_nan(Math.log(0.0, 0.0)) }
+ assert_nothing_raised { assert_nan(Math.log(Float::NAN)) }
+ assert_nothing_raised { assert_nan(Math.log(1.0, Float::NAN)) }
+ assert_nothing_raised { assert_infinity(-Math.log(0)) }
+ assert_nothing_raised { assert_infinity(-Math.log(0, 2)) }
+ check(307.95368556425274, Math.log(2**1023, 10))
end
def test_log2
check(0, Math.log2(1))
check(1, Math.log2(2))
check(2, Math.log2(4))
- assert_equal(1.0/0, Math.log2(1.0/0))
+ check(Math.log2(2.0 ** 64), Math.log2(1 << 64))
+ check(1024.0, Math.log2(2 ** 1024))
+ assert_nothing_raised { assert_infinity(Math.log2(1.0/0)) }
assert_nothing_raised { assert_infinity(-Math.log2(+0.0)) }
assert_nothing_raised { assert_infinity(-Math.log2(-0.0)) }
- assert_raise(Math::DomainError) { Math.log2(-1.0) }
+ assert_raise_with_message(Math::DomainError, /\blog2\b/) { Math.log2(-1.0) }
+ assert_raise_with_message(Math::DomainError, /\blog2\b/) { Math.log2(-Float::EPSILON) }
+ assert_nothing_raised { assert_nan(Math.log2(Float::NAN)) }
+ assert_nothing_raised { assert_infinity(-Math.log2(0)) }
end
def test_log10
check(0, Math.log10(1))
check(1, Math.log10(10))
check(2, Math.log10(100))
- assert_equal(1.0/0, Math.log10(1.0/0))
+ check(Math.log10(2.0 ** 64), Math.log10(1 << 64))
+ check(Math.log10(2) * 1024, Math.log10(2 ** 1024))
+ assert_nothing_raised { assert_infinity(Math.log10(1.0/0)) }
assert_nothing_raised { assert_infinity(-Math.log10(+0.0)) }
assert_nothing_raised { assert_infinity(-Math.log10(-0.0)) }
- assert_raise(Math::DomainError) { Math.log10(-1.0) }
+ assert_raise_with_message(Math::DomainError, /\blog10\b/) { Math.log10(-1.0) }
+ assert_raise_with_message(Math::DomainError, /\blog10\b/) { Math.log10(-Float::EPSILON) }
+ assert_nothing_raised { assert_nan(Math.log10(Float::NAN)) }
+ assert_nothing_raised { assert_infinity(-Math.log10(0)) }
+ end
+
+ def test_log1p
+ check(0, Math.log1p(0))
+ check(1, Math.log1p(Math::E - 1))
+ check(Math.log(2.0 ** 64 + 1), Math.log1p(1 << 64))
+ check(Math.log(2) * 1024.0, Math.log1p(2 ** 1024))
+ assert_nothing_raised { assert_infinity(Math.log1p(1.0/0)) }
+ assert_nothing_raised { assert_infinity(-Math.log1p(-1.0)) }
+ assert_raise_with_message(Math::DomainError, /\blog1p\b/) { Math.log1p(-1.1) }
+ assert_raise_with_message(Math::DomainError, /\blog1p\b/) { Math.log1p(-Float::EPSILON-1) }
+ assert_nothing_raised { assert_nan(Math.log1p(Float::NAN)) }
+ assert_nothing_raised { assert_infinity(-Math.log1p(-1)) }
end
def test_sqrt
check(0, Math.sqrt(0))
check(1, Math.sqrt(1))
check(2, Math.sqrt(4))
- assert_equal(1.0/0, Math.sqrt(1.0/0))
+ assert_nothing_raised { assert_infinity(Math.sqrt(1.0/0)) }
assert_equal("0.0", Math.sqrt(-0.0).to_s) # insure it is +0.0, not -0.0
- assert_raise(Math::DomainError) { Math.sqrt(-1.0) }
+ assert_raise_with_message(Math::DomainError, /\bsqrt\b/) { Math.sqrt(-1.0) }
+ assert_raise_with_message(Math::DomainError, /\bsqrt\b/) { Math.sqrt(-Float::EPSILON) }
+ assert_nothing_raised { assert_nan(Math.sqrt(Float::NAN)) }
+ end
+
+ def test_cbrt
+ check(1, Math.cbrt(1))
+ check(-2, Math.cbrt(-8))
+ check(3, Math.cbrt(27))
+ check(-0.1, Math.cbrt(-0.001))
+ check(0.0, Math.cbrt(0.0))
+ assert_nothing_raised { assert_infinity(Math.cbrt(1.0/0)) }
+ assert_operator(Math.cbrt(1.0 - Float::EPSILON), :<=, 1.0)
+ assert_nothing_raised { assert_nan(Math.sqrt(Float::NAN)) }
+ assert_nothing_raised { assert_nan(Math.cbrt(Float::NAN)) }
end
def test_frexp
- check(0.0, Math.frexp(0.0).first)
- assert_equal(0, Math.frexp(0).last)
- check(0.5, Math.frexp(0.5).first)
- assert_equal(0, Math.frexp(0.5).last)
- check(0.5, Math.frexp(1.0).first)
- assert_equal(1, Math.frexp(1.0).last)
- check(0.5, Math.frexp(2.0).first)
- assert_equal(2, Math.frexp(2.0).last)
- check(0.75, Math.frexp(3.0).first)
- assert_equal(2, Math.frexp(3.0).last)
+ assert_float_and_int([0.0, 0], Math.frexp(0.0))
+ assert_float_and_int([0.5, 0], Math.frexp(0.5))
+ assert_float_and_int([0.5, 1], Math.frexp(1.0))
+ assert_float_and_int([0.5, 2], Math.frexp(2.0))
+ assert_float_and_int([0.75, 2], Math.frexp(3.0))
+ assert_nan(Math.frexp(Float::NAN)[0])
end
def test_ldexp
@@ -200,11 +268,13 @@ class TestMath < Test::Unit::TestCase
def test_erf
check(0, Math.erf(0))
check(1, Math.erf(1.0 / 0.0))
+ assert_nan(Math.erf(Float::NAN))
end
def test_erfc
check(1, Math.erfc(0))
check(0, Math.erfc(1.0 / 0.0))
+ assert_nan(Math.erfc(Float::NAN))
end
def test_gamma
@@ -219,6 +289,8 @@ class TestMath < Test::Unit::TestCase
check(2, Math.gamma(3))
check(15 * sqrt_pi / 8, Math.gamma(3.5))
check(6, Math.gamma(4))
+ check(1.1240007277776077e+21, Math.gamma(23))
+ check(2.5852016738885062e+22, Math.gamma(24))
# no SEGV [ruby-core:25257]
31.upto(65) do |i|
@@ -227,55 +299,99 @@ class TestMath < Test::Unit::TestCase
assert_infinity(Math.gamma(i-1), "Math.gamma(#{i-1}) should be INF")
end
- assert_raise(Math::DomainError) { Math.gamma(-Float::INFINITY) }
+ assert_raise_with_message(Math::DomainError, /\bgamma\b/) { Math.gamma(-Float::INFINITY) }
+ assert_raise_with_message(Math::DomainError, /\bgamma\b/) { Math.gamma(-1.0) }
+ x = Math.gamma(-0.0)
+ mesg = "Math.gamma(-0.0) should be -INF"
+ assert_infinity(-x, mesg)
+ assert_nan(Math.gamma(Float::NAN))
end
def test_lgamma
sqrt_pi = Math.sqrt(Math::PI)
+ assert_float_and_int([Math.log(4 * sqrt_pi / 3), 1], Math.lgamma(-1.5))
+ assert_float_and_int([Math.log(2 * sqrt_pi), -1], Math.lgamma(-0.5))
+ assert_float_and_int([Math.log(sqrt_pi), 1], Math.lgamma(0.5))
+ assert_float_and_int([0, 1], Math.lgamma(1))
+ assert_float_and_int([Math.log(sqrt_pi / 2), 1], Math.lgamma(1.5))
+ assert_float_and_int([0, 1], Math.lgamma(2))
+ assert_float_and_int([Math.log(3 * sqrt_pi / 4), 1], Math.lgamma(2.5))
+ assert_float_and_int([Math.log(2), 1], Math.lgamma(3))
+ assert_float_and_int([Math.log(15 * sqrt_pi / 8), 1], Math.lgamma(3.5))
+ assert_float_and_int([Math.log(6), 1], Math.lgamma(4))
+
+ assert_raise_with_message(Math::DomainError, /\blgamma\b/) { Math.lgamma(-Float::INFINITY) }
+
+ x, sign = Math.lgamma(+0.0)
+ mesg = "Math.lgamma(+0.0) should be [INF, +1]"
+ assert_infinity(x, mesg)
+ assert_equal(+1, sign, mesg)
+
+ x, sign = Math.lgamma(-0.0)
+ mesg = "Math.lgamma(-0.0) should be [INF, -1]"
+ assert_infinity(x, mesg)
+ assert_equal(-1, sign, mesg)
+
+ x, = Math.lgamma(-1)
+ assert_infinity(x, "Math.lgamma(-1) should be +INF")
+
+ x, = Math.lgamma(Float::NAN)
+ assert_nan(x)
+ end
- g, s = Math.lgamma(-1.5)
- check(Math.log(4 * sqrt_pi / 3), g)
- assert_equal(s, 1)
-
- g, s = Math.lgamma(-0.5)
- check(Math.log(2 * sqrt_pi), g)
- assert_equal(s, -1)
-
- g, s = Math.lgamma(0.5)
- check(Math.log(sqrt_pi), g)
- assert_equal(s, 1)
-
- assert_equal([0, 1], Math.lgamma(1))
-
- g, s = Math.lgamma(1.5)
- check(Math.log(sqrt_pi / 2), g)
- assert_equal(s, 1)
+ def test_fixnum_to_f
+ check(12.0, Math.sqrt(144))
+ end
- assert_equal([0, 1], Math.lgamma(2))
+ def test_override_integer_to_f
+ Integer.class_eval do
+ alias _to_f to_f
+ def to_f
+ (self + 1)._to_f
+ end
+ end
- g, s = Math.lgamma(2.5)
- check(Math.log(3 * sqrt_pi / 4), g)
- assert_equal(s, 1)
+ check(Math.cos((0 + 1)._to_f), Math.cos(0))
+ check(Math.exp((0 + 1)._to_f), Math.exp(0))
+ check(Math.log((0 + 1)._to_f), Math.log(0))
+ ensure
+ Integer.class_eval { undef to_f; alias to_f _to_f; undef _to_f }
+ end
- g, s = Math.lgamma(3)
- check(Math.log(2), g)
- assert_equal(s, 1)
+ def test_bignum_to_f
+ check((1 << 65).to_f, Math.sqrt(1 << 130))
+ end
- g, s = Math.lgamma(3.5)
- check(Math.log(15 * sqrt_pi / 8), g)
- assert_equal(s, 1)
+ def test_override_bignum_to_f
+ Integer.class_eval do
+ alias _to_f to_f
+ def to_f
+ (self << 1)._to_f
+ end
+ end
- g, s = Math.lgamma(4)
- check(Math.log(6), g)
- assert_equal(s, 1)
+ check(Math.cos((1 << 64 << 1)._to_f), Math.cos(1 << 64))
+ check(Math.log((1 << 64 << 1)._to_f), Math.log(1 << 64))
+ ensure
+ Integer.class_eval { undef to_f; alias to_f _to_f; undef _to_f }
+ end
- assert_raise(Math::DomainError) { Math.lgamma(-Float::INFINITY) }
+ def test_rational_to_f
+ check((2 ** 31).fdiv(3 ** 20), Math.sqrt((2 ** 62)/(3 ** 40).to_r))
end
- def test_cbrt
- check(1, Math.cbrt(1))
- check(-2, Math.cbrt(-8))
- check(3, Math.cbrt(27))
- check(-0.1, Math.cbrt(-0.001))
+ def test_override_rational_to_f
+ Rational.class_eval do
+ alias _to_f to_f
+ def to_f
+ (self + 1)._to_f
+ end
+ end
+
+ check(Math.cos((0r + 1)._to_f), Math.cos(0r))
+ check(Math.exp((0r + 1)._to_f), Math.exp(0r))
+ check(Math.log((0r + 1)._to_f), Math.log(0r))
+ ensure
+ Rational.class_eval { undef to_f; alias to_f _to_f; undef _to_f }
end
end