12#include "ruby/internal/config.h"
29#include "internal/array.h"
30#include "internal/compilers.h"
31#include "internal/complex.h"
32#include "internal/enumerator.h"
33#include "internal/gc.h"
34#include "internal/hash.h"
35#include "internal/numeric.h"
36#include "internal/object.h"
37#include "internal/rational.h"
38#include "internal/string.h"
39#include "internal/util.h"
40#include "internal/variable.h"
50#define DBL_MIN 2.2250738585072014e-308
53#define DBL_MAX 1.7976931348623157e+308
56#define DBL_MIN_EXP (-1021)
59#define DBL_MAX_EXP 1024
62#define DBL_MIN_10_EXP (-307)
65#define DBL_MAX_10_EXP 308
71#define DBL_MANT_DIG 53
74#define DBL_EPSILON 2.2204460492503131e-16
77#define ACCURATE_POW10(ndigits) ((ndigits) < DBL_DIG)
79#ifndef USE_RB_INFINITY
80#elif !defined(WORDS_BIGENDIAN)
87#elif !defined(WORDS_BIGENDIAN)
101 x = f + (x - f >= 0.5);
105 x = f - (f - x >= 0.5);
112round_half_up(
double x,
double s)
114 double f, xs = x * s;
117 if (s == 1.0)
return f;
119 if ((
double)((f + 0.5) / s) <= x) f += 1;
123 if ((
double)((f - 0.5) / s) >= x) f -= 1;
130round_half_down(
double x,
double s)
132 double f, xs = x * s;
136 if ((
double)((f - 0.5) / s) >= x) f -= 1;
140 if ((
double)((f + 0.5) / s) <= x) f += 1;
147round_half_even(
double x,
double s)
149 double u, v, us, vs, f, d, uf;
161 else if (d == 0.5 || ((
double)((uf + 0.5) / s) <= x))
173 else if (d == 0.5 || ((
double)((uf - 0.5) / s) >= x))
182static VALUE fix_lshift(
long,
unsigned long);
183static VALUE fix_rshift(
long,
unsigned long);
184static VALUE int_pow(
long x,
unsigned long y);
185static VALUE rb_int_floor(
VALUE num,
int ndigits);
186static VALUE rb_int_ceil(
VALUE num,
int ndigits);
188static int float_round_overflow(
int ndigits,
int binexp);
189static int float_round_underflow(
int ndigits,
int binexp);
193#define id_divmod idDivmod
194#define id_to_i idTo_i
205static ID id_to, id_by;
213enum ruby_num_rounding_mode
214rb_num_get_rounding_option(
VALUE opts)
216 static ID round_kwds[1];
222 if (!round_kwds[0]) {
225 if (!
rb_get_kwargs(opts, round_kwds, 0, 1, &rounding))
goto noopt;
229 else if (
NIL_P(rounding)) {
234 if (
NIL_P(str))
goto invalid;
237 s = RSTRING_PTR(str);
238 switch (RSTRING_LEN(str)) {
240 if (rb_memcicmp(s,
"up", 2) == 0)
241 return RUBY_NUM_ROUND_HALF_UP;
244 if (rb_memcicmp(s,
"even", 4) == 0)
245 return RUBY_NUM_ROUND_HALF_EVEN;
246 if (strncasecmp(s,
"down", 4) == 0)
247 return RUBY_NUM_ROUND_HALF_DOWN;
251 rb_raise(rb_eArgError,
"invalid rounding mode: % "PRIsVALUE, rounding);
254 return RUBY_NUM_ROUND_DEFAULT;
259rb_num_to_uint(
VALUE val,
unsigned int *ret)
262#define NUMERR_NEGATIVE 2
263#define NUMERR_TOOLARGE 3
266#if SIZEOF_INT < SIZEOF_LONG
267 if (v > (
long)UINT_MAX)
return NUMERR_TOOLARGE;
269 if (v < 0)
return NUMERR_NEGATIVE;
270 *ret = (
unsigned int)v;
274 if (RB_BIGNUM_TYPE_P(val)) {
275 if (BIGNUM_NEGATIVE_P(val))
return NUMERR_NEGATIVE;
276#if SIZEOF_INT < SIZEOF_LONG
278 return NUMERR_TOOLARGE;
281 if (rb_absint_size(val, NULL) >
sizeof(
int))
return NUMERR_TOOLARGE;
282 *ret = (
unsigned int)rb_big2ulong((
VALUE)val);
289#define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
295 return FIXNUM_POSITIVE_P(num);
297 else if (RB_BIGNUM_TYPE_P(num)) {
298 return BIGNUM_POSITIVE_P(num);
307 return FIXNUM_NEGATIVE_P(num);
309 else if (RB_BIGNUM_TYPE_P(num)) {
310 return BIGNUM_NEGATIVE_P(num);
316rb_int_positive_p(
VALUE num)
318 return int_pos_p(num);
322rb_int_negative_p(
VALUE num)
324 return int_neg_p(num);
328rb_num_negative_p(
VALUE num)
330 return rb_num_negative_int_p(num);
334num_funcall_op_0(
VALUE x,
VALUE arg,
int recursive)
338 const char *name = rb_id2name(func);
343 else if (name[0] && name[1] ==
'@' && !name[2]) {
352 return rb_funcallv(x, func, 0, 0);
361NORETURN(
static void num_funcall_op_1_recursion(
VALUE x,
ID func,
VALUE y));
366 const char *name = rb_id2name(func);
368 rb_name_error(func,
"%"PRIsVALUE
".%"PRIsVALUE
"(%"PRIsVALUE
")",
378num_funcall_op_1(
VALUE y,
VALUE arg,
int recursive)
383 num_funcall_op_1_recursion(x, func, y);
392 args[0] = (
VALUE)func;
442NORETURN(
static void coerce_failed(
VALUE x,
VALUE y));
452 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" can't be coerced into %"PRIsVALUE,
462 coerce_failed(*x, *y);
466 if (!err &&
NIL_P(ary)) {
481 do_coerce(&x, &y, TRUE);
488 if (do_coerce(&x, &y, FALSE))
496 if (
NIL_P(c)) rb_cmperr(x, y);
503 VALUE x0 = x, y0 = y;
505 if (!do_coerce(&x, &y, FALSE)) {
509 return ensure_cmp(
rb_funcall(x, func, 1, y), x0, y0);
529 "can't define singleton method \"%"PRIsVALUE
"\" for %"PRIsVALUE,
551 return rb_immutable_obj_clone(argc, argv, x);
554# define num_clone rb_immutable_obj_clone
572num_imaginary(
VALUE num)
574 return rb_complex_new(
INT2FIX(0), num);
590 do_coerce(&zero, &num, TRUE);
592 return num_funcall1(zero,
'-', num);
631 return rb_funcall(num_funcall1(x,
'/', y), rb_intern(
"floor"), 0);
671 VALUE q = num_funcall1(x, id_div, y);
711 do_coerce(&x, &y, TRUE);
713 VALUE z = num_funcall1(x,
'%', y);
716 ((rb_num_negative_int_p(x) &&
717 rb_num_positive_int_p(y)) ||
718 (rb_num_positive_int_p(x) &&
719 rb_num_negative_int_p(y)))) {
779 if (rb_num_negative_int_p(num)) {
780 return num_funcall0(num, idUMinus);
806 return FIXNUM_ZERO_P(num);
809 return rb_bigzero_p(num);
813rb_int_zero_p(
VALUE num)
815 return RBOOL(int_zero_p(num));
839num_nonzero_p(
VALUE num)
841 if (
RTEST(num_funcall0(num, rb_intern(
"zero?")))) {
870 return num_funcall0(num, id_to_i);
882num_positive_p(
VALUE num)
890 else if (RB_BIGNUM_TYPE_P(num)) {
892 return RBOOL(BIGNUM_POSITIVE_P(num) && !rb_bigzero_p(num));
894 return rb_num_compare_with_zero(num, mid);
906num_negative_p(
VALUE num)
908 return RBOOL(rb_num_negative_int_p(num));
916#if SIZEOF_DOUBLE <= SIZEOF_VALUE
917 flt->float_value = d;
921 rb_float_value_type v;
923 flt->float_value = u.v;
953 enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
954 enum {float_dig = DBL_DIG+1};
955 char buf[float_dig + roomof(decimal_mant, CHAR_BIT) + 10];
959 int sign, decpt, digs;
962 static const char minf[] =
"-Infinity";
963 const int pos = (value > 0);
966 else if (isnan(value))
969 p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
971 if ((digs = (
int)(e - p)) >= (
int)
sizeof(buf)) digs = (
int)
sizeof(buf) - 1;
972 memcpy(buf, p, digs);
976 memmove(buf + decpt + 1, buf + decpt, digs - decpt);
980 else if (decpt <= DBL_DIG) {
984 rb_str_resize(s, (
len = RSTRING_LEN(s)) + decpt - digs + 2);
985 ptr = RSTRING_PTR(s) +
len;
987 memset(ptr,
'0', decpt - digs);
990 memcpy(ptr,
".0", 2);
996 else if (decpt > -4) {
1000 rb_str_resize(s, (
len = RSTRING_LEN(s)) - decpt + digs);
1001 ptr = RSTRING_PTR(s);
1002 memset(ptr +=
len,
'0', -decpt);
1003 memcpy(ptr -= decpt, buf, digs);
1012 memmove(buf + 2, buf + 1, digs - 1);
1020 rb_str_catf(s,
"e%+03d", decpt - 1);
1048rb_float_uminus(
VALUE flt)
1079 else if (RB_BIGNUM_TYPE_P(y)) {
1110 else if (RB_BIGNUM_TYPE_P(y)) {
1141 else if (RB_BIGNUM_TYPE_P(y)) {
1153double_div_double(
double x,
double y)
1155 if (LIKELY(y != 0.0)) {
1158 else if (x == 0.0) {
1162 double z = signbit(y) ? -1.0 : 1.0;
1163 return x * z * HUGE_VAL;
1172 double ret = double_div_double(num, den);
1200 else if (RB_BIGNUM_TYPE_P(y)) {
1201 den = rb_big2dbl(y);
1210 ret = double_div_double(num, den);
1231 return num_funcall1(x,
'/', y);
1235flodivmod(
double x,
double y,
double *divp,
double *modp)
1241 if (modp) *modp = y;
1242 if (divp) *divp = y;
1246 if ((x == 0.0) || (isinf(y) && !isinf(x)))
1258 if (isinf(x) && !isinf(y))
1261 div = (x - mod) / y;
1262 if (modp && divp) div = round(div);
1268 if (modp) *modp = mod;
1269 if (divp) *divp = div;
1278ruby_float_mod(
double x,
double y)
1281 flodivmod(x, y, 0, &mod);
1322 else if (RB_BIGNUM_TYPE_P(y)) {
1340 return rb_dbl2big(d);
1372 double fy, div, mod;
1373 volatile VALUE a, b;
1378 else if (RB_BIGNUM_TYPE_P(y)) {
1420 else if (RB_BIGNUM_TYPE_P(y)) {
1427 if (dx < 0 && dy != round(dy))
1428 return rb_dbl_complex_new_polar_pi(pow(-dx, dy), dy);
1462 if (RB_BIGNUM_TYPE_P(x)) {
1463 return rb_big_eql(x, y);
1489 if (x == y)
return INT2FIX(0);
1497 if (x == y)
return Qtrue;
1498 result = num_funcall1(y, id_eq, x);
1499 return RBOOL(
RTEST(result));
1522 volatile double a, b;
1525 return rb_integer_float_eq(y, x);
1531 return num_equal(x, y);
1534 return RBOOL(a == b);
1537#define flo_eq rb_float_equal
1538static VALUE rb_dbl_hash(
double d);
1556rb_dbl_hash(
double d)
1558 return ST2FIX(rb_dbl_long_hash(d));
1564 if (isnan(a) || isnan(b))
return Qnil;
1565 if (a == b)
return INT2FIX(0);
1567 if (a < b)
return INT2FIX(-1);
1608 if (isnan(a))
return Qnil;
1610 VALUE rel = rb_integer_float_cmp(y, x);
1619 if (isinf(a) && !UNDEF_P(i =
rb_check_funcall(y, rb_intern(
"infinite?"), 0, 0))) {
1621 int j = rb_cmpint(i, x, y);
1622 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1625 if (a > 0.0)
return INT2FIX(1);
1636 return NUM2INT(ensure_cmp(flo_cmp(x, y), x, y));
1661 VALUE rel = rb_integer_float_cmp(y, x);
1672 return RBOOL(a > b);
1698 VALUE rel = rb_integer_float_cmp(y, x);
1709 return RBOOL(a >= b);
1734 VALUE rel = rb_integer_float_cmp(y, x);
1745 return RBOOL(a < b);
1772 VALUE rel = rb_integer_float_cmp(y, x);
1783 return RBOOL(a <= b);
1810 return RBOOL(a == b);
1815#define flo_eql rb_float_eql
1818rb_float_abs(
VALUE flt)
1837flo_is_nan_p(
VALUE num)
1841 return RBOOL(isnan(value));
1868rb_flo_is_infinite_p(
VALUE num)
1873 return INT2FIX( value < 0 ? -1 : 1 );
1898rb_flo_is_finite_p(
VALUE num)
1902 return RBOOL(isfinite(value));
1906flo_nextafter(
VALUE flo,
double value)
1910 y = nextafter(x, value);
1959flo_next_float(
VALUE vx)
1961 return flo_nextafter(vx, HUGE_VAL);
2000flo_prev_float(
VALUE vx)
2002 return flo_nextafter(vx, -HUGE_VAL);
2006rb_float_floor(
VALUE num,
int ndigits)
2010 if (number == 0.0) {
2016 frexp(number, &binexp);
2017 if (float_round_overflow(ndigits, binexp))
return num;
2018 if (number > 0.0 && float_round_underflow(ndigits, binexp))
2020 if (!ACCURATE_POW10(ndigits)) {
2021 return rb_flo_floor_by_rational(num, ndigits);
2023 f = pow(10, ndigits);
2024 mul = floor(number * f);
2025 res = (mul + 1) / f;
2031 num = dbl2ival(floor(number));
2032 if (ndigits < 0) num = rb_int_floor(num, ndigits);
2038flo_ndigits(
int argc,
VALUE *argv)
2127 int ndigits = flo_ndigits(argc, argv);
2128 return rb_float_floor(num, ndigits);
2212 int ndigits = flo_ndigits(argc, argv);
2213 return rb_float_ceil(num, ndigits);
2217rb_float_ceil(
VALUE num,
int ndigits)
2222 if (number == 0.0) {
2227 frexp(number, &binexp);
2228 if (float_round_overflow(ndigits, binexp))
return num;
2229 if (number < 0.0 && float_round_underflow(ndigits, binexp))
2231 if (!ACCURATE_POW10(ndigits)) {
2232 return rb_flo_ceil_by_rational(num, ndigits);
2234 f = pow(10, ndigits);
2235 f = ceil(number * f) / f;
2239 num = dbl2ival(ceil(number));
2240 if (ndigits < 0) num = rb_int_ceil(num, ndigits);
2246int_round_zero_p(
VALUE num,
int ndigits)
2252 bytes =
sizeof(long);
2254 else if (RB_BIGNUM_TYPE_P(num)) {
2255 bytes = rb_big_size(num);
2260 return (-0.415241 * ndigits - 0.125 > bytes);
2267 if ((z * y - x) * 2 == y) {
2276 return (x + y / 2) / y * y;
2282 return (x + y / 2 - 1) / y * y;
2288 return (
int)rb_int_odd_p(rb_int_idiv(n, f));
2294 return int_pos_p(num);
2300 return int_neg_p(num);
2307rb_int_round(
VALUE num,
int ndigits,
enum ruby_num_rounding_mode mode)
2311 if (int_round_zero_p(num, ndigits)) {
2315 f = int_pow(10, -ndigits);
2320 x = ROUND_CALL(mode, int_round, (x, y));
2328 h = rb_int_idiv(f,
INT2FIX(2));
2329 r = rb_int_modulo(num, f);
2330 n = rb_int_minus(num, r);
2331 r = rb_int_cmp(r, h);
2332 if (FIXNUM_POSITIVE_P(r) ||
2333 (FIXNUM_ZERO_P(r) && ROUND_CALL(mode, int_half_p, (num, n, f)))) {
2334 n = rb_int_plus(n, f);
2340rb_int_floor(
VALUE num,
int ndigits)
2342 VALUE f = int_pow(10, -ndigits);
2346 if (neg) x = -x + y - 1;
2352 bool neg = int_neg_p(num);
2353 if (neg) num = rb_int_minus(rb_int_plus(rb_int_uminus(num), f),
INT2FIX(1));
2354 num = rb_int_mul(rb_int_div(num, f), f);
2355 if (neg) num = rb_int_uminus(num);
2361rb_int_ceil(
VALUE num,
int ndigits)
2363 VALUE f = int_pow(10, -ndigits);
2374 bool neg = int_neg_p(num);
2376 num = rb_int_uminus(num);
2378 num = rb_int_plus(num, rb_int_minus(f,
INT2FIX(1)));
2379 num = rb_int_mul(rb_int_div(num, f), f);
2380 if (neg) num = rb_int_uminus(num);
2386rb_int_truncate(
VALUE num,
int ndigits)
2391 if (int_round_zero_p(num, ndigits))
2393 f = int_pow(10, -ndigits);
2406 m = rb_int_modulo(num, f);
2407 if (int_neg_p(num)) {
2408 return rb_int_plus(num, rb_int_minus(f, m));
2411 return rb_int_minus(num, m);
2473 double number, f, x;
2476 enum ruby_num_rounding_mode mode;
2481 mode = rb_num_get_rounding_option(opt);
2483 if (number == 0.0) {
2487 return rb_int_round(flo_to_i(num), ndigits, mode);
2490 x = ROUND_CALL(mode, round, (number, 1.0));
2493 if (isfinite(number)) {
2495 frexp(number, &binexp);
2496 if (float_round_overflow(ndigits, binexp))
return num;
2497 if (float_round_underflow(ndigits, binexp))
return DBL2NUM(0);
2498 if (!ACCURATE_POW10(ndigits)) {
2499 return rb_flo_round_by_rational(num, ndigits, mode);
2501 f = pow(10, ndigits);
2502 x = ROUND_CALL(mode, round, (number, f));
2509float_round_overflow(
int ndigits,
int binexp)
2511 enum {float_dig = DBL_DIG+2};
2530 if (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1)) {
2537float_round_underflow(
int ndigits,
int binexp)
2539 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
2566 if (f > 0.0) f = floor(f);
2567 if (f < 0.0) f = ceil(f);
2608flo_truncate(
int argc,
VALUE *argv,
VALUE num)
2611 return flo_ceil(argc, argv, num);
2613 return flo_floor(argc, argv, num);
2633 return flo_floor(argc, argv,
rb_Float(num));
2653 return flo_ceil(argc, argv,
rb_Float(num));
2670 return flo_round(argc, argv,
rb_Float(num));
2685num_truncate(
int argc,
VALUE *argv,
VALUE num)
2687 return flo_truncate(argc, argv,
rb_Float(num));
2691ruby_float_step_size(
double beg,
double end,
double unit,
int excl)
2693 const double epsilon = DBL_EPSILON;
2700 return unit > 0 ? beg <= end : beg >= end;
2702 n= (end - beg)/unit;
2703 err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
2704 if (err>0.5) err=0.5;
2711 d = +((n + 1) * unit) + beg;
2716 else if (beg > end) {
2724 d = +((n + 1) * unit) + beg;
2729 else if (beg > end) {
2738ruby_float_step(
VALUE from,
VALUE to,
VALUE step,
int excl,
int allow_endless)
2743 double end = (allow_endless &&
NIL_P(to)) ? (unit < 0 ? -1 : 1)*HUGE_VAL :
NUM2DBL(to);
2744 double n = ruby_float_step_size(beg, end, unit, excl);
2751 else if (unit == 0) {
2757 for (i=0; i<n; i++) {
2758 double d = i*unit+beg;
2759 if (unit >= 0 ? end < d : d < end) d = end;
2769ruby_num_interval_step_size(
VALUE from,
VALUE to,
VALUE step,
int excl)
2794 if (isinf(n))
return DBL2NUM(n);
2796 return rb_dbl2big(n);
2802 case 0:
return DBL2NUM(HUGE_VAL);
2803 case -1: cmp =
'<';
break;
2815num_step_negative_p(
VALUE num)
2825 else if (RB_BIGNUM_TYPE_P(num)) {
2827 return BIGNUM_NEGATIVE_P(num);
2832 coerce_failed(num,
INT2FIX(0));
2842 argc =
rb_scan_args(argc, argv,
"02:", to, step, &hash);
2849 if (!UNDEF_P(values[0])) {
2850 if (argc > 0) rb_raise(rb_eArgError,
"to is given twice");
2853 if (!UNDEF_P(values[1])) {
2854 if (argc > 1) rb_raise(rb_eArgError,
"step is given twice");
2863num_step_check_fix_args(
int argc,
VALUE *to,
VALUE *step,
VALUE by,
int fix_nil,
int allow_zero_step)
2871 if (argc > 1 &&
NIL_P(*step)) {
2876 rb_raise(rb_eArgError,
"step can't be 0");
2881 desc = num_step_negative_p(*step);
2882 if (fix_nil &&
NIL_P(*to)) {
2889num_step_scan_args(
int argc,
const VALUE *argv,
VALUE *to,
VALUE *step,
int fix_nil,
int allow_zero_step)
2892 argc = num_step_extract_args(argc, argv, to, step, &by);
2893 return num_step_check_fix_args(argc, to, step, by, fix_nil, allow_zero_step);
2903 num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
2905 return ruby_num_interval_step_size(from, to, step, FALSE);
3013 num_step_extract_args(argc, argv, &to, &step, &by);
3021 rb_raise(rb_eArgError,
"step can't be 0");
3026 num_step_size, from, to, step, FALSE);
3032 desc = num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
3038 inf = isinf(f) && (signbit(f) ? desc : !desc);
3054 for (; i >= end; i += diff)
3058 for (; i <= end; i += diff)
3063 else if (!ruby_float_step(from, to, step, FALSE, FALSE)) {
3071 ID cmp = desc ?
'<' :
'>';
3081out_of_range_float(
char (*pbuf)[24],
VALUE val)
3083 char *
const buf = *pbuf;
3086 snprintf(buf,
sizeof(*pbuf),
"%-.10g",
RFLOAT_VALUE(val));
3087 if ((s = strchr(buf,
' ')) != 0) *s =
'\0';
3091#define FLOAT_OUT_OF_RANGE(val, type) do { \
3093 rb_raise(rb_eRangeError, "float %s out of range of "type, \
3094 out_of_range_float(&buf, (val))); \
3097#define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
3098#define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
3099#define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
3100#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3101 (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \
3103 LONG_MIN_MINUS_ONE < (n))
3110 rb_raise(
rb_eTypeError,
"no implicit conversion from nil to integer");
3117 && LONG_MIN_MINUS_ONE_IS_LESS_THAN(
RFLOAT_VALUE(val))) {
3121 FLOAT_OUT_OF_RANGE(val,
"integer");
3124 else if (RB_BIGNUM_TYPE_P(val)) {
3125 return rb_big2long(val);
3134rb_num2ulong_internal(
VALUE val,
int *wrap_p)
3138 rb_raise(
rb_eTypeError,
"no implicit conversion of nil into Integer");
3145 return (
unsigned long)l;
3149 if (d < ULONG_MAX_PLUS_ONE && LONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3151 *wrap_p = d <= -1.0;
3153 return (
unsigned long)d;
3154 return (
unsigned long)(long)d;
3157 FLOAT_OUT_OF_RANGE(val,
"integer");
3160 else if (RB_BIGNUM_TYPE_P(val)) {
3162 unsigned long ul = rb_big2ulong(val);
3164 *wrap_p = BIGNUM_NEGATIVE_P(val);
3177 return rb_num2ulong_internal(val, NULL);
3183 rb_raise(
rb_eRangeError,
"integer %"PRIdVALUE
" too %s to convert to 'int'",
3184 num, num < 0 ?
"small" :
"big");
3187#if SIZEOF_INT < SIZEOF_LONG
3191 if ((
long)(
int)num != num) {
3197check_uint(
unsigned long num,
int sign)
3201 if (num < (
unsigned long)INT_MIN)
3202 rb_raise(
rb_eRangeError,
"integer %ld too small to convert to 'unsigned int'", (
long)num);
3207 rb_raise(
rb_eRangeError,
"integer %lu too big to convert to 'unsigned int'", num);
3233 unsigned long num = rb_num2ulong_internal(val, &wrap);
3235 check_uint(num, wrap);
3249 check_uint(num, FIXNUM_NEGATIVE_P(val));
3278NORETURN(
static void rb_out_of_short(
SIGNED_VALUE num));
3282 rb_raise(
rb_eRangeError,
"integer %"PRIdVALUE
" too %s to convert to 'short'",
3283 num, num < 0 ?
"small" :
"big");
3287check_short(
long num)
3289 if ((
long)(
short)num != num) {
3290 rb_out_of_short(num);
3295check_ushort(
unsigned long num,
int sign)
3299 if (num < (
unsigned long)SHRT_MIN)
3300 rb_raise(
rb_eRangeError,
"integer %ld too small to convert to 'unsigned short'", (
long)num);
3304 if (USHRT_MAX < num)
3305 rb_raise(
rb_eRangeError,
"integer %lu too big to convert to 'unsigned short'", num);
3331 unsigned long num = rb_num2ulong_internal(val, &wrap);
3333 check_ushort(num, wrap);
3347 check_ushort(num, FIXNUM_NEGATIVE_P(val));
3360 rb_raise(
rb_eRangeError,
"integer %ld out of range of fixnum", v);
3366#define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
3367#define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
3368#define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
3370#define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
3372#define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3373 (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \
3375 LLONG_MIN_MINUS_ONE < (n))
3388 if (d < LLONG_MAX_PLUS_ONE && (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d))) {
3392 FLOAT_OUT_OF_RANGE(val,
"long long");
3395 else if (RB_BIGNUM_TYPE_P(val)) {
3396 return rb_big2ll(val);
3399 rb_raise(
rb_eTypeError,
"no implicit conversion from string");
3402 rb_raise(
rb_eTypeError,
"no implicit conversion from boolean");
3413 rb_raise(
rb_eTypeError,
"no implicit conversion of nil into Integer");
3420 if (d < ULLONG_MAX_PLUS_ONE && LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3426 FLOAT_OUT_OF_RANGE(val,
"unsigned long long");
3429 else if (RB_BIGNUM_TYPE_P(val)) {
3430 return rb_big2ull(val);
3443#ifndef HAVE_UINT128_T
3446rb_uint128_from_bignum_digits_fallback(rb_uint128_t *result, BDIGIT *digits,
size_t length)
3449 for (
long i = length - 1; i >= 0; i--) {
3451 uint64_t carry = result->parts.low >> (64 - (SIZEOF_BDIGIT * CHAR_BIT));
3452 result->parts.low = (result->parts.low << (SIZEOF_BDIGIT * CHAR_BIT)) | digits[i];
3453 result->parts.high = (result->parts.high << (SIZEOF_BDIGIT * CHAR_BIT)) | carry;
3460rb_uint128_twos_complement_negate(rb_uint128_t *value)
3462 if (value->parts.low == 0) {
3463 value->parts.high = ~value->parts.high + 1;
3466 value->parts.low = ~value->parts.low + 1;
3467 value->parts.high = ~value->parts.high + (value->parts.low == 0 ? 1 : 0);
3473rb_numeric_to_uint128(
VALUE x)
3475 rb_uint128_t result = {0};
3479 rb_raise(
rb_eRangeError,
"negative integer cannot be converted to unsigned 128-bit integer");
3481#ifdef HAVE_UINT128_T
3482 result.value = (uint128_t)value;
3484 result.parts.low = (uint64_t)value;
3485 result.parts.high = 0;
3489 else if (RB_BIGNUM_TYPE_P(x)) {
3490 if (BIGNUM_NEGATIVE_P(x)) {
3491 rb_raise(
rb_eRangeError,
"negative integer cannot be converted to unsigned 128-bit integer");
3493 size_t length = BIGNUM_LEN(x);
3494#ifdef HAVE_UINT128_T
3495 if (length > roomof(SIZEOF_INT128_T, SIZEOF_BDIGIT)) {
3496 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'unsigned 128-bit integer'");
3498 BDIGIT *digits = BIGNUM_DIGITS(x);
3500 for (
long i = length - 1; i >= 0; i--) {
3501 result.value = (result.value << (SIZEOF_BDIGIT * CHAR_BIT)) | digits[i];
3505 if (length > roomof(16, SIZEOF_BDIGIT)) {
3506 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'unsigned 128-bit integer'");
3508 BDIGIT *digits = BIGNUM_DIGITS(x);
3509 rb_uint128_from_bignum_digits_fallback(&result, digits, length);
3519rb_numeric_to_int128(
VALUE x)
3521 rb_int128_t result = {0};
3524#ifdef HAVE_UINT128_T
3525 result.value = (int128_t)value;
3530 result.parts.low = (uint64_t)value;
3531 result.parts.high = UINT64_MAX;
3534 result.parts.low = (uint64_t)value;
3535 result.parts.high = 0;
3540 else if (RB_BIGNUM_TYPE_P(x)) {
3541 size_t length = BIGNUM_LEN(x);
3542#ifdef HAVE_UINT128_T
3543 if (length > roomof(SIZEOF_INT128_T, SIZEOF_BDIGIT)) {
3544 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3546 BDIGIT *digits = BIGNUM_DIGITS(x);
3547 uint128_t unsigned_result = 0;
3548 for (
long i = length - 1; i >= 0; i--) {
3549 unsigned_result = (unsigned_result << (SIZEOF_BDIGIT * CHAR_BIT)) | digits[i];
3551 if (BIGNUM_NEGATIVE_P(x)) {
3554 if (unsigned_result > ((uint128_t)1 << 127)) {
3555 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3557 result.value = -(int128_t)(unsigned_result - 1) - 1;
3561 if (unsigned_result > (((uint128_t)1 << 127) - 1)) {
3562 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3564 result.value = (int128_t)unsigned_result;
3567 if (length > roomof(16, SIZEOF_BDIGIT)) {
3568 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3570 BDIGIT *digits = BIGNUM_DIGITS(x);
3571 rb_uint128_t unsigned_result = {0};
3572 rb_uint128_from_bignum_digits_fallback(&unsigned_result, digits, length);
3573 if (BIGNUM_NEGATIVE_P(x)) {
3575 uint64_t max_neg_high = (uint64_t)1 << 63;
3576 if (unsigned_result.parts.high > max_neg_high || (unsigned_result.parts.high == max_neg_high && unsigned_result.parts.low > 0)) {
3577 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3580 rb_uint128_twos_complement_negate(&unsigned_result);
3581 result.parts.low = unsigned_result.parts.low;
3582 result.parts.high = (int64_t)unsigned_result.parts.high;
3587 uint64_t max_pos_high = ((uint64_t)1 << 63) - 1;
3588 if (unsigned_result.parts.high > max_pos_high) {
3589 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3591 result.parts.low = unsigned_result.parts.low;
3592 result.parts.high = unsigned_result.parts.high;
3603rb_uint128_to_numeric(rb_uint128_t n)
3605#ifdef HAVE_UINT128_T
3609 return rb_uint128t2big(n.value);
3613 return LONG2FIX((
long)n.parts.low);
3616 VALUE bignum = rb_ull2big(n.parts.low);
3617 if (n.parts.high > 0) {
3618 VALUE high_bignum = rb_ull2big(n.parts.high);
3620 VALUE shifted_value = rb_int_lshift(high_bignum,
INT2FIX(64));
3621 bignum = rb_int_plus(bignum, shifted_value);
3628rb_int128_to_numeric(rb_int128_t n)
3630#ifdef HAVE_UINT128_T
3634 return rb_int128t2big(n.value);
3636 int64_t high = (int64_t)n.parts.high;
3639 return LONG2FIX((
long)n.parts.low);
3644 rb_uint128_t unsigned_value = {0};
3645 if (n.parts.low == 0) {
3646 unsigned_value.parts.low = 0;
3647 unsigned_value.parts.high = ~n.parts.high + 1;
3650 unsigned_value.parts.low = ~n.parts.low + 1;
3651 unsigned_value.parts.high = ~n.parts.high + (unsigned_value.parts.low == 0 ? 1 : 0);
3653 VALUE bignum = rb_uint128_to_numeric(unsigned_value);
3654 return rb_int_uminus(bignum);
3661 return rb_uint128_to_numeric(conversion.uint128);
3764rb_int_odd_p(
VALUE num)
3767 return RBOOL(num & 2);
3771 return rb_big_odd_p(num);
3776int_even_p(
VALUE num)
3779 return RBOOL((num & 2) == 0);
3783 return rb_big_even_p(num);
3788rb_int_even_p(
VALUE num)
3790 return int_even_p(num);
3820 return rb_int_equal(rb_int_and(num, mask), mask);
3850 return RBOOL(!int_zero_p(rb_int_and(num, mask)));
3880 return RBOOL(int_zero_p(rb_int_and(num, mask)));
3896rb_int_succ(
VALUE num)
3902 if (RB_BIGNUM_TYPE_P(num)) {
3903 return rb_big_plus(num,
INT2FIX(1));
3905 return num_funcall1(num,
'+',
INT2FIX(1));
3908#define int_succ rb_int_succ
3924rb_int_pred(
VALUE num)
3930 if (RB_BIGNUM_TYPE_P(num)) {
3931 return rb_big_minus(num,
INT2FIX(1));
3933 return num_funcall1(num,
'-',
INT2FIX(1));
3936#define int_pred rb_int_pred
3943 switch (n = rb_enc_codelen(code, enc)) {
3944 case ONIGERR_INVALID_CODE_POINT_VALUE:
3945 rb_raise(
rb_eRangeError,
"invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
3947 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
3952 str = rb_enc_str_new(0, n, enc);
3953 rb_enc_mbcput(code, RSTRING_PTR(str), enc);
3954 if (rb_enc_precise_mbclen(RSTRING_PTR(str),
RSTRING_END(str), enc) != n) {
3955 rb_raise(
rb_eRangeError,
"invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
3986 if (rb_num_to_uint(num, &i) == 0) {
4014 rb_error_arity(argc, 0, 1);
4016 enc = rb_to_encoding(argv[0]);
4027fix_uminus(
VALUE num)
4033rb_int_uminus(
VALUE num)
4036 return fix_uminus(num);
4040 return rb_big_uminus(num);
4047 char buf[
SIZEOF_VALUE*CHAR_BIT + 1], *
const e = buf +
sizeof buf, *b = e;
4052 if (base < 2 || 36 < base) {
4053 rb_raise(rb_eArgError,
"invalid radix %d", base);
4055#if SIZEOF_LONG < SIZEOF_VOIDP
4056# if SIZEOF_VOIDP == SIZEOF_LONG_LONG
4057 if ((val >= 0 && (x & 0xFFFFFFFF00000000ull)) ||
4058 (val < 0 && (x & 0xFFFFFFFF00000000ull) != 0xFFFFFFFF00000000ull)) {
4059 rb_bug(
"Unnormalized Fixnum value %p", (
void *)x);
4070 u = 1 + (
unsigned long)(-(val + 1));
4077 *--b = ruby_digitmap[(int)(u % base)];
4078 }
while (u /= base);
4086static VALUE rb_fix_to_s_static[10];
4092 if (i >= 0 && i < 10) {
4093 return rb_fix_to_s_static[i];
4125 return rb_int2str(x, base);
4129rb_int2str(
VALUE x,
int base)
4134 else if (RB_BIGNUM_TYPE_P(x)) {
4135 return rb_big2str(x, base);
4145 return rb_fix_plus_fix(x, y);
4147 else if (RB_BIGNUM_TYPE_P(y)) {
4148 return rb_big_plus(y, x);
4154 return rb_complex_plus(y, x);
4164 return fix_plus(x, y);
4189 return fix_plus(x, y);
4191 else if (RB_BIGNUM_TYPE_P(x)) {
4192 return rb_big_plus(x, y);
4201 return rb_fix_minus_fix(x, y);
4203 else if (RB_BIGNUM_TYPE_P(y)) {
4205 return rb_big_minus(x, y);
4234 return fix_minus(x, y);
4236 else if (RB_BIGNUM_TYPE_P(x)) {
4237 return rb_big_minus(x, y);
4243#define SQRT_LONG_MAX HALF_LONG_MSB
4245#define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
4251 return rb_fix_mul_fix(x, y);
4253 else if (RB_BIGNUM_TYPE_P(y)) {
4258 return rb_big_mul(y, x);
4264 return rb_complex_mul(y, x);
4290 return fix_mul(x, y);
4292 else if (RB_BIGNUM_TYPE_P(x)) {
4293 return rb_big_mul(x, y);
4303#if SIZEOF_LONG * CHAR_BIT > DBL_MANT_DIG
4304 if ((iy < 0 ? -iy : iy) >= (1L << DBL_MANT_DIG)) {
4305 return rb_big_fdiv_double(rb_int2big(
FIX2LONG(x)), rb_int2big(iy));
4308 return double_div_double(
FIX2LONG(x), iy);
4310 else if (RB_BIGNUM_TYPE_P(y)) {
4311 return rb_big_fdiv_double(rb_int2big(
FIX2LONG(x)), y);
4325 VALUE gcd = rb_gcd(x, y);
4326 if (!FIXNUM_ZERO_P(gcd) && gcd !=
INT2FIX(1)) {
4327 x = rb_int_idiv(x, gcd);
4328 y = rb_int_idiv(y, gcd);
4332 return fix_fdiv_double(x, y);
4334 else if (RB_BIGNUM_TYPE_P(x)) {
4335 return rb_big_fdiv_double(x, y);
4362 return DBL2NUM(rb_int_fdiv_double(x, y));
4372 return rb_fix_div_fix(x, y);
4374 else if (RB_BIGNUM_TYPE_P(y)) {
4376 return rb_big_div(x, y);
4381 return rb_flo_div_flo(
DBL2NUM(d), y);
4386 v = fix_divide(x, y,
'/');
4387 return flo_floor(0, 0, v);
4393 return rb_rational_reciprocal(y);
4401 return fix_divide(x, y,
'/');
4429 return fix_div(x, y);
4431 else if (RB_BIGNUM_TYPE_P(x)) {
4432 return rb_big_div(x, y);
4440 return fix_divide(x, y, id_div);
4465 return fix_idiv(x, y);
4467 else if (RB_BIGNUM_TYPE_P(x)) {
4468 return rb_big_idiv(x, y);
4470 return num_div(x, y);
4478 return rb_fix_mod_fix(x, y);
4480 else if (RB_BIGNUM_TYPE_P(y)) {
4482 return rb_big_modulo(x, y);
4524 return fix_mod(x, y);
4526 else if (RB_BIGNUM_TYPE_P(x)) {
4527 return rb_big_modulo(x, y);
4529 return num_modulo(x, y);
4560 VALUE z = fix_mod(x, y);
4563 z = fix_minus(z, y);
4566 else if (!RB_BIGNUM_TYPE_P(y)) {
4567 return num_remainder(x, y);
4571 else if (!RB_BIGNUM_TYPE_P(x)) {
4574 return rb_big_remainder(x, y);
4583 rb_fix_divmod_fix(x, y, &div, &mod);
4586 else if (RB_BIGNUM_TYPE_P(y)) {
4588 return rb_big_divmod(x, y);
4593 volatile VALUE a, b;
4635 return fix_divmod(x, y);
4637 else if (RB_BIGNUM_TYPE_P(x)) {
4638 return rb_big_divmod(x, y);
4660int_pow(
long x,
unsigned long y)
4665 if (y == 0)
return INT2FIX(1);
4674 while (y % 2 == 0) {
4675 if (!FIT_SQRT_LONG(x)) {
4682 if (MUL_OVERFLOW_FIXNUM_P(x, z)) {
4693 v = rb_big_pow(rb_int2big(x),
LONG2NUM(y));
4696 if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
4703 return int_pow(x, y);
4714 VALUE y = rb_int_pow(x, minusb);
4734 if (a == 1)
return INT2FIX(1);
4735 if (a == -1)
return INT2FIX(b % 2 ? -1 : 1);
4736 if (b < 0)
return fix_pow_inverted(x, fix_uminus(y));
4737 if (b == 0)
return INT2FIX(1);
4738 if (b == 1)
return x;
4739 if (a == 0)
return INT2FIX(0);
4740 return int_pow(a, b);
4742 else if (RB_BIGNUM_TYPE_P(y)) {
4743 if (a == 1)
return INT2FIX(1);
4744 if (a == -1)
return INT2FIX(int_even_p(y) ? 1 : -1);
4745 if (BIGNUM_NEGATIVE_P(y))
return fix_pow_inverted(x, rb_big_uminus(y));
4746 if (a == 0)
return INT2FIX(0);
4748 return rb_big_pow(x, y);
4752 if (dy == 0.0)
return DBL2NUM(1.0);
4754 return DBL2NUM(dy < 0 ? HUGE_VAL : 0.0);
4756 if (a == 1)
return DBL2NUM(1.0);
4757 if (a < 0 && dy != round(dy))
4758 return rb_dbl_complex_new_polar_pi(pow(-(
double)a, dy), dy);
4759 return DBL2NUM(pow((
double)a, dy));
4815 return fix_pow(x, y);
4817 else if (RB_BIGNUM_TYPE_P(x)) {
4818 return rb_big_pow(x, y);
4826 VALUE z = rb_int_pow(x, y);
4827 if (!
NIL_P(z))
return z;
4832 return rb_complex_pow(x, y);
4834 return rb_rational_pow(x, y);
4844 if (x == y)
return Qtrue;
4846 else if (RB_BIGNUM_TYPE_P(y)) {
4847 return rb_big_eq(y, x);
4850 return rb_integer_float_eq(x, y);
4853 return num_equal(x, y);
4873 return fix_equal(x, y);
4875 else if (RB_BIGNUM_TYPE_P(x)) {
4876 return rb_big_eq(x, y);
4884 if (x == y)
return INT2FIX(0);
4889 else if (RB_BIGNUM_TYPE_P(y)) {
4890 VALUE cmp = rb_big_cmp(y, x);
4898 return rb_integer_float_cmp(x, y);
4936 return fix_cmp(x, y);
4938 else if (RB_BIGNUM_TYPE_P(x)) {
4939 return rb_big_cmp(x, y);
4952 else if (RB_BIGNUM_TYPE_P(y)) {
4953 return RBOOL(rb_big_cmp(y, x) ==
INT2FIX(-1));
4956 return RBOOL(rb_integer_float_cmp(x, y) ==
INT2FIX(1));
4983 return fix_gt(x, y);
4985 else if (RB_BIGNUM_TYPE_P(x)) {
4986 return rb_big_gt(x, y);
4997 else if (RB_BIGNUM_TYPE_P(y)) {
4998 return RBOOL(rb_big_cmp(y, x) !=
INT2FIX(+1));
5001 VALUE rel = rb_integer_float_cmp(x, y);
5030 return fix_ge(x, y);
5032 else if (RB_BIGNUM_TYPE_P(x)) {
5033 return rb_big_ge(x, y);
5044 else if (RB_BIGNUM_TYPE_P(y)) {
5045 return RBOOL(rb_big_cmp(y, x) ==
INT2FIX(+1));
5048 return RBOOL(rb_integer_float_cmp(x, y) ==
INT2FIX(-1));
5074 return fix_lt(x, y);
5076 else if (RB_BIGNUM_TYPE_P(x)) {
5077 return rb_big_lt(x, y);
5088 else if (RB_BIGNUM_TYPE_P(y)) {
5089 return RBOOL(rb_big_cmp(y, x) !=
INT2FIX(-1));
5092 VALUE rel = rb_integer_float_cmp(x, y);
5121 return fix_le(x, y);
5123 else if (RB_BIGNUM_TYPE_P(x)) {
5124 return rb_big_le(x, y);
5136rb_int_comp(
VALUE num)
5139 return fix_comp(num);
5141 else if (RB_BIGNUM_TYPE_P(num)) {
5142 return rb_big_comp(num);
5148num_funcall_bit_1(
VALUE y,
VALUE arg,
int recursive)
5153 num_funcall_op_1_recursion(x, func, y);
5163 args[0] = (
VALUE)func;
5166 do_coerce(&args[1], &args[2], TRUE);
5168 args[2], args[1], (
VALUE)args);
5171 coerce_failed(x, y);
5184 if (RB_BIGNUM_TYPE_P(y)) {
5185 return rb_big_and(y, x);
5210 return fix_and(x, y);
5212 else if (RB_BIGNUM_TYPE_P(x)) {
5213 return rb_big_and(x, y);
5226 if (RB_BIGNUM_TYPE_P(y)) {
5227 return rb_big_or(y, x);
5252 return fix_or(x, y);
5254 else if (RB_BIGNUM_TYPE_P(x)) {
5255 return rb_big_or(x, y);
5268 if (RB_BIGNUM_TYPE_P(y)) {
5269 return rb_big_xor(y, x);
5294 return fix_xor(x, y);
5296 else if (RB_BIGNUM_TYPE_P(x)) {
5297 return rb_big_xor(x, y);
5310 return rb_big_lshift(rb_int2big(val), y);
5313 return fix_rshift(val, (
unsigned long)-width);
5314 return fix_lshift(val, width);
5318fix_lshift(
long val,
unsigned long width)
5320 if (width > (SIZEOF_LONG*CHAR_BIT-1)
5321 || ((
unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) {
5322 return rb_big_lshift(rb_int2big(val),
ULONG2NUM(width));
5349 return rb_fix_lshift(x, y);
5351 else if (RB_BIGNUM_TYPE_P(x)) {
5352 return rb_big_lshift(x, y);
5365 return rb_big_rshift(rb_int2big(val), y);
5367 if (i == 0)
return x;
5369 return fix_lshift(val, (
unsigned long)-i);
5370 return fix_rshift(val, i);
5374fix_rshift(
long val,
unsigned long i)
5376 if (i >=
sizeof(
long)*CHAR_BIT-1) {
5377 if (val < 0)
return INT2FIX(-1);
5380 val = RSHIFT(val, i);
5405 return rb_fix_rshift(x, y);
5407 else if (RB_BIGNUM_TYPE_P(x)) {
5408 return rb_big_rshift(x, y);
5421 idx = rb_big_norm(idx);
5423 if (!BIGNUM_SIGN(idx) || val >= 0)
5431 if (SIZEOF_LONG*CHAR_BIT-1 <= i) {
5432 if (val < 0)
return INT2FIX(1);
5454 return rb_cmpint(r, a, b);
5467 return rb_big_aref2(num, beg,
len);
5470 num = rb_int_rshift(num, beg);
5472 return rb_int_and(num, mask);
5485 if (!
RTEST(num_negative_p(end))) {
5486 if (!excl) end = rb_int_plus(end,
INT2FIX(1));
5487 VALUE mask = generate_mask(end);
5488 if (int_zero_p(rb_int_and(num, mask))) {
5492 rb_raise(rb_eArgError,
"The beginless range for Integer#[] results in infinity");
5500 int cmp = compare_indexes(beg, end);
5501 if (!
NIL_P(end) && cmp < 0) {
5502 VALUE len = rb_int_minus(end, beg);
5504 return int_aref2(num, beg,
len);
5506 else if (cmp == 0) {
5511 return rb_int_rshift(num, beg);
5516 return rb_fix_aref(num, arg);
5518 else if (RB_BIGNUM_TYPE_P(num)) {
5519 return rb_big_aref(num, arg);
5564int_aref(
int const argc,
VALUE *
const argv,
VALUE const num)
5568 return int_aref2(num, argv[0], argv[1]);
5570 return int_aref1(num, argv[0]);
5600 else if (RB_BIGNUM_TYPE_P(num)) {
5601 val = rb_big2dbl(num);
5621rb_int_abs(
VALUE num)
5624 return fix_abs(num);
5626 else if (RB_BIGNUM_TYPE_P(num)) {
5627 return rb_big_abs(num);
5639rb_int_size(
VALUE num)
5642 return fix_size(num);
5644 else if (RB_BIGNUM_TYPE_P(num)) {
5645 return rb_big_size_m(num);
5651rb_fix_bit_length(
VALUE fix)
5660rb_int_bit_length(
VALUE num)
5663 return rb_fix_bit_length(num);
5665 else if (RB_BIGNUM_TYPE_P(num)) {
5666 return rb_big_bit_length(num);
5672rb_fix_digits(
VALUE fix,
long base)
5680 rb_raise(rb_eArgError,
"invalid radix %ld", base);
5683 return rb_ary_new_from_args(1,
INT2FIX(0));
5699 VALUE digits, bases;
5703 if (RB_BIGNUM_TYPE_P(base))
5704 base = rb_big_norm(base);
5707 rb_raise(rb_eArgError,
"invalid radix %ld",
FIX2LONG(base));
5708 else if (RB_BIGNUM_TYPE_P(base) && BIGNUM_NEGATIVE_P(base))
5709 rb_raise(rb_eArgError,
"negative radix");
5712 return rb_fix_digits(num,
FIX2LONG(base));
5715 return rb_ary_new_from_args(1, num);
5717 if (int_lt(rb_int_div(rb_int_bit_length(num), rb_int_bit_length(base)),
INT2FIX(50))) {
5720 VALUE qr = rb_int_divmod(num, base);
5728 for (
VALUE b = base; int_le(b, num) ==
Qtrue; b = rb_int_mul(b, b)) {
5731 digits = rb_ary_new_from_args(1, num);
5735 for(i = last_idx; i >= 0; i--) {
5737 VALUE divmod = rb_int_divmod(n, b);
5765rb_int_digits(
int argc,
VALUE *argv,
VALUE num)
5770 if (rb_num_negative_p(num))
5776 rb_raise(
rb_eTypeError,
"wrong argument type %s (expected Integer)",
5778 if (RB_BIGNUM_TYPE_P(base_value))
5779 return rb_int_digits_bigbase(num, base_value);
5783 rb_raise(rb_eArgError,
"negative radix");
5785 rb_raise(rb_eArgError,
"invalid radix %ld", base);
5791 return rb_fix_digits(num, base);
5792 else if (RB_BIGNUM_TYPE_P(num))
5793 return rb_int_digits_bigbase(num,
LONG2FIX(base));
5832 for (i =
FIX2LONG(from); i <= end; i++) {
5843 ensure_cmp(c, i, to);
5882 for (i=
FIX2LONG(from); i >= end; i--) {
5893 if (
NIL_P(c)) rb_cmperr(i, to);
5901 return int_neg_p(num) ?
INT2FIX(0) : num;
5960 if (!
rb_scan_args(argc, argv,
"01:", &nd, &opt))
return num;
5962 mode = rb_num_get_rounding_option(opt);
5966 return rb_int_round(num, ndigits, mode);
6035 return rb_int_floor(num, ndigits);
6103 return rb_int_ceil(num, ndigits);
6130int_truncate(
int argc,
VALUE* argv,
VALUE num)
6139 return rb_int_truncate(num, ndigits);
6142#define DEFINE_INT_SQRT(rettype, prefix, argtype) \
6144prefix##_isqrt(argtype n) \
6146 if (!argtype##_IN_DOUBLE_P(n)) { \
6147 unsigned int b = bit_length(n); \
6149 rettype x = (rettype)(n >> (b/2+1)); \
6150 x |= ((rettype)1LU << (b-1)/2); \
6151 while ((t = n/x) < (argtype)x) x = (rettype)((x + t) >> 1); \
6154 rettype x = (rettype)sqrt(argtype##_TO_DOUBLE(n)); \
6157 if (x * x > n) x--; \
6161#if SIZEOF_LONG*CHAR_BIT > DBL_MANT_DIG
6162# define RB_ULONG_IN_DOUBLE_P(n) ((n) < (1UL << DBL_MANT_DIG))
6164# define RB_ULONG_IN_DOUBLE_P(n) 1
6166#define RB_ULONG_TO_DOUBLE(n) (double)(n)
6167#define RB_ULONG unsigned long
6168DEFINE_INT_SQRT(
unsigned long, rb_ulong, RB_ULONG)
6170#if 2*SIZEOF_BDIGIT > SIZEOF_LONG
6171# if 2*SIZEOF_BDIGIT*CHAR_BIT > DBL_MANT_DIG
6172# define BDIGIT_DBL_IN_DOUBLE_P(n) ((n) < ((BDIGIT_DBL)1UL << DBL_MANT_DIG))
6174# define BDIGIT_DBL_IN_DOUBLE_P(n) 1
6176# ifdef ULL_TO_DOUBLE
6177# define BDIGIT_DBL_TO_DOUBLE(n) ULL_TO_DOUBLE(n)
6179# define BDIGIT_DBL_TO_DOUBLE(n) (double)(n)
6181DEFINE_INT_SQRT(BDIGIT, rb_bdigit_dbl, BDIGIT_DBL)
6184#define domain_error(msg) \
6185 rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
6222 unsigned long n, sq;
6225 if (FIXNUM_NEGATIVE_P(num)) {
6226 domain_error(
"isqrt");
6229 sq = rb_ulong_isqrt(n);
6235 domain_error(
"isqrt");
6237 biglen = BIGNUM_LEN(num);
6238 if (biglen == 0)
return INT2FIX(0);
6239#if SIZEOF_BDIGIT <= SIZEOF_LONG
6242 n = BIGNUM_DIGITS(num)[0];
6243 sq = rb_ulong_isqrt(n);
6247 return rb_big_isqrt(num);
6270 return rb_check_integer_type(num);
6550#define fix_to_s_static(n) do { \
6551 VALUE lit = rb_fstring_literal(#n); \
6552 rb_fix_to_s_static[n] = lit; \
6553 rb_vm_register_global_object(lit); \
6568#undef fix_to_s_static
6693#undef rb_float_value
6697 return rb_float_value_inline(v);
6702rb_float_new(
double d)
6704 return rb_float_new_inline(d);
6707#include "numeric.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define ISALNUM
@old{rb_isalnum}
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
VALUE rb_float_new_in_heap(double d)
Identical to rb_float_new(), except it does not generate Flonums.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define NUM2LL
Old name of RB_NUM2LL.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define CLASS_OF
Old name of rb_class_of.
#define FIXABLE
Old name of RB_FIXABLE.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define FIX2ULONG
Old name of RB_FIX2ULONG.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define NUM2DBL
Old name of rb_num2dbl.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define NUM2ULL
Old name of RB_NUM2ULL.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
#define DBL2NUM
Old name of rb_float_new.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
VALUE rb_eZeroDivError
ZeroDivisionError exception.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eFloatDomainError
FloatDomainError exception.
VALUE rb_eMathDomainError
Math::DomainError exception.
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
VALUE rb_cInteger
Module class.
VALUE rb_cNumeric
Numeric class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_mComparable
Comparable module.
VALUE rb_cFloat
Float class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RUBY_FIXNUM_MAX
Maximum possible value that a fixnum can represent.
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
rb_encoding * rb_default_internal_encoding(void)
Queries the "default internal" encoding.
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
Encodes the passed code point into a series of bytes.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
#define RGENGC_WB_PROTECTED_FLOAT
This is a compile-time flag to enable/disable write barrier for struct RFloat.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
#define SIZED_ENUMERATOR_KW(obj, argc, argv, size_fn, kw_splat)
This is an implementation detail of RETURN_SIZED_ENUMERATOR_KW().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
ID rb_frame_this_func(void)
Queries the name of the Ruby level method that is calling this function.
void rb_num_zerodiv(void)
Just always raises an exception.
VALUE rb_num2fix(VALUE val)
Converts a numeric value into a Fixnum.
VALUE rb_fix2str(VALUE val, int base)
Generates a place-value representation of the given Fixnum, with given radix.
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
VALUE rb_dbl_cmp(double lhs, double rhs)
Compares two doubles.
VALUE rb_num_coerce_bit(VALUE lhs, VALUE rhs, ID op)
This one is optimised for bitwise operations, but the API is identical to rb_num_coerce_bin().
VALUE rb_num_coerce_relop(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_cmp(), except for return values.
VALUE rb_num_coerce_cmp(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_bin(), except for return values.
VALUE rb_num_coerce_bin(VALUE lhs, VALUE rhs, ID op)
Coerced binary operation.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
VALUE rb_rational_raw(VALUE num, VALUE den)
Identical to rb_rational_new(), except it skips argument validations.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
#define rb_usascii_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "US ASCII" encoding.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
void rb_remove_method_id(VALUE klass, ID mid)
Identical to rb_remove_method(), except it accepts the method name as ID.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
int len
Length of the buffer.
unsigned long rb_num2uint(VALUE num)
Converts an instance of rb_cNumeric into C's unsigned long.
long rb_fix2int(VALUE num)
Identical to rb_num2int().
long rb_num2int(VALUE num)
Converts an instance of rb_cNumeric into C's long.
unsigned long rb_fix2uint(VALUE num)
Identical to rb_num2uint().
LONG_LONG rb_num2ll(VALUE num)
Converts an instance of rb_cNumeric into C's long long.
unsigned LONG_LONG rb_num2ull(VALUE num)
Converts an instance of rb_cNumeric into C's unsigned long long.
VALUE rb_yield(VALUE val)
Yields the block.
#define RB_FIX2ULONG
Just another name of rb_fix2ulong.
#define RB_FIX2LONG
Just another name of rb_fix2long.
void rb_out_of_int(SIGNED_VALUE num)
This is an utility function to raise an rb_eRangeError.
long rb_num2long(VALUE num)
Converts an instance of rb_cNumeric into C's long.
unsigned long rb_num2ulong(VALUE num)
Converts an instance of rb_cNumeric into C's unsigned long.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static bool RBIGNUM_NEGATIVE_P(VALUE b)
Checks if the bignum is negative.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
short rb_num2short(VALUE num)
Converts an instance of rb_cNumeric into C's short.
unsigned short rb_num2ushort(VALUE num)
Converts an instance of rb_cNumeric into C's unsigned short.
short rb_fix2short(VALUE num)
Identical to rb_num2short().
unsigned short rb_fix2ushort(VALUE num)
Identical to rb_num2ushort().
static bool RB_FIXNUM_P(VALUE obj)
Checks if the given object is a so-called Fixnum.
#define RTEST
This is an old name of RB_TEST.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.