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/error.h"
34#include "internal/gc.h"
35#include "internal/hash.h"
36#include "internal/numeric.h"
37#include "internal/object.h"
38#include "internal/rational.h"
39#include "internal/string.h"
40#include "internal/util.h"
41#include "internal/variable.h"
51#define DBL_MIN 2.2250738585072014e-308
54#define DBL_MAX 1.7976931348623157e+308
57#define DBL_MIN_EXP (-1021)
60#define DBL_MAX_EXP 1024
63#define DBL_MIN_10_EXP (-307)
66#define DBL_MAX_10_EXP 308
72#define DBL_MANT_DIG 53
75#define DBL_EPSILON 2.2204460492503131e-16
78#define ACCURATE_POW10(ndigits) ((ndigits) < DBL_DIG)
80#ifndef USE_RB_INFINITY
81#elif !defined(WORDS_BIGENDIAN)
88#elif !defined(WORDS_BIGENDIAN)
102 x = f + (x - f >= 0.5);
106 x = f - (f - x >= 0.5);
113round_half_up(
double x,
double s)
115 double f, xs = x * s;
118 if (s == 1.0)
return f;
120 if ((
double)((f + 0.5) / s) <= x) f += 1;
124 if ((
double)((f - 0.5) / s) >= x) f -= 1;
131round_half_down(
double x,
double s)
133 double f, xs = x * s;
137 if ((
double)((f - 0.5) / s) >= x) f -= 1;
141 if ((
double)((f + 0.5) / s) <= x) f += 1;
148round_half_even(
double x,
double s)
150 double u, v, us, vs, f, d, uf;
162 else if (d == 0.5 || ((
double)((uf + 0.5) / s) <= x))
174 else if (d == 0.5 || ((
double)((uf - 0.5) / s) >= x))
183static VALUE fix_lshift(
long,
unsigned long);
184static VALUE fix_rshift(
long,
unsigned long);
185static VALUE int_pow(
long x,
unsigned long y);
186static VALUE rb_int_floor(
VALUE num,
int ndigits);
187static VALUE rb_int_ceil(
VALUE num,
int ndigits);
189static int float_round_overflow(
int ndigits,
int binexp);
190static int float_round_underflow(
int ndigits,
int binexp);
194#define id_divmod idDivmod
195#define id_to_i idTo_i
206static ID id_to, id_by;
214enum ruby_num_rounding_mode
215rb_num_get_rounding_option(
VALUE opts)
217 static ID round_kwds[1];
223 if (!round_kwds[0]) {
226 if (!
rb_get_kwargs(opts, round_kwds, 0, 1, &rounding))
goto noopt;
230 else if (
NIL_P(rounding)) {
235 if (
NIL_P(str))
goto invalid;
238 s = RSTRING_PTR(str);
239 switch (RSTRING_LEN(str)) {
241 if (rb_memcicmp(s,
"up", 2) == 0)
242 return RUBY_NUM_ROUND_HALF_UP;
245 if (rb_memcicmp(s,
"even", 4) == 0)
246 return RUBY_NUM_ROUND_HALF_EVEN;
247 if (strncasecmp(s,
"down", 4) == 0)
248 return RUBY_NUM_ROUND_HALF_DOWN;
252 rb_raise(rb_eArgError,
"invalid rounding mode: % "PRIsVALUE, rounding);
255 return RUBY_NUM_ROUND_DEFAULT;
260rb_num_to_uint(
VALUE val,
unsigned int *ret)
263#define NUMERR_NEGATIVE 2
264#define NUMERR_TOOLARGE 3
267#if SIZEOF_INT < SIZEOF_LONG
268 if (v > (
long)UINT_MAX)
return NUMERR_TOOLARGE;
270 if (v < 0)
return NUMERR_NEGATIVE;
271 *ret = (
unsigned int)v;
275 if (RB_BIGNUM_TYPE_P(val)) {
276 if (BIGNUM_NEGATIVE_P(val))
return NUMERR_NEGATIVE;
277#if SIZEOF_INT < SIZEOF_LONG
279 return NUMERR_TOOLARGE;
282 if (rb_absint_size(val, NULL) >
sizeof(int))
return NUMERR_TOOLARGE;
283 *ret = (
unsigned int)rb_big2ulong((
VALUE)val);
290#define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
296 return FIXNUM_POSITIVE_P(num);
298 else if (RB_BIGNUM_TYPE_P(num)) {
299 return BIGNUM_POSITIVE_P(num);
308 return FIXNUM_NEGATIVE_P(num);
310 else if (RB_BIGNUM_TYPE_P(num)) {
311 return BIGNUM_NEGATIVE_P(num);
317rb_int_positive_p(
VALUE num)
319 return int_pos_p(num);
323rb_int_negative_p(
VALUE num)
325 return int_neg_p(num);
329rb_num_negative_p(
VALUE num)
331 return rb_num_negative_int_p(num);
335num_funcall_op_0(
VALUE x,
VALUE arg,
int recursive)
339 const char *name = rb_id2name(func);
344 else if (name[0] && name[1] ==
'@' && !name[2]) {
353 return rb_funcallv(x, func, 0, 0);
362NORETURN(
static void num_funcall_op_1_recursion(
VALUE x,
ID func,
VALUE y));
367 const char *name = rb_id2name(func);
369 rb_name_error(func,
"%"PRIsVALUE
".%"PRIsVALUE
"(%"PRIsVALUE
")",
379num_funcall_op_1(
VALUE y,
VALUE arg,
int recursive)
384 num_funcall_op_1_recursion(x, func, y);
393 args[0] = (
VALUE)func;
443NORETURN(
static void coerce_failed(
VALUE x,
VALUE y));
453 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" can't be coerced into %"PRIsVALUE,
463 coerce_failed(*x, *y);
467 if (!err &&
NIL_P(ary)) {
482 do_coerce(&x, &y, TRUE);
489 if (do_coerce(&x, &y, FALSE))
497 if (
NIL_P(c)) rb_cmperr_reason(x, y,
"comparator returned nil");
504 VALUE x0 = x, y0 = y;
506 if (!do_coerce(&x, &y, FALSE)) {
507 rb_cmperr_reason(x0, y0,
"coercion was not possible");
510 return ensure_cmp(
rb_funcall(x, func, 1, y), x0, y0);
530 "can't define singleton method \"%"PRIsVALUE
"\" for %"PRIsVALUE,
552 return rb_immutable_obj_clone(argc, argv, x);
555# define num_clone rb_immutable_obj_clone
573num_imaginary(
VALUE num)
575 return rb_complex_new(
INT2FIX(0), num);
591 do_coerce(&zero, &num, TRUE);
593 return num_funcall1(zero,
'-', num);
632 return rb_funcall(num_funcall1(x,
'/', y), rb_intern(
"floor"), 0);
672 VALUE q = num_funcall1(x, id_div, y);
712 do_coerce(&x, &y, TRUE);
714 VALUE z = num_funcall1(x,
'%', y);
717 ((rb_num_negative_int_p(x) &&
718 rb_num_positive_int_p(y)) ||
719 (rb_num_positive_int_p(x) &&
720 rb_num_negative_int_p(y)))) {
780 if (rb_num_negative_int_p(num)) {
781 return num_funcall0(num, idUMinus);
807 return FIXNUM_ZERO_P(num);
810 return rb_bigzero_p(num);
814rb_int_zero_p(
VALUE num)
816 return RBOOL(int_zero_p(num));
840num_nonzero_p(
VALUE num)
842 if (
RTEST(num_funcall0(num, rb_intern(
"zero?")))) {
871 return num_funcall0(num, id_to_i);
883num_positive_p(
VALUE num)
891 else if (RB_BIGNUM_TYPE_P(num)) {
893 return RBOOL(BIGNUM_POSITIVE_P(num) && !rb_bigzero_p(num));
895 return rb_num_compare_with_zero(num, mid);
907num_negative_p(
VALUE num)
909 return RBOOL(rb_num_negative_int_p(num));
917#if SIZEOF_DOUBLE <= SIZEOF_VALUE
918 flt->float_value = d;
922 rb_float_value_type v;
924 flt->float_value = u.v;
954 enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
955 enum {float_dig = DBL_DIG+1};
956 char buf[float_dig + roomof(decimal_mant, CHAR_BIT) + 10];
960 int sign, decpt, digs;
963 static const char minf[] =
"-Infinity";
964 const int pos = (value > 0);
967 else if (isnan(value))
970 p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
972 if ((digs = (
int)(e - p)) >= (
int)
sizeof(buf)) digs = (int)
sizeof(buf) - 1;
973 memcpy(buf, p, digs);
977 memmove(buf + decpt + 1, buf + decpt, digs - decpt);
981 else if (decpt <= DBL_DIG) {
985 rb_str_resize(s, (
len = RSTRING_LEN(s)) + decpt - digs + 2);
986 ptr = RSTRING_PTR(s) +
len;
988 memset(ptr,
'0', decpt - digs);
991 memcpy(ptr,
".0", 2);
997 else if (decpt > -4) {
1001 rb_str_resize(s, (
len = RSTRING_LEN(s)) - decpt + digs);
1002 ptr = RSTRING_PTR(s);
1003 memset(ptr +=
len,
'0', -decpt);
1004 memcpy(ptr -= decpt, buf, digs);
1013 memmove(buf + 2, buf + 1, digs - 1);
1021 rb_str_catf(s,
"e%+03d", decpt - 1);
1049rb_float_uminus(
VALUE flt)
1080 else if (RB_BIGNUM_TYPE_P(y)) {
1111 else if (RB_BIGNUM_TYPE_P(y)) {
1142 else if (RB_BIGNUM_TYPE_P(y)) {
1154double_div_double(
double x,
double y)
1156 if (LIKELY(y != 0.0)) {
1159 else if (x == 0.0) {
1163 double z = signbit(y) ? -1.0 : 1.0;
1164 return x * z * HUGE_VAL;
1173 double ret = double_div_double(num, den);
1201 else if (RB_BIGNUM_TYPE_P(y)) {
1202 den = rb_big2dbl(y);
1211 ret = double_div_double(num, den);
1232 return num_funcall1(x,
'/', y);
1236flodivmod(
double x,
double y,
double *divp,
double *modp)
1242 if (modp) *modp = y;
1243 if (divp) *divp = y;
1247 if ((x == 0.0) || (isinf(y) && !isinf(x)))
1259 if (isinf(x) && !isinf(y))
1262 div = (x - mod) / y;
1263 if (modp && divp) div = round(div);
1269 if (modp) *modp = mod;
1270 if (divp) *divp = div;
1279ruby_float_mod(
double x,
double y)
1282 flodivmod(x, y, 0, &mod);
1323 else if (RB_BIGNUM_TYPE_P(y)) {
1341 return rb_dbl2big(d);
1373 double fy, div, mod;
1374 volatile VALUE a, b;
1379 else if (RB_BIGNUM_TYPE_P(y)) {
1421 else if (RB_BIGNUM_TYPE_P(y)) {
1428 if (dx < 0 && dy != round(dy))
1429 return rb_dbl_complex_new_polar_pi(pow(-dx, dy), dy);
1463 if (RB_BIGNUM_TYPE_P(x)) {
1464 return rb_big_eql(x, y);
1490 if (x == y)
return INT2FIX(0);
1498 if (x == y)
return Qtrue;
1499 result = num_funcall1(y, id_eq, x);
1500 return RBOOL(
RTEST(result));
1523 volatile double a, b;
1526 return rb_integer_float_eq(y, x);
1532 return num_equal(x, y);
1535 return RBOOL(a == b);
1538#define flo_eq rb_float_equal
1539static VALUE rb_dbl_hash(
double d);
1557rb_dbl_hash(
double d)
1559 return ST2FIX(rb_dbl_long_hash(d));
1565 if (isnan(a) || isnan(b))
return Qnil;
1566 if (a == b)
return INT2FIX(0);
1568 if (a < b)
return INT2FIX(-1);
1609 if (isnan(a))
return Qnil;
1611 VALUE rel = rb_integer_float_cmp(y, x);
1620 if (isinf(a) && !UNDEF_P(i =
rb_check_funcall(y, rb_intern(
"infinite?"), 0, 0))) {
1622 int j = rb_cmpint(i, x, y);
1623 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1626 if (a > 0.0)
return INT2FIX(1);
1637 return NUM2INT(ensure_cmp(flo_cmp(x, y), x, y));
1663 VALUE rel = rb_integer_float_cmp(y, x);
1674 return RBOOL(a > b);
1701 VALUE rel = rb_integer_float_cmp(y, x);
1712 return RBOOL(a >= b);
1737 VALUE rel = rb_integer_float_cmp(y, x);
1748 return RBOOL(a < b);
1775 VALUE rel = rb_integer_float_cmp(y, x);
1786 return RBOOL(a <= b);
1813 return RBOOL(a == b);
1818#define flo_eql rb_float_eql
1821rb_float_abs(
VALUE flt)
1840flo_is_nan_p(
VALUE num)
1844 return RBOOL(isnan(value));
1871rb_flo_is_infinite_p(
VALUE num)
1876 return INT2FIX( value < 0 ? -1 : 1 );
1901rb_flo_is_finite_p(
VALUE num)
1905 return RBOOL(isfinite(value));
1909flo_nextafter(
VALUE flo,
double value)
1913 y = nextafter(x, value);
1962flo_next_float(
VALUE vx)
1964 return flo_nextafter(vx, HUGE_VAL);
2003flo_prev_float(
VALUE vx)
2005 return flo_nextafter(vx, -HUGE_VAL);
2009rb_float_floor(
VALUE num,
int ndigits)
2013 if (number == 0.0) {
2019 frexp(number, &binexp);
2020 if (float_round_overflow(ndigits, binexp))
return num;
2021 if (number > 0.0 && float_round_underflow(ndigits, binexp))
2023 if (!ACCURATE_POW10(ndigits)) {
2024 return rb_flo_floor_by_rational(num, ndigits);
2026 f = pow(10, ndigits);
2027 mul = floor(number * f);
2028 res = (mul + 1) / f;
2034 num = dbl2ival(floor(number));
2035 if (ndigits < 0) num = rb_int_floor(num, ndigits);
2041flo_ndigits(
int argc,
VALUE *argv)
2130 int ndigits = flo_ndigits(argc, argv);
2131 return rb_float_floor(num, ndigits);
2215 int ndigits = flo_ndigits(argc, argv);
2216 return rb_float_ceil(num, ndigits);
2220rb_float_ceil(
VALUE num,
int ndigits)
2225 if (number == 0.0) {
2230 frexp(number, &binexp);
2231 if (float_round_overflow(ndigits, binexp))
return num;
2232 if (number < 0.0 && float_round_underflow(ndigits, binexp))
2234 if (!ACCURATE_POW10(ndigits)) {
2235 return rb_flo_ceil_by_rational(num, ndigits);
2237 f = pow(10, ndigits);
2238 f = ceil(number * f) / f;
2242 num = dbl2ival(ceil(number));
2243 if (ndigits < 0) num = rb_int_ceil(num, ndigits);
2249int_round_zero_p(
VALUE num,
int ndigits)
2255 bytes =
sizeof(long);
2257 else if (RB_BIGNUM_TYPE_P(num)) {
2258 bytes = rb_big_size(num);
2263 return (-0.415241 * ndigits - 0.125 > bytes);
2270 if ((z * y - x) * 2 == y) {
2279 return (x + y / 2) / y * y;
2285 return (x + y / 2 - 1) / y * y;
2291 return (
int)rb_int_odd_p(rb_int_idiv(n, f));
2297 return int_pos_p(num);
2303 return int_neg_p(num);
2310rb_int_round(
VALUE num,
int ndigits,
enum ruby_num_rounding_mode mode)
2314 if (int_round_zero_p(num, ndigits)) {
2318 f = int_pow(10, -ndigits);
2323 x = ROUND_CALL(mode, int_round, (x, y));
2331 h = rb_int_idiv(f,
INT2FIX(2));
2332 r = rb_int_modulo(num, f);
2333 n = rb_int_minus(num, r);
2334 r = rb_int_cmp(r, h);
2335 if (FIXNUM_POSITIVE_P(r) ||
2336 (FIXNUM_ZERO_P(r) && ROUND_CALL(mode, int_half_p, (num, n, f)))) {
2337 n = rb_int_plus(n, f);
2343rb_int_floor(
VALUE num,
int ndigits)
2345 VALUE f = int_pow(10, -ndigits);
2349 if (neg) x = -x + y - 1;
2355 bool neg = int_neg_p(num);
2356 if (neg) num = rb_int_minus(rb_int_plus(rb_int_uminus(num), f),
INT2FIX(1));
2357 num = rb_int_mul(rb_int_div(num, f), f);
2358 if (neg) num = rb_int_uminus(num);
2364rb_int_ceil(
VALUE num,
int ndigits)
2366 VALUE f = int_pow(10, -ndigits);
2377 bool neg = int_neg_p(num);
2379 num = rb_int_uminus(num);
2381 num = rb_int_plus(num, rb_int_minus(f,
INT2FIX(1)));
2382 num = rb_int_mul(rb_int_div(num, f), f);
2383 if (neg) num = rb_int_uminus(num);
2389rb_int_truncate(
VALUE num,
int ndigits)
2394 if (int_round_zero_p(num, ndigits))
2396 f = int_pow(10, -ndigits);
2409 m = rb_int_modulo(num, f);
2410 if (int_neg_p(num)) {
2411 return rb_int_plus(num, rb_int_minus(f, m));
2414 return rb_int_minus(num, m);
2476 double number, f, x;
2479 enum ruby_num_rounding_mode mode;
2484 mode = rb_num_get_rounding_option(opt);
2486 if (number == 0.0) {
2490 return rb_int_round(flo_to_i(num), ndigits, mode);
2493 x = ROUND_CALL(mode, round, (number, 1.0));
2496 if (isfinite(number)) {
2498 frexp(number, &binexp);
2499 if (float_round_overflow(ndigits, binexp))
return num;
2500 if (float_round_underflow(ndigits, binexp))
return DBL2NUM(0);
2501 if (!ACCURATE_POW10(ndigits)) {
2502 return rb_flo_round_by_rational(num, ndigits, mode);
2504 f = pow(10, ndigits);
2505 x = ROUND_CALL(mode, round, (number, f));
2512float_round_overflow(
int ndigits,
int binexp)
2514 enum {float_dig = DBL_DIG+2};
2533 if (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1)) {
2540float_round_underflow(
int ndigits,
int binexp)
2542 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
2569 if (f > 0.0) f = floor(f);
2570 if (f < 0.0) f = ceil(f);
2576rb_flo_to_i(
VALUE num)
2578 return flo_to_i(num);
2617flo_truncate(
int argc,
VALUE *argv,
VALUE num)
2620 return flo_ceil(argc, argv, num);
2622 return flo_floor(argc, argv, num);
2642 return flo_floor(argc, argv,
rb_Float(num));
2662 return flo_ceil(argc, argv,
rb_Float(num));
2679 return flo_round(argc, argv,
rb_Float(num));
2694num_truncate(
int argc,
VALUE *argv,
VALUE num)
2696 return flo_truncate(argc, argv,
rb_Float(num));
2700ruby_float_step_size(
double beg,
double end,
double unit,
int excl)
2702 const double epsilon = DBL_EPSILON;
2709 return unit > 0 ? beg <= end : beg >= end;
2711 n= (end - beg)/unit;
2712 err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
2713 if (err>0.5) err=0.5;
2720 d = +((n + 1) * unit) + beg;
2725 else if (beg > end) {
2733 d = +((n + 1) * unit) + beg;
2738 else if (beg > end) {
2747ruby_float_step(
VALUE from,
VALUE to,
VALUE step,
int excl,
int allow_endless)
2752 double end = (allow_endless &&
NIL_P(to)) ? (unit < 0 ? -1 : 1)*HUGE_VAL :
NUM2DBL(to);
2753 double n = ruby_float_step_size(beg, end, unit, excl);
2760 else if (unit == 0) {
2766 for (i=0; i<n; i++) {
2767 double d = i*unit+beg;
2768 if (unit >= 0 ? end < d : d < end) d = end;
2778ruby_num_interval_step_size(
VALUE from,
VALUE to,
VALUE step,
int excl)
2803 if (isinf(n))
return DBL2NUM(n);
2805 return rb_dbl2big(n);
2811 case 0:
return DBL2NUM(HUGE_VAL);
2812 case -1: cmp =
'<';
break;
2824num_step_negative_p(
VALUE num)
2834 else if (RB_BIGNUM_TYPE_P(num)) {
2836 return BIGNUM_NEGATIVE_P(num);
2841 coerce_failed(num,
INT2FIX(0));
2851 argc =
rb_scan_args(argc, argv,
"02:", to, step, &hash);
2858 if (!UNDEF_P(values[0])) {
2859 if (argc > 0) rb_raise(rb_eArgError,
"to is given twice");
2862 if (!UNDEF_P(values[1])) {
2863 if (argc > 1) rb_raise(rb_eArgError,
"step is given twice");
2872num_step_check_fix_args(
int argc,
VALUE *to,
VALUE *step,
VALUE by,
int fix_nil,
int allow_zero_step)
2880 if (argc > 1 &&
NIL_P(*step)) {
2885 rb_raise(rb_eArgError,
"step can't be 0");
2890 desc = num_step_negative_p(*step);
2891 if (fix_nil &&
NIL_P(*to)) {
2898num_step_scan_args(
int argc,
const VALUE *argv,
VALUE *to,
VALUE *step,
int fix_nil,
int allow_zero_step)
2901 argc = num_step_extract_args(argc, argv, to, step, &by);
2902 return num_step_check_fix_args(argc, to, step, by, fix_nil, allow_zero_step);
2912 num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
2914 return ruby_num_interval_step_size(from, to, step, FALSE);
3022 num_step_extract_args(argc, argv, &to, &step, &by);
3030 rb_raise(rb_eArgError,
"step can't be 0");
3034 return rb_arith_seq_new(from,
ID2SYM(rb_frame_this_func()), argc, argv,
3035 num_step_size, from, to, step, FALSE);
3041 desc = num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
3047 inf = isinf(f) && (signbit(f) ? desc : !desc);
3063 for (; i >= end; i += diff)
3067 for (; i <= end; i += diff)
3072 else if (!ruby_float_step(from, to, step, FALSE, FALSE)) {
3080 ID cmp = desc ?
'<' :
'>';
3090out_of_range_float(
char (*pbuf)[24],
VALUE val)
3092 char *
const buf = *pbuf;
3095 snprintf(buf,
sizeof(*pbuf),
"%-.10g",
RFLOAT_VALUE(val));
3096 if ((s = strchr(buf,
' ')) != 0) *s =
'\0';
3100#define FLOAT_OUT_OF_RANGE(val, type) do { \
3102 rb_raise(rb_eRangeError, "float %s out of range of "type, \
3103 out_of_range_float(&buf, (val))); \
3106#define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
3107#define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
3108#define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
3109#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3110 (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \
3112 LONG_MIN_MINUS_ONE < (n))
3119 rb_no_implicit_conversion(val,
"Integer");
3126 && LONG_MIN_MINUS_ONE_IS_LESS_THAN(
RFLOAT_VALUE(val))) {
3130 FLOAT_OUT_OF_RANGE(val,
"integer");
3133 else if (RB_BIGNUM_TYPE_P(val)) {
3134 return rb_big2long(val);
3143rb_num2ulong_internal(
VALUE val,
int *wrap_p)
3147 rb_no_implicit_conversion(val,
"Integer");
3154 return (
unsigned long)l;
3158 if (d < ULONG_MAX_PLUS_ONE && LONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3160 *wrap_p = d <= -1.0;
3162 return (
unsigned long)d;
3163 return (
unsigned long)(long)d;
3166 FLOAT_OUT_OF_RANGE(val,
"integer");
3169 else if (RB_BIGNUM_TYPE_P(val)) {
3171 unsigned long ul = rb_big2ulong(val);
3173 *wrap_p = BIGNUM_NEGATIVE_P(val);
3186 return rb_num2ulong_internal(val, NULL);
3192 rb_raise(
rb_eRangeError,
"integer %"PRIdVALUE
" too %s to convert to 'int'",
3193 num, num < 0 ?
"small" :
"big");
3196#if SIZEOF_INT < SIZEOF_LONG
3200 if ((
long)(
int)num != num) {
3206check_uint(
unsigned long num,
int sign)
3210 if (num < (
unsigned long)INT_MIN)
3211 rb_raise(
rb_eRangeError,
"integer %ld too small to convert to 'unsigned int'", (
long)num);
3216 rb_raise(
rb_eRangeError,
"integer %lu too big to convert to 'unsigned int'", num);
3242 unsigned long num = rb_num2ulong_internal(val, &wrap);
3244 check_uint(num, wrap);
3258 check_uint(num, FIXNUM_NEGATIVE_P(val));
3287NORETURN(
static void rb_out_of_short(
SIGNED_VALUE num));
3291 rb_raise(
rb_eRangeError,
"integer %"PRIdVALUE
" too %s to convert to 'short'",
3292 num, num < 0 ?
"small" :
"big");
3296check_short(
long num)
3298 if ((
long)(
short)num != num) {
3299 rb_out_of_short(num);
3304check_ushort(
unsigned long num,
int sign)
3308 if (num < (
unsigned long)SHRT_MIN)
3309 rb_raise(
rb_eRangeError,
"integer %ld too small to convert to 'unsigned short'", (
long)num);
3313 if (USHRT_MAX < num)
3314 rb_raise(
rb_eRangeError,
"integer %lu too big to convert to 'unsigned short'", num);
3340 unsigned long num = rb_num2ulong_internal(val, &wrap);
3342 check_ushort(num, wrap);
3356 check_ushort(num, FIXNUM_NEGATIVE_P(val));
3369 rb_raise(
rb_eRangeError,
"integer %ld out of range of fixnum", v);
3375#define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
3376#define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
3377#define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
3379#define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
3381#define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3382 (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \
3384 LLONG_MIN_MINUS_ONE < (n))
3390 rb_no_implicit_conversion(val,
"Integer");
3397 if (d < LLONG_MAX_PLUS_ONE && (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d))) {
3401 FLOAT_OUT_OF_RANGE(val,
"long long");
3404 else if (RB_BIGNUM_TYPE_P(val)) {
3405 return rb_big2ll(val);
3408 rb_no_implicit_conversion(val,
"Integer");
3419 rb_no_implicit_conversion(val,
"Integer");
3426 if (d < ULLONG_MAX_PLUS_ONE && LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3432 FLOAT_OUT_OF_RANGE(val,
"unsigned long long");
3435 else if (RB_BIGNUM_TYPE_P(val)) {
3436 return rb_big2ull(val);
3449#ifndef HAVE_UINT128_T
3452rb_uint128_from_bignum_digits_fallback(
rb_uint128_t *result, BDIGIT *digits,
size_t length)
3455 for (
long i = length - 1; i >= 0; i--) {
3457 uint64_t carry = result->parts.low >> (64 - (SIZEOF_BDIGIT * CHAR_BIT));
3458 result->parts.low = (result->parts.low << (SIZEOF_BDIGIT * CHAR_BIT)) | digits[i];
3459 result->parts.high = (result->parts.high << (SIZEOF_BDIGIT * CHAR_BIT)) | carry;
3468 if (value->parts.low == 0) {
3469 value->parts.high = ~value->parts.high + 1;
3472 value->parts.low = ~value->parts.low + 1;
3473 value->parts.high = ~value->parts.high + (value->parts.low == 0 ? 1 : 0);
3479rb_numeric_to_uint128(
VALUE x)
3485 rb_raise(
rb_eRangeError,
"negative integer cannot be converted to unsigned 128-bit integer");
3487#ifdef HAVE_UINT128_T
3488 result.value = (uint128_t)value;
3490 result.parts.low = (uint64_t)value;
3491 result.parts.high = 0;
3495 else if (RB_BIGNUM_TYPE_P(x)) {
3496 if (BIGNUM_NEGATIVE_P(x)) {
3497 rb_raise(
rb_eRangeError,
"negative integer cannot be converted to unsigned 128-bit integer");
3499 size_t length = BIGNUM_LEN(x);
3500#ifdef HAVE_UINT128_T
3501 if (length > roomof(SIZEOF_INT128_T, SIZEOF_BDIGIT)) {
3502 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'unsigned 128-bit integer'");
3504 BDIGIT *digits = BIGNUM_DIGITS(x);
3506 for (
long i = length - 1; i >= 0; i--) {
3507 result.value = (result.value << (SIZEOF_BDIGIT * CHAR_BIT)) | digits[i];
3511 if (length > roomof(16, SIZEOF_BDIGIT)) {
3512 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'unsigned 128-bit integer'");
3514 BDIGIT *digits = BIGNUM_DIGITS(x);
3515 rb_uint128_from_bignum_digits_fallback(&result, digits, length);
3525rb_numeric_to_int128(
VALUE x)
3530#ifdef HAVE_UINT128_T
3531 result.value = (int128_t)value;
3536 result.parts.low = (uint64_t)value;
3537 result.parts.high = UINT64_MAX;
3540 result.parts.low = (uint64_t)value;
3541 result.parts.high = 0;
3546 else if (RB_BIGNUM_TYPE_P(x)) {
3547 size_t length = BIGNUM_LEN(x);
3548#ifdef HAVE_UINT128_T
3549 if (length > roomof(SIZEOF_INT128_T, SIZEOF_BDIGIT)) {
3550 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3552 BDIGIT *digits = BIGNUM_DIGITS(x);
3553 uint128_t unsigned_result = 0;
3554 for (
long i = length - 1; i >= 0; i--) {
3555 unsigned_result = (unsigned_result << (SIZEOF_BDIGIT * CHAR_BIT)) | digits[i];
3557 if (BIGNUM_NEGATIVE_P(x)) {
3560 if (unsigned_result > ((uint128_t)1 << 127)) {
3561 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3563 result.value = -(int128_t)(unsigned_result - 1) - 1;
3567 if (unsigned_result > (((uint128_t)1 << 127) - 1)) {
3568 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3570 result.value = (int128_t)unsigned_result;
3573 if (length > roomof(16, SIZEOF_BDIGIT)) {
3574 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3576 BDIGIT *digits = BIGNUM_DIGITS(x);
3578 rb_uint128_from_bignum_digits_fallback(&unsigned_result, digits, length);
3579 if (BIGNUM_NEGATIVE_P(x)) {
3581 uint64_t max_neg_high = (uint64_t)1 << 63;
3582 if (unsigned_result.parts.high > max_neg_high || (unsigned_result.parts.high == max_neg_high && unsigned_result.parts.low > 0)) {
3583 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3586 rb_uint128_twos_complement_negate(&unsigned_result);
3587 result.parts.low = unsigned_result.parts.low;
3588 result.parts.high = (int64_t)unsigned_result.parts.high;
3593 uint64_t max_pos_high = ((uint64_t)1 << 63) - 1;
3594 if (unsigned_result.parts.high > max_pos_high) {
3595 rb_raise(
rb_eRangeError,
"bignum too big to convert into 'signed 128-bit integer'");
3597 result.parts.low = unsigned_result.parts.low;
3598 result.parts.high = unsigned_result.parts.high;
3611#ifdef HAVE_UINT128_T
3615 return rb_uint128t2big(n.value);
3619 return LONG2FIX((
long)n.parts.low);
3622 VALUE bignum = rb_ull2big(n.parts.low);
3623 if (n.parts.high > 0) {
3624 VALUE high_bignum = rb_ull2big(n.parts.high);
3626 VALUE shifted_value = rb_int_lshift(high_bignum,
INT2FIX(64));
3627 bignum = rb_int_plus(bignum, shifted_value);
3636#ifdef HAVE_UINT128_T
3640 return rb_int128t2big(n.value);
3642 int64_t high = (int64_t)n.parts.high;
3645 return LONG2FIX((
long)n.parts.low);
3651 if (n.parts.low == 0) {
3652 unsigned_value.parts.low = 0;
3653 unsigned_value.parts.high = ~n.parts.high + 1;
3656 unsigned_value.parts.low = ~n.parts.low + 1;
3657 unsigned_value.parts.high = ~n.parts.high + (unsigned_value.parts.low == 0 ? 1 : 0);
3659 VALUE bignum = rb_uint128_to_numeric(unsigned_value);
3660 return rb_int_uminus(bignum);
3667 return rb_uint128_to_numeric(conversion.uint128);
3770rb_int_odd_p(
VALUE num)
3773 return RBOOL(num & 2);
3777 return rb_big_odd_p(num);
3782int_even_p(
VALUE num)
3785 return RBOOL((num & 2) == 0);
3789 return rb_big_even_p(num);
3794rb_int_even_p(
VALUE num)
3796 return int_even_p(num);
3826 return rb_int_equal(rb_int_and(num, mask), mask);
3856 return RBOOL(!int_zero_p(rb_int_and(num, mask)));
3886 return RBOOL(int_zero_p(rb_int_and(num, mask)));
3902rb_int_succ(
VALUE num)
3908 if (RB_BIGNUM_TYPE_P(num)) {
3909 return rb_big_plus(num,
INT2FIX(1));
3911 return num_funcall1(num,
'+',
INT2FIX(1));
3914#define int_succ rb_int_succ
3930rb_int_pred(
VALUE num)
3936 if (RB_BIGNUM_TYPE_P(num)) {
3937 return rb_big_minus(num,
INT2FIX(1));
3939 return num_funcall1(num,
'-',
INT2FIX(1));
3942#define int_pred rb_int_pred
3949 switch (n = rb_enc_codelen(code, enc)) {
3950 case ONIGERR_INVALID_CODE_POINT_VALUE:
3951 rb_raise(
rb_eRangeError,
"invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
3953 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
3958 str = rb_enc_str_new(0, n, enc);
3959 rb_enc_mbcput(code, RSTRING_PTR(str), enc);
3960 if (rb_enc_precise_mbclen(RSTRING_PTR(str),
RSTRING_END(str), enc) != n) {
3961 rb_raise(
rb_eRangeError,
"invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
3992 if (rb_num_to_uint(num, &i) == 0) {
4004 enc = rb_default_internal_encoding();
4020 rb_error_arity(argc, 0, 1);
4022 enc = rb_to_encoding(argv[0]);
4023 if (!enc) enc = rb_ascii8bit_encoding();
4033fix_uminus(
VALUE num)
4039rb_int_uminus(
VALUE num)
4042 return fix_uminus(num);
4046 return rb_big_uminus(num);
4058 char buf[
SIZEOF_VALUE*CHAR_BIT + 1], *
const e = buf +
sizeof buf, *b = e;
4063 if (base < 2 || 36 < base) {
4064 rb_raise(rb_eArgError,
"invalid radix %d", base);
4066#if SIZEOF_LONG < SIZEOF_VOIDP
4067# if SIZEOF_VOIDP == SIZEOF_LONG_LONG
4068 if ((val >= 0 && (x & 0xFFFFFFFF00000000ull)) ||
4069 (val < 0 && (x & 0xFFFFFFFF00000000ull) != 0xFFFFFFFF00000000ull)) {
4070 rb_bug(
"Unnormalized Fixnum value %p", (
void *)x);
4081 u = 1 + (
unsigned long)(-(val + 1));
4094 unsigned long idx = (u % 100) * 2;
4097 b[0] = ruby_decimal_digit_pairs[idx];
4098 b[1] = ruby_decimal_digit_pairs[idx + 1];
4101 unsigned long idx = u * 2;
4103 b[0] = ruby_decimal_digit_pairs[idx];
4104 b[1] = ruby_decimal_digit_pairs[idx + 1];
4107 *--b = (char)(
'0' + u);
4112 *--b = ruby_digitmap[(int)(u % base)];
4113 }
while (u /= base);
4122static VALUE rb_fix_to_s_static[10];
4128 if (i >= 0 && i < 10) {
4129 return rb_fix_to_s_static[i];
4161 return rb_int2str(x, base);
4165rb_int2str(
VALUE x,
int base)
4170 else if (RB_BIGNUM_TYPE_P(x)) {
4171 return rb_big2str(x, base);
4181 return rb_fix_plus_fix(x, y);
4183 else if (RB_BIGNUM_TYPE_P(y)) {
4184 return rb_big_plus(y, x);
4190 return rb_complex_plus(y, x);
4200 return fix_plus(x, y);
4225 return fix_plus(x, y);
4227 else if (RB_BIGNUM_TYPE_P(x)) {
4228 return rb_big_plus(x, y);
4237 return rb_fix_minus_fix(x, y);
4239 else if (RB_BIGNUM_TYPE_P(y)) {
4241 return rb_big_minus(x, y);
4270 return fix_minus(x, y);
4272 else if (RB_BIGNUM_TYPE_P(x)) {
4273 return rb_big_minus(x, y);
4279#define SQRT_LONG_MAX HALF_LONG_MSB
4281#define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
4287 return rb_fix_mul_fix(x, y);
4289 else if (RB_BIGNUM_TYPE_P(y)) {
4294 return rb_big_mul(y, x);
4300 return rb_complex_mul(y, x);
4326 return fix_mul(x, y);
4328 else if (RB_BIGNUM_TYPE_P(x)) {
4329 return rb_big_mul(x, y);
4335accurate_in_double(
long i)
4337#if SIZEOF_LONG * CHAR_BIT > DBL_MANT_DIG
4338 return ((i < 0 ? -i : i) < (1L << DBL_MANT_DIG));
4349 if (!accurate_in_double(iy)) {
4350 return rb_big_fdiv_double(rb_int2big(
FIX2LONG(x)), rb_int2big(iy));
4352 return double_div_double(
FIX2LONG(x), iy);
4354 else if (RB_BIGNUM_TYPE_P(y)) {
4355 return rb_big_fdiv_double(rb_int2big(
FIX2LONG(x)), y);
4366int_accurate_in_double(
VALUE n)
4369 return accurate_in_double(
FIX2LONG(n));
4372#if SIZEOF_LONG * CHAR_BIT <= DBL_MANT_DIG
4374 size_t size = rb_absint_size(n, &nlz);
4375 const size_t mant_size = roomof(DBL_MANT_DIG, CHAR_BIT);
4376 if (size < mant_size)
return true;
4377 if (size > mant_size)
return false;
4378 if ((
size_t)nlz >= (CHAR_BIT * mant_size - DBL_MANT_DIG))
return true;
4387 !(int_accurate_in_double(x) && int_accurate_in_double(y))) {
4388 VALUE gcd = rb_gcd(x, y);
4389 if (!FIXNUM_ZERO_P(gcd) && gcd !=
INT2FIX(1)) {
4390 x = rb_int_idiv(x, gcd);
4391 y = rb_int_idiv(y, gcd);
4395 return fix_fdiv_double(x, y);
4397 else if (RB_BIGNUM_TYPE_P(x)) {
4398 return rb_big_fdiv_double(x, y);
4425 return DBL2NUM(rb_int_fdiv_double(x, y));
4435 return rb_fix_div_fix(x, y);
4437 else if (RB_BIGNUM_TYPE_P(y)) {
4439 return rb_big_div(x, y);
4444 return rb_flo_div_flo(
DBL2NUM(d), y);
4449 v = fix_divide(x, y,
'/');
4450 return flo_floor(0, 0, v);
4456 return rb_rational_reciprocal(y);
4464 return fix_divide(x, y,
'/');
4492 return fix_div(x, y);
4494 else if (RB_BIGNUM_TYPE_P(x)) {
4495 return rb_big_div(x, y);
4503 return fix_divide(x, y, id_div);
4528 return fix_idiv(x, y);
4530 else if (RB_BIGNUM_TYPE_P(x)) {
4531 return rb_big_idiv(x, y);
4533 return num_div(x, y);
4541 return rb_fix_mod_fix(x, y);
4543 else if (RB_BIGNUM_TYPE_P(y)) {
4545 return rb_big_modulo(x, y);
4587 return fix_mod(x, y);
4589 else if (RB_BIGNUM_TYPE_P(x)) {
4590 return rb_big_modulo(x, y);
4592 return num_modulo(x, y);
4623 VALUE z = fix_mod(x, y);
4626 z = fix_minus(z, y);
4629 else if (!RB_BIGNUM_TYPE_P(y)) {
4630 return num_remainder(x, y);
4634 else if (!RB_BIGNUM_TYPE_P(x)) {
4637 return rb_big_remainder(x, y);
4646 rb_fix_divmod_fix(x, y, &div, &mod);
4649 else if (RB_BIGNUM_TYPE_P(y)) {
4651 return rb_big_divmod(x, y);
4656 volatile VALUE a, b;
4698 return fix_divmod(x, y);
4700 else if (RB_BIGNUM_TYPE_P(x)) {
4701 return rb_big_divmod(x, y);
4723int_pow(
long x,
unsigned long y)
4728 if (y == 0)
return INT2FIX(1);
4737 while (y % 2 == 0) {
4738 if (!FIT_SQRT_LONG(x)) {
4745 if (MUL_OVERFLOW_FIXNUM_P(x, z)) {
4756 v = rb_big_pow(rb_int2big(x),
LONG2NUM(y));
4759 if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
4766 return int_pow(x, y);
4777 VALUE y = rb_int_pow(x, minusb);
4797 if (a == 1)
return INT2FIX(1);
4798 if (a == -1)
return INT2FIX(b % 2 ? -1 : 1);
4799 if (b < 0)
return fix_pow_inverted(x, fix_uminus(y));
4800 if (b == 0)
return INT2FIX(1);
4801 if (b == 1)
return x;
4802 if (a == 0)
return INT2FIX(0);
4803 return int_pow(a, b);
4805 else if (RB_BIGNUM_TYPE_P(y)) {
4806 if (a == 1)
return INT2FIX(1);
4807 if (a == -1)
return INT2FIX(int_even_p(y) ? 1 : -1);
4808 if (BIGNUM_NEGATIVE_P(y))
return fix_pow_inverted(x, rb_big_uminus(y));
4809 if (a == 0)
return INT2FIX(0);
4811 return rb_big_pow(x, y);
4815 if (dy == 0.0)
return DBL2NUM(1.0);
4817 return DBL2NUM(dy < 0 ? HUGE_VAL : 0.0);
4819 if (a == 1)
return DBL2NUM(1.0);
4820 if (a < 0 && dy != round(dy))
4821 return rb_dbl_complex_new_polar_pi(pow(-(
double)a, dy), dy);
4822 return DBL2NUM(pow((
double)a, dy));
4878 return fix_pow(x, y);
4880 else if (RB_BIGNUM_TYPE_P(x)) {
4881 return rb_big_pow(x, y);
4889 VALUE z = rb_int_pow(x, y);
4890 if (!
NIL_P(z))
return z;
4895 return rb_complex_pow(x, y);
4897 return rb_rational_pow(x, y);
4907 if (x == y)
return Qtrue;
4909 else if (RB_BIGNUM_TYPE_P(y)) {
4910 return rb_big_eq(y, x);
4913 return rb_integer_float_eq(x, y);
4916 return num_equal(x, y);
4936 return fix_equal(x, y);
4938 else if (RB_BIGNUM_TYPE_P(x)) {
4939 return rb_big_eq(x, y);
4947 if (x == y)
return INT2FIX(0);
4952 else if (RB_BIGNUM_TYPE_P(y)) {
4953 VALUE cmp = rb_big_cmp(y, x);
4961 return rb_integer_float_cmp(x, y);
4999 return fix_cmp(x, y);
5001 else if (RB_BIGNUM_TYPE_P(x)) {
5002 return rb_big_cmp(x, y);
5015 else if (RB_BIGNUM_TYPE_P(y)) {
5016 return RBOOL(rb_big_cmp(y, x) ==
INT2FIX(-1));
5019 return RBOOL(rb_integer_float_cmp(x, y) ==
INT2FIX(1));
5047 return fix_gt(x, y);
5049 else if (RB_BIGNUM_TYPE_P(x)) {
5050 return rb_big_gt(x, y);
5061 else if (RB_BIGNUM_TYPE_P(y)) {
5062 return RBOOL(rb_big_cmp(y, x) !=
INT2FIX(+1));
5065 VALUE rel = rb_integer_float_cmp(x, y);
5094 return fix_ge(x, y);
5096 else if (RB_BIGNUM_TYPE_P(x)) {
5097 return rb_big_ge(x, y);
5108 else if (RB_BIGNUM_TYPE_P(y)) {
5109 return RBOOL(rb_big_cmp(y, x) ==
INT2FIX(+1));
5112 return RBOOL(rb_integer_float_cmp(x, y) ==
INT2FIX(-1));
5138 return fix_lt(x, y);
5140 else if (RB_BIGNUM_TYPE_P(x)) {
5141 return rb_big_lt(x, y);
5152 else if (RB_BIGNUM_TYPE_P(y)) {
5153 return RBOOL(rb_big_cmp(y, x) !=
INT2FIX(-1));
5156 VALUE rel = rb_integer_float_cmp(x, y);
5185 return fix_le(x, y);
5187 else if (RB_BIGNUM_TYPE_P(x)) {
5188 return rb_big_le(x, y);
5200rb_int_comp(
VALUE num)
5203 return fix_comp(num);
5205 else if (RB_BIGNUM_TYPE_P(num)) {
5206 return rb_big_comp(num);
5212num_funcall_bit_1(
VALUE y,
VALUE arg,
int recursive)
5217 num_funcall_op_1_recursion(x, func, y);
5227 args[0] = (
VALUE)func;
5230 do_coerce(&args[1], &args[2], TRUE);
5232 args[2], args[1], (
VALUE)args);
5235 coerce_failed(x, y);
5248 if (RB_BIGNUM_TYPE_P(y)) {
5249 return rb_big_and(y, x);
5274 return fix_and(x, y);
5276 else if (RB_BIGNUM_TYPE_P(x)) {
5277 return rb_big_and(x, y);
5290 if (RB_BIGNUM_TYPE_P(y)) {
5291 return rb_big_or(y, x);
5316 return fix_or(x, y);
5318 else if (RB_BIGNUM_TYPE_P(x)) {
5319 return rb_big_or(x, y);
5332 if (RB_BIGNUM_TYPE_P(y)) {
5333 return rb_big_xor(y, x);
5358 return fix_xor(x, y);
5360 else if (RB_BIGNUM_TYPE_P(x)) {
5361 return rb_big_xor(x, y);
5374 return rb_big_lshift(rb_int2big(val), y);
5377 return fix_rshift(val, (
unsigned long)-width);
5378 return fix_lshift(val, width);
5382fix_lshift(
long val,
unsigned long width)
5384 if (width > (SIZEOF_LONG*CHAR_BIT-1)
5385 || ((
unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) {
5386 return rb_big_lshift(rb_int2big(val),
ULONG2NUM(width));
5413 return rb_fix_lshift(x, y);
5415 else if (RB_BIGNUM_TYPE_P(x)) {
5416 return rb_big_lshift(x, y);
5429 return rb_big_rshift(rb_int2big(val), y);
5431 if (i == 0)
return x;
5433 return fix_lshift(val, (
unsigned long)-i);
5434 return fix_rshift(val, i);
5438fix_rshift(
long val,
unsigned long i)
5440 if (i >=
sizeof(
long)*CHAR_BIT-1) {
5441 if (val < 0)
return INT2FIX(-1);
5444 val = RSHIFT(val, i);
5469 return rb_fix_rshift(x, y);
5471 else if (RB_BIGNUM_TYPE_P(x)) {
5472 return rb_big_rshift(x, y);
5485 idx = rb_big_norm(idx);
5487 if (!BIGNUM_SIGN(idx) || val >= 0)
5495 if (SIZEOF_LONG*CHAR_BIT-1 <= i) {
5496 if (val < 0)
return INT2FIX(1);
5518 return rb_cmpint(r, a, b);
5531 return rb_big_aref2(num, beg,
len);
5534 num = rb_int_rshift(num, beg);
5536 return rb_int_and(num, mask);
5549 if (!
RTEST(num_negative_p(end))) {
5550 if (!excl) end = rb_int_plus(end,
INT2FIX(1));
5551 VALUE mask = generate_mask(end);
5552 if (int_zero_p(rb_int_and(num, mask))) {
5556 rb_raise(rb_eArgError,
"The beginless range for Integer#[] results in infinity");
5564 int cmp = compare_indexes(beg, end);
5565 if (!
NIL_P(end) && cmp < 0) {
5566 VALUE len = rb_int_minus(end, beg);
5568 return int_aref2(num, beg,
len);
5570 else if (cmp == 0) {
5575 return rb_int_rshift(num, beg);
5580 return rb_fix_aref(num, arg);
5582 else if (RB_BIGNUM_TYPE_P(num)) {
5583 return rb_big_aref(num, arg);
5628int_aref(
int const argc,
VALUE *
const argv,
VALUE const num)
5632 return int_aref2(num, argv[0], argv[1]);
5634 return int_aref1(num, argv[0]);
5664 else if (RB_BIGNUM_TYPE_P(num)) {
5665 val = rb_big2dbl(num);
5685rb_int_abs(
VALUE num)
5688 return fix_abs(num);
5690 else if (RB_BIGNUM_TYPE_P(num)) {
5691 return rb_big_abs(num);
5703rb_int_size(
VALUE num)
5706 return fix_size(num);
5708 else if (RB_BIGNUM_TYPE_P(num)) {
5709 return rb_big_size_m(num);
5715rb_fix_bit_length(
VALUE fix)
5724rb_int_bit_length(
VALUE num)
5727 return rb_fix_bit_length(num);
5729 else if (RB_BIGNUM_TYPE_P(num)) {
5730 return rb_big_bit_length(num);
5736rb_fix_digits(
VALUE fix,
long base)
5744 rb_raise(rb_eArgError,
"invalid radix %ld", base);
5747 return rb_ary_new_from_args(1,
INT2FIX(0));
5763 VALUE digits, bases;
5767 if (RB_BIGNUM_TYPE_P(base))
5768 base = rb_big_norm(base);
5771 rb_raise(rb_eArgError,
"invalid radix %ld",
FIX2LONG(base));
5772 else if (RB_BIGNUM_TYPE_P(base) && BIGNUM_NEGATIVE_P(base))
5773 rb_raise(rb_eArgError,
"negative radix");
5776 return rb_fix_digits(num,
FIX2LONG(base));
5779 return rb_ary_new_from_args(1, num);
5781 if (int_lt(rb_int_div(rb_int_bit_length(num), rb_int_bit_length(base)),
INT2FIX(50))) {
5784 VALUE qr = rb_int_divmod(num, base);
5792 for (
VALUE b = base; int_le(b, num) ==
Qtrue; b = rb_int_mul(b, b)) {
5795 digits = rb_ary_new_from_args(1, num);
5799 for(i = last_idx; i >= 0; i--) {
5801 VALUE divmod = rb_int_divmod(n, b);
5829rb_int_digits(
int argc,
VALUE *argv,
VALUE num)
5834 if (rb_num_negative_p(num))
5840 rb_raise(
rb_eTypeError,
"wrong argument type %s (expected Integer)",
5842 if (RB_BIGNUM_TYPE_P(base_value))
5843 return rb_int_digits_bigbase(num, base_value);
5847 rb_raise(rb_eArgError,
"negative radix");
5849 rb_raise(rb_eArgError,
"invalid radix %ld", base);
5855 return rb_fix_digits(num, base);
5856 else if (RB_BIGNUM_TYPE_P(num))
5857 return rb_int_digits_bigbase(num,
LONG2FIX(base));
5896 for (i =
FIX2LONG(from); i <= end; i++) {
5907 ensure_cmp(c, i, to);
5946 for (i=
FIX2LONG(from); i >= end; i--) {
5957 ensure_cmp(c, i, to);
5965 return int_neg_p(num) ?
INT2FIX(0) : num;
6024 if (!
rb_scan_args(argc, argv,
"01:", &nd, &opt))
return num;
6026 mode = rb_num_get_rounding_option(opt);
6030 return rb_int_round(num, ndigits, mode);
6099 return rb_int_floor(num, ndigits);
6167 return rb_int_ceil(num, ndigits);
6194int_truncate(
int argc,
VALUE* argv,
VALUE num)
6203 return rb_int_truncate(num, ndigits);
6206#define DEFINE_INT_SQRT(rettype, prefix, argtype) \
6208prefix##_isqrt(argtype n) \
6210 if (!argtype##_IN_DOUBLE_P(n)) { \
6211 unsigned int b = bit_length(n); \
6213 rettype x = (rettype)(n >> (b/2+1)); \
6214 x |= ((rettype)1LU << (b-1)/2); \
6215 while ((t = n/x) < (argtype)x) x = (rettype)((x + t) >> 1); \
6218 rettype x = (rettype)sqrt(argtype##_TO_DOUBLE(n)); \
6221 if (x * x > n) x--; \
6225#if SIZEOF_LONG*CHAR_BIT > DBL_MANT_DIG
6226# define RB_ULONG_IN_DOUBLE_P(n) ((n) < (1UL << DBL_MANT_DIG))
6228# define RB_ULONG_IN_DOUBLE_P(n) 1
6230#define RB_ULONG_TO_DOUBLE(n) (double)(n)
6231#define RB_ULONG unsigned long
6232DEFINE_INT_SQRT(
unsigned long, rb_ulong, RB_ULONG)
6234#if 2*SIZEOF_BDIGIT > SIZEOF_LONG
6235# if 2*SIZEOF_BDIGIT*CHAR_BIT > DBL_MANT_DIG
6236# define BDIGIT_DBL_IN_DOUBLE_P(n) ((n) < ((BDIGIT_DBL)1UL << DBL_MANT_DIG))
6238# define BDIGIT_DBL_IN_DOUBLE_P(n) 1
6240# ifdef ULL_TO_DOUBLE
6241# define BDIGIT_DBL_TO_DOUBLE(n) ULL_TO_DOUBLE(n)
6243# define BDIGIT_DBL_TO_DOUBLE(n) (double)(n)
6245DEFINE_INT_SQRT(BDIGIT, rb_bdigit_dbl, BDIGIT_DBL)
6248#define domain_error(msg) \
6249 rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
6286 unsigned long n, sq;
6289 if (FIXNUM_NEGATIVE_P(num)) {
6290 domain_error(
"isqrt");
6293 sq = rb_ulong_isqrt(n);
6299 domain_error(
"isqrt");
6301 biglen = BIGNUM_LEN(num);
6302 if (biglen == 0)
return INT2FIX(0);
6303#if SIZEOF_BDIGIT <= SIZEOF_LONG
6306 n = BIGNUM_DIGITS(num)[0];
6307 sq = rb_ulong_isqrt(n);
6311 return rb_big_isqrt(num);
6334 return rb_check_integer_type(num);
6614#define fix_to_s_static(n) do { \
6615 VALUE lit = rb_fstring_literal(#n); \
6616 rb_fix_to_s_static[n] = lit; \
6617 rb_vm_register_global_object(lit); \
6632#undef fix_to_s_static
6757#undef rb_float_value
6761 return rb_float_value_inline(v);
6766rb_float_new(
double d)
6768 return rb_float_new_inline(d);
6771#include "numeric.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#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_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 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 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 ISALNUM
Old name of rb_isalnum.
#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_cObject
Object class.
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.
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.
Defines RBIMPL_HAS_BUILTIN.
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.
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.