diff --git a/ChangeLog b/ChangeLog index 8a1c3c6153..5c3ed8b5c2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Jun 25 21:26:00 2013 Kenta Murata + + * ext/bigdecimal/bigdecimal.c (BigMath_s_exp): Fix for the cases when + the argument x is not a BigDecimal. + This change is based on the patch made by Garth Snyder. + [Fix GH-332] https://github.com/ruby/ruby/pull/332 + Tue Jun 25 20:36:31 2013 Tanaka Akira * bignum.c (big2ulong): "check" argument removed. diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index ba8777635f..87587f81a0 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -2725,7 +2725,7 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec) else if (vx == NULL) { cannot_be_coerced_into_BigDecimal(rb_eArgError, x); } - RB_GC_GUARD(vx->obj); + x = RB_GC_GUARD(vx->obj); n = prec + rmpd_double_figures(); negative = VpGetSign(vx) < 0; diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 07c79a8874..5cbafdafd4 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1276,11 +1276,35 @@ class TestBigDecimal < Test::Unit::TestCase end def test_BigMath_exp - n = 20 - assert_in_epsilon(Math.exp(n), BigMath.exp(BigDecimal("20"), n)) - assert_in_epsilon(Math.exp(40), BigMath.exp(BigDecimal("40"), n)) - assert_in_epsilon(Math.exp(-n), BigMath.exp(BigDecimal("-20"), n)) - assert_in_epsilon(Math.exp(-40), BigMath.exp(BigDecimal("-40"), n)) + prec = 20 + assert_in_epsilon(Math.exp(20), BigMath.exp(BigDecimal("20"), prec)) + assert_in_epsilon(Math.exp(40), BigMath.exp(BigDecimal("40"), prec)) + assert_in_epsilon(Math.exp(-20), BigMath.exp(BigDecimal("-20"), prec)) + assert_in_epsilon(Math.exp(-40), BigMath.exp(BigDecimal("-40"), prec)) + end + + def test_BigMath_exp_with_float + prec = 20 + assert_in_epsilon(Math.exp(20), BigMath.exp(20.0, prec)) + assert_in_epsilon(Math.exp(40), BigMath.exp(40.0, prec)) + assert_in_epsilon(Math.exp(-20), BigMath.exp(-20.0, prec)) + assert_in_epsilon(Math.exp(-40), BigMath.exp(-40.0, prec)) + end + + def test_BigMath_exp_with_fixnum + prec = 20 + assert_in_epsilon(Math.exp(20), BigMath.exp(20, prec)) + assert_in_epsilon(Math.exp(40), BigMath.exp(40, prec)) + assert_in_epsilon(Math.exp(-20), BigMath.exp(-20, prec)) + assert_in_epsilon(Math.exp(-40), BigMath.exp(-40, prec)) + end + + def test_BigMath_exp_with_rational + prec = 20 + assert_in_epsilon(Math.exp(20), BigMath.exp(Rational(40,2), prec)) + assert_in_epsilon(Math.exp(40), BigMath.exp(Rational(80,2), prec)) + assert_in_epsilon(Math.exp(-20), BigMath.exp(Rational(-40,2), prec)) + assert_in_epsilon(Math.exp(-40), BigMath.exp(Rational(-80,2), prec)) end def test_BigMath_exp_under_gc_stress