2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestMath < Test::Unit::TestCase
|
2008-01-23 08:49:06 -05:00
|
|
|
def check(a, b)
|
|
|
|
assert(a - b < Float::EPSILON * 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_atan2
|
|
|
|
check(Math.atan2(0, 1), 0)
|
|
|
|
check(Math.atan2(1, 1), Math::PI / 4)
|
|
|
|
check(Math.atan2(1, 0), Math::PI / 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cos
|
|
|
|
check(Math.cos(0 * Math::PI / 4), 1.0)
|
|
|
|
check(Math.cos(1 * Math::PI / 4), 1.0 / Math.sqrt(2))
|
|
|
|
check(Math.cos(2 * Math::PI / 4), 0.0)
|
|
|
|
check(Math.cos(4 * Math::PI / 4), -1.0)
|
|
|
|
check(Math.cos(6 * Math::PI / 4), 0.0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sin
|
|
|
|
check(Math.sin(0 * Math::PI / 4), 0.0)
|
|
|
|
check(Math.sin(1 * Math::PI / 4), 1.0 / Math.sqrt(2))
|
|
|
|
check(Math.sin(2 * Math::PI / 4), 1.0)
|
|
|
|
check(Math.sin(4 * Math::PI / 4), 0.0)
|
|
|
|
check(Math.sin(6 * Math::PI / 4), -1.0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tan
|
|
|
|
check(Math.tan(0 * Math::PI / 4), 0.0)
|
|
|
|
check(Math.tan(1 * Math::PI / 4), 1.0)
|
|
|
|
assert(Math.tan(2 * Math::PI / 4).abs > 1024)
|
|
|
|
check(Math.tan(4 * Math::PI / 4), 0.0)
|
|
|
|
assert(Math.tan(6 * Math::PI / 4).abs > 1024)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_acos
|
|
|
|
check(0 * Math::PI / 4, Math.acos( 1.0))
|
|
|
|
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(Errno::EDOM, Errno::ERANGE) { Math.acos(2.0) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_asin
|
|
|
|
check( 0 * Math::PI / 4, Math.asin( 0.0))
|
|
|
|
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(Errno::EDOM, Errno::ERANGE) { Math.asin(2.0) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_atan
|
|
|
|
check( 0 * Math::PI / 4, Math.atan( 0.0))
|
|
|
|
check( 1 * Math::PI / 4, Math.atan( 1.0))
|
|
|
|
check( 2 * Math::PI / 4, Math.atan(1.0 / 0.0))
|
|
|
|
check(-1 * Math::PI / 4, Math.atan(-1.0))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cosh
|
|
|
|
check(1, Math.cosh(0))
|
|
|
|
check((Math::E ** 1 + Math::E ** -1) / 2, Math.cosh(1))
|
|
|
|
check((Math::E ** 2 + Math::E ** -2) / 2, Math.cosh(2))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sinh
|
|
|
|
check(0, Math.sinh(0))
|
|
|
|
check((Math::E ** 1 - Math::E ** -1) / 2, Math.sinh(1))
|
|
|
|
check((Math::E ** 2 - Math::E ** -2) / 2, Math.sinh(2))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tanh
|
|
|
|
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))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_acosh
|
|
|
|
check(Math.acosh(1), 0)
|
|
|
|
check(Math.acosh((Math::E ** 1 + Math::E ** -1) / 2), 1)
|
|
|
|
check(Math.acosh((Math::E ** 2 + Math::E ** -2) / 2), 2)
|
|
|
|
assert_raise(Errno::EDOM, Errno::ERANGE) { Math.acosh(0) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_asinh
|
|
|
|
check(Math.asinh(0), 0)
|
|
|
|
check(Math.asinh((Math::E ** 1 - Math::E ** -1) / 2), 1)
|
|
|
|
check(Math.asinh((Math::E ** 2 - Math::E ** -2) / 2), 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_atanh
|
|
|
|
check(Math.atanh(Math.sinh(0) / Math.cosh(0)), 0)
|
|
|
|
check(Math.atanh(Math.sinh(1) / Math.cosh(1)), 1)
|
|
|
|
check(Math.atanh(Math.sinh(2) / Math.cosh(2)), 2)
|
|
|
|
assert_raise(Errno::EDOM, Errno::ERANGE) { Math.atanh(-1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exp
|
|
|
|
check(1, Math.exp(0))
|
|
|
|
check(Math.sqrt(Math::E), Math.exp(0.5))
|
|
|
|
check(Math::E, Math.exp(1))
|
|
|
|
check(Math::E ** 2, Math.exp(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_raise(Errno::EDOM, Errno::ERANGE) { Math.log(0) }
|
|
|
|
assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log(-1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_log2
|
|
|
|
check(0, Math.log2(1))
|
|
|
|
check(1, Math.log2(2))
|
|
|
|
check(2, Math.log2(4))
|
|
|
|
assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log2(0) }
|
|
|
|
assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log2(-1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_log10
|
|
|
|
check(0, Math.log10(1))
|
|
|
|
check(1, Math.log10(10))
|
|
|
|
check(2, Math.log10(100))
|
|
|
|
assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log10(0) }
|
|
|
|
assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log10(-1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sqrt
|
|
|
|
check(0, Math.sqrt(0))
|
|
|
|
check(1, Math.sqrt(1))
|
|
|
|
check(2, Math.sqrt(4))
|
|
|
|
assert_raise(Errno::EDOM, Errno::ERANGE) { Math.sqrt(-1) }
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ldexp
|
|
|
|
check(0.0, Math.ldexp(0.0, 0.0))
|
|
|
|
check(0.5, Math.ldexp(0.5, 0.0))
|
|
|
|
check(1.0, Math.ldexp(0.5, 1.0))
|
|
|
|
check(2.0, Math.ldexp(0.5, 2.0))
|
|
|
|
check(3.0, Math.ldexp(0.75, 2.0))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hypot
|
|
|
|
check(5, Math.hypot(3, 4))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_erf
|
|
|
|
check(0, Math.erf(0))
|
|
|
|
check(1, Math.erf(1.0 / 0.0))
|
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2008-01-23 08:49:06 -05:00
|
|
|
def test_erfc
|
|
|
|
check(1, Math.erfc(0))
|
|
|
|
check(0, Math.erfc(1.0 / 0.0))
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
end
|