ruby-changes:16541
From: mrkn <ko1@a...>
Date: Sat, 3 Jul 2010 16:47:19 +0900 (JST)
Subject: [ruby-changes:16541] Ruby:r28533 (trunk): * ext/bigdecimal/bigdecimal.c (Init_bigdecimal): add two new constants BigDecimal::INFINITY and BigDecimal::NAN.
mrkn 2010-07-03 16:47:00 +0900 (Sat, 03 Jul 2010) New Revision: 28533 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=28533 Log: * ext/bigdecimal/bigdecimal.c (Init_bigdecimal): add two new constants BigDecimal::INFINITY and BigDecimal::NAN. * ext/bigdecimal/lib/bigdecimal/math.rb (BigMath.exp): modify the behaviors for infinity arguments as same as Math.exp. Modified files: trunk/ChangeLog trunk/ext/bigdecimal/bigdecimal.c trunk/ext/bigdecimal/lib/bigdecimal/math.rb trunk/test/bigdecimal/test_bigdecimal.rb trunk/test/bigdecimal/test_bigmath.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 28532) +++ ChangeLog (revision 28533) @@ -1,5 +1,13 @@ -Sat Jul 3 09:47:26 2010 Aaron Patterson <aaron@t...> +Sat Jul 3 16:14:10 2010 Kenta Murata <mrkn@m...> + * ext/bigdecimal/bigdecimal.c (Init_bigdecimal): add two new constants + BigDecimal::INFINITY and BigDecimal::NAN. + + * ext/bigdecimal/lib/bigdecimal/math.rb (BigMath.exp): modify the + behaviors for infinity arguments as same as Math.exp. + +Sat Jul 3 09:47:26 2010 Aaron Patterson <aaron@t...> + * ext/psych/lib/psych/visitors/to_ruby.rb(visit_Psych_Nodes_Scalar): teaching Psych to deserialize DateTime objects. [Bug #1390] @@ -11,7 +19,7 @@ * test/psych/test_date_time.rb: tests for dumping DateTime objects. -Sat Jul 3 09:13:55 2010 Aaron Patterson <aaron@t...> +Sat Jul 3 09:13:55 2010 Aaron Patterson <aaron@t...> * ext/psych/lib/psych/visitors/yaml_tree.rb (visit_Time): use Time#nsec to accurately serialize time objects. [ruby-core:29233] Index: ext/bigdecimal/bigdecimal.c =================================================================== --- ext/bigdecimal/bigdecimal.c (revision 28532) +++ ext/bigdecimal/bigdecimal.c (revision 28533) @@ -1902,6 +1902,8 @@ void Init_bigdecimal(void) { + VALUE arg; + /* Initialize VP routines */ VpInit((U_LONG)0); @@ -2023,6 +2025,12 @@ /* -3: Indicates that a value is negative and infinite. See BigDecimal.sign. */ rb_define_const(rb_cBigDecimal, "SIGN_NEGATIVE_INFINITE",INT2FIX(VP_SIGN_NEGATIVE_INFINITE)); + arg = rb_str_new2("+Infinity"); + rb_define_const(rb_cBigDecimal, "INFINITY", BigDecimal_global_new(1, &arg, rb_cBigDecimal)); + arg = rb_str_new2("NaN"); + rb_define_const(rb_cBigDecimal, "NAN", BigDecimal_global_new(1, &arg, rb_cBigDecimal)); + + /* instance methods */ rb_define_method(rb_cBigDecimal, "precs", BigDecimal_prec, 0); Index: ext/bigdecimal/lib/bigdecimal/math.rb =================================================================== --- ext/bigdecimal/lib/bigdecimal/math.rb (revision 28532) +++ ext/bigdecimal/lib/bigdecimal/math.rb (revision 28533) @@ -155,7 +155,15 @@ # -> "0.271828182845904523536028752390026306410273E1" def exp(x, prec) raise ArgumentError, "Zero or negative precision for exp" if prec <= 0 - return BigDecimal("NaN") if x.infinite? || x.nan? + if x.infinite? + if x < 0 + return BigDecimal("0", prec) + else + return BigDecimal("+Infinity", prec) + end + elsif x.nan? + return BigDecimal("NaN", prec) + end n = prec + BigDecimal.double_fig one = BigDecimal("1") x = -x if neg = x < 0 Index: test/bigdecimal/test_bigdecimal.rb =================================================================== --- test/bigdecimal/test_bigdecimal.rb (revision 28532) +++ test/bigdecimal/test_bigdecimal.rb (revision 28533) @@ -709,4 +709,12 @@ ensure GC.stress = stress end + + def test_INFINITY + assert(BigDecimal::INFINITY.infinite?, "BigDecimal::INFINITY is not a infinity") + end + + def test_NAN + assert(BigDecimal::NAN.nan?, "BigDecimal::NAN is not NaN") + end end Index: test/bigdecimal/test_bigmath.rb =================================================================== --- test/bigdecimal/test_bigmath.rb (revision 28532) +++ test/bigdecimal/test_bigmath.rb (revision 28533) @@ -67,6 +67,14 @@ 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)) + begin + old_mode = BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY) + BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false) + assert(exp(BigDecimal::INFINITY, N).infinite?, "exp(INFINITY) is not an infinity") + ensure + #BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, old_mode) + end + assert_equal(0.0, exp(-BigDecimal::INFINITY, N)) end def test_log -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/