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#ifndef USE_RB_INFINITY
78#elif !defined(WORDS_BIGENDIAN)
85#elif !defined(WORDS_BIGENDIAN)
99 x = f + (x - f >= 0.5);
103 x = f - (f - x >= 0.5);
110round_half_up(
double x,
double s)
112 double f, xs = x * s;
115 if (s == 1.0)
return f;
117 if ((
double)((f + 0.5) / s) <= x) f += 1;
121 if ((
double)((f - 0.5) / s) >= x) f -= 1;
128round_half_down(
double x,
double s)
130 double f, xs = x * s;
134 if ((
double)((f - 0.5) / s) >= x) f -= 1;
138 if ((
double)((f + 0.5) / s) <= x) f += 1;
145round_half_even(
double x,
double s)
147 double f, d, xs = x * s;
154 else if (d == 0.5 || ((
double)((f + 0.5) / s) <= x))
165 else if (d == 0.5 || ((
double)((f - 0.5) / s) >= x))
174static VALUE fix_lshift(
long,
unsigned long);
175static VALUE fix_rshift(
long,
unsigned long);
176static VALUE int_pow(
long x,
unsigned long y);
177static VALUE rb_int_floor(
VALUE num,
int ndigits);
178static VALUE rb_int_ceil(
VALUE num,
int ndigits);
180static int float_round_overflow(
int ndigits,
int binexp);
181static int float_round_underflow(
int ndigits,
int binexp);
185#define id_divmod idDivmod
186#define id_to_i idTo_i
197static ID id_to, id_by;
205enum ruby_num_rounding_mode
206rb_num_get_rounding_option(
VALUE opts)
208 static ID round_kwds[1];
214 if (!round_kwds[0]) {
217 if (!
rb_get_kwargs(opts, round_kwds, 0, 1, &rounding))
goto noopt;
221 else if (
NIL_P(rounding)) {
226 if (
NIL_P(str))
goto invalid;
233 return RUBY_NUM_ROUND_HALF_UP;
237 return RUBY_NUM_ROUND_HALF_EVEN;
238 if (strncasecmp(s,
"down", 4) == 0)
239 return RUBY_NUM_ROUND_HALF_DOWN;
246 return RUBY_NUM_ROUND_DEFAULT;
251rb_num_to_uint(
VALUE val,
unsigned int *ret)
254#define NUMERR_NEGATIVE 2
255#define NUMERR_TOOLARGE 3
258#if SIZEOF_INT < SIZEOF_LONG
259 if (v > (
long)UINT_MAX)
return NUMERR_TOOLARGE;
261 if (v < 0)
return NUMERR_NEGATIVE;
262 *ret = (
unsigned int)v;
266 if (RB_BIGNUM_TYPE_P(val)) {
267 if (BIGNUM_NEGATIVE_P(val))
return NUMERR_NEGATIVE;
268#if SIZEOF_INT < SIZEOF_LONG
270 return NUMERR_TOOLARGE;
273 if (rb_absint_size(val, NULL) >
sizeof(int))
return NUMERR_TOOLARGE;
274 *ret = (
unsigned int)rb_big2ulong((
VALUE)val);
281#define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
287 return FIXNUM_POSITIVE_P(num);
289 else if (RB_BIGNUM_TYPE_P(num)) {
290 return BIGNUM_POSITIVE_P(num);
299 return FIXNUM_NEGATIVE_P(num);
301 else if (RB_BIGNUM_TYPE_P(num)) {
302 return BIGNUM_NEGATIVE_P(num);
308rb_int_positive_p(
VALUE num)
310 return int_pos_p(num);
314rb_int_negative_p(
VALUE num)
316 return int_neg_p(num);
320rb_num_negative_p(
VALUE num)
322 return rb_num_negative_int_p(num);
326num_funcall_op_0(
VALUE x,
VALUE arg,
int recursive)
335 else if (name[0] && name[1] ==
'@' && !name[2]) {
344 return rb_funcallv(x, func, 0, 0);
353NORETURN(
static void num_funcall_op_1_recursion(
VALUE x,
ID func,
VALUE y));
360 rb_name_error(func,
"%"PRIsVALUE
".%"PRIsVALUE
"(%"PRIsVALUE
")",
370num_funcall_op_1(
VALUE y,
VALUE arg,
int recursive)
375 num_funcall_op_1_recursion(x, func, y);
384 args[0] = (
VALUE)func;
428 return rb_assoc_new(y, x);
431 return rb_assoc_new(y, x);
434NORETURN(
static void coerce_failed(
VALUE x,
VALUE y));
454 coerce_failed(*x, *y);
458 if (!err &&
NIL_P(ary)) {
473 do_coerce(&x, &y, TRUE);
480 if (do_coerce(&x, &y, FALSE))
488 if (
NIL_P(c)) rb_cmperr(x, y);
495 VALUE x0 = x, y0 = y;
497 if (!do_coerce(&x, &y, FALSE)) {
501 return ensure_cmp(
rb_funcall(x, func, 1, y), x0, y0);
521 "can't define singleton method \"%"PRIsVALUE
"\" for %"PRIsVALUE,
543 return rb_immutable_obj_clone(argc, argv, x);
546# define num_clone rb_immutable_obj_clone
565# define num_dup num_uplus
597num_imaginary(
VALUE num)
599 return rb_complex_new(
INT2FIX(0), num);
615 do_coerce(&zero, &num, TRUE);
617 return num_funcall1(zero,
'-', num);
698 VALUE q = num_funcall1(x, id_div, y);
737 VALUE z = num_funcall1(x,
'%', y);
740 ((rb_num_negative_int_p(x) &&
741 rb_num_positive_int_p(y)) ||
742 (rb_num_positive_int_p(x) &&
743 rb_num_negative_int_p(y)))) {
785 return rb_assoc_new(num_div(x, y), num_modulo(x, y));
805 if (rb_num_negative_int_p(num)) {
806 return num_funcall0(num, idUMinus);
832 return FIXNUM_ZERO_P(num);
834 assert(RB_BIGNUM_TYPE_P(num));
835 return rb_bigzero_p(num);
839rb_int_zero_p(
VALUE num)
841 return RBOOL(int_zero_p(num));
863num_nonzero_p(
VALUE num)
893 return num_funcall0(num, id_to_i);
905num_positive_p(
VALUE num)
913 else if (RB_BIGNUM_TYPE_P(num)) {
915 return RBOOL(BIGNUM_POSITIVE_P(num) && !rb_bigzero_p(num));
917 return rb_num_compare_with_zero(num, mid);
929num_negative_p(
VALUE num)
931 return RBOOL(rb_num_negative_int_p(num));
1018#if SIZEOF_DOUBLE <= SIZEOF_VALUE
1019 flt->float_value = d;
1023 rb_float_value_type v;
1025 flt->float_value = u.v;
1056 enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
1057 enum {float_dig = DBL_DIG+1};
1058 char buf[float_dig + roomof(decimal_mant, CHAR_BIT) + 10];
1062 int sign, decpt, digs;
1065 static const char minf[] =
"-Infinity";
1066 const int pos = (value > 0);
1069 else if (isnan(value))
1072 p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
1074 if ((digs = (
int)(e - p)) >= (
int)
sizeof(buf)) digs = (int)
sizeof(buf) - 1;
1075 memcpy(buf, p, digs);
1079 memmove(buf + decpt + 1, buf + decpt, digs - decpt);
1083 else if (decpt <= DBL_DIG) {
1090 memset(ptr,
'0', decpt - digs);
1091 ptr += decpt - digs;
1093 memcpy(ptr,
".0", 2);
1099 else if (decpt > -4) {
1105 memset(ptr += len,
'0', -decpt);
1106 memcpy(ptr -= decpt, buf, digs);
1115 memmove(buf + 2, buf + 1, digs - 1);
1147 return rb_assoc_new(
rb_Float(y), x);
1150MJIT_FUNC_EXPORTED
VALUE
1151rb_float_uminus(
VALUE flt)
1176 else if (RB_BIGNUM_TYPE_P(y)) {
1207 else if (RB_BIGNUM_TYPE_P(y)) {
1237 else if (RB_BIGNUM_TYPE_P(y)) {
1249double_div_double(
double x,
double y)
1251 if (LIKELY(y != 0.0)) {
1254 else if (x == 0.0) {
1258 double z = signbit(y) ? -1.0 : 1.0;
1259 return x * z * HUGE_VAL;
1263MJIT_FUNC_EXPORTED
VALUE
1268 double ret = double_div_double(num, den);
1296 else if (RB_BIGNUM_TYPE_P(y)) {
1297 den = rb_big2dbl(y);
1306 ret = double_div_double(num, den);
1329 return num_funcall1(x,
'/', y);
1333flodivmod(
double x,
double y,
double *divp,
double *modp)
1339 if (modp) *modp = y;
1340 if (divp) *divp = y;
1344 if ((x == 0.0) || (isinf(y) && !isinf(x)))
1356 if (isinf(x) && !isinf(y))
1359 div = (x - mod) / y;
1360 if (modp && divp) div = round(div);
1366 if (modp) *modp = mod;
1367 if (divp) *divp = div;
1375MJIT_FUNC_EXPORTED
double
1376ruby_float_mod(
double x,
double y)
1379 flodivmod(x, y, 0, &mod);
1422 else if (RB_BIGNUM_TYPE_P(y)) {
1440 return rb_dbl2big(d);
1472 double fy, div, mod;
1473 volatile VALUE a, b;
1478 else if (RB_BIGNUM_TYPE_P(y)) {
1490 return rb_assoc_new(a, b);
1520 else if (RB_BIGNUM_TYPE_P(y)) {
1527 if (dx < 0 && dy != round(dy))
1528 return rb_dbl_complex_new_polar_pi(pow(-dx, dy), dy);
1562 if (RB_BIGNUM_TYPE_P(x)) {
1563 return rb_big_eql(x, y);
1582 if (x == y)
return INT2FIX(0);
1590 if (x == y)
return Qtrue;
1591 result = num_funcall1(y, id_eq, x);
1592 return RBOOL(
RTEST(result));
1612MJIT_FUNC_EXPORTED
VALUE
1615 volatile double a, b;
1618 return rb_integer_float_eq(y, x);
1622#if MSC_VERSION_BEFORE(1300)
1623 if (isnan(b))
return Qfalse;
1627 return num_equal(x, y);
1630#if MSC_VERSION_BEFORE(1300)
1631 if (isnan(a))
return Qfalse;
1633 return RBOOL(a == b);
1636#define flo_eq rb_float_equal
1637static VALUE rb_dbl_hash(
double d);
1655rb_dbl_hash(
double d)
1657 return ST2FIX(rb_dbl_long_hash(d));
1663 if (isnan(a) || isnan(b))
return Qnil;
1664 if (a == b)
return INT2FIX(0);
1666 if (a < b)
return INT2FIX(-1);
1705 if (isnan(a))
return Qnil;
1707 VALUE rel = rb_integer_float_cmp(y, x);
1718 int j = rb_cmpint(i, x, y);
1719 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1722 if (a > 0.0)
return INT2FIX(1);
1730MJIT_FUNC_EXPORTED
int
1733 return NUM2INT(ensure_cmp(flo_cmp(x, y), x, y));
1758 VALUE rel = rb_integer_float_cmp(y, x);
1765#if MSC_VERSION_BEFORE(1300)
1766 if (isnan(b))
return Qfalse;
1772#if MSC_VERSION_BEFORE(1300)
1773 if (isnan(a))
return Qfalse;
1775 return RBOOL(a > b);
1801 VALUE rel = rb_integer_float_cmp(y, x);
1808#if MSC_VERSION_BEFORE(1300)
1809 if (isnan(b))
return Qfalse;
1815#if MSC_VERSION_BEFORE(1300)
1816 if (isnan(a))
return Qfalse;
1818 return RBOOL(a >= b);
1843 VALUE rel = rb_integer_float_cmp(y, x);
1850#if MSC_VERSION_BEFORE(1300)
1851 if (isnan(b))
return Qfalse;
1857#if MSC_VERSION_BEFORE(1300)
1858 if (isnan(a))
return Qfalse;
1860 return RBOOL(a < b);
1886 VALUE rel = rb_integer_float_cmp(y, x);
1893#if MSC_VERSION_BEFORE(1300)
1894 if (isnan(b))
return Qfalse;
1900#if MSC_VERSION_BEFORE(1300)
1901 if (isnan(a))
return Qfalse;
1903 return RBOOL(a <= b);
1924MJIT_FUNC_EXPORTED
VALUE
1930#if MSC_VERSION_BEFORE(1300)
1931 if (isnan(a) || isnan(b))
return Qfalse;
1933 return RBOOL(a == b);
1938#define flo_eql rb_float_eql
1940MJIT_FUNC_EXPORTED
VALUE
1941rb_float_abs(
VALUE flt)
1960flo_is_nan_p(
VALUE num)
1964 return RBOOL(isnan(value));
1991rb_flo_is_infinite_p(
VALUE num)
1996 return INT2FIX( value < 0 ? -1 : 1 );
2021rb_flo_is_finite_p(
VALUE num)
2025 return RBOOL(isfinite(value));
2029flo_nextafter(
VALUE flo,
double value)
2033 y = nextafter(x, value);
2082flo_next_float(
VALUE vx)
2084 return flo_nextafter(vx, HUGE_VAL);
2123flo_prev_float(
VALUE vx)
2125 return flo_nextafter(vx, -HUGE_VAL);
2129rb_float_floor(
VALUE num,
int ndigits)
2133 if (number == 0.0) {
2139 frexp(number, &binexp);
2140 if (float_round_overflow(ndigits, binexp))
return num;
2141 if (number > 0.0 && float_round_underflow(ndigits, binexp))
2143 f = pow(10, ndigits);
2144 mul = floor(number * f);
2145 res = (mul + 1) / f;
2151 num = dbl2ival(floor(number));
2152 if (ndigits < 0) num = rb_int_floor(num, ndigits);
2158flo_ndigits(
int argc,
VALUE *argv)
2205 int ndigits = flo_ndigits(argc, argv);
2206 return rb_float_floor(num, ndigits);
2248 int ndigits = flo_ndigits(argc, argv);
2249 return rb_float_ceil(num, ndigits);
2253rb_float_ceil(
VALUE num,
int ndigits)
2258 if (number == 0.0) {
2263 frexp(number, &binexp);
2264 if (float_round_overflow(ndigits, binexp))
return num;
2265 if (number < 0.0 && float_round_underflow(ndigits, binexp))
2267 f = pow(10, ndigits);
2268 f = ceil(number * f) / f;
2272 num = dbl2ival(ceil(number));
2273 if (ndigits < 0) num = rb_int_ceil(num, ndigits);
2279int_round_zero_p(
VALUE num,
int ndigits)
2285 bytes =
sizeof(long);
2287 else if (RB_BIGNUM_TYPE_P(num)) {
2288 bytes = rb_big_size(num);
2293 return (-0.415241 * ndigits - 0.125 > bytes);
2300 if ((z * y - x) * 2 == y) {
2309 return (x + y / 2) / y * y;
2315 return (x + y / 2 - 1) / y * y;
2321 return (
int)rb_int_odd_p(rb_int_idiv(n, f));
2327 return int_pos_p(num);
2333 return int_neg_p(num);
2340rb_int_round(
VALUE num,
int ndigits,
enum ruby_num_rounding_mode mode)
2344 if (int_round_zero_p(num, ndigits)) {
2348 f = int_pow(10, -ndigits);
2353 x = ROUND_CALL(mode, int_round, (x, y));
2361 h = rb_int_idiv(f,
INT2FIX(2));
2362 r = rb_int_modulo(num, f);
2363 n = rb_int_minus(num, r);
2364 r = rb_int_cmp(r, h);
2365 if (FIXNUM_POSITIVE_P(r) ||
2366 (FIXNUM_ZERO_P(r) && ROUND_CALL(mode, int_half_p, (num, n, f)))) {
2367 n = rb_int_plus(n, f);
2373rb_int_floor(
VALUE num,
int ndigits)
2377 if (int_round_zero_p(num, ndigits))
2379 f = int_pow(10, -ndigits);
2383 if (neg) x = -x + y - 1;
2392 return rb_int_minus(num, rb_int_modulo(num, f));
2396rb_int_ceil(
VALUE num,
int ndigits)
2400 if (int_round_zero_p(num, ndigits))
2402 f = int_pow(10, -ndigits);
2416 return rb_int_plus(num, rb_int_minus(f, rb_int_modulo(num, f)));
2420rb_int_truncate(
VALUE num,
int ndigits)
2425 if (int_round_zero_p(num, ndigits))
2427 f = int_pow(10, -ndigits);
2440 m = rb_int_modulo(num, f);
2441 if (int_neg_p(num)) {
2442 return rb_int_plus(num, rb_int_minus(f, m));
2445 return rb_int_minus(num, m);
2507 double number, f, x;
2510 enum ruby_num_rounding_mode mode;
2515 mode = rb_num_get_rounding_option(opt);
2517 if (number == 0.0) {
2521 return rb_int_round(flo_to_i(num), ndigits, mode);
2524 x = ROUND_CALL(mode, round, (number, 1.0));
2527 if (isfinite(number)) {
2529 frexp(number, &binexp);
2530 if (float_round_overflow(ndigits, binexp))
return num;
2531 if (float_round_underflow(ndigits, binexp))
return DBL2NUM(0);
2534 return rb_flo_round_by_rational(argc, argv, num);
2536 f = pow(10, ndigits);
2537 x = ROUND_CALL(mode, round, (number, f));
2544float_round_overflow(
int ndigits,
int binexp)
2546 enum {float_dig = DBL_DIG+2};
2565 if (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1)) {
2572float_round_underflow(
int ndigits,
int binexp)
2574 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
2602 if (f > 0.0) f = floor(f);
2603 if (f < 0.0) f = ceil(f);
2644flo_truncate(
int argc,
VALUE *argv,
VALUE num)
2647 return flo_ceil(argc, argv, num);
2649 return flo_floor(argc, argv, num);
2666 return flo_floor(argc, argv,
rb_Float(num));
2683 return flo_ceil(argc, argv,
rb_Float(num));
2700 return flo_round(argc, argv,
rb_Float(num));
2715num_truncate(
int argc,
VALUE *argv,
VALUE num)
2717 return flo_truncate(argc, argv,
rb_Float(num));
2721ruby_float_step_size(
double beg,
double end,
double unit,
int excl)
2723 const double epsilon = DBL_EPSILON;
2730 return unit > 0 ? beg <= end : beg >= end;
2732 n= (end - beg)/unit;
2733 err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
2734 if (err>0.5) err=0.5;
2741 d = +((n + 1) * unit) + beg;
2746 else if (beg > end) {
2754 d = +((n + 1) * unit) + beg;
2759 else if (beg > end) {
2768ruby_float_step(
VALUE from,
VALUE to,
VALUE step,
int excl,
int allow_endless)
2773 double end = (allow_endless &&
NIL_P(to)) ? (unit < 0 ? -1 : 1)*HUGE_VAL :
NUM2DBL(to);
2774 double n = ruby_float_step_size(beg, end, unit, excl);
2781 else if (unit == 0) {
2787 for (i=0; i<n; i++) {
2788 double d = i*unit+beg;
2789 if (unit >= 0 ? end < d : d < end) d = end;
2799ruby_num_interval_step_size(
VALUE from,
VALUE to,
VALUE step,
int excl)
2824 if (isinf(n))
return DBL2NUM(n);
2826 return rb_dbl2big(n);
2832 case 0:
return DBL2NUM(HUGE_VAL);
2833 case -1: cmp =
'<';
break;
2845num_step_negative_p(
VALUE num)
2855 else if (RB_BIGNUM_TYPE_P(num)) {
2857 return BIGNUM_NEGATIVE_P(num);
2862 coerce_failed(num,
INT2FIX(0));
2872 argc =
rb_scan_args(argc, argv,
"02:", to, step, &hash);
2879 if (!UNDEF_P(values[0])) {
2883 if (!UNDEF_P(values[1])) {
2893num_step_check_fix_args(
int argc,
VALUE *to,
VALUE *step,
VALUE by,
int fix_nil,
int allow_zero_step)
2901 if (argc > 1 &&
NIL_P(*step)) {
2911 desc = num_step_negative_p(*step);
2912 if (fix_nil &&
NIL_P(*to)) {
2919num_step_scan_args(
int argc,
const VALUE *argv,
VALUE *to,
VALUE *step,
int fix_nil,
int allow_zero_step)
2922 argc = num_step_extract_args(argc, argv, to, step, &by);
2923 return num_step_check_fix_args(argc, to, step, by, fix_nil, allow_zero_step);
2933 num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
2935 return ruby_num_interval_step_size(from, to, step, FALSE);
3043 num_step_extract_args(argc, argv, &to, &step, &by);
3055 return rb_arith_seq_new(from,
ID2SYM(rb_frame_this_func()), argc, argv,
3056 num_step_size, from, to, step, FALSE);
3062 desc = num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
3068 inf = isinf(f) && (signbit(f) ? desc : !desc);
3084 for (; i >= end; i += diff)
3088 for (; i <= end; i += diff)
3093 else if (!ruby_float_step(from, to, step, FALSE, FALSE)) {
3101 ID cmp = desc ?
'<' :
'>';
3111out_of_range_float(
char (*pbuf)[24],
VALUE val)
3113 char *
const buf = *pbuf;
3116 snprintf(buf,
sizeof(*pbuf),
"%-.10g",
RFLOAT_VALUE(val));
3117 if ((s = strchr(buf,
' ')) != 0) *s =
'\0';
3121#define FLOAT_OUT_OF_RANGE(val, type) do { \
3123 rb_raise(rb_eRangeError, "float %s out of range of "type, \
3124 out_of_range_float(&buf, (val))); \
3127#define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
3128#define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
3129#define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
3130#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3131 (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \
3133 LONG_MIN_MINUS_ONE < (n))
3147 && LONG_MIN_MINUS_ONE_IS_LESS_THAN(
RFLOAT_VALUE(val))) {
3151 FLOAT_OUT_OF_RANGE(val,
"integer");
3154 else if (RB_BIGNUM_TYPE_P(val)) {
3155 return rb_big2long(val);
3164rb_num2ulong_internal(
VALUE val,
int *wrap_p)
3175 return (
unsigned long)l;
3179 if (d < ULONG_MAX_PLUS_ONE && LONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3181 *wrap_p = d <= -1.0;
3183 return (
unsigned long)d;
3184 return (
unsigned long)(long)d;
3187 FLOAT_OUT_OF_RANGE(val,
"integer");
3190 else if (RB_BIGNUM_TYPE_P(val)) {
3192 unsigned long ul = rb_big2ulong(val);
3194 *wrap_p = BIGNUM_NEGATIVE_P(val);
3207 return rb_num2ulong_internal(val, NULL);
3214 num, num < 0 ?
"small" :
"big");
3217#if SIZEOF_INT < SIZEOF_LONG
3221 if ((
long)(
int)num != num) {
3227check_uint(
unsigned long num,
int sign)
3231 if (num < (
unsigned long)INT_MIN)
3263 unsigned long num = rb_num2ulong_internal(val, &wrap);
3265 check_uint(num, wrap);
3279 check_uint(num, FIXNUM_NEGATIVE_P(val));
3308NORETURN(
static void rb_out_of_short(
SIGNED_VALUE num));
3313 num, num < 0 ?
"small" :
"big");
3317check_short(
long num)
3319 if ((
long)(
short)num != num) {
3320 rb_out_of_short(num);
3325check_ushort(
unsigned long num,
int sign)
3329 if (num < (
unsigned long)SHRT_MIN)
3334 if (USHRT_MAX < num)
3361 unsigned long num = rb_num2ulong_internal(val, &wrap);
3363 check_ushort(num, wrap);
3377 check_ushort(num, FIXNUM_NEGATIVE_P(val));
3396#define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
3397#define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
3398#define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
3400#define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
3402#define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3403 (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \
3405 LLONG_MIN_MINUS_ONE < (n))
3418 if (d < LLONG_MAX_PLUS_ONE && (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d))) {
3422 FLOAT_OUT_OF_RANGE(val,
"long long");
3425 else if (RB_BIGNUM_TYPE_P(val)) {
3426 return rb_big2ll(val);
3450 if (d < ULLONG_MAX_PLUS_ONE && LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3456 FLOAT_OUT_OF_RANGE(val,
"unsigned long long");
3459 else if (RB_BIGNUM_TYPE_P(val)) {
3460 return rb_big2ull(val);
3570rb_int_odd_p(
VALUE num)
3573 return RBOOL(num & 2);
3576 assert(RB_BIGNUM_TYPE_P(num));
3577 return rb_big_odd_p(num);
3582int_even_p(
VALUE num)
3585 return RBOOL((num & 2) == 0);
3588 assert(RB_BIGNUM_TYPE_P(num));
3589 return rb_big_even_p(num);
3594rb_int_even_p(
VALUE num)
3596 return int_even_p(num);
3626 return rb_int_equal(rb_int_and(num, mask), mask);
3656 return RBOOL(!int_zero_p(rb_int_and(num, mask)));
3686 return RBOOL(int_zero_p(rb_int_and(num, mask)));
3704rb_int_succ(
VALUE num)
3710 if (RB_BIGNUM_TYPE_P(num)) {
3711 return rb_big_plus(num,
INT2FIX(1));
3713 return num_funcall1(num,
'+',
INT2FIX(1));
3716#define int_succ rb_int_succ
3732rb_int_pred(
VALUE num)
3738 if (RB_BIGNUM_TYPE_P(num)) {
3739 return rb_big_minus(num,
INT2FIX(1));
3741 return num_funcall1(num,
'-',
INT2FIX(1));
3744#define int_pred rb_int_pred
3751 switch (n = rb_enc_codelen(code, enc)) {
3752 case ONIGERR_INVALID_CODE_POINT_VALUE:
3755 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
3794 if (rb_num_to_uint(num, &i) == 0) {
3806 enc = rb_default_internal_encoding();
3822 rb_error_arity(argc, 0, 1);
3824 enc = rb_to_encoding(argv[0]);
3825 if (!enc) enc = rb_ascii8bit_encoding();
3835fix_uminus(
VALUE num)
3841rb_int_uminus(
VALUE num)
3844 return fix_uminus(num);
3847 assert(RB_BIGNUM_TYPE_P(num));
3848 return rb_big_uminus(num);
3855 char buf[
SIZEOF_VALUE*CHAR_BIT + 1], *
const e = buf +
sizeof buf, *b = e;
3860 if (base < 2 || 36 < base) {
3863#if SIZEOF_LONG < SIZEOF_VOIDP
3864# if SIZEOF_VOIDP == SIZEOF_LONG_LONG
3865 if ((val >= 0 && (x & 0xFFFFFFFF00000000ull)) ||
3866 (val < 0 && (x & 0xFFFFFFFF00000000ull) != 0xFFFFFFFF00000000ull)) {
3867 rb_bug(
"Unnormalized Fixnum value %p", (
void *)x);
3878 u = 1 + (
unsigned long)(-(val + 1));
3885 *--b = ruby_digitmap[(int)(u % base)];
3886 }
while (u /= base);
3894static VALUE rb_fix_to_s_static[10];
3896MJIT_FUNC_EXPORTED
VALUE
3900 if (i >= 0 && i < 10) {
3901 return rb_fix_to_s_static[i];
3927MJIT_FUNC_EXPORTED
VALUE
3936 return rb_int2str(x, base);
3940rb_int2str(
VALUE x,
int base)
3945 else if (RB_BIGNUM_TYPE_P(x)) {
3946 return rb_big2str(x, base);
3956 return rb_fix_plus_fix(x, y);
3958 else if (RB_BIGNUM_TYPE_P(y)) {
3959 return rb_big_plus(y, x);
3965 return rb_complex_plus(y, x);
3975 return fix_plus(x, y);
3997 return fix_plus(x, y);
3999 else if (RB_BIGNUM_TYPE_P(x)) {
4000 return rb_big_plus(x, y);
4009 return rb_fix_minus_fix(x, y);
4011 else if (RB_BIGNUM_TYPE_P(y)) {
4013 return rb_big_minus(x, y);
4042 return fix_minus(x, y);
4044 else if (RB_BIGNUM_TYPE_P(x)) {
4045 return rb_big_minus(x, y);
4051#define SQRT_LONG_MAX HALF_LONG_MSB
4053#define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
4059 return rb_fix_mul_fix(x, y);
4061 else if (RB_BIGNUM_TYPE_P(y)) {
4066 return rb_big_mul(y, x);
4072 return rb_complex_mul(y, x);
4097 return fix_mul(x, y);
4099 else if (RB_BIGNUM_TYPE_P(x)) {
4100 return rb_big_mul(x, y);
4111 else if (RB_BIGNUM_TYPE_P(y)) {
4112 return rb_big_fdiv_double(rb_int2big(
FIX2LONG(x)), y);
4126 VALUE gcd = rb_gcd(x, y);
4127 if (!FIXNUM_ZERO_P(gcd)) {
4128 x = rb_int_idiv(x, gcd);
4129 y = rb_int_idiv(y, gcd);
4133 return fix_fdiv_double(x, y);
4135 else if (RB_BIGNUM_TYPE_P(x)) {
4136 return rb_big_fdiv_double(x, y);
4163 return DBL2NUM(rb_int_fdiv_double(x, y));
4173 return rb_fix_div_fix(x, y);
4175 else if (RB_BIGNUM_TYPE_P(y)) {
4177 return rb_big_div(x, y);
4182 return rb_flo_div_flo(
DBL2NUM(d), y);
4187 v = fix_divide(x, y,
'/');
4188 return flo_floor(0, 0, v);
4194 return rb_rational_reciprocal(y);
4202 return fix_divide(x, y,
'/');
4228 return fix_div(x, y);
4230 else if (RB_BIGNUM_TYPE_P(x)) {
4231 return rb_big_div(x, y);
4239 return fix_divide(x, y, id_div);
4264 return fix_idiv(x, y);
4266 else if (RB_BIGNUM_TYPE_P(x)) {
4267 return rb_big_idiv(x, y);
4269 return num_div(x, y);
4277 return rb_fix_mod_fix(x, y);
4279 else if (RB_BIGNUM_TYPE_P(y)) {
4281 return rb_big_modulo(x, y);
4325 return fix_mod(x, y);
4327 else if (RB_BIGNUM_TYPE_P(x)) {
4328 return rb_big_modulo(x, y);
4330 return num_modulo(x, y);
4360 return num_remainder(x, y);
4362 else if (RB_BIGNUM_TYPE_P(x)) {
4363 return rb_big_remainder(x, y);
4374 rb_fix_divmod_fix(x, y, &div, &mod);
4375 return rb_assoc_new(div, mod);
4377 else if (RB_BIGNUM_TYPE_P(y)) {
4379 return rb_big_divmod(x, y);
4384 volatile VALUE a, b;
4389 return rb_assoc_new(a, b);
4426 return fix_divmod(x, y);
4428 else if (RB_BIGNUM_TYPE_P(x)) {
4429 return rb_big_divmod(x, y);
4451int_pow(
long x,
unsigned long y)
4456 if (y == 0)
return INT2FIX(1);
4465 while (y % 2 == 0) {
4466 if (!FIT_SQRT_LONG(x)) {
4473 if (MUL_OVERFLOW_FIXNUM_P(x, z)) {
4484 v = rb_big_pow(rb_int2big(x),
LONG2NUM(y));
4487 if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
4494 return int_pow(x, y);
4505 VALUE y = rb_int_pow(x, minusb);
4525 if (a == 1)
return INT2FIX(1);
4526 if (a == -1)
return INT2FIX(b % 2 ? -1 : 1);
4527 if (b < 0)
return fix_pow_inverted(x, fix_uminus(y));
4528 if (b == 0)
return INT2FIX(1);
4529 if (b == 1)
return x;
4530 if (a == 0)
return INT2FIX(0);
4531 return int_pow(a, b);
4533 else if (RB_BIGNUM_TYPE_P(y)) {
4534 if (a == 1)
return INT2FIX(1);
4535 if (a == -1)
return INT2FIX(int_even_p(y) ? 1 : -1);
4536 if (BIGNUM_NEGATIVE_P(y))
return fix_pow_inverted(x, rb_big_uminus(y));
4537 if (a == 0)
return INT2FIX(0);
4539 return rb_big_pow(x, y);
4543 if (dy == 0.0)
return DBL2NUM(1.0);
4545 return DBL2NUM(dy < 0 ? HUGE_VAL : 0.0);
4547 if (a == 1)
return DBL2NUM(1.0);
4548 if (a < 0 && dy != round(dy))
4549 return rb_dbl_complex_new_polar_pi(pow(-(
double)a, dy), dy);
4550 return DBL2NUM(pow((
double)a, dy));
4576 return fix_pow(x, y);
4578 else if (RB_BIGNUM_TYPE_P(x)) {
4579 return rb_big_pow(x, y);
4587 VALUE z = rb_int_pow(x, y);
4588 if (!
NIL_P(z))
return z;
4593 return rb_complex_pow(x, y);
4595 return rb_rational_pow(x, y);
4605 if (x == y)
return Qtrue;
4607 else if (RB_BIGNUM_TYPE_P(y)) {
4608 return rb_big_eq(y, x);
4611 return rb_integer_float_eq(x, y);
4614 return num_equal(x, y);
4637 return fix_equal(x, y);
4639 else if (RB_BIGNUM_TYPE_P(x)) {
4640 return rb_big_eq(x, y);
4648 if (x == y)
return INT2FIX(0);
4653 else if (RB_BIGNUM_TYPE_P(y)) {
4654 VALUE cmp = rb_big_cmp(y, x);
4662 return rb_integer_float_cmp(x, y);
4699 return fix_cmp(x, y);
4701 else if (RB_BIGNUM_TYPE_P(x)) {
4702 return rb_big_cmp(x, y);
4715 else if (RB_BIGNUM_TYPE_P(y)) {
4716 return RBOOL(rb_big_cmp(y, x) ==
INT2FIX(-1));
4719 return RBOOL(rb_integer_float_cmp(x, y) ==
INT2FIX(1));
4746 return fix_gt(x, y);
4748 else if (RB_BIGNUM_TYPE_P(x)) {
4749 return rb_big_gt(x, y);
4760 else if (RB_BIGNUM_TYPE_P(y)) {
4761 return RBOOL(rb_big_cmp(y, x) !=
INT2FIX(+1));
4764 VALUE rel = rb_integer_float_cmp(x, y);
4793 return fix_ge(x, y);
4795 else if (RB_BIGNUM_TYPE_P(x)) {
4796 return rb_big_ge(x, y);
4807 else if (RB_BIGNUM_TYPE_P(y)) {
4808 return RBOOL(rb_big_cmp(y, x) ==
INT2FIX(+1));
4811 return RBOOL(rb_integer_float_cmp(x, y) ==
INT2FIX(-1));
4838 return fix_lt(x, y);
4840 else if (RB_BIGNUM_TYPE_P(x)) {
4841 return rb_big_lt(x, y);
4852 else if (RB_BIGNUM_TYPE_P(y)) {
4853 return RBOOL(rb_big_cmp(y, x) !=
INT2FIX(-1));
4856 VALUE rel = rb_integer_float_cmp(x, y);
4885 return fix_le(x, y);
4887 else if (RB_BIGNUM_TYPE_P(x)) {
4888 return rb_big_le(x, y);
4900rb_int_comp(
VALUE num)
4903 return fix_comp(num);
4905 else if (RB_BIGNUM_TYPE_P(num)) {
4906 return rb_big_comp(num);
4912num_funcall_bit_1(
VALUE y,
VALUE arg,
int recursive)
4917 num_funcall_op_1_recursion(x, func, y);
4927 args[0] = (
VALUE)func;
4930 do_coerce(&args[1], &args[2], TRUE);
4932 args[2], args[1], (
VALUE)args);
4935 coerce_failed(x, y);
4948 if (RB_BIGNUM_TYPE_P(y)) {
4949 return rb_big_and(y, x);
4974 return fix_and(x, y);
4976 else if (RB_BIGNUM_TYPE_P(x)) {
4977 return rb_big_and(x, y);
4990 if (RB_BIGNUM_TYPE_P(y)) {
4991 return rb_big_or(y, x);
5016 return fix_or(x, y);
5018 else if (RB_BIGNUM_TYPE_P(x)) {
5019 return rb_big_or(x, y);
5032 if (RB_BIGNUM_TYPE_P(y)) {
5033 return rb_big_xor(y, x);
5058 return fix_xor(x, y);
5060 else if (RB_BIGNUM_TYPE_P(x)) {
5061 return rb_big_xor(x, y);
5074 return rb_big_lshift(rb_int2big(val), y);
5077 return fix_rshift(val, (
unsigned long)-width);
5078 return fix_lshift(val, width);
5082fix_lshift(
long val,
unsigned long width)
5084 if (width > (SIZEOF_LONG*CHAR_BIT-1)
5085 || ((
unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) {
5086 return rb_big_lshift(rb_int2big(val),
ULONG2NUM(width));
5113 return rb_fix_lshift(x, y);
5115 else if (RB_BIGNUM_TYPE_P(x)) {
5116 return rb_big_lshift(x, y);
5129 return rb_big_rshift(rb_int2big(val), y);
5131 if (i == 0)
return x;
5133 return fix_lshift(val, (
unsigned long)-i);
5134 return fix_rshift(val, i);
5138fix_rshift(
long val,
unsigned long i)
5140 if (i >=
sizeof(
long)*CHAR_BIT-1) {
5141 if (val < 0)
return INT2FIX(-1);
5144 val = RSHIFT(val, i);
5169 return rb_fix_rshift(x, y);
5171 else if (RB_BIGNUM_TYPE_P(x)) {
5172 return rb_big_rshift(x, y);
5177MJIT_FUNC_EXPORTED
VALUE
5185 idx = rb_big_norm(idx);
5187 if (!BIGNUM_SIGN(idx) || val >= 0)
5195 if (SIZEOF_LONG*CHAR_BIT-1 <= i) {
5196 if (val < 0)
return INT2FIX(1);
5218 return rb_cmpint(r, a, b);
5222generate_mask(
VALUE len)
5230 VALUE orig_num = num, beg, end;
5236 if (!
RTEST(num_negative_p(end))) {
5237 if (!excl) end = rb_int_plus(end,
INT2FIX(1));
5238 VALUE mask = generate_mask(end);
5239 if (int_zero_p(rb_int_and(num, mask))) {
5250 num = rb_int_rshift(num, beg);
5252 int cmp = compare_indexes(beg, end);
5253 if (!
NIL_P(end) && cmp < 0) {
5254 VALUE len = rb_int_minus(end, beg);
5255 if (!excl) len = rb_int_plus(len,
INT2FIX(1));
5256 VALUE mask = generate_mask(len);
5257 num = rb_int_and(num, mask);
5259 else if (cmp == 0) {
5270 return rb_fix_aref(num, arg);
5272 else if (RB_BIGNUM_TYPE_P(num)) {
5273 return rb_big_aref(num, arg);
5281 num = rb_int_rshift(num, beg);
5282 VALUE mask = generate_mask(len);
5283 num = rb_int_and(num, mask);
5327int_aref(
int const argc,
VALUE *
const argv,
VALUE const num)
5331 return int_aref2(num, argv[0], argv[1]);
5333 return int_aref1(num, argv[0]);
5363 else if (RB_BIGNUM_TYPE_P(num)) {
5364 val = rb_big2dbl(num);
5384rb_int_abs(
VALUE num)
5387 return fix_abs(num);
5389 else if (RB_BIGNUM_TYPE_P(num)) {
5390 return rb_big_abs(num);
5401MJIT_FUNC_EXPORTED
VALUE
5402rb_int_size(
VALUE num)
5405 return fix_size(num);
5407 else if (RB_BIGNUM_TYPE_P(num)) {
5408 return rb_big_size_m(num);
5414rb_fix_bit_length(
VALUE fix)
5423rb_int_bit_length(
VALUE num)
5426 return rb_fix_bit_length(num);
5428 else if (RB_BIGNUM_TYPE_P(num)) {
5429 return rb_big_bit_length(num);
5435rb_fix_digits(
VALUE fix,
long base)
5446 return rb_ary_new_from_args(1,
INT2FIX(0));
5448 digits = rb_ary_new();
5461 VALUE digits, bases;
5463 assert(!rb_num_negative_p(num));
5465 if (RB_BIGNUM_TYPE_P(base))
5466 base = rb_big_norm(base);
5470 else if (RB_BIGNUM_TYPE_P(base) && BIGNUM_NEGATIVE_P(base))
5474 return rb_fix_digits(num,
FIX2LONG(base));
5477 return rb_ary_new_from_args(1, num);
5479 if (int_lt(rb_int_div(rb_int_bit_length(num), rb_int_bit_length(base)),
INT2FIX(50))) {
5480 digits = rb_ary_new();
5482 VALUE qr = rb_int_divmod(num, base);
5489 bases = rb_ary_new();
5490 for (
VALUE b = base; int_lt(b, num) ==
Qtrue; b = rb_int_mul(b, b)) {
5491 rb_ary_push(bases, b);
5493 digits = rb_ary_new_from_args(1, num);
5495 VALUE b = rb_ary_pop(bases);
5497 for(i = last_idx; i >= 0; i--) {
5499 VALUE divmod = rb_int_divmod(n, b);
5502 if (i != last_idx || div !=
INT2FIX(0)) rb_ary_store(digits, 2 * i + 1, div);
5503 rb_ary_store(digits, 2 * i, mod);
5527rb_int_digits(
int argc,
VALUE *argv,
VALUE num)
5532 if (rb_num_negative_p(num))
5540 if (RB_BIGNUM_TYPE_P(base_value))
5541 return rb_int_digits_bigbase(num, base_value);
5553 return rb_fix_digits(num, base);
5554 else if (RB_BIGNUM_TYPE_P(num))
5555 return rb_int_digits_bigbase(num,
LONG2FIX(base));
5594 for (i =
FIX2LONG(from); i <= end; i++) {
5605 ensure_cmp(c, i, to);
5644 for (i=
FIX2LONG(from); i >= end; i--) {
5655 if (
NIL_P(c)) rb_cmperr(i, to);
5688int_dotimes(
VALUE num)
5696 for (i=0; i<end; i++) {
5704 if (!
RTEST(int_le(i, num)))
break;
5706 i = rb_int_plus(i,
INT2FIX(1));
5768 if (!
rb_scan_args(argc, argv,
"01:", &nd, &opt))
return num;
5770 mode = rb_num_get_rounding_option(opt);
5774 return rb_int_round(num, ndigits, mode);
5811 return rb_int_floor(num, ndigits);
5848 return rb_int_ceil(num, ndigits);
5875int_truncate(
int argc,
VALUE* argv,
VALUE num)
5884 return rb_int_truncate(num, ndigits);
5887#define DEFINE_INT_SQRT(rettype, prefix, argtype) \
5889prefix##_isqrt(argtype n) \
5891 if (!argtype##_IN_DOUBLE_P(n)) { \
5892 unsigned int b = bit_length(n); \
5894 rettype x = (rettype)(n >> (b/2+1)); \
5895 x |= ((rettype)1LU << (b-1)/2); \
5896 while ((t = n/x) < (argtype)x) x = (rettype)((x + t) >> 1); \
5899 return (rettype)sqrt(argtype##_TO_DOUBLE(n)); \
5902#if SIZEOF_LONG*CHAR_BIT > DBL_MANT_DIG
5903# define RB_ULONG_IN_DOUBLE_P(n) ((n) < (1UL << DBL_MANT_DIG))
5905# define RB_ULONG_IN_DOUBLE_P(n) 1
5907#define RB_ULONG_TO_DOUBLE(n) (double)(n)
5908#define RB_ULONG unsigned long
5909DEFINE_INT_SQRT(
unsigned long, rb_ulong, RB_ULONG)
5911#if 2*SIZEOF_BDIGIT > SIZEOF_LONG
5912# if 2*SIZEOF_BDIGIT*CHAR_BIT > DBL_MANT_DIG
5913# define BDIGIT_DBL_IN_DOUBLE_P(n) ((n) < ((BDIGIT_DBL)1UL << DBL_MANT_DIG))
5915# define BDIGIT_DBL_IN_DOUBLE_P(n) 1
5917# ifdef ULL_TO_DOUBLE
5918# define BDIGIT_DBL_TO_DOUBLE(n) ULL_TO_DOUBLE(n)
5920# define BDIGIT_DBL_TO_DOUBLE(n) (double)(n)
5922DEFINE_INT_SQRT(BDIGIT, rb_bdigit_dbl, BDIGIT_DBL)
5925#define domain_error(msg) \
5926 rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
5963 unsigned long n, sq;
5966 if (FIXNUM_NEGATIVE_P(num)) {
5967 domain_error(
"isqrt");
5970 sq = rb_ulong_isqrt(n);
5976 domain_error(
"isqrt");
5978 biglen = BIGNUM_LEN(num);
5979 if (biglen == 0)
return INT2FIX(0);
5980#if SIZEOF_BDIGIT <= SIZEOF_LONG
5983 n = BIGNUM_DIGITS(num)[0];
5984 sq = rb_ulong_isqrt(n);
5988 return rb_big_isqrt(num);
5996 return rb_check_integer_type(num);
6279 rb_fix_to_s_static[0] = rb_fstring_literal(
"0");
6280 rb_fix_to_s_static[1] = rb_fstring_literal(
"1");
6281 rb_fix_to_s_static[2] = rb_fstring_literal(
"2");
6282 rb_fix_to_s_static[3] = rb_fstring_literal(
"3");
6283 rb_fix_to_s_static[4] = rb_fstring_literal(
"4");
6284 rb_fix_to_s_static[5] = rb_fstring_literal(
"5");
6285 rb_fix_to_s_static[6] = rb_fstring_literal(
"6");
6286 rb_fix_to_s_static[7] = rb_fstring_literal(
"7");
6287 rb_fix_to_s_static[8] = rb_fstring_literal(
"8");
6288 rb_fix_to_s_static[9] = rb_fstring_literal(
"9");
6289 for(
int i = 0; i < 10; i++) {
6290 rb_gc_register_mark_object(rb_fix_to_s_static[i]);
6416#undef rb_float_value
6420 return rb_float_value_inline(v);
6425rb_float_new(
double d)
6427 return rb_float_new_inline(d);
6430#include "numeric.rbinc"
#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 NEWOBJ_OF
Old name of RB_NEWOBJ_OF.
#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 xfree
Old name of ruby_xfree.
#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 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 ISALNUM
Old name of rb_isalnum.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
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_eArgError
ArgumentError 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.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
static int rb_enc_mbcput(unsigned int c, void *buf, rb_encoding *enc)
Identical to rb_enc_uint_chr(), except it writes back to the passed buffer instead of allocating one.
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
Encodes the passed code point into a series of bytes.
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it additionally takes an encoding.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
#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.
int rb_memcicmp(const void *s1, const void *s2, long n)
Identical to st_locale_insensitive_strcasecmp(), except it is timing safe and returns something diffe...
#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_str_resize(VALUE str, long len)
Overwrites the length of the string.
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().
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
ID rb_to_id(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
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().
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
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.
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.
#define RGENGC_WB_PROTECTED_FLOAT
This is a compile-time flag to enable/disable write barrier for struct RFloat.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries 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().
#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.