[前][次][番号順一覧][スレッド一覧]

ruby-changes:20006

From: mrkn <ko1@a...>
Date: Mon, 13 Jun 2011 18:52:00 +0900 (JST)
Subject: [ruby-changes:20006] mrkn:r32052 (trunk): * ext/bigdecimal/bigdecimal.c (BigDecimalCmp): use GetVpValueWithPrec

mrkn	2011-06-13 18:51:48 +0900 (Mon, 13 Jun 2011)

  New Revision: 32052

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=32052

  Log:
    * ext/bigdecimal/bigdecimal.c (BigDecimalCmp): use GetVpValueWithPrec
      for Float and Rational arguments.
    * test/bigdecimal/test_bigdecimal.rb (test_new, test_cmp, test_power):
      add and modify tests for the above change.
    * ext/bigdecimal/bigdecimal.c (BigDecimalCmp): modify coding style to
      match ruby's standard.

  Modified files:
    trunk/ChangeLog
    trunk/ext/bigdecimal/bigdecimal.c
    trunk/test/bigdecimal/test_bigdecimal.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 32051)
+++ ChangeLog	(revision 32052)
@@ -1,3 +1,14 @@
+Mon Jun 13 18:49:00 2011  Kenta Murata  <mrkn@m...>
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimalCmp): use GetVpValueWithPrec
+	  for Float and Rational arguments.
+
+	* test/bigdecimal/test_bigdecimal.rb (test_new, test_cmp, test_power):
+	  add and modify tests for the above change.
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimalCmp): modify coding style to
+	  match ruby's standard.
+
 Mon Jun 13 18:33:04 2011  Tanaka Akira  <akr@f...>
 
 	* lib/securerandom.rb (SecureRandom.random_bytes): modify PRNG state
Index: ext/bigdecimal/bigdecimal.c
===================================================================
--- ext/bigdecimal/bigdecimal.c	(revision 32051)
+++ ext/bigdecimal/bigdecimal.c	(revision 32052)
@@ -794,34 +794,90 @@
 {
     ENTER(5);
     SIGNED_VALUE e;
-    Real *a, *b;
+    Real *a, *b=0;
     GUARD_OBJ(a,GetVpValue(self,1));
-    b = GetVpValue(r,0);
-    if(!b) {
+    switch (TYPE(r)) {
+    case T_DATA:
+	if (!rb_typeddata_is_kind_of(r, &BigDecimal_data_type)) break;
+	/* fall through */
+    case T_FIXNUM:
+	/* fall through */
+    case T_BIGNUM:
+	GUARD_OBJ(b, GetVpValue(r,0));
+	break;
+
+    case T_FLOAT:
+	GUARD_OBJ(b, GetVpValueWithPrec(r, DBL_DIG+1, 0));
+	break;
+
+    case T_RATIONAL:
+	GUARD_OBJ(b, GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 0));
+	break;
+
+    default:
+	break;
+    }
+    if (b == NULL) {
 	ID f = 0;
 
-	switch(op)
-	{
-	  case '*': return rb_num_coerce_cmp(self,r,rb_intern("<=>"));
-	  case '=': return RTEST(rb_num_coerce_cmp(self,r,rb_intern("=="))) ? Qtrue : Qfalse;
-	  case 'G': f = rb_intern(">="); break;
-	  case 'L': f = rb_intern("<="); break;
-	  case '>': case '<': f = (ID)op; break;
+	switch (op) {
+	case '*':
+	    return rb_num_coerce_cmp(self, r, rb_intern("<=>"));
+
+	case '=':
+	    return RTEST(rb_num_coerce_cmp(self, r, rb_intern("=="))) ? Qtrue : Qfalse;
+
+	case 'G':
+	    f = rb_intern(">=");
+	    break;
+
+	case 'L':
+	    f = rb_intern("<=");
+	    break;
+
+	case '>':
+	    /* fall through */
+	case '<':
+	    f = (ID)op;
+	    break;
+
+	default:
+	    break;
 	}
-	return rb_num_coerce_relop(self,r,f);
+	return rb_num_coerce_relop(self, r, f);
     }
     SAVE(b);
     e = VpComp(a, b);
-    if(e==999) return (op == '*') ? Qnil : Qfalse;
-    switch(op)
-    {
-    case '*': return   INT2FIX(e); /* any op */
-    case '=': if(e==0) return Qtrue ; return Qfalse;
-    case 'G': if(e>=0) return Qtrue ; return Qfalse;
-    case '>': if(e> 0) return Qtrue ; return Qfalse;
-    case 'L': if(e<=0) return Qtrue ; return Qfalse;
-    case '<': if(e< 0) return Qtrue ; return Qfalse;
+    if (e == 999)
+	return (op == '*') ? Qnil : Qfalse;
+    switch (op) {
+    case '*':
+	return   INT2FIX(e); /* any op */
+
+    case '=':
+	if(e==0) return Qtrue;
+	return Qfalse;
+
+    case 'G':
+	if(e>=0) return Qtrue;
+	return Qfalse;
+
+    case '>':
+	if(e> 0) return Qtrue;
+	return Qfalse;
+
+    case 'L':
+	if(e<=0) return Qtrue;
+	return Qfalse;
+
+    case '<':
+	if(e< 0) return Qtrue;
+	return Qfalse;
+
+    default:
+	break;
     }
+
     rb_bug("Undefined operation in BigDecimalCmp()");
 }
 
Index: test/bigdecimal/test_bigdecimal.rb
===================================================================
--- test/bigdecimal/test_bigdecimal.rb	(revision 32051)
+++ test/bigdecimal/test_bigdecimal.rb	(revision 32052)
@@ -56,6 +56,7 @@
     assert_equal(1, BigDecimal.new(" 1 "))
     assert_equal(111, BigDecimal.new("1_1_1_"))
     assert_equal(0, BigDecimal.new("_1_1_1"))
+    assert_equal(10**(-1), BigDecimal.new("1E-1"), '#4825')
 
     BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
     BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
@@ -351,6 +352,9 @@
     inf = BigDecimal.new("Infinity")
     assert_operator(inf, :>, 1)
     assert_operator(1, :<, inf)
+
+    assert_operator(BigDecimal("1E-1"), :==, 10**(-1), '#4825')
+    assert_equal(0, BigDecimal("1E-1") <=> 10**(-1), '#4825')
   end
 
   def test_cmp_nan
@@ -763,7 +767,8 @@
   def test_power
     x = BigDecimal.new("3")
     assert_equal(81, x ** 4)
-    assert_equal(1.0/81, x ** -4)
+    assert_equal(1.0/81, (x ** -4).to_f)
+    assert_equal(1.quo(81), x ** -4)
     assert_equal(1, x ** 0)
     assert_raise(TypeError) { x ** x }
     assert_equal(0, BigDecimal.new("0") ** 4)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]