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

numeric.c: basic arithmetic

* numeric.c (rb_int_{uminus,plus,minus,mul,idiv,modulo}): basic
  arithmetic functions for generic Integers.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54296 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-03-26 01:54:16 +00:00
parent ed47a0ec80
commit 79b209f19a
3 changed files with 83 additions and 1 deletions

View file

@ -1,4 +1,7 @@
Sat Mar 26 10:51:58 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
Sat Mar 26 10:54:14 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* numeric.c (rb_int_{uminus,plus,minus,mul,idiv,modulo}): basic
arithmetic functions for generic Integers.
* numeric.c (FIXNUM_{POSITIVE,NEGATIVE,ZERO}_P): predict macros
only for Fixnum.

View file

@ -1007,6 +1007,12 @@ double ruby_float_mod(double x, double y);
int rb_num_negative_p(VALUE);
VALUE rb_int_succ(VALUE num);
VALUE rb_int_pred(VALUE num);
VALUE rb_int_uminus(VALUE num);
VALUE rb_int_plus(VALUE x, VALUE y);
VALUE rb_int_minus(VALUE x, VALUE y);
VALUE rb_int_mul(VALUE x, VALUE y);
VALUE rb_int_idiv(VALUE x, VALUE y);
VALUE rb_int_modulo(VALUE x, VALUE y);
VALUE rb_dbl_hash(double d);
VALUE rb_fix_plus(VALUE x, VALUE y);

View file

@ -2925,6 +2925,18 @@ fix_uminus(VALUE num)
return LONG2NUM(-FIX2LONG(num));
}
VALUE
rb_int_uminus(VALUE num)
{
if (FIXNUM_P(num)) {
return fix_uminus(num);
}
else if (RB_TYPE_P(num, T_BIGNUM)) {
return rb_big_uminus(num);
}
return rb_funcall(num, idUMinus, 0, 0);
}
VALUE
rb_fix2str(VALUE x, int base)
{
@ -3038,6 +3050,18 @@ rb_fix_plus(VALUE x, VALUE y)
return fix_plus(x, y);
}
VALUE
rb_int_plus(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_plus(x, y);
}
else if (RB_TYPE_P(x, T_BIGNUM)) {
return rb_big_plus(x, y);
}
return rb_num_coerce_bin(x, y, '+');
}
/*
* call-seq:
* fix - numeric -> numeric_result
@ -3072,6 +3096,19 @@ fix_minus(VALUE x, VALUE y)
}
}
VALUE
rb_int_minus(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_minus(x, y);
}
else if (RB_TYPE_P(x, T_BIGNUM)) {
return rb_big_minus(x, y);
}
return rb_num_coerce_bin(x, y, '-');
}
#define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
/*tests if N*N would overflow*/
#define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
@ -3106,6 +3143,18 @@ fix_mul(VALUE x, VALUE y)
}
}
VALUE
rb_int_mul(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_mul(x, y);
}
else if (RB_TYPE_P(x, T_BIGNUM)) {
return rb_big_mul(x, y);
}
return rb_num_coerce_bin(x, y, '*');
}
/*
* call-seq:
* fix.fdiv(numeric) -> float
@ -3196,6 +3245,18 @@ fix_idiv(VALUE x, VALUE y)
return fix_divide(x, y, id_div);
}
VALUE
rb_int_idiv(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_idiv(x, y);
}
else if (RB_TYPE_P(x, T_BIGNUM)) {
return rb_big_idiv(x, y);
}
return num_div(x, y);
}
/*
* call-seq:
* fix % other -> real
@ -3225,6 +3286,18 @@ fix_mod(VALUE x, VALUE y)
}
}
VALUE
rb_int_modulo(VALUE x, VALUE y)
{
if (FIXNUM_P(x)) {
return fix_mod(x, y);
}
else if (RB_TYPE_P(x, T_BIGNUM)) {
return rb_big_modulo(x, y);
}
return num_modulo(x, y);
}
/*
* call-seq:
* fix.divmod(numeric) -> array