2010-04-25 21:55:51 -04:00
|
|
|
require_relative "testbase"
|
2009-09-20 11:07:38 -04:00
|
|
|
require "bigdecimal/math"
|
|
|
|
|
|
|
|
class TestBigMath < Test::Unit::TestCase
|
2010-04-25 21:55:51 -04:00
|
|
|
include TestBigDecimalBase
|
2009-09-20 11:07:38 -04:00
|
|
|
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)}
|
2009-09-23 13:21:26 -04:00
|
|
|
assert_raise(FloatDomainError) {sqrt(PINF, N)}
|
2009-09-20 11:07:38 -04:00
|
|
|
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))
|
2009-09-23 13:21:26 -04:00
|
|
|
assert_in_delta(Math::PI/2, atan(PINF, N))
|
2010-05-11 01:11:59 -04:00
|
|
|
assert_equal(BigDecimal("0.823840753418636291769355073102514088959345624027952954058347023122539489"),
|
|
|
|
atan(BigDecimal("1.08"), 72).round(72), '[ruby-dev:41257]')
|
2009-09-20 11:07:38 -04:00
|
|
|
end
|
2014-01-11 05:26:05 -05:00
|
|
|
|
|
|
|
def test_log
|
|
|
|
assert_equal(0, BigMath.log(BigDecimal("1.0"), 10))
|
|
|
|
assert_in_epsilon(Math.log(10)*1000, BigMath.log(BigDecimal("1e1000"), 10))
|
|
|
|
assert_raise(Math::DomainError) {BigMath.log(BigDecimal("0"), 10)}
|
|
|
|
assert_raise(Math::DomainError) {BigMath.log(BigDecimal("-1"), 10)}
|
2014-01-11 05:26:07 -05:00
|
|
|
assert_separately(%w[-rbigdecimal], <<-SRC)
|
2014-01-12 03:20:39 -05:00
|
|
|
begin
|
|
|
|
x = BigMath.log(BigDecimal("1E19999999999999"), 10)
|
|
|
|
rescue FloatDomainError
|
|
|
|
else
|
|
|
|
unless x.infinite?
|
|
|
|
assert_in_epsilon(Math.log(10)*19999999999999, x)
|
|
|
|
end
|
|
|
|
end
|
2014-01-11 05:26:07 -05:00
|
|
|
SRC
|
2014-01-11 05:26:05 -05:00
|
|
|
end
|
2009-09-20 11:07:38 -04:00
|
|
|
end
|