summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c650
1 files changed, 324 insertions, 326 deletions
diff --git a/numeric.c b/numeric.c
index f9704dc9be..c88558bc1e 100644
--- a/numeric.c
+++ b/numeric.c
@@ -17,21 +17,21 @@
static ID coerce;
static ID to_i;
-VALUE cNumeric;
-VALUE cFloat;
-VALUE cInteger;
-VALUE cFixnum;
+VALUE rb_cNumeric;
+VALUE rb_cFloat;
+VALUE rb_cInteger;
+VALUE rb_cFixnum;
-VALUE eZeroDiv;
+VALUE rb_eZeroDiv;
ID rb_frame_last_func();
-VALUE float_new();
-double big2dbl();
+VALUE rb_float_new();
+double rb_big2dbl();
void
-num_zerodiv()
+rb_num_zerodiv()
{
- Raise(eZeroDiv, "divided by 0");
+ rb_raise(rb_eZeroDiv, "divided by 0");
}
static VALUE
@@ -39,8 +39,8 @@ num_coerce(x, y)
VALUE x, y;
{
if (CLASS_OF(x) == CLASS_OF(y))
- return assoc_new(x, y);
- return assoc_new(rb_Float(x), rb_Float(y));
+ return rb_assoc_new(x, y);
+ return rb_assoc_new(rb_Float(x), rb_Float(y));
}
static VALUE
@@ -54,11 +54,11 @@ static VALUE
coerce_rescue(x)
VALUE *x;
{
- TypeError("%s can't be coerced into %s",
- rb_special_const_p(x[1])?
- STR2CSTR(rb_inspect(x[1])):
- rb_class2name(CLASS_OF(x[1])),
- rb_class2name(CLASS_OF(x[0])));
+ rb_raise(rb_eTypeError, "%s can't be coerced into %s",
+ rb_special_const_p(x[1])?
+ STR2CSTR(rb_inspect(x[1])):
+ rb_class2name(CLASS_OF(x[1])),
+ rb_class2name(CLASS_OF(x[0])));
}
static void
@@ -71,7 +71,7 @@ do_coerce(x, y)
a[0] = *x; a[1] = *y;
ary = rb_rescue(coerce_body, (VALUE)a, coerce_rescue, (VALUE)a);
if (TYPE(ary) != T_ARRAY || RARRAY(ary)->len != 2) {
- TypeError("coerce must return [x, y]");
+ rb_raise(rb_eTypeError, "coerce must return [x, y]");
}
*x = RARRAY(ary)->ptr[0];
@@ -79,7 +79,7 @@ do_coerce(x, y)
}
VALUE
-num_coerce_bin(x, y)
+rb_num_coerce_bin(x, y)
VALUE x, y;
{
do_coerce(&x, &y);
@@ -116,18 +116,18 @@ num_divmod(x, y)
double d = floor(RFLOAT(div)->value);
if (RFLOAT(div)->value > d) {
- div = float_new(d);
+ div = rb_float_new(d);
}
}
mod = rb_funcall(x, '%', 1, y);
- return assoc_new(div, mod);
+ return rb_assoc_new(div, mod);
}
static VALUE
num_int_p(num)
VALUE num;
{
- return FALSE;
+ return Qfalse;
}
static VALUE
@@ -138,9 +138,9 @@ num_chr(num)
long i = NUM2LONG(num);
if (i < 0 || 0xff < i)
- Fail("%d out of char range", i);
+ rb_raise(rb_eTypeError, "%d out of char range", i);
c = i;
- return str_new(&c, 1);
+ return rb_str_new(&c, 1);
}
static VALUE
@@ -158,9 +158,9 @@ num_zero_p(num)
VALUE num;
{
if (RTEST(rb_equal(num, INT2FIX(0)))) {
- return TRUE;
+ return Qtrue;
}
- return FALSE;
+ return Qfalse;
}
static VALUE
@@ -168,17 +168,17 @@ num_nonzero_p(num)
VALUE num;
{
if (RTEST(rb_funcall(num, rb_intern("zero?"), 0, 0))) {
- return FALSE;
+ return Qfalse;
}
return num;
}
VALUE
-float_new(d)
+rb_float_new(d)
double d;
{
NEWOBJ(flt, struct RFloat);
- OBJSETUP(flt, cFloat, T_FLOAT);
+ OBJSETUP(flt, rb_cFloat, T_FLOAT);
flt->value = d;
return (VALUE)flt;
@@ -204,21 +204,21 @@ flo_to_s(flt)
}
}
- return str_new2(buf);
+ return rb_str_new2(buf);
}
static VALUE
flo_coerce(x, y)
VALUE x, y;
{
- return assoc_new(rb_Float(y), x);
+ return rb_assoc_new(rb_Float(y), x);
}
static VALUE
flo_uminus(flt)
VALUE flt;
{
- return float_new(-RFLOAT(flt)->value);
+ return rb_float_new(-RFLOAT(flt)->value);
}
static VALUE
@@ -227,13 +227,13 @@ flo_plus(x, y)
{
switch (TYPE(y)) {
case T_FIXNUM:
- return float_new(RFLOAT(x)->value + (double)FIX2LONG(y));
+ return rb_float_new(RFLOAT(x)->value + (double)FIX2LONG(y));
case T_BIGNUM:
- return float_new(RFLOAT(x)->value + big2dbl(y));
+ return rb_float_new(RFLOAT(x)->value + rb_big2dbl(y));
case T_FLOAT:
- return float_new(RFLOAT(x)->value + RFLOAT(y)->value);
+ return rb_float_new(RFLOAT(x)->value + RFLOAT(y)->value);
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -243,13 +243,13 @@ flo_minus(x, y)
{
switch (TYPE(y)) {
case T_FIXNUM:
- return float_new(RFLOAT(x)->value - (double)FIX2LONG(y));
+ return rb_float_new(RFLOAT(x)->value - (double)FIX2LONG(y));
case T_BIGNUM:
- return float_new(RFLOAT(x)->value - big2dbl(y));
+ return rb_float_new(RFLOAT(x)->value - rb_big2dbl(y));
case T_FLOAT:
- return float_new(RFLOAT(x)->value - RFLOAT(y)->value);
+ return rb_float_new(RFLOAT(x)->value - RFLOAT(y)->value);
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -259,13 +259,13 @@ flo_mul(x, y)
{
switch (TYPE(y)) {
case T_FIXNUM:
- return float_new(RFLOAT(x)->value * (double)FIX2LONG(y));
+ return rb_float_new(RFLOAT(x)->value * (double)FIX2LONG(y));
case T_BIGNUM:
- return float_new(RFLOAT(x)->value * big2dbl(y));
+ return rb_float_new(RFLOAT(x)->value * rb_big2dbl(y));
case T_FLOAT:
- return float_new(RFLOAT(x)->value * RFLOAT(y)->value);
+ return rb_float_new(RFLOAT(x)->value * RFLOAT(y)->value);
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -279,17 +279,17 @@ flo_div(x, y)
switch (TYPE(y)) {
case T_FIXNUM:
f_y = FIX2LONG(y);
- if (f_y == 0) num_zerodiv();
- return float_new(RFLOAT(x)->value / (double)f_y);
+ if (f_y == 0) rb_num_zerodiv();
+ return rb_float_new(RFLOAT(x)->value / (double)f_y);
case T_BIGNUM:
- d = big2dbl(y);
- if (d == 0.0) num_zerodiv();
- return float_new(RFLOAT(x)->value / d);
+ d = rb_big2dbl(y);
+ if (d == 0.0) rb_num_zerodiv();
+ return rb_float_new(RFLOAT(x)->value / d);
case T_FLOAT:
- if (RFLOAT(y)->value == 0.0) num_zerodiv();
- return float_new(RFLOAT(x)->value / RFLOAT(y)->value);
+ if (RFLOAT(y)->value == 0.0) rb_num_zerodiv();
+ return rb_float_new(RFLOAT(x)->value / RFLOAT(y)->value);
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -305,13 +305,13 @@ flo_modulo(x, y, modulo)
value = (double)FIX2LONG(y);
break;
case T_BIGNUM:
- value = big2dbl(y);
+ value = rb_big2dbl(y);
break;
case T_FLOAT:
value = RFLOAT(y)->value;
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
#ifdef HAVE_FMOD
@@ -329,7 +329,7 @@ flo_modulo(x, y, modulo)
(RFLOAT(x)->value < 0.0) != (result < 0.0) && result != 0.0) {
result += value;
}
- return float_new(result);
+ return rb_float_new(result);
}
static VALUE
@@ -346,19 +346,19 @@ flo_remainder(x, y)
return flo_modulo(x,y,0);
}
-VALUE
+static VALUE
flo_pow(x, y)
VALUE x, y;
{
switch (TYPE(y)) {
case T_FIXNUM:
- return float_new(pow(RFLOAT(x)->value, (double)FIX2LONG(y)));
+ return rb_float_new(pow(RFLOAT(x)->value, (double)FIX2LONG(y)));
case T_BIGNUM:
- return float_new(pow(RFLOAT(x)->value, big2dbl(y)));
+ return rb_float_new(pow(RFLOAT(x)->value, rb_big2dbl(y)));
case T_FLOAT:
- return float_new(pow(RFLOAT(x)->value, RFLOAT(y)->value));
+ return rb_float_new(pow(RFLOAT(x)->value, RFLOAT(y)->value));
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -366,7 +366,7 @@ static VALUE
num_eql(x, y)
VALUE x, y;
{
- if (TYPE(x) != TYPE(y)) return FALSE;
+ if (TYPE(x) != TYPE(y)) return Qfalse;
return rb_equal(x, y);
}
@@ -385,12 +385,12 @@ flo_eq(x, y)
{
switch (TYPE(y)) {
case T_FIXNUM:
- if (RFLOAT(x)->value == FIX2LONG(y)) return TRUE;
- return FALSE;
+ if (RFLOAT(x)->value == FIX2LONG(y)) return Qtrue;
+ return Qfalse;
case T_BIGNUM:
- return (RFLOAT(x)->value == big2dbl(y))?TRUE:FALSE;
+ return (RFLOAT(x)->value == rb_big2dbl(y))?Qtrue:Qfalse;
case T_FLOAT:
- return (RFLOAT(x)->value == RFLOAT(y)->value)?TRUE:FALSE;
+ return (RFLOAT(x)->value == RFLOAT(y)->value)?Qtrue:Qfalse;
default:
return num_equal(x, y);
}
@@ -426,7 +426,7 @@ flo_cmp(x, y)
break;
case T_BIGNUM:
- b = big2dbl(y);
+ b = rb_big2dbl(y);
break;
case T_FLOAT:
@@ -434,7 +434,7 @@ flo_cmp(x, y)
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
if (a == b) return INT2FIX(0);
if (a > b) return INT2FIX(1);
@@ -454,7 +454,7 @@ flo_gt(x, y)
break;
case T_BIGNUM:
- b = big2dbl(y);
+ b = rb_big2dbl(y);
break;
case T_FLOAT:
@@ -462,9 +462,9 @@ flo_gt(x, y)
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
- return (a > b)?TRUE:FALSE;
+ return (a > b)?Qtrue:Qfalse;
}
static VALUE
@@ -480,7 +480,7 @@ flo_ge(x, y)
break;
case T_BIGNUM:
- b = big2dbl(y);
+ b = rb_big2dbl(y);
break;
case T_FLOAT:
@@ -488,9 +488,9 @@ flo_ge(x, y)
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
- return (a >= b)?TRUE:FALSE;
+ return (a >= b)?Qtrue:Qfalse;
}
static VALUE
@@ -506,7 +506,7 @@ flo_lt(x, y)
break;
case T_BIGNUM:
- b = big2dbl(y);
+ b = rb_big2dbl(y);
break;
case T_FLOAT:
@@ -514,9 +514,9 @@ flo_lt(x, y)
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
- return (a < b)?TRUE:FALSE;
+ return (a < b)?Qtrue:Qfalse;
}
static VALUE
@@ -532,7 +532,7 @@ flo_le(x, y)
break;
case T_BIGNUM:
- b = big2dbl(y);
+ b = rb_big2dbl(y);
break;
case T_FLOAT:
@@ -540,9 +540,9 @@ flo_le(x, y)
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
- return (a <= b)?TRUE:FALSE;
+ return (a <= b)?Qtrue:Qfalse;
}
static VALUE
@@ -550,9 +550,9 @@ flo_eql(x, y)
VALUE x, y;
{
if (TYPE(y) == T_FLOAT) {
- if (RFLOAT(x)->value == RFLOAT(y)->value) return TRUE;
+ if (RFLOAT(x)->value == RFLOAT(y)->value) return Qtrue;
}
- return FALSE;
+ return Qfalse;
}
static VALUE
@@ -563,7 +563,7 @@ flo_to_i(num)
long val;
if (!FIXABLE(f)) {
- return dbl2big(f);
+ return rb_dbl2big(f);
}
val = f;
return INT2FIX(val);
@@ -577,7 +577,7 @@ flo_floor(num)
long val;
if (!FIXABLE(f)) {
- return dbl2big(f);
+ return rb_dbl2big(f);
}
val = f;
return INT2FIX(val);
@@ -591,7 +591,7 @@ flo_ceil(num)
long val;
if (!FIXABLE(f)) {
- return dbl2big(f);
+ return rb_dbl2big(f);
}
val = f;
return INT2FIX(val);
@@ -608,7 +608,7 @@ flo_round(num)
if (f < 0.0) f = ceil(f-0.5);
if (!FIXABLE(f)) {
- return dbl2big(f);
+ return rb_dbl2big(f);
}
val = f;
return INT2FIX(val);
@@ -626,7 +626,7 @@ flo_abs(flt)
VALUE flt;
{
double val = fabs(RFLOAT(flt)->value);
- return float_new(val);
+ return rb_float_new(val);
}
static VALUE
@@ -634,9 +634,9 @@ flo_zero_p(num)
VALUE num;
{
if (RFLOAT(num)->value == 0.0) {
- return TRUE;
+ return Qtrue;
}
- return FALSE;
+ return Qfalse;
}
static VALUE
@@ -650,91 +650,91 @@ static VALUE
fail_to_integer(val)
VALUE val;
{
- TypeError("failed to convert %s into Integer",
- rb_class2name(CLASS_OF(val)));
+ rb_raise(rb_eTypeError, "failed to convert %s into Integer",
+ rb_class2name(CLASS_OF(val)));
}
long
-num2long(val)
+rb_num2long(val)
VALUE val;
{
if (NIL_P(val)) {
- TypeError("no implicit conversion from nil");
+ rb_raise(rb_eTypeError, "no implicit conversion from nil");
}
- switch (TYPE(val)) {
- case T_FIXNUM:
- return FIX2LONG(val);
+ if (FIXNUM_P(val)) return FIX2LONG(val);
+ switch (TYPE(val)) {
case T_FLOAT:
if (RFLOAT(val)->value <= (double)LONG_MAX
&& RFLOAT(val)->value >= (double)LONG_MIN) {
return (long)(RFLOAT(val)->value);
}
else {
- TypeError("float %g out of rang of integer", RFLOAT(val)->value);
+ rb_raise(rb_eTypeError, "float %g out of rang of integer",
+ RFLOAT(val)->value);
}
case T_BIGNUM:
- return big2long(val);
+ return rb_big2long(val);
case T_STRING:
- TypeError("no implicit conversion from string");
+ rb_raise(rb_eTypeError, "no implicit conversion from string");
return Qnil; /* not reached */
default:
val = rb_rescue(to_integer, val, fail_to_integer, val);
- if (!obj_is_kind_of(val, cInteger)) {
- TypeError("`to_i' need to return integer");
+ if (!rb_obj_is_kind_of(val, rb_cInteger)) {
+ rb_raise(rb_eTypeError, "`to_i' need to return integer");
}
return NUM2LONG(val);
}
}
unsigned long
-num2ulong(val)
+rb_num2ulong(val)
VALUE val;
{
if (TYPE(val) == T_BIGNUM) {
- return big2ulong(val);
+ return rb_big2ulong(val);
}
- return (unsigned long)num2long(val);
+ return (unsigned long)rb_num2long(val);
}
#if SIZEOF_INT < SIZEOF_LONG
int
-num2int(val)
+rb_num2int(val)
VALUE val;
{
- long num = num2long(val);
+ long num = rb_num2long(val);
if (num < INT_MIN || INT_MAX < num) {
- ArgError("integer %d too big to convert to `int'.", num);
+ rb_raise(rb_eArgError, "integer %d too big to convert to `int'.", num);
}
return (int)num;
}
int
-fix2int(val)
+rb_fix2int(val)
VALUE val;
{
- long num = FIXNUM_P(val)?FIX2LONG(val):num2long(val);
+ long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
if (num < INT_MIN || INT_MAX < num) {
- ArgError("integer %d too big to convert to `int'.", num);
+ rb_raise(rb_eArgError, "integer %d too big to convert to `int'.", num);
}
return (int)num;
}
#else
int
-num2int(val)
+rb_num2int(val)
VALUE val;
{
- return num2long(val);
+ return rb_num2long(val);
}
int
-fix2int(val)
+rb_fix2int(val)
VALUE val;
{
return FIX2INT(val);
@@ -742,16 +742,16 @@ fix2int(val)
#endif
VALUE
-num2fix(val)
+rb_num2fix(val)
VALUE val;
{
long v;
if (FIXNUM_P(val)) return val;
- v = num2long(val);
+ v = rb_num2long(val);
if (!FIXABLE(v))
- Fail("integer %d out of range of fixnum", v);
+ rb_raise(rb_eTypeError, "integer %d out of range of fixnum", v);
return INT2FIX(v);
}
@@ -759,7 +759,7 @@ static VALUE
int_int_p(num)
VALUE num;
{
- return TRUE;
+ return Qtrue;
}
static VALUE
@@ -773,11 +773,11 @@ static VALUE
fix_uminus(num)
VALUE num;
{
- return int2inum(-FIX2LONG(num));
+ return rb_int2inum(-FIX2LONG(num));
}
VALUE
-fix2str(x, base)
+rb_fix2str(x, base)
VALUE x;
int base;
{
@@ -787,115 +787,106 @@ fix2str(x, base)
if (base == 10) fmt[2] = 'd';
else if (base == 16) fmt[2] = 'x';
else if (base == 8) fmt[2] = 'o';
- else Fatal("fixnum cannot treat base %d", base);
+ else rb_fatal("fixnum cannot treat base %d", base);
sprintf(buf, fmt, FIX2LONG(x));
- return str_new2(buf);
+ return rb_str_new2(buf);
}
-VALUE
+static VALUE
fix_to_s(in)
VALUE in;
{
- return fix2str(in, 10);
+ return rb_fix2str(in, 10);
}
static VALUE
fix_plus(x, y)
VALUE x, y;
{
- switch (TYPE(y)) {
- case T_FIXNUM:
- {
- long a, b, c;
- VALUE r;
-
- a = FIX2LONG(x);
- b = FIX2LONG(y);
- c = a + b;
- r = INT2FIX(c);
-
- if (FIX2LONG(r) != c) {
- r = big_plus(int2big(a), int2big(b));
- }
- return r;
+ if (FIXNUM_P(y)) {
+ long a, b, c;
+ VALUE r;
+
+ a = FIX2LONG(x);
+ b = FIX2LONG(y);
+ c = a + b;
+ r = INT2FIX(c);
+
+ if (FIX2LONG(r) != c) {
+ r = rb_big_plus(rb_int2big(a), rb_int2big(b));
}
- case T_FLOAT:
- return float_new((double)FIX2LONG(x) + RFLOAT(y)->value);
- default:
- return num_coerce_bin(x, y);
+ return r;
}
+ if (TYPE(y) == T_FLOAT) {
+ return rb_float_new((double)FIX2LONG(x) + RFLOAT(y)->value);
+ }
+ return rb_num_coerce_bin(x, y);
}
static VALUE
fix_minus(x, y)
VALUE x, y;
{
- switch (TYPE(y)) {
- case T_FIXNUM:
- {
- long a, b, c;
- VALUE r;
-
- a = FIX2LONG(x);
- b = FIX2LONG(y);
- c = a - b;
- r = INT2FIX(c);
-
- if (FIX2LONG(r) != c) {
- r = big_minus(int2big(a), int2big(b));
- }
- return r;
+ if (FIXNUM_P(y)) {
+ long a, b, c;
+ VALUE r;
+
+ a = FIX2LONG(x);
+ b = FIX2LONG(y);
+ c = a - b;
+ r = INT2FIX(c);
+
+ if (FIX2LONG(r) != c) {
+ r = rb_big_minus(rb_int2big(a), rb_int2big(b));
}
- case T_FLOAT:
- return float_new((double)FIX2LONG(x) - RFLOAT(y)->value);
- default:
- return num_coerce_bin(x, y);
+ return r;
+ }
+ if (TYPE(y) == T_FLOAT) {
+ return rb_float_new((double)FIX2LONG(x) - RFLOAT(y)->value);
}
+ return rb_num_coerce_bin(x, y);
}
static VALUE
fix_mul(x, y)
VALUE x, y;
{
- switch (TYPE(y)) {
- case T_FIXNUM:
- {
- long a, b, c;
- VALUE r;
+ if (FIXNUM_P(y)) {
+ long a, b, c;
+ VALUE r;
- a = FIX2LONG(x);
- if (a == 0) return x;
+ a = FIX2LONG(x);
+ if (a == 0) return x;
- b = FIX2LONG(y);
- c = a * b;
- r = INT2FIX(c);
+ b = FIX2LONG(y);
+ c = a * b;
+ r = INT2FIX(c);
- if (FIX2LONG(r) != c || c/a != b) {
- r = big_mul(int2big(a), int2big(b));
- }
- return r;
+ if (FIX2LONG(r) != c || c/a != b) {
+ r = rb_big_mul(rb_int2big(a), rb_int2big(b));
}
- case T_FLOAT:
- return float_new((double)FIX2LONG(x) * RFLOAT(y)->value);
- default:
- return num_coerce_bin(x, y);
+ return r;
+ }
+ if (TYPE(y) == T_FLOAT) {
+ return rb_float_new((double)FIX2LONG(x) * RFLOAT(y)->value);
}
+ return rb_num_coerce_bin(x, y);
}
static VALUE
fix_div(x, y)
VALUE x, y;
{
- long i;
+ if (FIXNUM_P(y)) {
+ long i;
- if (TYPE(y) == T_FIXNUM) {
i = FIX2LONG(y);
- if (i == 0) num_zerodiv();
+ if (i == 0) rb_num_zerodiv();
i = FIX2LONG(x)/i;
return INT2FIX(i);
}
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
static VALUE
@@ -904,9 +895,9 @@ fix_modulo(x, y, modulo)
{
long i;
- if (TYPE(y) == T_FIXNUM) {
+ if (FIXNUM_P(y)) {
i = FIX2LONG(y);
- if (i == 0) num_zerodiv();
+ if (i == 0) rb_num_zerodiv();
i = FIX2LONG(x)%i;
if (modulo &&
(FIX2LONG(x) < 0) != (FIX2LONG(y) < 0) &&
@@ -915,7 +906,7 @@ fix_modulo(x, y, modulo)
}
return INT2FIX(i);
}
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
static VALUE
@@ -943,14 +934,14 @@ fix_pow(x, y)
if (b == 0) return INT2FIX(1);
a = FIX2LONG(x);
if (b > 0) {
- return big_pow(int2big(a), y);
+ return rb_big_pow(rb_int2big(a), y);
}
- return float_new(pow((double)a, (double)b));
+ return rb_float_new(pow((double)a, (double)b));
}
else if (NIL_P(y)) {
return INT2FIX(1);
}
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
static VALUE
@@ -958,7 +949,7 @@ fix_equal(x, y)
VALUE x, y;
{
if (FIXNUM_P(y)) {
- return (FIX2LONG(x) == FIX2LONG(y))?TRUE:FALSE;
+ return (FIX2LONG(x) == FIX2LONG(y))?Qtrue:Qfalse;
}
else {
return num_equal(x, y);
@@ -977,7 +968,7 @@ fix_cmp(x, y)
return INT2FIX(-1);
}
else {
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -988,11 +979,11 @@ fix_gt(x, y)
if (FIXNUM_P(y)) {
long a = FIX2LONG(x), b = FIX2LONG(y);
- if (a > b) return TRUE;
- return FALSE;
+ if (a > b) return Qtrue;
+ return Qfalse;
}
else {
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -1003,11 +994,11 @@ fix_ge(x, y)
if (FIXNUM_P(y)) {
long a = FIX2LONG(x), b = FIX2LONG(y);
- if (a >= b) return TRUE;
- return FALSE;
+ if (a >= b) return Qtrue;
+ return Qfalse;
}
else {
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -1018,11 +1009,11 @@ fix_lt(x, y)
if (FIXNUM_P(y)) {
long a = FIX2LONG(x), b = FIX2LONG(y);
- if (a < b) return TRUE;
- return FALSE;
+ if (a < b) return Qtrue;
+ return Qfalse;
}
else {
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -1033,11 +1024,11 @@ fix_le(x, y)
if (FIXNUM_P(y)) {
long a = FIX2LONG(x), b = FIX2LONG(y);
- if (a <= b) return TRUE;
- return FALSE;
+ if (a <= b) return Qtrue;
+ return Qfalse;
}
else {
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
@@ -1048,7 +1039,7 @@ fix_rev(num)
unsigned long val = FIX2UINT(num);
val = ~val;
- return int2inum(val);
+ return rb_int2inum(val);
}
static VALUE
@@ -1058,10 +1049,10 @@ fix_and(x, y)
long val;
if (TYPE(y) == T_BIGNUM) {
- return big_and(y, x);
+ return rb_big_and(y, x);
}
val = FIX2LONG(x) & NUM2LONG(y);
- return int2inum(val);
+ return rb_int2inum(val);
}
static VALUE
@@ -1071,10 +1062,10 @@ fix_or(x, y)
long val;
if (TYPE(y) == T_BIGNUM) {
- return big_or(y, x);
+ return rb_big_or(y, x);
}
val = FIX2LONG(x) | NUM2LONG(y);
- return int2inum(val);
+ return rb_int2inum(val);
}
static VALUE
@@ -1084,10 +1075,10 @@ fix_xor(x, y)
long val;
if (TYPE(y) == T_BIGNUM) {
- return big_xor(y, x);
+ return rb_big_xor(y, x);
}
val = FIX2LONG(x) ^ NUM2LONG(y);
- return int2inum(val);
+ return rb_int2inum(val);
}
static VALUE
@@ -1101,10 +1092,10 @@ fix_lshift(x, y)
width = NUM2INT(y);
if (width > (sizeof(VALUE)*CHAR_BIT-1)
|| ((unsigned long)val)>>(sizeof(VALUE)*CHAR_BIT-1-width) > 0) {
- return big_lshift(int2big(val), y);
+ return rb_big_lshift(rb_int2big(val), y);
}
val = val << width;
- return int2inum(val);
+ return rb_int2inum(val);
}
static VALUE
@@ -1151,14 +1142,14 @@ fix_to_f(num)
val = (double)FIX2LONG(num);
- return float_new(val);
+ return rb_float_new(val);
}
static VALUE
fix_type(fix)
VALUE fix;
{
- return cFixnum;
+ return rb_cFixnum;
}
static VALUE
@@ -1169,7 +1160,7 @@ fix_abs(fix)
if (i < 0) i = -i;
- return int2inum(i);
+ return rb_int2inum(i);
}
static VALUE
@@ -1177,7 +1168,7 @@ fix_id2name(fix)
VALUE fix;
{
char *name = rb_id2name(FIX2UINT(fix));
- if (name) return str_new2(name);
+ if (name) return rb_str_new2(name);
return Qnil;
}
@@ -1187,7 +1178,7 @@ fix_succ(fix)
{
long i = FIX2LONG(fix) + 1;
- return int2inum(i);
+ return rb_int2inum(i);
}
static VALUE
@@ -1197,7 +1188,7 @@ fix_size(fix)
return INT2FIX(sizeof(long));
}
-VALUE
+static VALUE
num_upto(from, to)
VALUE from, to;
{
@@ -1233,7 +1224,7 @@ num_step(from, to, step)
ID cmp;
if (step == INT2FIX(0)) {
- ArgError("step cannot be 0");
+ rb_raise(rb_eArgError, "step cannot be 0");
}
if (RTEST(rb_funcall(step, '>', 1, INT2FIX(0)))) {
@@ -1264,7 +1255,7 @@ num_dotimes(num)
return num;
}
-VALUE
+static VALUE
fix_upto(from, to)
VALUE from, to;
{
@@ -1279,6 +1270,13 @@ fix_upto(from, to)
return from;
}
+VALUE
+rb_fix_upto(from, to)
+ VALUE from, to;
+{
+ return fix_upto(from, to);
+}
+
static VALUE
fix_downto(from, to)
VALUE from, to;
@@ -1307,7 +1305,7 @@ fix_step(from, to, step)
diff = FIX2LONG(step);
if (diff == 0) {
- ArgError("step cannot be 0");
+ rb_raise(rb_eArgError, "step cannot be 0");
}
else if (diff > 0) {
for (i=FIX2LONG(from); i <= end; i+=diff) {
@@ -1340,9 +1338,9 @@ fix_zero_p(num)
VALUE num;
{
if (FIX2LONG(num) == 0) {
- return TRUE;
+ return Qtrue;
}
- return FALSE;
+ return Qfalse;
}
void
@@ -1351,109 +1349,109 @@ Init_Numeric()
coerce = rb_intern("coerce");
to_i = rb_intern("to_i");
- eZeroDiv = rb_define_class("ZeroDivisionError", eStandardError);
- cNumeric = rb_define_class("Numeric", cObject);
-
- rb_include_module(cNumeric, mComparable);
- rb_define_method(cNumeric, "coerce", num_coerce, 1);
-
- rb_define_method(cNumeric, "+@", num_uplus, 0);
- rb_define_method(cNumeric, "-@", num_uminus, 0);
- rb_define_method(cNumeric, "eql?", num_eql, 1);
- rb_define_method(cNumeric, "divmod", num_divmod, 1);
- rb_define_method(cNumeric, "abs", num_abs, 0);
-
- rb_define_method(cNumeric, "upto", num_upto, 1);
- rb_define_method(cNumeric, "downto", num_downto, 1);
- rb_define_method(cNumeric, "step", num_step, 2);
- rb_define_method(cNumeric, "times", num_dotimes, 0);
- rb_define_method(cNumeric, "integer?", num_int_p, 0);
- rb_define_method(cNumeric, "chr", num_chr, 0);
- rb_define_method(cNumeric, "zero?", num_zero_p, 0);
- rb_define_method(cNumeric, "nonzero?", num_nonzero_p, 0);
-
- cInteger = rb_define_class("Integer", cNumeric);
- rb_define_method(cInteger, "integer?", int_int_p, 0);
- rb_define_method(cInteger, "succ", int_succ, 0);
- rb_define_method(cInteger, "next", int_succ, 0);
-
- cFixnum = rb_define_class("Fixnum", cInteger);
-
- rb_undef_method(CLASS_OF(cFixnum), "new");
-
- rb_define_method(cFixnum, "to_s", fix_to_s, 0);
- rb_define_method(cFixnum, "type", fix_type, 0);
-
- rb_define_method(cFixnum, "id2name", fix_id2name, 0);
-
- rb_define_method(cFixnum, "-@", fix_uminus, 0);
- rb_define_method(cFixnum, "+", fix_plus, 1);
- rb_define_method(cFixnum, "-", fix_minus, 1);
- rb_define_method(cFixnum, "*", fix_mul, 1);
- rb_define_method(cFixnum, "/", fix_div, 1);
- rb_define_method(cFixnum, "%", fix_mod, 1);
- rb_define_method(cFixnum, "remainder", fix_remainder, 1);
- rb_define_method(cFixnum, "**", fix_pow, 1);
-
- rb_define_method(cFixnum, "abs", fix_abs, 0);
-
- rb_define_method(cFixnum, "==", fix_equal, 1);
- rb_define_method(cFixnum, "<=>", fix_cmp, 1);
- rb_define_method(cFixnum, ">", fix_gt, 1);
- rb_define_method(cFixnum, ">=", fix_ge, 1);
- rb_define_method(cFixnum, "<", fix_lt, 1);
- rb_define_method(cFixnum, "<=", fix_le, 1);
-
- rb_define_method(cFixnum, "~", fix_rev, 0);
- rb_define_method(cFixnum, "&", fix_and, 1);
- rb_define_method(cFixnum, "|", fix_or, 1);
- rb_define_method(cFixnum, "^", fix_xor, 1);
- rb_define_method(cFixnum, "[]", fix_aref, 1);
-
- rb_define_method(cFixnum, "<<", fix_lshift, 1);
- rb_define_method(cFixnum, ">>", fix_rshift, 1);
-
- rb_define_method(cFixnum, "to_i", fix_to_i, 0);
- rb_define_method(cFixnum, "to_f", fix_to_f, 0);
-
- rb_define_method(cFixnum, "succ", fix_succ, 0);
- rb_define_method(cFixnum, "next", fix_succ, 0);
- rb_define_method(cFixnum, "size", fix_size, 0);
-
- rb_define_method(cFixnum, "upto", fix_upto, 1);
- rb_define_method(cFixnum, "downto", fix_downto, 1);
- rb_define_method(cFixnum, "step", fix_step, 2);
- rb_define_method(cFixnum, "times", fix_dotimes, 0);
- rb_define_method(cFixnum, "zero?", fix_zero_p, 0);
-
- cFloat = rb_define_class("Float", cNumeric);
-
- rb_undef_method(CLASS_OF(cFloat), "new");
-
- rb_define_method(cFloat, "to_s", flo_to_s, 0);
- rb_define_method(cFloat, "coerce", flo_coerce, 1);
- rb_define_method(cFloat, "-@", flo_uminus, 0);
- rb_define_method(cFloat, "+", flo_plus, 1);
- rb_define_method(cFloat, "-", flo_minus, 1);
- rb_define_method(cFloat, "*", flo_mul, 1);
- rb_define_method(cFloat, "/", flo_div, 1);
- rb_define_method(cFloat, "%", flo_mod, 1);
- rb_define_method(cFloat, "remainder", flo_remainder, 1);
- rb_define_method(cFloat, "**", flo_pow, 1);
- rb_define_method(cFloat, "==", flo_eq, 1);
- rb_define_method(cFloat, "<=>", flo_cmp, 1);
- rb_define_method(cFloat, ">", flo_gt, 1);
- rb_define_method(cFloat, ">=", flo_ge, 1);
- rb_define_method(cFloat, "<", flo_lt, 1);
- rb_define_method(cFloat, "<=", flo_le, 1);
- rb_define_method(cFloat, "eql?", flo_eql, 1);
- rb_define_method(cFloat, "hash", flo_hash, 0);
- rb_define_method(cFloat, "to_i", flo_to_i, 0);
- rb_define_method(cFloat, "to_f", flo_to_f, 0);
- rb_define_method(cFloat, "abs", flo_abs, 0);
- rb_define_method(cFloat, "zero?", flo_zero_p, 0);
-
- rb_define_method(cFloat, "floor", flo_floor, 0);
- rb_define_method(cFloat, "ceil", flo_ceil, 0);
- rb_define_method(cFloat, "round", flo_round, 0);
+ rb_eZeroDiv = rb_define_class("ZeroDivisionError", rb_eStandardError);
+ rb_cNumeric = rb_define_class("Numeric", rb_cObject);
+
+ rb_include_module(rb_cNumeric, rb_mComparable);
+ rb_define_method(rb_cNumeric, "coerce", num_coerce, 1);
+
+ rb_define_method(rb_cNumeric, "+@", num_uplus, 0);
+ rb_define_method(rb_cNumeric, "-@", num_uminus, 0);
+ rb_define_method(rb_cNumeric, "eql?", num_eql, 1);
+ rb_define_method(rb_cNumeric, "divmod", num_divmod, 1);
+ rb_define_method(rb_cNumeric, "abs", num_abs, 0);
+
+ rb_define_method(rb_cNumeric, "upto", num_upto, 1);
+ rb_define_method(rb_cNumeric, "downto", num_downto, 1);
+ rb_define_method(rb_cNumeric, "step", num_step, 2);
+ rb_define_method(rb_cNumeric, "times", num_dotimes, 0);
+ rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
+ rb_define_method(rb_cNumeric, "chr", num_chr, 0);
+ rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
+ rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
+
+ rb_cInteger = rb_define_class("Integer", rb_cNumeric);
+ rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
+ rb_define_method(rb_cInteger, "succ", int_succ, 0);
+ rb_define_method(rb_cInteger, "next", int_succ, 0);
+
+ rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
+
+ rb_undef_method(CLASS_OF(rb_cFixnum), "new");
+
+ rb_define_method(rb_cFixnum, "to_s", fix_to_s, 0);
+ rb_define_method(rb_cFixnum, "type", fix_type, 0);
+
+ rb_define_method(rb_cFixnum, "id2name", fix_id2name, 0);
+
+ rb_define_method(rb_cFixnum, "-@", fix_uminus, 0);
+ rb_define_method(rb_cFixnum, "+", fix_plus, 1);
+ rb_define_method(rb_cFixnum, "-", fix_minus, 1);
+ rb_define_method(rb_cFixnum, "*", fix_mul, 1);
+ rb_define_method(rb_cFixnum, "/", fix_div, 1);
+ rb_define_method(rb_cFixnum, "%", fix_mod, 1);
+ rb_define_method(rb_cFixnum, "remainder", fix_remainder, 1);
+ rb_define_method(rb_cFixnum, "**", fix_pow, 1);
+
+ rb_define_method(rb_cFixnum, "abs", fix_abs, 0);
+
+ rb_define_method(rb_cFixnum, "==", fix_equal, 1);
+ rb_define_method(rb_cFixnum, "<=>", fix_cmp, 1);
+ rb_define_method(rb_cFixnum, ">", fix_gt, 1);
+ rb_define_method(rb_cFixnum, ">=", fix_ge, 1);
+ rb_define_method(rb_cFixnum, "<", fix_lt, 1);
+ rb_define_method(rb_cFixnum, "<=", fix_le, 1);
+
+ rb_define_method(rb_cFixnum, "~", fix_rev, 0);
+ rb_define_method(rb_cFixnum, "&", fix_and, 1);
+ rb_define_method(rb_cFixnum, "|", fix_or, 1);
+ rb_define_method(rb_cFixnum, "^", fix_xor, 1);
+ rb_define_method(rb_cFixnum, "[]", fix_aref, 1);
+
+ rb_define_method(rb_cFixnum, "<<", fix_lshift, 1);
+ rb_define_method(rb_cFixnum, ">>", fix_rshift, 1);
+
+ rb_define_method(rb_cFixnum, "to_i", fix_to_i, 0);
+ rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0);
+
+ rb_define_method(rb_cFixnum, "succ", fix_succ, 0);
+ rb_define_method(rb_cFixnum, "next", fix_succ, 0);
+ rb_define_method(rb_cFixnum, "size", fix_size, 0);
+
+ rb_define_method(rb_cFixnum, "upto", fix_upto, 1);
+ rb_define_method(rb_cFixnum, "downto", fix_downto, 1);
+ rb_define_method(rb_cFixnum, "step", fix_step, 2);
+ rb_define_method(rb_cFixnum, "times", fix_dotimes, 0);
+ rb_define_method(rb_cFixnum, "zero?", fix_zero_p, 0);
+
+ rb_cFloat = rb_define_class("Float", rb_cNumeric);
+
+ rb_undef_method(CLASS_OF(rb_cFloat), "new");
+
+ rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
+ rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
+ rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
+ rb_define_method(rb_cFloat, "+", flo_plus, 1);
+ rb_define_method(rb_cFloat, "-", flo_minus, 1);
+ rb_define_method(rb_cFloat, "*", flo_mul, 1);
+ rb_define_method(rb_cFloat, "/", flo_div, 1);
+ rb_define_method(rb_cFloat, "%", flo_mod, 1);
+ rb_define_method(rb_cFloat, "remainder", flo_remainder, 1);
+ rb_define_method(rb_cFloat, "**", flo_pow, 1);
+ rb_define_method(rb_cFloat, "==", flo_eq, 1);
+ rb_define_method(rb_cFloat, "<=>", flo_cmp, 1);
+ rb_define_method(rb_cFloat, ">", flo_gt, 1);
+ rb_define_method(rb_cFloat, ">=", flo_ge, 1);
+ rb_define_method(rb_cFloat, "<", flo_lt, 1);
+ rb_define_method(rb_cFloat, "<=", flo_le, 1);
+ rb_define_method(rb_cFloat, "eql?", flo_eql, 1);
+ rb_define_method(rb_cFloat, "hash", flo_hash, 0);
+ rb_define_method(rb_cFloat, "to_i", flo_to_i, 0);
+ rb_define_method(rb_cFloat, "to_f", flo_to_f, 0);
+ rb_define_method(rb_cFloat, "abs", flo_abs, 0);
+ rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0);
+
+ rb_define_method(rb_cFloat, "floor", flo_floor, 0);
+ rb_define_method(rb_cFloat, "ceil", flo_ceil, 0);
+ rb_define_method(rb_cFloat, "round", flo_round, 0);
}