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

ruby-changes:64808

From: Kenta <ko1@a...>
Date: Sat, 9 Jan 2021 22:07:08 +0900 (JST)
Subject: [ruby-changes:64808] 3d9c95996d (master): [ruby/bigdecimal] Stop using GetVpValueWithPrec in rb_rational_convert_to_BigDecimal

https://git.ruby-lang.org/ruby.git/commit/?id=3d9c95996d

From 3d9c95996dfc8e1d6bc263509273f13caaf06f77 Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Sat, 9 Jan 2021 15:28:08 +0900
Subject: [ruby/bigdecimal] Stop using GetVpValueWithPrec in
 rb_rational_convert_to_BigDecimal

https://github.com/ruby/bigdecimal/commit/b4f470da61
https://github.com/ruby/bigdecimal/commit/44a78df866

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index ef17882..460456d 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -200,17 +200,16 @@ cannot_be_coerced_into_BigDecimal(VALUE exc_class, VALUE v) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L200
 
 static inline VALUE BigDecimal_div2(VALUE, VALUE, VALUE);
 static VALUE rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
+static VALUE rb_rational_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
 
 static Real*
 GetVpValueWithPrec(VALUE v, long prec, int must)
 {
     ENTER(1);
     Real *pv;
-    VALUE num, bg;
+    VALUE bg;
     char szD[128];
-    VALUE orig = Qundef;
 
-again:
     switch(TYPE(v)) {
       case T_FLOAT: {
         VALUE obj = rb_float_convert_to_BigDecimal(v, prec, must);
@@ -218,20 +217,11 @@ again: https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L217
         return pv;
       }
 
-      case T_RATIONAL:
-	if (prec < 0) goto unable_to_coerce_without_prec;
-
-	if (orig == Qundef ? (orig = v, 1) : orig != v) {
-	    num = rb_rational_num(v);
-	    pv = GetVpValueWithPrec(num, -1, must);
-	    if (pv == NULL) goto SomeOneMayDoIt;
-
-            v = BigDecimal_div2(VpCheckGetValue(pv), rb_rational_den(v), LONG2NUM(prec));
-	    goto again;
-	}
-
-	v = orig;
-	goto SomeOneMayDoIt;
+      case T_RATIONAL: {
+        VALUE obj = rb_rational_convert_to_BigDecimal(v, prec, must);
+        TypedData_Get_Struct(obj, Real, &BigDecimal_data_type, pv);
+        return pv;
+      }
 
       case T_DATA:
 	if (is_kind_of_BigDecimal(v)) {
@@ -268,14 +258,6 @@ SomeOneMayDoIt: https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L258
 	cannot_be_coerced_into_BigDecimal(rb_eTypeError, v);
     }
     return NULL; /* NULL means to coerce */
-
-unable_to_coerce_without_prec:
-    if (must) {
-	rb_raise(rb_eArgError,
-		 "%"PRIsVALUE" can't be coerced into BigDecimal without a precision",
-		 RB_OBJ_CLASSNAME(v));
-    }
-    return NULL;
 }
 
 static Real*
@@ -2794,8 +2776,6 @@ rb_inum_convert_to_BigDecimal(VALUE val, RB_UNUSED_VAR(size_t digs), int raise_e https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2776
     }
 }
 
-static VALUE rb_rational_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
-
 static VALUE
 rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
 {
@@ -2849,8 +2829,10 @@ rb_rational_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2829
                  "can't omit precision for a %"PRIsVALUE".",
                  CLASS_OF(val));
     }
-    Real *vp = GetVpValueWithPrec(val, digs, 1);
-    return check_exception(vp->obj);
+
+    VALUE num = rb_inum_convert_to_BigDecimal(rb_rational_num(val), 0, raise_exception);
+    VALUE d = BigDecimal_div2(num, rb_rational_den(val), SIZET2NUM(digs));
+    return d;
 }
 
 static VALUE
-- 
cgit v0.10.2


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

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