33#include "internal/gc.h"
39#include "internal/variable.h"
49#define DBL_MIN 2.2250738585072014e-308
52#define DBL_MAX 1.7976931348623157e+308
55#define DBL_MIN_EXP (-1021)
58#define DBL_MAX_EXP 1024
61#define DBL_MIN_10_EXP (-307)
64#define DBL_MAX_10_EXP 308
70#define DBL_MANT_DIG 53
73#define DBL_EPSILON 2.2204460492503131e-16
76#ifndef USE_RB_INFINITY
77#elif !defined(WORDS_BIGENDIAN)
84#elif !defined(WORDS_BIGENDIAN)
98 x =
f + (x -
f >= 0.5);
102 x =
f - (
f - x >= 0.5);
109round_half_up(
double x,
double s)
111 double f, xs = x * s;
114 if (s == 1.0)
return f;
116 if ((
double)((
f + 0.5) / s) <= x)
f += 1;
120 if ((
double)((
f - 0.5) / s) >= x)
f -= 1;
127round_half_down(
double x,
double s)
129 double f, xs = x * s;
133 if ((
double)((
f - 0.5) / s) >= x)
f -= 1;
137 if ((
double)((
f + 0.5) / s) <= x)
f += 1;
144round_half_even(
double x,
double s)
146 double f, d, xs = x * s;
153 else if (d == 0.5 || ((
double)((
f + 0.5) / s) <= x))
164 else if (d == 0.5 || ((
double)((
f - 0.5) / s) >= x))
173static VALUE fix_lshift(
long,
unsigned long);
174static VALUE fix_rshift(
long,
unsigned long);
175static VALUE int_pow(
long x,
unsigned long y);
179static int float_round_overflow(
int ndigits,
int binexp);
180static int float_round_underflow(
int ndigits,
int binexp);
184#define id_divmod idDivmod
185#define id_to_i idTo_i
196static ID id_to, id_by;
207 static ID round_kwds[1];
213 if (!round_kwds[0]) {
214 round_kwds[0] = rb_intern_const(
"half");
216 if (!
rb_get_kwargs(opts, round_kwds, 0, 1, &rounding))
goto noopt;
220 else if (
NIL_P(rounding)) {
253#define NUMERR_NEGATIVE 2
254#define NUMERR_TOOLARGE 3
257#if SIZEOF_INT < SIZEOF_LONG
261 *ret = (
unsigned int)v;
267#if SIZEOF_INT < SIZEOF_LONG
280#define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
286 return FIXNUM_POSITIVE_P(
num);
289 return BIGNUM_POSITIVE_P(
num);
298 return FIXNUM_NEGATIVE_P(
num);
301 return BIGNUM_NEGATIVE_P(
num);
309 return int_pos_p(
num);
315 return int_neg_p(
num);
321 return rb_num_negative_int_p(
num);
325num_funcall_op_0(
VALUE x,
VALUE arg,
int recursive)
369num_funcall_op_1(
VALUE y,
VALUE arg,
int recursive)
374 num_funcall_op_1_recursion(x, func, y);
383 args[0] = (
VALUE)func;
435 coerce_failed(*x, *y);
454 do_coerce(&x, &y,
TRUE);
461 if (do_coerce(&x, &y,
FALSE))
476 VALUE x0 = x, y0 = y;
478 if (!do_coerce(&x, &y,
FALSE)) {
482 return ensure_cmp(
rb_funcall(x, func, 1, y), x0, y0);
522# define num_clone rb_immutable_obj_clone
538# define num_dup num_uplus
586 return num_funcall1(zero,
'-',
num);
652 VALUE z = num_funcall1(x,
'%', y);
655 ((rb_num_negative_int_p(x) &&
656 rb_num_positive_int_p(y)) ||
657 (rb_num_positive_int_p(x) &&
658 rb_num_negative_int_p(y)))) {
759 if (rb_num_negative_int_p(
num)) {
785 if (FIXNUM_ZERO_P(
num)) {
802 return int_zero_p(
num);
889 return rb_num_compare_with_zero(
num, mid);
926 flt->float_value = d;
949 int sign, decpt, digs;
952 static const char minf[] =
"-Infinity";
953 const int pos = (value > 0);
956 else if (
isnan(value))
959 p =
ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
961 if ((digs = (
int)(e - p)) >= (
int)
sizeof(
buf)) digs = (
int)
sizeof(
buf) - 1;
977 memset(
ptr,
'0', decpt - digs);
986 else if (decpt > -4) {
992 memset(
ptr +=
len,
'0', -decpt);
1062 else if (RB_TYPE_P(y,
T_FLOAT)) {
1086 else if (RB_TYPE_P(y,
T_FLOAT)) {
1110 else if (RB_TYPE_P(y,
T_FLOAT)) {
1121 return FLOAT_ZERO_P(
f);
1125double_div_double(
double x,
double y)
1130 else if (x == 0.0) {
1134 double z =
signbit(y) ? -1.0 : 1.0;
1144 double ret = double_div_double(
num, den);
1168 else if (RB_TYPE_P(y,
T_FLOAT)) {
1175 ret = double_div_double(
num, den);
1190 return num_funcall1(x,
'/', y);
1194flodivmod(
double x,
double y,
double *divp,
double *modp)
1200 if (modp) *modp = y;
1201 if (divp) *divp = y;
1227 if (modp) *modp =
mod;
1228 if (divp) *divp =
div;
1240 flodivmod(x, y, 0, &
mod);
1266 else if (RB_TYPE_P(y,
T_FLOAT)) {
1298 volatile VALUE a, b;
1306 else if (RB_TYPE_P(y,
T_FLOAT)) {
1343 else if (RB_TYPE_P(y,
T_FLOAT)) {
1346 if (dx < 0 && dy !=
round(dy))
1389 if (x == y)
return INT2FIX(0);
1397 if (x == y)
return Qtrue;
1398 result = num_funcall1(y,
id_eq, x);
1419 volatile double a, b;
1424 else if (RB_TYPE_P(y,
T_FLOAT)) {
1426#if MSC_VERSION_BEFORE(1300)
1431 return num_equal(x, y);
1434#if MSC_VERSION_BEFORE(1300)
1440#define flo_eq rb_float_equal
1441static VALUE rb_dbl_hash(
double d);
1459rb_dbl_hash(
double d)
1468 if (a == b)
return INT2FIX(0);
1470 if (a < b)
return INT2FIX(-1);
1502 else if (RB_TYPE_P(y,
T_FLOAT)) {
1509 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1512 if (a > 0.0)
return INT2FIX(1);
1523 return NUM2INT(ensure_cmp(flo_cmp(x, y), x, y));
1548 else if (RB_TYPE_P(y,
T_FLOAT)) {
1550#if MSC_VERSION_BEFORE(1300)
1557#if MSC_VERSION_BEFORE(1300)
1585 else if (RB_TYPE_P(y,
T_FLOAT)) {
1587#if MSC_VERSION_BEFORE(1300)
1594#if MSC_VERSION_BEFORE(1300)
1622 else if (RB_TYPE_P(y,
T_FLOAT)) {
1624#if MSC_VERSION_BEFORE(1300)
1631#if MSC_VERSION_BEFORE(1300)
1659 else if (RB_TYPE_P(y,
T_FLOAT)) {
1661#if MSC_VERSION_BEFORE(1300)
1668#if MSC_VERSION_BEFORE(1300)
1693#if MSC_VERSION_BEFORE(1300)
1702#define flo_eql rb_float_eql
1789 return INT2FIX( value < 0 ? -1 : 1 );
1820flo_nextafter(
VALUE flo,
double value)
1881flo_next_float(
VALUE vx)
1883 return flo_nextafter(vx,
HUGE_VAL);
1929flo_prev_float(
VALUE vx)
1931 return flo_nextafter(vx, -
HUGE_VAL);
1939 if (number == 0.0) {
1944 frexp(number, &binexp);
1945 if (float_round_overflow(ndigits, binexp))
return num;
1946 if (number > 0.0 && float_round_underflow(ndigits, binexp))
1948 f = pow(10, ndigits);
1949 f = floor(number *
f) /
f;
1953 num = dbl2ival(floor(number));
1954 if (ndigits < 0)
num = rb_int_floor(
num, ndigits);
2064 if (number == 0.0) {
2069 frexp(number, &binexp);
2070 if (float_round_overflow(ndigits, binexp))
return num;
2071 if (number < 0.0 && float_round_underflow(ndigits, binexp))
2073 f = pow(10, ndigits);
2074 f = ceil(number *
f) /
f;
2078 num = dbl2ival(ceil(number));
2079 if (ndigits < 0)
num = rb_int_ceil(
num, ndigits);
2085int_round_zero_p(
VALUE num,
int ndigits)
2091 bytes =
sizeof(
long);
2099 return (-0.415241 * ndigits - 0.125 > bytes);
2106 if ((z * y - x) * 2 == y) {
2115 return (x + y / 2) / y * y;
2121 return (x + y / 2 - 1) / y * y;
2133 return int_pos_p(
num);
2139 return int_neg_p(
num);
2150 if (int_round_zero_p(
num, ndigits)) {
2154 f = int_pow(10, -ndigits);
2171 if (FIXNUM_POSITIVE_P(r) ||
2179rb_int_floor(
VALUE num,
int ndigits)
2183 if (int_round_zero_p(
num, ndigits))
2185 f = int_pow(10, -ndigits);
2189 if (
neg) x = -x + y - 1;
2206 if (int_round_zero_p(
num, ndigits))
2208 f = int_pow(10, -ndigits);
2231 if (int_round_zero_p(
num, ndigits))
2233 f = int_pow(10, -ndigits);
2247 if (int_neg_p(
num)) {
2310 double number,
f, x;
2320 if (number == 0.0) {
2324 return rb_int_round(flo_to_i(
num), ndigits,
mode);
2332 frexp(number, &binexp);
2333 if (float_round_overflow(ndigits, binexp))
return num;
2334 if (float_round_underflow(ndigits, binexp))
return DBL2NUM(0);
2335 f = pow(10, ndigits);
2343float_round_overflow(
int ndigits,
int binexp)
2364 if (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1)) {
2371float_round_underflow(
int ndigits,
int binexp)
2373 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
2402 if (
f > 0.0)
f = floor(
f);
2403 if (
f < 0.0)
f = ceil(
f);
2545 n= (end - beg)/unit;
2546 err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
2548 return unit > 0 ? beg <= end : beg >= end;
2579 else if (unit == 0) {
2585 for (i=0; i<n; i++) {
2586 double d = i*unit+beg;
2587 if (unit >= 0 ? end < d : d < end) d = end;
2631 case -1: cmp =
'<';
break;
2655 return BIGNUM_NEGATIVE_P(
num);
2677 if (values[0] !=
Qundef) {
2681 if (values[1] !=
Qundef) {
2691num_step_check_fix_args(
int argc,
VALUE *to,
VALUE *step,
VALUE by,
int fix_nil,
int allow_zero_step)
2709 desc = num_step_negative_p(*step);
2710 if (fix_nil &&
NIL_P(*to)) {
2720 argc = num_step_extract_args(
argc,
argv, to, step, &by);
2721 return num_step_check_fix_args(
argc, to, step, by, fix_nil, allow_zero_step);
2728 int argc = args ? RARRAY_LENINT(args) : 0;
2801 num_step_extract_args(
argc,
argv, &to, &step, &by);
2814 num_step_size, from, to, step,
FALSE);
2824 else if (RB_TYPE_P(to,
T_FLOAT)) {
2842 for (; i >= end; i += diff)
2846 for (; i <= end; i += diff)
2859 ID cmp = desc ?
'<' :
'>';
2869out_of_range_float(
char (*pbuf)[24],
VALUE val)
2871 char *
const buf = *pbuf;
2875 if ((s =
strchr(
buf,
' ')) != 0) *s =
'\0';
2879#define FLOAT_OUT_OF_RANGE(val, type) do { \
2881 rb_raise(rb_eRangeError, "float %s out of range of "type, \
2882 out_of_range_float(&buf, (val))); \
2885#define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
2886#define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
2887#define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
2888#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
2889 (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \
2891 LONG_MIN_MINUS_ONE < (n))
2903 else if (RB_TYPE_P(val,
T_FLOAT)) {
2912 else if (RB_TYPE_P(val,
T_BIGNUM)) {
2922rb_num2ulong_internal(
VALUE val,
int *wrap_p)
2933 return (
unsigned long)l;
2935 else if (RB_TYPE_P(val,
T_FLOAT)) {
2939 *wrap_p = d <= -1.0;
2941 return (
unsigned long)d;
2942 return (
unsigned long)(
long)d;
2948 else if (RB_TYPE_P(val,
T_BIGNUM)) {
2952 *wrap_p = BIGNUM_NEGATIVE_P(val);
2965 return rb_num2ulong_internal(val,
NULL);
2972 num,
num < 0 ?
"small" :
"big");
2975#if SIZEOF_INT < SIZEOF_LONG
2979 if ((
long)(
int)
num !=
num) {
2985check_uint(
unsigned long num,
int sign)
2989 if (
num < (
unsigned long)INT_MIN)
3021 unsigned long num = rb_num2ulong_internal(val, &wrap);
3023 check_uint(
num, wrap);
3037 check_uint(
num, rb_num_negative_int_p(val));
3071 num,
num < 0 ?
"small" :
"big");
3075check_short(
long num)
3077 if ((
long)(
short)
num !=
num) {
3078 rb_out_of_short(
num);
3083check_ushort(
unsigned long num,
int sign)
3087 if (
num < (
unsigned long)SHRT_MIN)
3092 if (USHRT_MAX <
num)
3119 unsigned long num = rb_num2ulong_internal(val, &wrap);
3121 check_ushort(
num, wrap);
3135 check_ushort(
num, rb_num_negative_int_p(val));
3154#define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
3155#define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
3156#define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
3158#define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
3160#define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3161 (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \
3163 LLONG_MIN_MINUS_ONE < (n))
3174 else if (RB_TYPE_P(val,
T_FLOAT)) {
3176 if (d < LLONG_MAX_PLUS_ONE && (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d))) {
3177 return (LONG_LONG)d;
3183 else if (RB_TYPE_P(val,
T_BIGNUM)) {
3184 return rb_big2ll(val);
3186 else if (RB_TYPE_P(val,
T_STRING)) {
3189 else if (RB_TYPE_P(val,
T_TRUE) || RB_TYPE_P(val,
T_FALSE)) {
3200 if (RB_TYPE_P(val,
T_NIL)) {
3203 else if (RB_TYPE_P(val,
T_FIXNUM)) {
3206 else if (RB_TYPE_P(val,
T_FLOAT)) {
3208 if (d < ULLONG_MAX_PLUS_ONE && LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3210 return (
unsigned LONG_LONG)d;
3211 return (
unsigned LONG_LONG)(LONG_LONG)d;
3217 else if (RB_TYPE_P(val,
T_BIGNUM)) {
3218 return rb_big2ull(val);
3220 else if (RB_TYPE_P(val,
T_STRING)) {
3223 else if (RB_TYPE_P(val,
T_TRUE) || RB_TYPE_P(val,
T_FALSE)) {
3261 if ((
num & 2) == 0) {
3275 return int_even_p(
num);
3349#define int_succ rb_int_succ
3375#define int_pred rb_int_pred
3469 return fix_uminus(
num);
3502 if (base < 2 || 36 < base) {
3505#if SIZEOF_LONG < SIZEOF_VOIDP
3506# if SIZEOF_VOIDP == SIZEOF_LONG_LONG
3507 if ((val >= 0 && (x & 0xFFFFFFFF00000000ull)) ||
3508 (val < 0 && (x & 0xFFFFFFFF00000000ull) != 0xFFFFFFFF00000000ull)) {
3509 rb_bug(
"Unnormalized Fixnum value %p", (
void *)x);
3520 u = 1 + (
unsigned long)(-(val + 1));
3528 }
while (u /= base);
3574 return rb_fix_plus_fix(x, y);
3579 else if (RB_TYPE_P(y,
T_FLOAT)) {
3593 return fix_plus(x, y);
3600 return fix_plus(x, y);
3621 return rb_fix_minus_fix(x, y);
3627 else if (RB_TYPE_P(y,
T_FLOAT)) {
3639 return fix_minus(x, y);
3648#define SQRT_LONG_MAX HALF_LONG_MSB
3650#define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
3665 return rb_fix_mul_fix(x, y);
3674 else if (RB_TYPE_P(y,
T_FLOAT)) {
3689 return fix_mul(x, y);
3706 else if (RB_TYPE_P(y,
T_FLOAT)) {
3719 if (!FIXNUM_ZERO_P(
gcd)) {
3725 return fix_fdiv_double(x, y);
3770 return rb_fix_div_fix(x, y);
3776 else if (RB_TYPE_P(y,
T_FLOAT)) {
3784 v = fix_divide(x, y,
'/');
3785 return flo_floor(0, 0, v);
3799 return fix_divide(x, y,
'/');
3806 return fix_div(x, y);
3826 return fix_divide(x, y,
id_div);
3833 return fix_idiv(x, y);
3838 return num_div(x, y);
3858 return rb_fix_mod_fix(x, y);
3864 else if (RB_TYPE_P(y,
T_FLOAT)) {
3876 return fix_mod(x, y);
3881 return num_modulo(x, y);
3905 return num_remainder(x, y);
3926 rb_fix_divmod_fix(x, y, &
div, &
mod);
3933 else if (RB_TYPE_P(y,
T_FLOAT)) {
3936 volatile VALUE a, b;
3953 return fix_divmod(x, y);
3981int_pow(
long x,
unsigned long y)
3986 if (y == 0)
return INT2FIX(1);
3995 while (y % 2 == 0) {
4015 if (RB_FLOAT_TYPE_P(v))
4024 return int_pow(x, y);
4037 if (RB_FLOAT_TYPE_P(y)) {
4055 if (a == 1)
return INT2FIX(1);
4056 if (a == -1)
return INT2FIX(b % 2 ? -1 : 1);
4057 if (b < 0)
return fix_pow_inverted(x, fix_uminus(y));
4058 if (b == 0)
return INT2FIX(1);
4059 if (b == 1)
return x;
4060 if (a == 0)
return INT2FIX(0);
4061 return int_pow(a, b);
4064 if (a == 1)
return INT2FIX(1);
4065 if (a == -1)
return INT2FIX(int_even_p(y) ? 1 : -1);
4066 if (BIGNUM_NEGATIVE_P(y))
return fix_pow_inverted(x,
rb_big_uminus(y));
4067 if (a == 0)
return INT2FIX(0);
4071 else if (RB_TYPE_P(y,
T_FLOAT)) {
4073 if (dy == 0.0)
return DBL2NUM(1.0);
4077 if (a == 1)
return DBL2NUM(1.0);
4078 if (a < 0 && dy !=
round(dy))
4080 return DBL2NUM(pow((
double)a, dy));
4091 return fix_pow(x, y);
4103 if (!
NIL_P(z))
return z;
4133 if (x == y)
return Qtrue;
4138 else if (RB_TYPE_P(y,
T_FLOAT)) {
4142 return num_equal(x, y);
4150 return fix_equal(x, y);
4174 if (x == y)
return INT2FIX(0);
4187 else if (RB_TYPE_P(y,
T_FLOAT)) {
4199 return fix_cmp(x, y);
4227 else if (RB_TYPE_P(y,
T_FLOAT)) {
4239 return fix_gt(x, y);
4266 else if (RB_TYPE_P(y,
T_FLOAT)) {
4279 return fix_ge(x, y);
4305 else if (RB_TYPE_P(y,
T_FLOAT)) {
4317 return fix_lt(x, y);
4344 else if (RB_TYPE_P(y,
T_FLOAT)) {
4357 return fix_le(x, y);
4368 return ~num | FIXNUM_FLAG;
4375 return fix_comp(
num);
4384num_funcall_bit_1(
VALUE y,
VALUE arg,
int recursive)
4389 num_funcall_op_1_recursion(x, func, y);
4399 args[0] = (
VALUE)func;
4402 do_coerce(&args[1], &args[2],
TRUE);
4404 args[2], args[1], (
VALUE)args);
4407 coerce_failed(x, y);
4439 return fix_and(x, y);
4474 return fix_or(x, y);
4509 return fix_xor(x, y);
4537 return fix_rshift(val, (
unsigned long)-width);
4538 return fix_lshift(val, width);
4542fix_lshift(
long val,
unsigned long width)
4544 if (width > (SIZEOF_LONG*
CHAR_BIT-1)
4545 || ((
unsigned long)val)>>(SIZEOF_LONG*
CHAR_BIT-1-width) > 0) {
4556 return rb_fix_lshift(x, y);
4583 if (i == 0)
return x;
4585 return fix_lshift(val, (
unsigned long)-i);
4586 return fix_rshift(val, i);
4590fix_rshift(
long val,
unsigned long i)
4592 if (i >=
sizeof(
long)*
CHAR_BIT-1) {
4593 if (val < 0)
return INT2FIX(-1);
4596 val = RSHIFT(val, i);
4604 return rb_fix_rshift(x, y);
4622 if (!BIGNUM_SIGN(idx) || val >= 0)
4631 if (val < 0)
return INT2FIX(1);
4671 if (!
RTEST(num_negative_p(end))) {
4685 num = rb_int_rshift(
num, beg);
4687 int cmp = compare_indexes(beg, end);
4688 if (!
NIL_P(end) && cmp < 0) {
4694 else if (cmp == 0) {
4716 num = rb_int_rshift(
num, beg);
4767 return int_aref1(
num,
argv[0]);
4829 return fix_abs(
num);
4863 return fix_size(
num);
4872rb_fix_bit_length(
VALUE fix)
4884 return rb_fix_bit_length(
num);
4913rb_fix_digits(
VALUE fix,
long base)
4948 else if (RB_TYPE_P(base,
T_BIGNUM) && BIGNUM_NEGATIVE_P(base))
4981 if (RB_TYPE_P(base_value,
T_BIGNUM))
4982 return rb_int_digits_bigbase(
num, base_value);
4994 return rb_fix_digits(
num, base);
4996 return rb_int_digits_bigbase(
num,
LONG2FIX(base));
5029 for (i =
FIX2LONG(from); i <= end; i++) {
5040 ensure_cmp(c, i, to);
5075 for (i=
FIX2LONG(from); i >= end; i--) {
5126 for (i=0; i<end; i++) {
5187 return rb_int_round(
num, ndigits,
mode);
5219 return rb_int_floor(
num, ndigits);
5251 return rb_int_ceil(
num, ndigits);
5286#define DEFINE_INT_SQRT(rettype, prefix, argtype) \
5288prefix##_isqrt(argtype n) \
5290 if (!argtype##_IN_DOUBLE_P(n)) { \
5291 unsigned int b = bit_length(n); \
5293 rettype x = (rettype)(n >> (b/2+1)); \
5294 x |= ((rettype)1LU << (b-1)/2); \
5295 while ((t = n/x) < (argtype)x) x = (rettype)((x + t) >> 1); \
5298 return (rettype)sqrt(argtype##_TO_DOUBLE(n)); \
5301#if SIZEOF_LONG*CHAR_BIT > DBL_MANT_DIG
5302# define RB_ULONG_IN_DOUBLE_P(n) ((n) < (1UL << DBL_MANT_DIG))
5304# define RB_ULONG_IN_DOUBLE_P(n) 1
5306#define RB_ULONG_TO_DOUBLE(n) (double)(n)
5307#define RB_ULONG unsigned long
5310#if 2*SIZEOF_BDIGIT > SIZEOF_LONG
5311# if 2*SIZEOF_BDIGIT*CHAR_BIT > DBL_MANT_DIG
5312# define BDIGIT_DBL_IN_DOUBLE_P(n) ((n) < ((BDIGIT_DBL)1UL << DBL_MANT_DIG))
5314# define BDIGIT_DBL_IN_DOUBLE_P(n) 1
5316# ifdef ULL_TO_DOUBLE
5317# define BDIGIT_DBL_TO_DOUBLE(n) ULL_TO_DOUBLE(n)
5319# define BDIGIT_DBL_TO_DOUBLE(n) (double)(n)
5324#define domain_error(msg) \
5325 rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
5358 unsigned long n, sq;
5361 if (FIXNUM_NEGATIVE_P(
num)) {
5370 if (RBIGNUM_NEGATIVE_P(
num)) {
5373 biglen = BIGNUM_LEN(
num);
5374 if (biglen == 0)
return INT2FIX(0);
5375#if SIZEOF_BDIGIT <= SIZEOF_LONG
5378 n = BIGNUM_DIGITS(
num)[0];
5489 id_coerce = rb_intern_const(
"coerce");
5490 id_to = rb_intern_const(
"to");
5491 id_by = rb_intern_const(
"by");
5723#undef rb_float_value
5727 return rb_float_value_inline(v);
5734 return rb_float_new_inline(d);
5737#include "integer.rbinc"
VALUE rb_ary_push(VALUE ary, VALUE item)
VALUE rb_assoc_new(VALUE car, VALUE cdr)
#define UNREACHABLE_RETURN
VALUE rb_big_lshift(VALUE x, VALUE y)
VALUE rb_big_even_p(VALUE num)
VALUE rb_big_and(VALUE x, VALUE y)
VALUE rb_big_or(VALUE x, VALUE y)
size_t rb_big_size(VALUE big)
VALUE rb_big_minus(VALUE x, VALUE y)
VALUE rb_big_modulo(VALUE x, VALUE y)
double rb_big_fdiv_double(VALUE x, VALUE y)
VALUE rb_big_pow(VALUE x, VALUE y)
VALUE rb_int2big(intptr_t n)
int rb_bigzero_p(VALUE x)
VALUE rb_big_comp(VALUE x)
VALUE rb_big_plus(VALUE x, VALUE y)
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
unsigned long rb_big2ulong(VALUE x)
VALUE rb_big_idiv(VALUE x, VALUE y)
VALUE rb_big_gt(VALUE x, VALUE y)
VALUE rb_big_ge(VALUE x, VALUE y)
VALUE rb_big2str(VALUE x, int base)
VALUE rb_big_aref(VALUE x, VALUE y)
COMPILER_WARNING_POP VALUE rb_integer_float_eq(VALUE x, VALUE y)
VALUE rb_big_bit_length(VALUE big)
VALUE rb_big_cmp(VALUE x, VALUE y)
VALUE rb_dbl2big(double d)
VALUE rb_big_eq(VALUE x, VALUE y)
VALUE rb_big_eql(VALUE x, VALUE y)
VALUE rb_big_mul(VALUE x, VALUE y)
VALUE rb_big_lt(VALUE x, VALUE y)
VALUE rb_big_remainder(VALUE x, VALUE y)
VALUE rb_big_odd_p(VALUE num)
VALUE rb_big_size_m(VALUE big)
const char ruby_digitmap[]
VALUE rb_int_powm(int const argc, VALUE *const argv, VALUE const num)
VALUE rb_integer_float_cmp(VALUE x, VALUE y)
VALUE rb_big_divmod(VALUE x, VALUE y)
VALUE rb_big_xor(VALUE x, VALUE y)
VALUE rb_big_div(VALUE x, VALUE y)
VALUE rb_big_norm(VALUE x)
VALUE rb_big_uminus(VALUE x)
VALUE rb_big_rshift(VALUE x, VALUE y)
double rb_big2dbl(VALUE x)
VALUE rb_big_abs(VALUE x)
long rb_big2long(VALUE x)
unsigned long rb_ulong_isqrt(unsigned long)
VALUE rb_big_le(VALUE x, VALUE y)
#define MUL_OVERFLOW_FIXNUM_P(a, b)
int decode(struct state *s, struct huffman *h)
void rb_cmperr(VALUE x, VALUE y)
Internal header absorbing C compipler differences.
VALUE rb_complex_plus(VALUE self, VALUE other)
VALUE rb_complex_pow(VALUE self, VALUE other)
VALUE rb_dbl_complex_new_polar_pi(double abs, double ang)
VALUE rb_complex_new(VALUE x, VALUE y)
VALUE rb_complex_mul(VALUE self, VALUE other)
Our own, locale independent, character handling routines.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
char * strchr(char *, char)
#define MJIT_FUNC_EXPORTED
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
rb_encoding * rb_ascii8bit_encoding(void)
rb_encoding * rb_default_internal_encoding(void)
rb_encoding * rb_to_encoding(VALUE enc)
int rb_enc_codelen(int c, rb_encoding *enc)
#define rb_cmpint(cmp, a, b)
VALUE rb_arith_seq_new(VALUE obj, VALUE meth, int argc, VALUE const *argv, rb_enumerator_size_func *size_fn, VALUE beg, VALUE end, VALUE step, int excl)
char str[HTML_ESCAPE_MAX_LEN+1]
#define rb_deprecate_constant(mod, name)
#define RSTRING_LEN(string)
#define RSTRING_PTR(string)
VALUE rb_eMathDomainError
void rb_include_module(VALUE klass, VALUE module)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
ID rb_frame_this_func(void)
The original name of the current method.
void rb_undef_method(VALUE klass, const char *name)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
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)
void rb_raise(VALUE exc, const char *fmt,...)
void rb_bug(const char *fmt,...)
void rb_name_error(ID id, const char *fmt,...)
VALUE rb_Float(VALUE)
Equivalent to Kernel#Float in Ruby.
VALUE rb_cObject
Object class.
VALUE rb_any_to_s(VALUE)
Default implementation of #to_s.
VALUE rb_obj_class(VALUE)
VALUE rb_inspect(VALUE)
Convenient wrapper of Object::inspect.
VALUE rb_equal(VALUE, VALUE)
This function is an optimized version of calling #==.
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
VALUE rb_immutable_obj_clone(int, VALUE *, VALUE)
VALUE rb_to_int(VALUE)
Converts val into Integer.
unsigned gcd(unsigned a, unsigned b)
long rb_dbl_long_hash(double d)
#define rb_enc_mbcput(c, buf, enc)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Thin wrapper to ruby/config.h.
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Defines RBIMPL_HAS_BUILTIN.
#define SIZED_ENUMERATOR(obj, argc, argv, size_fn)
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
void rb_error_arity(int, int, int)
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
VALUE rb_rational_raw(VALUE, VALUE)
int rb_memcicmp(const void *, const void *, long)
VALUE rb_str_resize(VALUE, long)
void rb_must_asciicompat(VALUE)
#define rb_str_new(str, len)
VALUE rb_str_cat(VALUE, const char *, long)
#define rb_usascii_str_new(str, len)
VALUE rb_check_string_type(VALUE)
#define rb_usascii_str_new_cstr(str)
#define rb_usascii_str_new2
VALUE rb_exec_recursive_paired(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE, VALUE)
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
void rb_remove_method_id(VALUE, ID)
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
void rb_undef_alloc_func(VALUE)
const char * rb_id2name(ID)
ID rb_intern(const char *)
void rb_define_const(VALUE, const char *, VALUE)
void * memmove(void *, const void *, size_t)
double nextafter(double x, double y)
unsigned LONG_LONG rb_num2ull(VALUE)
LONG_LONG rb_num2ll(VALUE)
Internal header for Array.
Internal header for Complex.
Internal header for Enumerator.
Internal header for Hash.
Internal header for Numeric.
@ RUBY_NUM_ROUND_HALF_EVEN
@ RUBY_NUM_ROUND_HALF_DOWN
#define ROUND_CALL(mode, name, args)
Internal header for Object.
Internal header for Rational.
VALUE rb_rational_pow(VALUE self, VALUE other)
VALUE rb_rational_reciprocal(VALUE x)
VALUE rb_gcd(VALUE x, VALUE y)
Internal header corresponding util.c.
char * ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
VALUE rb_yield_1(VALUE val)
#define rb_ary_new_from_args(...)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
Internal header for Math.
int rb_int_negative_p(VALUE num)
void rb_num_zerodiv(void)
VALUE rb_float_uminus(VALUE flt)
const union bytesequence4_or_float rb_infinity
VALUE rb_float_floor(VALUE num, int ndigits)
VALUE rb_int_modulo(VALUE x, VALUE y)
VALUE rb_fix_aref(VALUE fix, VALUE idx)
#define FLOAT_OUT_OF_RANGE(val, type)
VALUE rb_num_pow(VALUE x, VALUE y)
VALUE rb_int_comp(VALUE num)
VALUE rb_int_truncate(VALUE num, int ndigits)
VALUE rb_num2fix(VALUE val)
VALUE rb_int_abs(VALUE num)
VALUE rb_int_div(VALUE x, VALUE y)
#define method_basic_p(klass)
VALUE rb_int_zero_p(VALUE num)
short rb_fix2short(VALUE val)
VALUE rb_float_pow(VALUE x, VALUE y)
VALUE rb_int_pow(VALUE x, VALUE y)
long rb_num2long(VALUE val)
VALUE rb_float_equal(VALUE x, VALUE y)
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
VALUE rb_int_uminus(VALUE num)
VALUE rb_float_mul(VALUE x, VALUE y)
VALUE rb_int_equal(VALUE x, VALUE y)
VALUE rb_float_plus(VALUE x, VALUE y)
VALUE rb_int_positive_pow(long x, unsigned long y)
VALUE rb_flo_div_flo(VALUE x, VALUE y)
VALUE rb_num_coerce_cmp(VALUE x, VALUE y, ID func)
VALUE rb_int_cmp(VALUE x, VALUE y)
#define ULONG_MAX_PLUS_ONE
VALUE rb_int2str(VALUE x, int base)
double ruby_float_mod(double x, double y)
VALUE rb_float_eql(VALUE x, VALUE y)
unsigned long rb_fix2uint(VALUE val)
#define domain_error(msg)
VALUE rb_int_and(VALUE x, VALUE y)
VALUE rb_float_abs(VALUE flt)
VALUE rb_int_even_p(VALUE num)
VALUE rb_num_coerce_bin(VALUE x, VALUE y, ID func)
#define DEFINE_INT_SQRT(rettype, prefix, argtype)
VALUE rb_int_lshift(VALUE x, VALUE y)
double ruby_float_step_size(double beg, double end, double unit, int excl)
short rb_num2short(VALUE val)
VALUE rb_float_gt(VALUE x, VALUE y)
VALUE rb_int_fdiv(VALUE x, VALUE y)
VALUE rb_float_ceil(VALUE num, int ndigits)
double rb_int_fdiv_double(VALUE x, VALUE y)
int rb_int_positive_p(VALUE num)
VALUE rb_eFloatDomainError
void rb_out_of_int(SIGNED_VALUE num)
VALUE ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
unsigned long rb_num2uint(VALUE val)
int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl, int allow_endless)
enum ruby_num_rounding_mode rb_num_get_rounding_option(VALUE opts)
VALUE rb_flo_is_finite_p(VALUE num)
const union bytesequence4_or_float rb_nan
VALUE rb_num_coerce_bit(VALUE x, VALUE y, ID func)
VALUE rb_int_ge(VALUE x, VALUE y)
VALUE rb_float_div(VALUE x, VALUE y)
VALUE rb_fix2str(VALUE x, int base)
#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n)
VALUE rb_int_minus(VALUE x, VALUE y)
VALUE rb_flo_is_infinite_p(VALUE num)
VALUE rb_int_odd_p(VALUE num)
long rb_num2int(VALUE val)
int rb_num_to_uint(VALUE val, unsigned int *ret)
VALUE rb_fix_plus(VALUE x, VALUE y)
unsigned short rb_fix2ushort(VALUE val)
unsigned long rb_num2ulong(VALUE val)
VALUE rb_int_divmod(VALUE x, VALUE y)
int rb_float_cmp(VALUE x, VALUE y)
VALUE rb_float_new_in_heap(double d)
VALUE rb_int_bit_length(VALUE num)
VALUE rb_int_idiv(VALUE x, VALUE y)
VALUE rb_big_isqrt(VALUE)
int rb_num_negative_p(VALUE num)
VALUE rb_dbl_cmp(double a, double b)
VALUE rb_float_minus(VALUE x, VALUE y)
unsigned short rb_num2ushort(VALUE val)
VALUE rb_num_coerce_relop(VALUE x, VALUE y, ID func)
#define LONG_MAX_PLUS_ONE
long rb_fix2int(VALUE val)
VALUE rb_int_plus(VALUE x, VALUE y)
VALUE rb_int_gt(VALUE x, VALUE y)
VALUE rb_int_succ(VALUE num)
VALUE rb_int_mul(VALUE x, VALUE y)
#define ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
#define ONIGERR_INVALID_CODE_POINT_VALUE
#define RARRAY_CONST_PTR(s)
#define RARRAY_AREF(a, i)
#define RGENGC_WB_PROTECTED_FLOAT
const char * rb_obj_classname(VALUE)
#define RB_INTEGER_TYPE_P(obj)
VALUE rb_str_catf(VALUE, const char *,...)
size_t strlen(const char *)
int inf(FILE *source, FILE *dest)