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
61 #ifndef DBL_MIN_10_EXP
62 #define DBL_MIN_10_EXP (-307)
64 #ifndef DBL_MAX_10_EXP
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);
110 round_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;
128 round_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;
145 round_half_even(
double x,
double s)
147 double u, v, us, vs, f, d, uf;
159 else if (d == 0.5 || ((
double)((uf + 0.5) / s) <= x))
171 else if (d == 0.5 || ((
double)((uf - 0.5) / s) >= x))
180 static VALUE fix_lshift(
long,
unsigned long);
181 static VALUE fix_rshift(
long,
unsigned long);
182 static VALUE int_pow(
long x,
unsigned long y);
183 static VALUE rb_int_floor(
VALUE num,
int ndigits);
184 static VALUE rb_int_ceil(
VALUE num,
int ndigits);
186 static int float_round_overflow(
int ndigits,
int binexp);
187 static int float_round_underflow(
int ndigits,
int binexp);
191 #define id_divmod idDivmod
192 #define id_to_i idTo_i
203 static ID id_to, id_by;
211 enum ruby_num_rounding_mode
212 rb_num_get_rounding_option(
VALUE opts)
214 static ID round_kwds[1];
220 if (!round_kwds[0]) {
223 if (!
rb_get_kwargs(opts, round_kwds, 0, 1, &rounding))
goto noopt;
227 else if (
NIL_P(rounding)) {
232 if (
NIL_P(str))
goto invalid;
239 return RUBY_NUM_ROUND_HALF_UP;
243 return RUBY_NUM_ROUND_HALF_EVEN;
244 if (strncasecmp(s,
"down", 4) == 0)
245 return RUBY_NUM_ROUND_HALF_DOWN;
252 return RUBY_NUM_ROUND_DEFAULT;
257 rb_num_to_uint(
VALUE val,
unsigned int *ret)
259 #define NUMERR_TYPE 1
260 #define NUMERR_NEGATIVE 2
261 #define NUMERR_TOOLARGE 3
264 #if SIZEOF_INT < SIZEOF_LONG
265 if (v > (
long)UINT_MAX)
return NUMERR_TOOLARGE;
267 if (v < 0)
return NUMERR_NEGATIVE;
268 *ret = (
unsigned int)v;
272 if (RB_BIGNUM_TYPE_P(val)) {
273 if (BIGNUM_NEGATIVE_P(val))
return NUMERR_NEGATIVE;
274 #if SIZEOF_INT < SIZEOF_LONG
276 return NUMERR_TOOLARGE;
279 if (
rb_absint_size(val, NULL) >
sizeof(int))
return NUMERR_TOOLARGE;
287 #define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
293 return FIXNUM_POSITIVE_P(num);
295 else if (RB_BIGNUM_TYPE_P(num)) {
296 return BIGNUM_POSITIVE_P(num);
305 return FIXNUM_NEGATIVE_P(num);
307 else if (RB_BIGNUM_TYPE_P(num)) {
308 return BIGNUM_NEGATIVE_P(num);
314 rb_int_positive_p(
VALUE num)
316 return int_pos_p(num);
320 rb_int_negative_p(
VALUE num)
322 return int_neg_p(num);
326 rb_num_negative_p(
VALUE num)
328 return rb_num_negative_int_p(num);
332 num_funcall_op_0(
VALUE x,
VALUE arg,
int recursive)
341 else if (name[0] && name[1] ==
'@' && !name[2]) {
359 NORETURN(
static void num_funcall_op_1_recursion(
VALUE x,
ID func,
VALUE y));
366 rb_name_error(func,
"%"PRIsVALUE
".%"PRIsVALUE
"(%"PRIsVALUE
")",
376 num_funcall_op_1(
VALUE y,
VALUE arg,
int recursive)
381 num_funcall_op_1_recursion(x, func, y);
390 args[0] = (
VALUE)func;
440 NORETURN(
static void coerce_failed(
VALUE x,
VALUE y));
460 coerce_failed(*x, *y);
464 if (!err &&
NIL_P(ary)) {
479 do_coerce(&x, &y, TRUE);
486 if (do_coerce(&x, &y, FALSE))
501 VALUE x0 = x, y0 = y;
503 if (!do_coerce(&x, &y, FALSE)) {
507 return ensure_cmp(
rb_funcall(x, func, 1, y), x0, y0);
527 "can't define singleton method \"%"PRIsVALUE
"\" for %"PRIsVALUE,
549 return rb_immutable_obj_clone(argc, argv, x);
552 # define num_clone rb_immutable_obj_clone
570 num_imaginary(
VALUE num)
583 num_uminus(
VALUE num)
588 do_coerce(&zero, &num, TRUE);
590 return num_funcall1(zero,
'-', num);
669 VALUE q = num_funcall1(x, id_div, y);
709 do_coerce(&x, &y, TRUE);
711 VALUE z = num_funcall1(x,
'%', y);
714 ((rb_num_negative_int_p(x) &&
715 rb_num_positive_int_p(y)) ||
716 (rb_num_positive_int_p(x) &&
717 rb_num_negative_int_p(y)))) {
777 if (rb_num_negative_int_p(num)) {
778 return num_funcall0(num, idUMinus);
795 num_zero_p(
VALUE num)
801 int_zero_p(
VALUE num)
804 return FIXNUM_ZERO_P(num);
811 rb_int_zero_p(
VALUE num)
813 return RBOOL(int_zero_p(num));
837 num_nonzero_p(
VALUE num)
865 num_to_int(
VALUE num)
867 return num_funcall0(num, id_to_i);
879 num_positive_p(
VALUE num)
887 else if (RB_BIGNUM_TYPE_P(num)) {
889 return RBOOL(BIGNUM_POSITIVE_P(num) && !
rb_bigzero_p(num));
891 return rb_num_compare_with_zero(num, mid);
903 num_negative_p(
VALUE num)
905 return RBOOL(rb_num_negative_int_p(num));
995 #if SIZEOF_DOUBLE <= SIZEOF_VALUE
996 flt->float_value = d;
1000 rb_float_value_type v;
1002 flt->float_value = u.v;
1032 enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
1033 enum {float_dig = DBL_DIG+1};
1034 char buf[float_dig + roomof(decimal_mant, CHAR_BIT) + 10];
1038 int sign, decpt, digs;
1041 static const char minf[] =
"-Infinity";
1042 const int pos = (value > 0);
1045 else if (isnan(value))
1048 p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
1050 if ((digs = (
int)(e - p)) >= (
int)
sizeof(buf)) digs = (int)
sizeof(buf) - 1;
1051 memcpy(buf, p, digs);
1055 memmove(buf + decpt + 1, buf + decpt, digs - decpt);
1059 else if (decpt <= DBL_DIG) {
1066 memset(
ptr,
'0', decpt - digs);
1067 ptr += decpt - digs;
1069 memcpy(
ptr,
".0", 2);
1075 else if (decpt > -4) {
1081 memset(
ptr +=
len,
'0', -decpt);
1082 memcpy(
ptr -= decpt, buf, digs);
1091 memmove(buf + 2, buf + 1, digs - 1);
1127 rb_float_uminus(
VALUE flt)
1152 else if (RB_BIGNUM_TYPE_P(y)) {
1183 else if (RB_BIGNUM_TYPE_P(y)) {
1213 else if (RB_BIGNUM_TYPE_P(y)) {
1225 double_div_double(
double x,
double y)
1227 if (LIKELY(y != 0.0)) {
1230 else if (x == 0.0) {
1234 double z = signbit(y) ? -1.0 : 1.0;
1235 return x * z * HUGE_VAL;
1244 double ret = double_div_double(num, den);
1272 else if (RB_BIGNUM_TYPE_P(y)) {
1282 ret = double_div_double(num, den);
1303 return num_funcall1(x,
'/', y);
1307 flodivmod(
double x,
double y,
double *divp,
double *modp)
1313 if (modp) *modp = y;
1314 if (divp) *divp = y;
1318 if ((x == 0.0) || (isinf(y) && !isinf(x)))
1330 if (isinf(x) && !isinf(y))
1333 div = (x - mod) / y;
1334 if (modp && divp) div = round(div);
1340 if (modp) *modp = mod;
1341 if (divp) *divp = div;
1350 ruby_float_mod(
double x,
double y)
1353 flodivmod(x, y, 0, &mod);
1394 else if (RB_BIGNUM_TYPE_P(y)) {
1444 double fy, div, mod;
1445 volatile VALUE a, b;
1450 else if (RB_BIGNUM_TYPE_P(y)) {
1492 else if (RB_BIGNUM_TYPE_P(y)) {
1499 if (dx < 0 && dy != round(dy))
1500 return rb_dbl_complex_new_polar_pi(pow(-dx, dy), dy);
1534 if (RB_BIGNUM_TYPE_P(x)) {
1554 if (x == y)
return INT2FIX(0);
1562 if (x == y)
return Qtrue;
1563 result = num_funcall1(y, id_eq, x);
1564 return RBOOL(
RTEST(result));
1587 volatile double a, b;
1590 return rb_integer_float_eq(y, x);
1594 #if MSC_VERSION_BEFORE(1300)
1595 if (isnan(b))
return Qfalse;
1599 return num_equal(x, y);
1602 #if MSC_VERSION_BEFORE(1300)
1603 if (isnan(a))
return Qfalse;
1605 return RBOOL(a == b);
1608 #define flo_eq rb_float_equal
1609 static VALUE rb_dbl_hash(
double d);
1627 rb_dbl_hash(
double d)
1629 return ST2FIX(rb_dbl_long_hash(d));
1635 if (isnan(a) || isnan(b))
return Qnil;
1636 if (a == b)
return INT2FIX(0);
1638 if (a < b)
return INT2FIX(-1);
1677 if (isnan(a))
return Qnil;
1679 VALUE rel = rb_integer_float_cmp(y, x);
1691 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1694 if (a > 0.0)
return INT2FIX(1);
1705 return NUM2INT(ensure_cmp(flo_cmp(x, y), x, y));
1730 VALUE rel = rb_integer_float_cmp(y, x);
1737 #if MSC_VERSION_BEFORE(1300)
1738 if (isnan(b))
return Qfalse;
1744 #if MSC_VERSION_BEFORE(1300)
1745 if (isnan(a))
return Qfalse;
1747 return RBOOL(a > b);
1773 VALUE rel = rb_integer_float_cmp(y, x);
1780 #if MSC_VERSION_BEFORE(1300)
1781 if (isnan(b))
return Qfalse;
1787 #if MSC_VERSION_BEFORE(1300)
1788 if (isnan(a))
return Qfalse;
1790 return RBOOL(a >= b);
1815 VALUE rel = rb_integer_float_cmp(y, x);
1822 #if MSC_VERSION_BEFORE(1300)
1823 if (isnan(b))
return Qfalse;
1829 #if MSC_VERSION_BEFORE(1300)
1830 if (isnan(a))
return Qfalse;
1832 return RBOOL(a < b);
1858 VALUE rel = rb_integer_float_cmp(y, x);
1865 #if MSC_VERSION_BEFORE(1300)
1866 if (isnan(b))
return Qfalse;
1872 #if MSC_VERSION_BEFORE(1300)
1873 if (isnan(a))
return Qfalse;
1875 return RBOOL(a <= b);
1902 #if MSC_VERSION_BEFORE(1300)
1903 if (isnan(a) || isnan(b))
return Qfalse;
1905 return RBOOL(a == b);
1910 #define flo_eql rb_float_eql
1913 rb_float_abs(
VALUE flt)
1932 flo_is_nan_p(
VALUE num)
1936 return RBOOL(isnan(value));
1963 rb_flo_is_infinite_p(
VALUE num)
1968 return INT2FIX( value < 0 ? -1 : 1 );
1993 rb_flo_is_finite_p(
VALUE num)
1997 return RBOOL(isfinite(value));
2001 flo_nextafter(
VALUE flo,
double value)
2005 y = nextafter(x, value);
2054 flo_next_float(
VALUE vx)
2056 return flo_nextafter(vx, HUGE_VAL);
2095 flo_prev_float(
VALUE vx)
2097 return flo_nextafter(vx, -HUGE_VAL);
2101 rb_float_floor(
VALUE num,
int ndigits)
2105 if (number == 0.0) {
2111 frexp(number, &binexp);
2112 if (float_round_overflow(ndigits, binexp))
return num;
2113 if (number > 0.0 && float_round_underflow(ndigits, binexp))
2115 f = pow(10, ndigits);
2116 mul = floor(number * f);
2117 res = (mul + 1) / f;
2123 num = dbl2ival(floor(number));
2124 if (ndigits < 0) num = rb_int_floor(num, ndigits);
2130 flo_ndigits(
int argc,
VALUE *argv)
2219 int ndigits = flo_ndigits(argc, argv);
2220 return rb_float_floor(num, ndigits);
2304 int ndigits = flo_ndigits(argc, argv);
2305 return rb_float_ceil(num, ndigits);
2309 rb_float_ceil(
VALUE num,
int ndigits)
2314 if (number == 0.0) {
2319 frexp(number, &binexp);
2320 if (float_round_overflow(ndigits, binexp))
return num;
2321 if (number < 0.0 && float_round_underflow(ndigits, binexp))
2323 f = pow(10, ndigits);
2324 f = ceil(number * f) / f;
2328 num = dbl2ival(ceil(number));
2329 if (ndigits < 0) num = rb_int_ceil(num, ndigits);
2335 int_round_zero_p(
VALUE num,
int ndigits)
2341 bytes =
sizeof(long);
2343 else if (RB_BIGNUM_TYPE_P(num)) {
2344 bytes = rb_big_size(num);
2349 return (-0.415241 * ndigits - 0.125 > bytes);
2356 if ((z * y - x) * 2 == y) {
2365 return (x + y / 2) / y * y;
2371 return (x + y / 2 - 1) / y * y;
2377 return (
int)rb_int_odd_p(rb_int_idiv(n, f));
2383 return int_pos_p(num);
2389 return int_neg_p(num);
2396 rb_int_round(
VALUE num,
int ndigits,
enum ruby_num_rounding_mode mode)
2400 if (int_round_zero_p(num, ndigits)) {
2404 f = int_pow(10, -ndigits);
2409 x = ROUND_CALL(mode, int_round, (x, y));
2417 h = rb_int_idiv(f,
INT2FIX(2));
2418 r = rb_int_modulo(num, f);
2419 n = rb_int_minus(num, r);
2420 r = rb_int_cmp(r, h);
2421 if (FIXNUM_POSITIVE_P(r) ||
2422 (FIXNUM_ZERO_P(r) && ROUND_CALL(mode, int_half_p, (num, n, f)))) {
2423 n = rb_int_plus(n, f);
2429 rb_int_floor(
VALUE num,
int ndigits)
2431 VALUE f = int_pow(10, -ndigits);
2435 if (neg) x = -x + y - 1;
2441 bool neg = int_neg_p(num);
2442 if (neg) num = rb_int_minus(rb_int_plus(rb_int_uminus(num), f),
INT2FIX(1));
2443 num = rb_int_mul(rb_int_div(num, f), f);
2444 if (neg) num = rb_int_uminus(num);
2450 rb_int_ceil(
VALUE num,
int ndigits)
2452 VALUE f = int_pow(10, -ndigits);
2463 bool neg = int_neg_p(num);
2465 num = rb_int_uminus(num);
2467 num = rb_int_plus(num, rb_int_minus(f,
INT2FIX(1)));
2468 num = rb_int_mul(rb_int_div(num, f), f);
2469 if (neg) num = rb_int_uminus(num);
2475 rb_int_truncate(
VALUE num,
int ndigits)
2480 if (int_round_zero_p(num, ndigits))
2482 f = int_pow(10, -ndigits);
2495 m = rb_int_modulo(num, f);
2496 if (int_neg_p(num)) {
2497 return rb_int_plus(num, rb_int_minus(f, m));
2500 return rb_int_minus(num, m);
2562 double number, f, x;
2565 enum ruby_num_rounding_mode mode;
2570 mode = rb_num_get_rounding_option(opt);
2572 if (number == 0.0) {
2576 return rb_int_round(flo_to_i(num), ndigits, mode);
2579 x = ROUND_CALL(mode, round, (number, 1.0));
2582 if (isfinite(number)) {
2584 frexp(number, &binexp);
2585 if (float_round_overflow(ndigits, binexp))
return num;
2586 if (float_round_underflow(ndigits, binexp))
return DBL2NUM(0);
2589 return rb_flo_round_by_rational(argc, argv, num);
2591 f = pow(10, ndigits);
2592 x = ROUND_CALL(mode, round, (number, f));
2599 float_round_overflow(
int ndigits,
int binexp)
2601 enum {float_dig = DBL_DIG+2};
2620 if (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1)) {
2627 float_round_underflow(
int ndigits,
int binexp)
2629 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
2656 if (f > 0.0) f = floor(f);
2657 if (f < 0.0) f = ceil(f);
2698 flo_truncate(
int argc,
VALUE *argv,
VALUE num)
2701 return flo_ceil(argc, argv, num);
2703 return flo_floor(argc, argv, num);
2723 return flo_floor(argc, argv,
rb_Float(num));
2743 return flo_ceil(argc, argv,
rb_Float(num));
2760 return flo_round(argc, argv,
rb_Float(num));
2775 num_truncate(
int argc,
VALUE *argv,
VALUE num)
2777 return flo_truncate(argc, argv,
rb_Float(num));
2781 ruby_float_step_size(
double beg,
double end,
double unit,
int excl)
2783 const double epsilon = DBL_EPSILON;
2790 return unit > 0 ? beg <= end : beg >= end;
2792 n= (end - beg)/unit;
2793 err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
2794 if (err>0.5) err=0.5;
2801 d = +((n + 1) * unit) + beg;
2806 else if (beg > end) {
2814 d = +((n + 1) * unit) + beg;
2819 else if (beg > end) {
2828 ruby_float_step(
VALUE from,
VALUE to,
VALUE step,
int excl,
int allow_endless)
2833 double end = (allow_endless &&
NIL_P(to)) ? (unit < 0 ? -1 : 1)*HUGE_VAL :
NUM2DBL(to);
2834 double n = ruby_float_step_size(beg, end, unit, excl);
2841 else if (unit == 0) {
2847 for (i=0; i<n; i++) {
2848 double d = i*unit+beg;
2849 if (unit >= 0 ? end < d : d < end) d = end;
2859 ruby_num_interval_step_size(
VALUE from,
VALUE to,
VALUE step,
int excl)
2884 if (isinf(n))
return DBL2NUM(n);
2892 case 0:
return DBL2NUM(HUGE_VAL);
2893 case -1: cmp =
'<';
break;
2905 num_step_negative_p(
VALUE num)
2915 else if (RB_BIGNUM_TYPE_P(num)) {
2917 return BIGNUM_NEGATIVE_P(num);
2922 coerce_failed(num,
INT2FIX(0));
2932 argc =
rb_scan_args(argc, argv,
"02:", to, step, &hash);
2939 if (!UNDEF_P(values[0])) {
2943 if (!UNDEF_P(values[1])) {
2953 num_step_check_fix_args(
int argc,
VALUE *to,
VALUE *step,
VALUE by,
int fix_nil,
int allow_zero_step)
2961 if (argc > 1 &&
NIL_P(*step)) {
2971 desc = num_step_negative_p(*step);
2972 if (fix_nil &&
NIL_P(*to)) {
2979 num_step_scan_args(
int argc,
const VALUE *argv,
VALUE *to,
VALUE *step,
int fix_nil,
int allow_zero_step)
2982 argc = num_step_extract_args(argc, argv, to, step, &by);
2983 return num_step_check_fix_args(argc, to, step, by, fix_nil, allow_zero_step);
2993 num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
2995 return ruby_num_interval_step_size(from, to, step, FALSE);
3103 num_step_extract_args(argc, argv, &to, &step, &by);
3116 num_step_size, from, to, step, FALSE);
3122 desc = num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
3128 inf = isinf(f) && (signbit(f) ? desc : !desc);
3144 for (; i >= end; i += diff)
3148 for (; i <= end; i += diff)
3153 else if (!ruby_float_step(from, to, step, FALSE, FALSE)) {
3161 ID cmp = desc ?
'<' :
'>';
3171 out_of_range_float(
char (*pbuf)[24],
VALUE val)
3173 char *
const buf = *pbuf;
3176 snprintf(buf,
sizeof(*pbuf),
"%-.10g",
RFLOAT_VALUE(val));
3177 if ((s = strchr(buf,
' ')) != 0) *s =
'\0';
3181 #define FLOAT_OUT_OF_RANGE(val, type) do { \
3183 rb_raise(rb_eRangeError, "float %s out of range of "type, \
3184 out_of_range_float(&buf, (val))); \
3187 #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
3188 #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
3189 #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
3190 #define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3191 (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \
3193 LONG_MIN_MINUS_ONE < (n))
3207 && LONG_MIN_MINUS_ONE_IS_LESS_THAN(
RFLOAT_VALUE(val))) {
3211 FLOAT_OUT_OF_RANGE(val,
"integer");
3214 else if (RB_BIGNUM_TYPE_P(val)) {
3223 static unsigned long
3224 rb_num2ulong_internal(
VALUE val,
int *wrap_p)
3235 return (
unsigned long)l;
3239 if (d < ULONG_MAX_PLUS_ONE && LONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3241 *wrap_p = d <= -1.0;
3243 return (
unsigned long)d;
3244 return (
unsigned long)(long)d;
3247 FLOAT_OUT_OF_RANGE(val,
"integer");
3250 else if (RB_BIGNUM_TYPE_P(val)) {
3254 *wrap_p = BIGNUM_NEGATIVE_P(val);
3267 return rb_num2ulong_internal(val, NULL);
3274 num, num < 0 ?
"small" :
"big");
3277 #if SIZEOF_INT < SIZEOF_LONG
3281 if ((
long)(
int)num != num) {
3287 check_uint(
unsigned long num,
int sign)
3291 if (num < (
unsigned long)INT_MIN)
3323 unsigned long num = rb_num2ulong_internal(val, &wrap);
3325 check_uint(num, wrap);
3339 check_uint(num, FIXNUM_NEGATIVE_P(val));
3368 NORETURN(
static void rb_out_of_short(
SIGNED_VALUE num));
3373 num, num < 0 ?
"small" :
"big");
3377 check_short(
long num)
3379 if ((
long)(
short)num != num) {
3380 rb_out_of_short(num);
3385 check_ushort(
unsigned long num,
int sign)
3389 if (num < (
unsigned long)SHRT_MIN)
3394 if (USHRT_MAX < num)
3421 unsigned long num = rb_num2ulong_internal(val, &wrap);
3423 check_ushort(num, wrap);
3437 check_ushort(num, FIXNUM_NEGATIVE_P(val));
3456 #define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
3457 #define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
3458 #define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
3460 #define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
3462 #define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3463 (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \
3465 LLONG_MIN_MINUS_ONE < (n))
3478 if (d < LLONG_MAX_PLUS_ONE && (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d))) {
3482 FLOAT_OUT_OF_RANGE(val,
"long long");
3485 else if (RB_BIGNUM_TYPE_P(val)) {
3486 return rb_big2ll(val);
3510 if (d < ULLONG_MAX_PLUS_ONE && LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3516 FLOAT_OUT_OF_RANGE(val,
"unsigned long long");
3519 else if (RB_BIGNUM_TYPE_P(val)) {
3520 return rb_big2ull(val);
3628 rb_int_odd_p(
VALUE num)
3631 return RBOOL(num & 2);
3635 return rb_big_odd_p(num);
3640 int_even_p(
VALUE num)
3643 return RBOOL((num & 2) == 0);
3647 return rb_big_even_p(num);
3652 rb_int_even_p(
VALUE num)
3654 return int_even_p(num);
3684 return rb_int_equal(rb_int_and(num, mask), mask);
3714 return RBOOL(!int_zero_p(rb_int_and(num, mask)));
3744 return RBOOL(int_zero_p(rb_int_and(num, mask)));
3760 rb_int_succ(
VALUE num)
3766 if (RB_BIGNUM_TYPE_P(num)) {
3769 return num_funcall1(num,
'+',
INT2FIX(1));
3772 #define int_succ rb_int_succ
3788 rb_int_pred(
VALUE num)
3794 if (RB_BIGNUM_TYPE_P(num)) {
3797 return num_funcall1(num,
'-',
INT2FIX(1));
3800 #define int_pred rb_int_pred
3808 case ONIGERR_INVALID_CODE_POINT_VALUE:
3811 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
3850 if (rb_num_to_uint(num, &i) == 0) {
3878 rb_error_arity(argc, 0, 1);
3891 fix_uminus(
VALUE num)
3897 rb_int_uminus(
VALUE num)
3900 return fix_uminus(num);
3904 return rb_big_uminus(num);
3911 char buf[
SIZEOF_VALUE*CHAR_BIT + 1], *
const e = buf +
sizeof buf, *b = e;
3916 if (base < 2 || 36 < base) {
3919 #if SIZEOF_LONG < SIZEOF_VOIDP
3920 # if SIZEOF_VOIDP == SIZEOF_LONG_LONG
3921 if ((val >= 0 && (x & 0xFFFFFFFF00000000ull)) ||
3922 (val < 0 && (x & 0xFFFFFFFF00000000ull) != 0xFFFFFFFF00000000ull)) {
3923 rb_bug(
"Unnormalized Fixnum value %p", (
void *)x);
3934 u = 1 + (
unsigned long)(-(val + 1));
3941 *--b = ruby_digitmap[(int)(u % base)];
3942 }
while (u /= base);
3950 static VALUE rb_fix_to_s_static[10];
3953 rb_fix_to_s(
VALUE x)
3956 if (i >= 0 && i < 10) {
3957 return rb_fix_to_s_static[i];
3989 return rb_int2str(x, base);
3993 rb_int2str(
VALUE x,
int base)
3998 else if (RB_BIGNUM_TYPE_P(x)) {
4009 return rb_fix_plus_fix(x, y);
4011 else if (RB_BIGNUM_TYPE_P(y)) {
4028 return fix_plus(x, y);
4050 return fix_plus(x, y);
4052 else if (RB_BIGNUM_TYPE_P(x)) {
4062 return rb_fix_minus_fix(x, y);
4064 else if (RB_BIGNUM_TYPE_P(y)) {
4095 return fix_minus(x, y);
4097 else if (RB_BIGNUM_TYPE_P(x)) {
4104 #define SQRT_LONG_MAX HALF_LONG_MSB
4106 #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
4112 return rb_fix_mul_fix(x, y);
4114 else if (RB_BIGNUM_TYPE_P(y)) {
4150 return fix_mul(x, y);
4152 else if (RB_BIGNUM_TYPE_P(x)) {
4163 #if SIZEOF_LONG * CHAR_BIT > DBL_MANT_DIG
4164 if ((iy < 0 ? -iy : iy) >= (1L << DBL_MANT_DIG)) {
4168 return double_div_double(
FIX2LONG(x), iy);
4170 else if (RB_BIGNUM_TYPE_P(y)) {
4185 VALUE gcd = rb_gcd(x, y);
4186 if (!FIXNUM_ZERO_P(gcd) && gcd !=
INT2FIX(1)) {
4187 x = rb_int_idiv(x, gcd);
4188 y = rb_int_idiv(y, gcd);
4192 return fix_fdiv_double(x, y);
4194 else if (RB_BIGNUM_TYPE_P(x)) {
4195 return rb_big_fdiv_double(x, y);
4222 return DBL2NUM(rb_int_fdiv_double(x, y));
4232 return rb_fix_div_fix(x, y);
4234 else if (RB_BIGNUM_TYPE_P(y)) {
4241 return rb_flo_div_flo(
DBL2NUM(d), y);
4246 v = fix_divide(x, y,
'/');
4247 return flo_floor(0, 0, v);
4253 return rb_rational_reciprocal(y);
4261 return fix_divide(x, y,
'/');
4287 return fix_div(x, y);
4289 else if (RB_BIGNUM_TYPE_P(x)) {
4298 return fix_divide(x, y, id_div);
4323 return fix_idiv(x, y);
4325 else if (RB_BIGNUM_TYPE_P(x)) {
4328 return num_div(x, y);
4336 return rb_fix_mod_fix(x, y);
4338 else if (RB_BIGNUM_TYPE_P(y)) {
4382 return fix_mod(x, y);
4384 else if (RB_BIGNUM_TYPE_P(x)) {
4387 return num_modulo(x, y);
4418 VALUE z = fix_mod(x, y);
4421 z = fix_minus(z, y);
4424 else if (!RB_BIGNUM_TYPE_P(y)) {
4425 return num_remainder(x, y);
4429 else if (!RB_BIGNUM_TYPE_P(x)) {
4432 return rb_big_remainder(x, y);
4441 rb_fix_divmod_fix(x, y, &div, &mod);
4444 else if (RB_BIGNUM_TYPE_P(y)) {
4451 volatile VALUE a, b;
4493 return fix_divmod(x, y);
4495 else if (RB_BIGNUM_TYPE_P(x)) {
4518 int_pow(
long x,
unsigned long y)
4523 if (y == 0)
return INT2FIX(1);
4532 while (y % 2 == 0) {
4533 if (!FIT_SQRT_LONG(x)) {
4540 if (MUL_OVERFLOW_FIXNUM_P(x, z)) {
4561 return int_pow(x, y);
4572 VALUE y = rb_int_pow(x, minusb);
4592 if (a == 1)
return INT2FIX(1);
4593 if (a == -1)
return INT2FIX(b % 2 ? -1 : 1);
4594 if (b < 0)
return fix_pow_inverted(x, fix_uminus(y));
4595 if (b == 0)
return INT2FIX(1);
4596 if (b == 1)
return x;
4597 if (a == 0)
return INT2FIX(0);
4598 return int_pow(a, b);
4600 else if (RB_BIGNUM_TYPE_P(y)) {
4601 if (a == 1)
return INT2FIX(1);
4602 if (a == -1)
return INT2FIX(int_even_p(y) ? 1 : -1);
4603 if (BIGNUM_NEGATIVE_P(y))
return fix_pow_inverted(x, rb_big_uminus(y));
4604 if (a == 0)
return INT2FIX(0);
4610 if (dy == 0.0)
return DBL2NUM(1.0);
4612 return DBL2NUM(dy < 0 ? HUGE_VAL : 0.0);
4614 if (a == 1)
return DBL2NUM(1.0);
4615 if (a < 0 && dy != round(dy))
4616 return rb_dbl_complex_new_polar_pi(pow(-(
double)a, dy), dy);
4617 return DBL2NUM(pow((
double)a, dy));
4643 return fix_pow(x, y);
4645 else if (RB_BIGNUM_TYPE_P(x)) {
4654 VALUE z = rb_int_pow(x, y);
4655 if (!
NIL_P(z))
return z;
4662 return rb_rational_pow(x, y);
4672 if (x == y)
return Qtrue;
4674 else if (RB_BIGNUM_TYPE_P(y)) {
4678 return rb_integer_float_eq(x, y);
4681 return num_equal(x, y);
4701 return fix_equal(x, y);
4703 else if (RB_BIGNUM_TYPE_P(x)) {
4712 if (x == y)
return INT2FIX(0);
4717 else if (RB_BIGNUM_TYPE_P(y)) {
4726 return rb_integer_float_cmp(x, y);
4763 return fix_cmp(x, y);
4765 else if (RB_BIGNUM_TYPE_P(x)) {
4779 else if (RB_BIGNUM_TYPE_P(y)) {
4783 return RBOOL(rb_integer_float_cmp(x, y) ==
INT2FIX(1));
4810 return fix_gt(x, y);
4812 else if (RB_BIGNUM_TYPE_P(x)) {
4813 return rb_big_gt(x, y);
4824 else if (RB_BIGNUM_TYPE_P(y)) {
4828 VALUE rel = rb_integer_float_cmp(x, y);
4857 return fix_ge(x, y);
4859 else if (RB_BIGNUM_TYPE_P(x)) {
4860 return rb_big_ge(x, y);
4871 else if (RB_BIGNUM_TYPE_P(y)) {
4875 return RBOOL(rb_integer_float_cmp(x, y) ==
INT2FIX(-1));
4902 return fix_lt(x, y);
4904 else if (RB_BIGNUM_TYPE_P(x)) {
4905 return rb_big_lt(x, y);
4916 else if (RB_BIGNUM_TYPE_P(y)) {
4920 VALUE rel = rb_integer_float_cmp(x, y);
4949 return fix_le(x, y);
4951 else if (RB_BIGNUM_TYPE_P(x)) {
4952 return rb_big_le(x, y);
4964 rb_int_comp(
VALUE num)
4967 return fix_comp(num);
4969 else if (RB_BIGNUM_TYPE_P(num)) {
4970 return rb_big_comp(num);
4976 num_funcall_bit_1(
VALUE y,
VALUE arg,
int recursive)
4981 num_funcall_op_1_recursion(x, func, y);
4991 args[0] = (
VALUE)func;
4994 do_coerce(&args[1], &args[2], TRUE);
4996 args[2], args[1], (
VALUE)args);
4999 coerce_failed(x, y);
5012 if (RB_BIGNUM_TYPE_P(y)) {
5038 return fix_and(x, y);
5040 else if (RB_BIGNUM_TYPE_P(x)) {
5054 if (RB_BIGNUM_TYPE_P(y)) {
5080 return fix_or(x, y);
5082 else if (RB_BIGNUM_TYPE_P(x)) {
5096 if (RB_BIGNUM_TYPE_P(y)) {
5122 return fix_xor(x, y);
5124 else if (RB_BIGNUM_TYPE_P(x)) {
5141 return fix_rshift(val, (
unsigned long)-width);
5142 return fix_lshift(val, width);
5146 fix_lshift(
long val,
unsigned long width)
5148 if (width > (SIZEOF_LONG*CHAR_BIT-1)
5149 || ((
unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) {
5177 return rb_fix_lshift(x, y);
5179 else if (RB_BIGNUM_TYPE_P(x)) {
5195 if (i == 0)
return x;
5197 return fix_lshift(val, (
unsigned long)-i);
5198 return fix_rshift(val, i);
5202 fix_rshift(
long val,
unsigned long i)
5204 if (i >=
sizeof(
long)*CHAR_BIT-1) {
5205 if (val < 0)
return INT2FIX(-1);
5208 val = RSHIFT(val, i);
5233 return rb_fix_rshift(x, y);
5235 else if (RB_BIGNUM_TYPE_P(x)) {
5251 if (!BIGNUM_SIGN(idx) || val >= 0)
5259 if (SIZEOF_LONG*CHAR_BIT-1 <= i) {
5260 if (val < 0)
return INT2FIX(1);
5294 VALUE orig_num = num, beg, end;
5300 if (!
RTEST(num_negative_p(end))) {
5301 if (!excl) end = rb_int_plus(end,
INT2FIX(1));
5302 VALUE mask = generate_mask(end);
5303 if (int_zero_p(rb_int_and(num, mask))) {
5314 num = rb_int_rshift(num, beg);
5316 int cmp = compare_indexes(beg, end);
5317 if (!
NIL_P(end) && cmp < 0) {
5318 VALUE len = rb_int_minus(end, beg);
5321 num = rb_int_and(num, mask);
5323 else if (cmp == 0) {
5334 return rb_fix_aref(num, arg);
5336 else if (RB_BIGNUM_TYPE_P(num)) {
5337 return rb_big_aref(num, arg);
5345 num = rb_int_rshift(num, beg);
5347 num = rb_int_and(num, mask);
5391 int_aref(
int const argc,
VALUE *
const argv,
VALUE const num)
5395 return int_aref2(num, argv[0], argv[1]);
5397 return int_aref1(num, argv[0]);
5427 else if (RB_BIGNUM_TYPE_P(num)) {
5448 rb_int_abs(
VALUE num)
5451 return fix_abs(num);
5453 else if (RB_BIGNUM_TYPE_P(num)) {
5454 return rb_big_abs(num);
5466 rb_int_size(
VALUE num)
5469 return fix_size(num);
5471 else if (RB_BIGNUM_TYPE_P(num)) {
5472 return rb_big_size_m(num);
5478 rb_fix_bit_length(
VALUE fix)
5487 rb_int_bit_length(
VALUE num)
5490 return rb_fix_bit_length(num);
5492 else if (RB_BIGNUM_TYPE_P(num)) {
5493 return rb_big_bit_length(num);
5499 rb_fix_digits(
VALUE fix,
long base)
5526 VALUE digits, bases;
5530 if (RB_BIGNUM_TYPE_P(base))
5535 else if (RB_BIGNUM_TYPE_P(base) && BIGNUM_NEGATIVE_P(base))
5539 return rb_fix_digits(num,
FIX2LONG(base));
5544 if (int_lt(rb_int_div(rb_int_bit_length(num), rb_int_bit_length(base)),
INT2FIX(50))) {
5547 VALUE qr = rb_int_divmod(num, base);
5555 for (
VALUE b = base; int_lt(b, num) ==
Qtrue; b = rb_int_mul(b, b)) {
5562 for(i = last_idx; i >= 0; i--) {
5564 VALUE divmod = rb_int_divmod(n, b);
5592 rb_int_digits(
int argc,
VALUE *argv,
VALUE num)
5597 if (rb_num_negative_p(num))
5605 if (RB_BIGNUM_TYPE_P(base_value))
5606 return rb_int_digits_bigbase(num, base_value);
5618 return rb_fix_digits(num, base);
5619 else if (RB_BIGNUM_TYPE_P(num))
5620 return rb_int_digits_bigbase(num,
LONG2FIX(base));
5659 for (i =
FIX2LONG(from); i <= end; i++) {
5670 ensure_cmp(c, i, to);
5709 for (i=
FIX2LONG(from); i >= end; i--) {
5728 return int_neg_p(num) ?
INT2FIX(0) : num;
5787 if (!
rb_scan_args(argc, argv,
"01:", &nd, &opt))
return num;
5789 mode = rb_num_get_rounding_option(opt);
5793 return rb_int_round(num, ndigits, mode);
5862 return rb_int_floor(num, ndigits);
5930 return rb_int_ceil(num, ndigits);
5957 int_truncate(
int argc,
VALUE* argv,
VALUE num)
5966 return rb_int_truncate(num, ndigits);
5969 #define DEFINE_INT_SQRT(rettype, prefix, argtype) \
5971 prefix##_isqrt(argtype n) \
5973 if (!argtype##_IN_DOUBLE_P(n)) { \
5974 unsigned int b = bit_length(n); \
5976 rettype x = (rettype)(n >> (b/2+1)); \
5977 x |= ((rettype)1LU << (b-1)/2); \
5978 while ((t = n/x) < (argtype)x) x = (rettype)((x + t) >> 1); \
5981 return (rettype)sqrt(argtype##_TO_DOUBLE(n)); \
5984 #if SIZEOF_LONG*CHAR_BIT > DBL_MANT_DIG
5985 # define RB_ULONG_IN_DOUBLE_P(n) ((n) < (1UL << DBL_MANT_DIG))
5987 # define RB_ULONG_IN_DOUBLE_P(n) 1
5989 #define RB_ULONG_TO_DOUBLE(n) (double)(n)
5990 #define RB_ULONG unsigned long
5991 DEFINE_INT_SQRT(
unsigned long, rb_ulong, RB_ULONG)
5993 #if 2*SIZEOF_BDIGIT > SIZEOF_LONG
5994 # if 2*SIZEOF_BDIGIT*CHAR_BIT > DBL_MANT_DIG
5995 # define BDIGIT_DBL_IN_DOUBLE_P(n) ((n) < ((BDIGIT_DBL)1UL << DBL_MANT_DIG))
5997 # define BDIGIT_DBL_IN_DOUBLE_P(n) 1
5999 # ifdef ULL_TO_DOUBLE
6000 # define BDIGIT_DBL_TO_DOUBLE(n) ULL_TO_DOUBLE(n)
6002 # define BDIGIT_DBL_TO_DOUBLE(n) (double)(n)
6004 DEFINE_INT_SQRT(BDIGIT, rb_bdigit_dbl, BDIGIT_DBL)
6007 #define domain_error(msg) \
6008 rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
6045 unsigned long n, sq;
6048 if (FIXNUM_NEGATIVE_P(num)) {
6049 domain_error(
"isqrt");
6052 sq = rb_ulong_isqrt(n);
6058 domain_error(
"isqrt");
6060 biglen = BIGNUM_LEN(num);
6061 if (biglen == 0)
return INT2FIX(0);
6062 #if SIZEOF_BDIGIT <= SIZEOF_LONG
6065 n = BIGNUM_DIGITS(num)[0];
6066 sq = rb_ulong_isqrt(n);
6070 return rb_big_isqrt(num);
6093 return rb_check_integer_type(num);
6373 #define fix_to_s_static(n) do { \
6374 VALUE lit = rb_fstring_literal(#n); \
6375 rb_fix_to_s_static[n] = lit; \
6376 rb_vm_register_global_object(lit); \
6391 #undef fix_to_s_static
6516 #undef rb_float_value
6520 return rb_float_value_inline(v);
6527 return rb_float_new_inline(d);
6530 #include "numeric.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
double rb_float_value(VALUE num)
Extracts its double value from an instance of rb_cFloat.
VALUE rb_float_new_in_heap(double d)
Identical to rb_float_new(), except it does not generate Flonums.
VALUE rb_float_new(double d)
Converts a C's double into an instance of rb_cFloat.
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.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
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 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.
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
Queries the number of bytes of the character at the passed pointer.
int rb_enc_codelen(int code, rb_encoding *enc)
Queries the number of bytes requested to represent the passed code point using the passed encoding.
rb_encoding * rb_default_internal_encoding(void)
Queries the "default internal" encoding.
rb_encoding * rb_to_encoding(VALUE obj)
Identical to rb_find_encoding(), except it raises an exception instead of returning NULL.
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
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.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
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_str_new(), except it additionally takes an encoding.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
#define RGENGC_WB_PROTECTED_FLOAT
This is a compile-time flag to enable/disable write barrier for struct RFloat.
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_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
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.
VALUE rb_big_lshift(VALUE x, VALUE y)
Performs shift left.
VALUE rb_big_and(VALUE x, VALUE y)
Performs bitwise and of the passed two objects.
VALUE rb_big_or(VALUE x, VALUE y)
Performs bitwise or of the passed two objects.
VALUE rb_big_minus(VALUE x, VALUE y)
Performs subtraction of the passed two objects.
VALUE rb_big_modulo(VALUE x, VALUE y)
Performs modulo of the passed two objects.
VALUE rb_big_pow(VALUE x, VALUE y)
Raises x to the powerof y.
int rb_bigzero_p(VALUE x)
Queries if the passed bignum instance is a "bigzero".
VALUE rb_big_plus(VALUE x, VALUE y)
Performs addition of the passed two objects.
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
Calculates the number of bytes needed to represent the absolute value of the passed integer.
unsigned long rb_big2ulong(VALUE x)
Converts a bignum into C's unsigned long.
VALUE rb_big_idiv(VALUE x, VALUE y)
Performs "integer division".
VALUE rb_big2str(VALUE x, int base)
Generates a place-value representation of the passed integer.
VALUE rb_big_cmp(VALUE lhs, VALUE rhs)
Compares the passed two bignums.
VALUE rb_dbl2big(double d)
Converts a C's double into a bignum.
VALUE rb_big_mul(VALUE x, VALUE y)
Performs multiplication of the passed two objects.
VALUE rb_big_eql(VALUE lhs, VALUE rhs)
Equality, in terms of eql?.
VALUE rb_big_divmod(VALUE x, VALUE y)
Performs "divmod" operation.
VALUE rb_big_xor(VALUE x, VALUE y)
Performs exclusive or of the passed two objects.
VALUE rb_big_div(VALUE x, VALUE y)
Performs division of the passed two objects.
VALUE rb_big_norm(VALUE x)
Normalises the passed bignum.
VALUE rb_big_rshift(VALUE x, VALUE y)
Performs shift right.
double rb_big2dbl(VALUE x)
Converts a bignum into C's double.
long rb_big2long(VALUE x)
Converts a bignum into C's long.
VALUE rb_big_eq(VALUE lhs, VALUE rhs)
Equality, in terms of ==.
int rb_cmpint(VALUE val, VALUE a, VALUE b)
Canonicalises the passed val, which is the return value of a <=> b, into C's {-1, 0,...
void rb_cmperr(VALUE a, VALUE b)
Raises "comparison failed" error.
VALUE rb_complex_new(VALUE real, VALUE imag)
Constructs a Complex, by first multiplying the imaginary part with 1i then adds it to the real part.
VALUE rb_complex_plus(VALUE x, VALUE y)
Performs addition of the passed two objects.
VALUE rb_complex_mul(VALUE x, VALUE y)
Performs multiplication of the passed two objects.
VALUE rb_complex_pow(VALUE base, VALUE exp)
Performs exponentiation of the passed two objects.
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
#define SIZED_ENUMERATOR_KW(obj, argc, argv, size_fn, kw_splat)
This is an implementation detail of RETURN_SIZED_ENUMERATOR_KW().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
ID rb_frame_this_func(void)
Queries the name of the Ruby level method that is calling this function.
void rb_num_zerodiv(void)
Just always raises an exception.
VALUE rb_num2fix(VALUE val)
Converts a numeric value into a Fixnum.
VALUE rb_fix2str(VALUE val, int base)
Generates a place-value representation of the given Fixnum, with given radix.
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
VALUE rb_dbl_cmp(double lhs, double rhs)
Compares two doubles.
VALUE rb_num_coerce_bit(VALUE lhs, VALUE rhs, ID op)
This one is optimised for bitwise operations, but the API is identical to rb_num_coerce_bin().
VALUE rb_num_coerce_relop(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_cmp(), except for return values.
VALUE rb_num_coerce_cmp(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_bin(), except for return values.
VALUE rb_num_coerce_bin(VALUE lhs, VALUE rhs, ID op)
Coerced binary operation.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
VALUE rb_rational_raw(VALUE num, VALUE den)
Identical to rb_rational_new(), except it skips argument validations.
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...
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
VALUE rb_usascii_str_new(const char *ptr, long len)
Identical to rb_str_new(), except it generates a string of "US ASCII" encoding.
VALUE rb_usascii_str_new_cstr(const char *ptr)
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_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
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().
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
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.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
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().
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_int2big(intptr_t i)
Converts a C's intptr_t into an instance of rb_cInteger.
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.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length 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.