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)
257 a[0] = *x; a[1] = *y;
320 "can't define singleton method \"%s\" for %s",
641 flt->float_value = d;
658 char *
ruby_dtoa(
double d_,
int mode,
int ndigits,
int *decpt,
int *sign,
char **rve);
665 int sign, decpt, digs;
669 else if (
isnan(value))
672 p =
ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
674 if ((digs = (
int)(e - p)) >= (
int)
sizeof(
buf)) digs = (int)
sizeof(
buf) - 1;
675 memcpy(
buf, p, digs);
690 memset(ptr,
'0', decpt - digs);
693 memcpy(ptr,
".0", 2);
699 else if (decpt > -4) {
705 memset(ptr += len,
'0', -decpt);
706 memcpy(ptr -= decpt,
buf, digs);
872 flodivmod(
double x,
double y,
double *divp,
double *modp)
897 if (modp) *modp =
mod;
898 if (divp) *divp =
div;
1013 if (dx < 0 && dy !=
round(dy))
1054 if (x == y)
return INT2FIX(0);
1061 if (x == y)
return Qtrue;
1082 volatile double a, b;
1089 #if defined(_MSC_VER) && _MSC_VER < 1300
1097 #if defined(_MSC_VER) && _MSC_VER < 1300
1118 if (d == 0.0) d = 0.0;
1127 if (a == b)
return INT2FIX(0);
1129 if (a < b)
return INT2FIX(-1);
1167 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1170 if (a > 0.0)
return INT2FIX(1);
1202 #if defined(_MSC_VER) && _MSC_VER < 1300
1209 #if defined(_MSC_VER) && _MSC_VER < 1300
1239 #if defined(_MSC_VER) && _MSC_VER < 1300
1246 #if defined(_MSC_VER) && _MSC_VER < 1300
1276 #if defined(_MSC_VER) && _MSC_VER < 1300
1283 #if defined(_MSC_VER) && _MSC_VER < 1300
1313 #if defined(_MSC_VER) && _MSC_VER < 1300
1320 #if defined(_MSC_VER) && _MSC_VER < 1300
1345 #if defined(_MSC_VER) && _MSC_VER < 1300
1446 return INT2FIX( value < 0 ? -1 : 1 );
1467 if (!isfinite(value))
1539 if (-0.415241 * ndigits - 0.125 > bytes ) {
1548 x = (x + y / 2) / y * y;
1609 if (argc > 0 &&
rb_scan_args(argc, argv,
"01", &nd) == 1) {
1619 frexp(number, &binexp);
1639 (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1))) {
1642 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
1645 f = pow(10, ndigits);
1666 if (f > 0.0) f = floor(f);
1667 if (f < 0.0) f = ceil(f);
1757 double n = (end - beg)/unit;
1758 double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
1761 return unit > 0 ? beg <= end : beg >= end;
1766 if (err>0.5) err=0.5;
1795 else if (unit == 0) {
1801 for (i=0; i<n; i++) {
1802 double d = i*unit+beg;
1803 if (unit >= 0 ? end < d : d < end) d = end;
1847 case -1: cmp =
'<';
break;
1864 argc =
rb_scan_args(argc, argv,
"02:", to, step, &hash);
1871 if (values[0] !=
Qundef) {
1875 if (values[1] !=
Qundef) {
1882 if (argc > 1 &&
NIL_P(*step)) {
1994 for (; i >= end; i +=
diff)
1998 for (; i <= end; i +=
diff)
2011 ID cmp = desc ?
'<' :
'>';
2020 #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
2021 #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
2022 #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
2023 #define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
2024 (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \
2026 LONG_MIN_MINUS_ONE < (n))
2048 if ((s =
strchr(buf,
' ')) != 0) *s =
'\0';
2061 static unsigned long
2073 return (
unsigned long)l;
2080 *wrap_p = d <= -1.0;
2082 return (
unsigned long)d;
2083 return (
unsigned long)(long)d;
2090 if ((s =
strchr(buf,
' ')) != 0) *s =
'\0';
2114 #if SIZEOF_INT < SIZEOF_LONG
2116 rb_out_of_int(SIGNED_VALUE num)
2119 num, num < 0 ?
"small" :
"big");
2125 if ((
long)(
int)num != num) {
2131 check_uint(
unsigned long num,
int sign)
2135 if (num < (
unsigned long)INT_MIN)
2164 rb_num2uint(
VALUE val)
2169 check_uint(num, wrap);
2174 rb_fix2uint(
VALUE val)
2179 return rb_num2uint(val);
2204 num, num < 0 ?
"small" :
"big");
2210 if ((
long)(
short)num != num) {
2220 if (num < (
unsigned long)SHRT_MIN)
2225 if (USHRT_MAX < num)
2287 #define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
2288 #define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
2289 #define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
2291 #define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
2293 #define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
2294 (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \
2296 LLONG_MIN_MINUS_ONE < (n))
2299 rb_num2ll(
VALUE val)
2309 && (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(
RFLOAT_VALUE(val)))) {
2317 if ((s =
strchr(buf,
' ')) != 0) *s =
'\0';
2322 return rb_big2ll(val);
2336 rb_num2ull(
VALUE val)
2346 && LLONG_MIN_MINUS_ONE_IS_LESS_THAN(
RFLOAT_VALUE(val))) {
2349 return (
unsigned LONG_LONG)(LONG_LONG)(
RFLOAT_VALUE(val));
2356 if ((s =
strchr(buf,
' ')) != 0) *s =
'\0';
2361 return rb_big2ull(val);
2371 return NUM2ULL(val);
2486 #define int_succ rb_int_succ
2511 #define int_pred rb_int_pred
2651 if (base < 2 || 36 < base) {
2663 *--b = ruby_digitmap[(int)(val % base)];
2664 }
while (val /= base);
2692 if (argc == 0) base = 10;
2770 #define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
2772 #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
2792 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
2801 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
2802 d = (LONG_LONG)a * b;
2804 return rb_ll2inum(d);
2806 if (a == 0)
return x;
2844 if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
2848 if (divp) *divp =
div;
2849 if (modp) *modp =
mod;
2998 volatile VALUE a, b;
3024 while (y % 2 == 0) {
3072 if (a == 1)
return INT2FIX(1);
3082 if (b == 0)
return INT2FIX(1);
3083 if (b == 1)
return x;
3091 if (a == 1)
return INT2FIX(1);
3098 if (a == 0)
return INT2FIX(0);
3107 if (a == 1)
return DBL2NUM(1.0);
3110 if (a < 0 && dy !=
round(dy))
3112 return DBL2NUM(pow((
double)a, dy));
3133 if (x == y)
return Qtrue;
3161 if (x == y)
return INT2FIX(0);
3301 if (!err)
return FALSE;
3404 return fix_rshift(val, (
unsigned long)-width);
3411 if (width > (SIZEOF_LONG*
CHAR_BIT-1)
3412 || ((
unsigned long)val)>>(SIZEOF_LONG*
CHAR_BIT-1-width) > 0) {
3435 if (i == 0)
return x;
3444 if (i >=
sizeof(
long)*
CHAR_BIT-1) {
3445 if (val < 0)
return INT2FIX(-1);
3448 val = RSHIFT(val, i);
3485 if (val < 0)
return INT2FIX(1);
3621 for (i =
FIX2LONG(from); i <= end; i++) {
3666 for (i=
FIX2LONG(from); i >= end; i--) {
3719 for (i=0; i<end; i++) {
3755 if (argc == 0)
return num;
3848 #define rb_intern(str) rb_intern_const(str)
3850 #if defined(__FreeBSD__) && __FreeBSD__ < 4
3852 fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL));
3853 #elif defined(_UNICOSMP)
3856 #elif defined(__BORLANDC__)
3858 _control87(MCW_EM, MCW_EM);
3859 _control87(_control87(0,0),0x1FFF);
4106 #undef rb_float_value