1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* numeric.c (rb_num_coerce_bin): add ID argument to specify

caller's method name.  [ruby-dev:33663]

* numeric.c (rb_num_coerce_cmp): ditto.

* numeric.c (rb_num_coerce_relop): ditto.

* ext/bigdecimal/bigdecimal.c (DoSomeOne): add function name argument.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15437 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2008-02-12 02:46:21 +00:00
parent 030a513a49
commit f3ac3dc79c
5 changed files with 76 additions and 52 deletions

View file

@ -1,3 +1,14 @@
Tue Feb 12 11:38:57 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* numeric.c (rb_num_coerce_bin): add ID argument to specify
caller's method name. [ruby-dev:33663]
* numeric.c (rb_num_coerce_cmp): ditto.
* numeric.c (rb_num_coerce_relop): ditto.
* ext/bigdecimal/bigdecimal.c (DoSomeOne): add function name argument.
Tue Feb 12 10:25:02 2008
* lib/rdoc/rdoc.rb: Wrap parse_files' read in version check for

View file

@ -1184,7 +1184,7 @@ rb_big_cmp(VALUE x, VALUE y)
return rb_dbl_cmp(rb_big2dbl(x), RFLOAT_VALUE(y));
default:
return rb_num_coerce_cmp(x, y);
return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
}
if (RBIGNUM_SIGN(x) > RBIGNUM_SIGN(y)) return INT2FIX(1);
@ -1419,7 +1419,7 @@ rb_big_plus(VALUE x, VALUE y)
return DOUBLE2NUM(rb_big2dbl(x) + RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '+');
}
}
@ -1444,7 +1444,7 @@ rb_big_minus(VALUE x, VALUE y)
return DOUBLE2NUM(rb_big2dbl(x) - RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '-');
}
}
@ -1508,7 +1508,7 @@ rb_big_mul0(VALUE x, VALUE y)
return DOUBLE2NUM(rb_big2dbl(x) * RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '*');
}
bms.x = x;
@ -1742,7 +1742,7 @@ rb_big_div(VALUE x, VALUE y)
return DOUBLE2NUM(rb_big2dbl(x) / RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '/');
}
bigdivmod(x, y, &z, 0);
@ -1772,7 +1772,7 @@ rb_big_modulo(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '%');
}
bigdivmod(x, y, 0, &z);
@ -1802,7 +1802,7 @@ rb_big_remainder(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("remainder"));
}
bigdivrem(x, y, 0, &z);
@ -1830,7 +1830,7 @@ rb_big_divmod(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("divmod"));
}
bigdivmod(x, y, &div, &mod);
@ -1931,7 +1931,7 @@ rb_big_quo(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("quo"));
}
return DOUBLE2NUM(dx / dy);
}
@ -2038,7 +2038,7 @@ rb_big_pow(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("**"));
}
return DOUBLE2NUM(pow(rb_big2dbl(x), d));
}

View file

@ -38,7 +38,7 @@ VALUE rb_cBigDecimal;
/*
* ================== Ruby Interface part ==========================
*/
#define DoSomeOne(x,y) rb_num_coerce_bin(x,y)
#define DoSomeOne(x,y,f) rb_num_coerce_bin(x,y,f)
#if 0
/* BigDecimal provides arbitrary-precision floating point decimal arithmetic.
@ -657,7 +657,7 @@ BigDecimal_add(VALUE self, VALUE r)
U_LONG mx;
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return DoSomeOne(self,r);
if(!b) return DoSomeOne(self,r,'+');
SAVE(b);
if(VpIsNaN(b)) return b->obj;
if(VpIsNaN(a)) return a->obj;
@ -696,7 +696,7 @@ BigDecimal_sub(VALUE self, VALUE r)
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return DoSomeOne(self,r);
if(!b) return DoSomeOne(self,r,'-');
SAVE(b);
if(VpIsNaN(b)) return b->obj;
@ -725,7 +725,20 @@ BigDecimalCmp(VALUE self, VALUE r,char op)
Real *a, *b;
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return rb_num_coerce_cmp(self,r);
if(!b) {
ID f;
switch(op)
{
case '*': return INT2FIX(e); /* any op */
case '=': f = rb_intern("=="); break;
case '!': f = rb_intern("!="); break;
case 'G': f = rb_intern(">="); break;
case 'L': f = rb_intern("<="); break;
case '>': case '<': f = (ID)op; break;
}
return rb_num_coerce_cmp(self,r,f);
}
SAVE(b);
e = VpComp(a, b);
if(e==999) return Qnil;
@ -862,7 +875,7 @@ BigDecimal_mult(VALUE self, VALUE r)
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return DoSomeOne(self,r);
if(!b) return DoSomeOne(self,r,'*');
SAVE(b);
mx = a->Prec + b->Prec;
@ -881,7 +894,7 @@ BigDecimal_divide(Real **c, Real **res, Real **div, VALUE self, VALUE r)
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return DoSomeOne(self,r);
if(!b) return DoSomeOne(self,r,'/');
SAVE(b);
*div = b;
mx =(a->MaxPrec + b->MaxPrec + 1) * VpBaseFig();
@ -942,7 +955,7 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod)
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return DoSomeOne(self,r);
if(!b) return DoSomeOne(self,r,rb_intern("divmod"));
SAVE(b);
if(VpIsNaN(a) || VpIsNaN(b)) goto NaN;
@ -1015,7 +1028,7 @@ BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return DoSomeOne(self,r);
if(!b) return DoSomeOne(self,r,rb_intern("remainder"));
SAVE(b);
mx =(a->MaxPrec + b->MaxPrec) *VpBaseFig();

View file

@ -364,9 +364,9 @@ VALUE rb_marshal_load(VALUE);
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE (*dumper)(VALUE), VALUE (*loader)(VALUE, VALUE));
/* numeric.c */
void rb_num_zerodiv(void);
VALUE rb_num_coerce_bin(VALUE, VALUE);
VALUE rb_num_coerce_cmp(VALUE, VALUE);
VALUE rb_num_coerce_relop(VALUE, VALUE);
VALUE rb_num_coerce_bin(VALUE, VALUE, ID);
VALUE rb_num_coerce_cmp(VALUE, VALUE, ID);
VALUE rb_num_coerce_relop(VALUE, VALUE, ID);
VALUE rb_float_new(double);
VALUE rb_num2fix(VALUE);
VALUE rb_fix2str(VALUE, int);

View file

@ -162,27 +162,27 @@ do_coerce(VALUE *x, VALUE *y, int err)
}
VALUE
rb_num_coerce_bin(VALUE x, VALUE y)
rb_num_coerce_bin(VALUE x, VALUE y, ID func)
{
do_coerce(&x, &y, Qtrue);
return rb_funcall(x, rb_frame_this_func(), 1, y);
return rb_funcall(x, func, 1, y);
}
VALUE
rb_num_coerce_cmp(VALUE x, VALUE y)
rb_num_coerce_cmp(VALUE x, VALUE y, ID func)
{
if (do_coerce(&x, &y, Qfalse))
return rb_funcall(x, rb_frame_this_func(), 1, y);
return rb_funcall(x, func, 1, y);
return Qnil;
}
VALUE
rb_num_coerce_relop(VALUE x, VALUE y)
rb_num_coerce_relop(VALUE x, VALUE y, ID func)
{
VALUE c, x0 = x, y0 = y;
if (!do_coerce(&x, &y, Qfalse) ||
NIL_P(c = rb_funcall(x, rb_frame_this_func(), 1, y))) {
NIL_P(c = rb_funcall(x, func, 1, y))) {
rb_cmperr(x0, y0);
return Qnil; /* not reached */
}
@ -567,7 +567,7 @@ flo_plus(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '+');
}
}
@ -590,7 +590,7 @@ flo_minus(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '-');
}
}
@ -613,7 +613,7 @@ flo_mul(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '*');
}
}
@ -641,7 +641,7 @@ flo_div(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM(RFLOAT_VALUE(x) / RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '/');
}
}
@ -701,7 +701,7 @@ flo_mod(VALUE x, VALUE y)
fy = RFLOAT_VALUE(y);
break;
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '%');
}
flodivmod(RFLOAT_VALUE(x), fy, 0, &mod);
return DOUBLE2NUM(mod);
@ -731,7 +731,7 @@ flo_divmod(VALUE x, VALUE y)
fy = RFLOAT_VALUE(y);
break;
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("divmod"));
}
flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
if (FIXABLE(div)) {
@ -767,7 +767,7 @@ flo_pow(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM(pow(RFLOAT_VALUE(x), RFLOAT_VALUE(y)));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("**"));
}
}
@ -906,7 +906,7 @@ flo_cmp(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_cmp(x, y);
return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
}
return rb_dbl_cmp(a, b);
}
@ -939,7 +939,7 @@ flo_gt(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_relop(x, y);
return rb_num_coerce_relop(x, y, '>');
}
if (isnan(a)) return Qfalse;
return (a > b)?Qtrue:Qfalse;
@ -974,7 +974,7 @@ flo_ge(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_relop(x, y);
return rb_num_coerce_relop(x, y, rb_intern(">="));
}
if (isnan(a)) return Qfalse;
return (a >= b)?Qtrue:Qfalse;
@ -1008,7 +1008,7 @@ flo_lt(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_relop(x, y);
return rb_num_coerce_relop(x, y, '<');
}
if (isnan(a)) return Qfalse;
return (a < b)?Qtrue:Qfalse;
@ -1043,7 +1043,7 @@ flo_le(VALUE x, VALUE y)
break;
default:
return rb_num_coerce_relop(x, y);
return rb_num_coerce_relop(x, y, rb_intern("<="));
}
if (isnan(a)) return Qfalse;
return (a <= b)?Qtrue:Qfalse;
@ -2047,7 +2047,7 @@ fix_plus(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '+');
}
}
@ -2081,7 +2081,7 @@ fix_minus(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM((double)FIX2LONG(x) - RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '-');
}
}
@ -2140,7 +2140,7 @@ fix_mul(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '*');
}
}
@ -2196,7 +2196,7 @@ fix_quo(VALUE x, VALUE y)
case T_FLOAT:
return DOUBLE2NUM((double)FIX2LONG(x) / RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("quo"));
}
}
@ -2222,7 +2222,7 @@ fix_divide(VALUE x, VALUE y, int flo)
return LONG2NUM(div);
}
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, flo ? '/' : rb_intern("div"));
}
}
@ -2284,7 +2284,7 @@ fix_mod(VALUE x, VALUE y)
return DOUBLE2NUM(mod);
}
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, '%');
}
}
@ -2319,7 +2319,7 @@ fix_divmod(VALUE x, VALUE y)
return rb_assoc_new(a, b);
}
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("divmod"));
}
}
@ -2416,7 +2416,7 @@ fix_pow(VALUE x, VALUE y)
if (a == 1) return DOUBLE2NUM(1.0);
return DOUBLE2NUM(pow((double)a, RFLOAT_VALUE(y)));
default:
return rb_num_coerce_bin(x, y);
return rb_num_coerce_bin(x, y, rb_intern("**"));
}
}
@ -2469,7 +2469,7 @@ fix_cmp(VALUE x, VALUE y)
case T_FLOAT:
return rb_dbl_cmp((double)FIX2LONG(x), RFLOAT_VALUE(y));
default:
return rb_num_coerce_cmp(x, y);
return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
}
}
@ -2494,7 +2494,7 @@ fix_gt(VALUE x, VALUE y)
case T_FLOAT:
return (double)FIX2LONG(x) > RFLOAT_VALUE(y) ? Qtrue : Qfalse;
default:
return rb_num_coerce_relop(x, y);
return rb_num_coerce_relop(x, y, '>');
}
}
@ -2519,7 +2519,7 @@ fix_ge(VALUE x, VALUE y)
case T_FLOAT:
return (double)FIX2LONG(x) >= RFLOAT_VALUE(y) ? Qtrue : Qfalse;
default:
return rb_num_coerce_relop(x, y);
return rb_num_coerce_relop(x, y, rb_intern(">="));
}
}
@ -2544,7 +2544,7 @@ fix_lt(VALUE x, VALUE y)
case T_FLOAT:
return (double)FIX2LONG(x) < RFLOAT_VALUE(y) ? Qtrue : Qfalse;
default:
return rb_num_coerce_relop(x, y);
return rb_num_coerce_relop(x, y, '<');
}
}
@ -2569,7 +2569,7 @@ fix_le(VALUE x, VALUE y)
case T_FLOAT:
return (double)FIX2LONG(x) <= RFLOAT_VALUE(y) ? Qtrue : Qfalse;
default:
return rb_num_coerce_relop(x, y);
return rb_num_coerce_relop(x, y, rb_intern("<="));
}
}