21 #if defined(__FreeBSD__) && __FreeBSD__ < 4 22 #include <floatingpoint.h> 33 #if !defined HAVE_ISFINITE && !defined isfinite 34 #if defined HAVE_FINITE && !defined finite && !defined _WIN32 36 # define HAVE_ISFINITE 1 37 # define isfinite(x) finite(x) 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 60 #ifndef DBL_MIN_10_EXP 61 #define DBL_MIN_10_EXP (-307) 63 #ifndef DBL_MAX_10_EXP 64 #define DBL_MAX_10_EXP 308 70 #define DBL_MANT_DIG 53 73 #define DBL_EPSILON 2.2204460492503131e-16 77 #elif !defined(WORDS_BIGENDIAN) 84 #elif !defined(WORDS_BIGENDIAN) 98 x =
f + (x -
f >= 0.5);
102 x =
f - (
f - x >= 0.5);
134 #define NUMERR_TYPE 1 135 #define NUMERR_NEGATIVE 2 136 #define NUMERR_TOOLARGE 3 139 #if SIZEOF_INT < SIZEOF_LONG 143 *ret = (
unsigned int)v;
149 #if SIZEOF_INT < SIZEOF_LONG 162 #define method_basic_p(klass) rb_method_basic_definition_p(klass, mid) 262 a[0] = *x; a[1] = *y;
302 VALUE c, x0 = x, y0 = y;
325 "can't define singleton method \"%s\" for %s",
646 flt->float_value = d;
663 char *
ruby_dtoa(
double d_,
int mode,
int ndigits,
int *decpt,
int *sign,
char **rve);
670 int sign, decpt, digs;
674 else if (
isnan(value))
677 p =
ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
679 if ((digs = (
int)(e - p)) >= (
int)
sizeof(
buf)) digs = (int)
sizeof(
buf) - 1;
680 memcpy(
buf, p, digs);
695 memset(ptr,
'0', decpt - digs);
698 memcpy(ptr,
".0", 2);
704 else if (decpt > -4) {
710 memset(ptr += len,
'0', -decpt);
711 memcpy(ptr -= decpt,
buf, digs);
877 flodivmod(
double x,
double y,
double *divp,
double *modp)
902 if (modp) *modp =
mod;
903 if (divp) *divp =
div;
1018 if (dx < 0 && dy !=
round(dy))
1059 if (x == y)
return INT2FIX(0);
1066 if (x == y)
return Qtrue;
1087 volatile double a, b;
1094 #if defined(_MSC_VER) && _MSC_VER < 1300 1102 #if defined(_MSC_VER) && _MSC_VER < 1300 1127 if (d == 0.0) d = 0.0;
1136 if (a == b)
return INT2FIX(0);
1138 if (a < b)
return INT2FIX(-1);
1176 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1179 if (a > 0.0)
return INT2FIX(1);
1211 #if defined(_MSC_VER) && _MSC_VER < 1300 1218 #if defined(_MSC_VER) && _MSC_VER < 1300 1248 #if defined(_MSC_VER) && _MSC_VER < 1300 1255 #if defined(_MSC_VER) && _MSC_VER < 1300 1285 #if defined(_MSC_VER) && _MSC_VER < 1300 1292 #if defined(_MSC_VER) && _MSC_VER < 1300 1322 #if defined(_MSC_VER) && _MSC_VER < 1300 1329 #if defined(_MSC_VER) && _MSC_VER < 1300 1354 #if defined(_MSC_VER) && _MSC_VER < 1300 1455 return INT2FIX( value < 0 ? -1 : 1 );
1476 if (!isfinite(value))
1548 if (-0.415241 * ndigits - 0.125 > bytes ) {
1557 x = (x + y / 2) / y * y;
1628 frexp(number, &binexp);
1648 (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1))) {
1651 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
1654 f = pow(10, ndigits);
1675 if (
f > 0.0)
f = floor(
f);
1676 if (
f < 0.0)
f = ceil(
f);
1766 double n = (end - beg)/unit;
1767 double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
1770 return unit > 0 ? beg <= end : beg >= end;
1804 else if (unit == 0) {
1810 for (i=0; i<n; i++) {
1811 double d = i*unit+beg;
1812 if (unit >= 0 ? end < d : d < end) d = end;
1856 case -1:
cmp =
'<';
break;
1880 if (values[0] !=
Qundef) {
1884 if (values[1] !=
Qundef) {
2003 for (; i >= end; i +=
diff)
2007 for (; i <= end; i +=
diff)
2020 ID cmp = desc ?
'<' :
'>';
2029 #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1) 2030 #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1)) 2031 #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1)) 2032 #define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \ 2033 (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \ 2035 LONG_MIN_MINUS_ONE < (n)) 2057 if ((s =
strchr(
buf,
' ')) != 0) *s =
'\0';
2070 static unsigned long 2082 return (
unsigned long)l;
2089 *wrap_p = d <= -1.0;
2091 return (
unsigned long)d;
2092 return (
unsigned long)(long)d;
2099 if ((s =
strchr(
buf,
' ')) != 0) *s =
'\0';
2123 #if SIZEOF_INT < SIZEOF_LONG 2128 num, num < 0 ?
"small" :
"big");
2134 if ((
long)(
int)num != num) {
2140 check_uint(
unsigned long num,
int sign)
2144 if (num < (
unsigned long)INT_MIN)
2178 check_uint(num, wrap);
2188 return rb_num2uint(
val);
2213 num, num < 0 ?
"small" :
"big");
2219 if ((
long)(
short)num != num) {
2229 if (num < (
unsigned long)SHRT_MIN)
2234 if (USHRT_MAX < num)
2296 #define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1) 2297 #define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1)) 2298 #define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1)) 2300 #define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1) 2302 #define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \ 2303 (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \ 2305 LLONG_MIN_MINUS_ONE < (n)) 2326 if ((s =
strchr(
buf,
' ')) != 0) *s =
'\0';
2331 return rb_big2ll(
val);
2365 if ((s =
strchr(
buf,
' ')) != 0) *s =
'\0';
2370 return rb_big2ull(
val);
2380 return NUM2ULL(
val);
2495 #define int_succ rb_int_succ 2520 #define int_pred rb_int_pred 2660 if (base < 2 || 36 < base) {
2673 }
while (
val /= base);
2701 if (
argc == 0) base = 10;
2779 #define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2)) 2781 #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX)) 2801 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG 2810 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG 2811 d = (LONG_LONG)a * b;
2813 return rb_ll2inum(d);
2815 if (a == 0)
return x;
2853 if ((mod < 0 && y > 0) || (
mod > 0 && y < 0)) {
2857 if (divp) *divp =
div;
2858 if (modp) *modp =
mod;
3007 volatile VALUE a, b;
3033 while (y % 2 == 0) {
3081 if (a == 1)
return INT2FIX(1);
3091 if (b == 0)
return INT2FIX(1);
3092 if (b == 1)
return x;
3100 if (a == 1)
return INT2FIX(1);
3107 if (a == 0)
return INT2FIX(0);
3116 if (a == 1)
return DBL2NUM(1.0);
3119 if (a < 0 && dy !=
round(dy))
3121 return DBL2NUM(pow((
double)a, dy));
3142 if (x == y)
return Qtrue;
3170 if (x == y)
return INT2FIX(0);
3421 if (width > (SIZEOF_LONG*
CHAR_BIT-1)
3422 || ((
unsigned long)
val)>>(SIZEOF_LONG*
CHAR_BIT-1-width) > 0) {
3445 if (i == 0)
return x;
3454 if (i >=
sizeof(
long)*
CHAR_BIT-1) {
3631 for (i =
FIX2LONG(from); i <= end; i++) {
3676 for (i=
FIX2LONG(from); i >= end; i--) {
3729 for (i=0; i<end; i++) {
3765 if (
argc == 0)
return num;
3858 #define rb_intern(str) rb_intern_const(str) 3860 #if defined(__FreeBSD__) && __FreeBSD__ < 4 3862 fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL));
3863 #elif defined(_UNICOSMP) 3866 #elif defined(__BORLANDC__) 3868 _control87(MCW_EM, MCW_EM);
3869 _control87(_control87(0,0),0x1FFF);
4116 #undef rb_float_value unsigned short rb_fix2ushort(VALUE val)
VALUE rb_big_modulo(VALUE x, VALUE y)
static VALUE fix_fdiv(VALUE x, VALUE y)
static int do_coerce(VALUE *x, VALUE *y, int err)
static VALUE flo_to_s(VALUE flt)
static VALUE num_abs(VALUE num)
int rb_enc_codelen(int c, rb_encoding *enc)
static VALUE num_remainder(VALUE x, VALUE y)
short rb_num2short(VALUE val)
static VALUE num_coerce(VALUE x, VALUE y)
static VALUE fix_minus(VALUE x, VALUE y)
static VALUE flo_plus(VALUE x, VALUE y)
static VALUE fix_le(VALUE x, VALUE y)
#define ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
static VALUE flo_hash(VALUE num)
static VALUE flo_cmp(VALUE x, VALUE y)
static VALUE flo_zero_p(VALUE num)
static VALUE rb_fix_rshift(VALUE x, VALUE y)
VALUE rb_num_coerce_bit(VALUE x, VALUE y, ID func)
static VALUE num_step_size(VALUE from, VALUE args, VALUE eobj)
VALUE rb_big2ulong(VALUE x)
void rb_undef_alloc_func(VALUE)
RUBY_EXTERN int signbit(double x)
static VALUE num_equal(VALUE x, VALUE y)
static VALUE fix_xor(VALUE x, VALUE y)
static VALUE fix_size(VALUE fix)
#define rb_usascii_str_new2
const union bytesequence4_or_float rb_infinity
VALUE rb_str_cat(VALUE, const char *, long)
static VALUE num_zero_p(VALUE num)
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
static double ruby_float_step_size(double beg, double end, double unit, int excl)
#define LONG_MAX_PLUS_ONE
double rb_float_value(VALUE v)
const char ruby_digitmap[]
VALUE rb_big_plus(VALUE x, VALUE y)
rb_encoding * rb_to_encoding(VALUE enc)
double ruby_float_mod(double x, double y)
VALUE rb_fix2str(VALUE x, int base)
VALUE rb_dbl_cmp(double a, double b)
static VALUE fix_mul(VALUE x, VALUE y)
rb_encoding * rb_default_internal_encoding(void)
static VALUE flo_quo(VALUE x, VALUE y)
SSL_METHOD *(* func)(void)
st_index_t rb_memhash(const void *ptr, long len)
static VALUE fix_zero_p(VALUE num)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
static VALUE fix_to_s(int argc, VALUE *argv, VALUE x)
static VALUE flo_to_f(VALUE num)
static VALUE num_modulo(VALUE x, VALUE y)
VALUE rb_big_fdiv(VALUE x, VALUE y)
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_integer_float_cmp(VALUE x, VALUE y)
VALUE rb_num2ulong(VALUE val)
int rb_num_negative_p(VALUE num)
void rb_include_module(VALUE klass, VALUE module)
static VALUE num_imaginary(VALUE num)
double rb_big2dbl(VALUE x)
VALUE rb_num2fix(VALUE val)
#define rb_complex_raw1(x)
static VALUE rb_fix_bit_length(VALUE fix)
unsigned short rb_num2ushort(VALUE val)
RUBY_EXTERN int finite(double)
static VALUE dbl2ival(double d)
static VALUE flo_abs(VALUE flt)
static int negative_int_p(VALUE num)
VALUE rb_int_pred(VALUE num)
static VALUE flo_ge(VALUE x, VALUE y)
static VALUE fix_idiv(VALUE x, VALUE y)
void rb_undef_method(VALUE klass, const char *name)
int rb_cmpint(VALUE val, VALUE a, VALUE b)
static VALUE flo_divmod(VALUE x, VALUE y)
static VALUE fix_or(VALUE x, VALUE y)
static VALUE flo_lt(VALUE x, VALUE y)
static VALUE flo_truncate(VALUE num)
const char * rb_obj_classname(VALUE)
static VALUE flo_pow(VALUE x, VALUE y)
RUBY_EXTERN void * memmove(void *, const void *, size_t)
short rb_fix2short(VALUE val)
static int bit_coerce(VALUE *x, VALUE *y, int err)
#define NEWOBJ_OF(obj, type, klass, flags)
const union bytesequence4_or_float rb_nan
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
#define RB_TYPE_P(obj, type)
VALUE rb_big_divmod(VALUE x, VALUE y)
VALUE rb_rescue(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2)
static VALUE coerce_body(VALUE *x)
#define rb_rational_raw1(x)
VALUE rb_dbl2big(double d)
static VALUE num_real_p(VALUE num)
VALUE rb_big_eq(VALUE x, VALUE y)
static VALUE fix_divide(VALUE x, VALUE y, ID op)
RUBY_EXTERN VALUE rb_cObject
static VALUE int_even_p(VALUE num)
static int positive_int_p(VALUE num)
#define RBIGNUM_POSITIVE_P(b)
VALUE rb_int_positive_pow(long x, unsigned long y)
static VALUE flo_is_nan_p(VALUE num)
static VALUE num_to_int(VALUE num)
VALUE rb_big_cmp(VALUE x, VALUE y)
static VALUE num_uplus(VALUE num)
static VALUE int_dotimes(VALUE num)
VALUE rb_num_coerce_relop(VALUE x, VALUE y, ID func)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
static VALUE num_floor(VALUE num)
static VALUE int_upto(VALUE from, VALUE to)
void rb_define_const(VALUE, const char *, VALUE)
static VALUE fix_div(VALUE x, VALUE y)
void rb_remove_method_id(VALUE, ID)
static VALUE int_odd_p(VALUE num)
static VALUE num_ceil(VALUE num)
int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl)
static VALUE coerce_rescue(VALUE *x)
RUBY_EXTERN int isinf(double)
static VALUE fix_pow(VALUE x, VALUE y)
static VALUE fix_rshift(long, unsigned long)
static VALUE fix_equal(VALUE x, VALUE y)
static VALUE num_fdiv(VALUE x, VALUE y)
static VALUE int_downto(VALUE from, VALUE to)
SIGNED_VALUE rb_big2long(VALUE x)
static VALUE flo_ceil(VALUE num)
void rb_num_zerodiv(void)
VALUE rb_eFloatDomainError
static VALUE int_chr(int argc, VALUE *argv, VALUE num)
static VALUE fix_plus(VALUE x, VALUE y)
static VALUE rb_float_new_inline(double d)
static VALUE fix_gt(VALUE x, VALUE y)
static VALUE num_step(int argc, VALUE *argv, VALUE from)
static VALUE int_round(int argc, VALUE *argv, VALUE num)
VALUE rb_str_resize(VALUE, long)
static VALUE fix_uminus(VALUE num)
static VALUE fix_lt(VALUE x, VALUE y)
static VALUE fix_odd_p(VALUE num)
static VALUE num_truncate(VALUE num)
static VALUE num_int_p(VALUE num)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
VALUE rb_big_minus(VALUE x, VALUE y)
static VALUE fix_even_p(VALUE num)
#define MUL_OVERFLOW_FIXNUM_P(a, b)
long rb_fix2int(VALUE val)
VALUE rb_big_div(VALUE x, VALUE y)
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
static VALUE fix_aref(VALUE fix, VALUE idx)
static VALUE fix_and(VALUE x, VALUE y)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
unsigned char buf[MIME_BUF_SIZE]
VALUE rb_assoc_new(VALUE car, VALUE cdr)
static unsigned long rb_num2ulong_internal(VALUE val, int *wrap_p)
static VALUE num_divmod(VALUE x, VALUE y)
VALUE rb_num_coerce_cmp(VALUE x, VALUE y, ID func)
static VALUE fix_cmp(VALUE x, VALUE y)
VALUE rb_big_mul(VALUE x, VALUE y)
static VALUE flo_is_finite_p(VALUE num)
static VALUE int_downto_size(VALUE from, VALUE args, VALUE eobj)
void rb_out_of_short(SIGNED_VALUE num)
#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n)
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
static void coerce_failed(VALUE x, VALUE y)
char * strchr(char *, char)
static VALUE flo_gt(VALUE x, VALUE y)
static VALUE num_cmp(VALUE x, VALUE y)
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
static VALUE int_round_0(VALUE num, int ndigits)
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
#define RARRAY_LENINT(ary)
VALUE rb_complex_new(VALUE x, VALUE y)
static VALUE fix_succ(VALUE num)
static VALUE int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
int rb_respond_to(VALUE, ID)
static int num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step)
static VALUE int_pow(long x, unsigned long y)
static VALUE int_int_p(VALUE num)
static VALUE flo_coerce(VALUE x, VALUE y)
static VALUE flo_round(int argc, VALUE *argv, VALUE num)
static VALUE fix_ge(VALUE x, VALUE y)
VALUE rb_equal(VALUE, VALUE)
static VALUE flo_minus(VALUE x, VALUE y)
static VALUE flo_eq(VALUE x, VALUE y)
static VALUE num_eql(VALUE x, VALUE y)
VALUE rb_dbl_hash(double d)
long rb_num2int(VALUE val)
#define RARRAY_AREF(a, i)
static VALUE num_div(VALUE x, VALUE y)
static void check_short(long num)
static VALUE int_to_i(VALUE num)
static VALUE num_init_copy(VALUE x, VALUE y)
VALUE rb_big_and(VALUE x, VALUE y)
VALUE rb_rational_reciprocal(VALUE x)
static VALUE fix_mod(VALUE x, VALUE y)
VALUE rb_str_catf(VALUE str, const char *format,...)
VALUE rb_big_norm(VALUE x)
VALUE rb_float_new(double d)
VALUE rb_big_lshift(VALUE x, VALUE y)
SIGNED_VALUE rb_num2long(VALUE val)
#define ULONG_MAX_PLUS_ONE
VALUE rb_float_new_in_heap(double d)
static Bigint * diff(Bigint *a, Bigint *b)
static VALUE flo_div(VALUE x, VALUE y)
#define RGENGC_WB_PROTECTED_FLOAT
#define method_basic_p(klass)
static VALUE int_ord(VALUE num)
static VALUE flo_floor(VALUE num)
VALUE rb_big_pow(VALUE x, VALUE y)
static void check_ushort(unsigned long num, int sign)
int rb_num_to_uint(VALUE val, unsigned int *ret)
static VALUE num_uminus(VALUE num)
VALUE rb_int2big(SIGNED_VALUE n)
static unsigned int hash(const char *str, unsigned int len)
VALUE ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
static VALUE flo_le(VALUE x, VALUE y)
VALUE rb_integer_float_eq(VALUE x, VALUE y)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
NORETURN(static void coerce_failed(VALUE x, VALUE y))
VALUE rb_int_succ(VALUE num)
static VALUE fix_lshift(long, unsigned long)
static VALUE int_upto_size(VALUE from, VALUE args, VALUE eobj)
const char * rb_id2name(ID id)
static double rb_float_value_inline(VALUE v)
rb_encoding * rb_ascii8bit_encoding(void)
static VALUE flo_is_infinite_p(VALUE num)
static VALUE flo_eql(VALUE x, VALUE y)
static VALUE flo_mod(VALUE x, VALUE y)
static VALUE flo_mul(VALUE x, VALUE y)
#define RBIGNUM_NEGATIVE_P(b)
VALUE rb_big_rshift(VALUE x, VALUE y)
#define SPECIAL_CONST_P(x)
static VALUE num_round(int argc, VALUE *argv, VALUE num)
#define ONIGERR_INVALID_CODE_POINT_VALUE
#define rb_enc_mbcput(c, buf, enc)
static VALUE fix_abs(VALUE fix)
VALUE rb_usascii_str_new(const char *, long)
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
static VALUE rb_fix_lshift(VALUE x, VALUE y)
static VALUE flo_uminus(VALUE flt)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
static VALUE num_sadded(VALUE x, VALUE name)
VALUE rb_big_or(VALUE x, VALUE y)
static void flodivmod(double x, double y, double *divp, double *modp)
static VALUE fix_divmod(VALUE x, VALUE y)
static void fixdivmod(long x, long y, long *divp, long *modp)
void rb_cmperr(VALUE x, VALUE y)
static VALUE fix_rev(VALUE num)
VALUE rb_usascii_str_new_cstr(const char *)
VALUE rb_big_xor(VALUE x, VALUE y)
static VALUE fix_to_f(VALUE num)
VALUE rb_num_coerce_bin(VALUE x, VALUE y, ID func)
char * ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
static VALUE num_nonzero_p(VALUE num)
VALUE rb_str_new(const char *, long)
VALUE rb_obj_class(VALUE)