20 #if defined(__FreeBSD__) && __FreeBSD__ < 4
21 #include <floatingpoint.h>
40 #define DBL_MIN 2.2250738585072014e-308
43 #define DBL_MAX 1.7976931348623157e+308
46 #define DBL_MIN_EXP (-1021)
49 #define DBL_MAX_EXP 1024
51 #ifndef DBL_MIN_10_EXP
52 #define DBL_MIN_10_EXP (-307)
54 #ifndef DBL_MAX_10_EXP
55 #define DBL_MAX_10_EXP 308
61 #define DBL_MANT_DIG 53
64 #define DBL_EPSILON 2.2204460492503131e-16
68 #elif !defined(WORDS_BIGENDIAN)
71 const unsigned char rb_infinity[] =
"\x7f\x80\x00\x00";
75 #elif !defined(WORDS_BIGENDIAN)
76 const unsigned char rb_nan[] =
"\x00\x00\xc0\x7f";
78 const unsigned char rb_nan[] =
"\x7f\xc0\x00\x00";
89 x = f + (x - f >= 0.5);
93 x = f - (f - x >= 0.5);
123 #define NUMERR_TYPE 1
124 #define NUMERR_NEGATIVE 2
125 #define NUMERR_TOOLARGE 3
128 #if SIZEOF_INT < SIZEOF_LONG
132 *ret = (
unsigned int)v;
139 #if SIZEOF_INT < SIZEOF_LONG
144 #define DIGSPERLONG (SIZEOF_LONG/SIZEOF_BDIGITS)
204 a[0] = *x; a[1] = *y;
237 VALUE c, x0 = x, y0 = y;
260 "can't define singleton method \"%s\" for %s",
587 flt->float_value = d;
604 char *
ruby_dtoa(
double d_,
int mode,
int ndigits,
int *decpt,
int *sign,
char **rve);
611 int sign, decpt, digs;
615 else if (
isnan(value))
618 p =
ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
620 if ((digs = (
int)(e - p)) >= (
int)
sizeof(
buf)) digs = (int)
sizeof(
buf) - 1;
621 memcpy(
buf, p, digs);
629 else if (decpt - digs < float_dig) {
636 memset(ptr,
'0', decpt - digs);
639 memcpy(ptr,
".0", 2);
645 else if (decpt > -4) {
651 memset(ptr += len,
'0', -decpt);
652 memcpy(ptr -= decpt,
buf, digs);
812 flodivmod(
double x,
double y,
double *divp,
double *modp)
837 if (modp) *modp =
mod;
838 if (divp) *divp =
div;
949 if (dx < 0 && dy !=
round(dy))
996 if (x == y)
return Qtrue;
1015 volatile double a, b;
1026 #if defined(_MSC_VER) && _MSC_VER < 1300
1034 #if defined(_MSC_VER) && _MSC_VER < 1300
1055 if (d == 0.0) d = 0.0;
1064 if (a == b)
return INT2FIX(0);
1066 if (a < b)
return INT2FIX(-1);
1094 if (a > 0.0)
return INT2FIX(1);
1108 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1111 if (a > 0.0)
return INT2FIX(1);
1143 #if defined(_MSC_VER) && _MSC_VER < 1300
1151 #if defined(_MSC_VER) && _MSC_VER < 1300
1182 #if defined(_MSC_VER) && _MSC_VER < 1300
1190 #if defined(_MSC_VER) && _MSC_VER < 1300
1220 #if defined(_MSC_VER) && _MSC_VER < 1300
1228 #if defined(_MSC_VER) && _MSC_VER < 1300
1259 #if defined(_MSC_VER) && _MSC_VER < 1300
1267 #if defined(_MSC_VER) && _MSC_VER < 1300
1290 #if defined(_MSC_VER) && _MSC_VER < 1300
1387 return INT2FIX( value < 0 ? -1 : 1 );
1482 if (-0.415241 * ndigits - 0.125 > bytes ) {
1491 x = (x + y / 2) / y * y;
1551 if (argc > 0 &&
rb_scan_args(argc, argv,
"01", &nd) == 1) {
1561 frexp(number, &binexp);
1581 (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1))) {
1584 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
1587 f = pow(10, ndigits);
1606 if (f > 0.0) f = floor(f);
1607 if (f < 0.0) f = ceil(f);
1696 double n = (end - beg)/unit;
1697 double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
1704 if (err>0.5) err=0.5;
1706 if (!excl || ((
long)n)*unit+beg < end) n++;
1707 for (i=0; i<n; i++) {
1808 #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
1809 #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
1810 #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
1822 switch (
TYPE(val)) {
1833 if ((s =
strchr(buf,
' ')) != 0) *s =
'\0';
1856 switch (
TYPE(val)) {
1867 if ((s =
strchr(buf,
' ')) != 0) *s =
'\0';
1880 #if SIZEOF_INT < SIZEOF_VALUE
1885 num, num < 0 ?
"small" :
"big");
1903 if ((num & mask) != mask || (num & ~mask) <= INT_MAX + 1UL)
1904 #define VALUE_MSBMASK ((VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1))
1909 if ((num & mask) != 0)
1933 rb_num2uint(
VALUE val)
1938 return (
unsigned long)num;
1942 rb_fix2uint(
VALUE val)
1947 return rb_num2uint(val);
1983 #define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
1984 #define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
1985 #define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
1987 #define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
1991 rb_num2ll(
VALUE val)
1999 switch (
TYPE(val)) {
2010 if ((s =
strchr(buf,
' ')) != 0) *s =
'\0';
2015 return rb_big2ll(val);
2033 rb_num2ull(
VALUE val)
2035 switch (
TYPE(val)) {
2052 if ((s =
strchr(buf,
' ')) != 0) *s =
'\0';
2057 return rb_big2ull(val);
2070 return NUM2ULL(val);
2186 #define int_succ rb_int_succ
2208 #define int_pred rb_int_pred
2347 if (base < 2 || 36 < base) {
2359 *--b = ruby_digitmap[(int)(val % base)];
2360 }
while (val /= base);
2388 if (argc == 0) base = 10;
2466 #define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
2468 #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
2488 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
2498 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
2499 d = (LONG_LONG)a * b;
2501 return rb_ll2inum(d);
2508 if (a == 0)
return x;
2509 if (
FIX2LONG(r) != c || c/a != b) {
2544 if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
2548 if (divp) *divp =
div;
2549 if (modp) *modp =
mod;
2696 volatile VALUE a, b;
2721 while (y % 2 == 0) {
2733 volatile long xz = x * z;
2767 if (b == 0)
return INT2FIX(1);
2768 if (b == 1)
return x;
2773 if (a == 1)
return INT2FIX(1);
2788 if (a == 0)
return INT2FIX(0);
2789 if (a == 1)
return INT2FIX(1);
2801 if (a == 1)
return DBL2NUM(1.0);
2804 if (a < 0 && dy !=
round(dy))
2806 return DBL2NUM(pow((
double)a, dy));
2827 if (x == y)
return Qtrue;
2852 if (x == y)
return INT2FIX(0);
3069 return fix_rshift(val, (
unsigned long)-width);
3076 if (width > (SIZEOF_LONG*
CHAR_BIT-1)
3077 || ((
unsigned long)val)>>(SIZEOF_LONG*
CHAR_BIT-1-width) > 0) {
3100 if (i == 0)
return x;
3109 if (i >=
sizeof(
long)*
CHAR_BIT-1) {
3110 if (val < 0)
return INT2FIX(-1);
3113 val = RSHIFT(val, i);
3152 if (val < 0)
return INT2FIX(1);
3245 for (i =
FIX2LONG(from); i <= end; i++) {
3287 for (i=
FIX2LONG(from); i >= end; i--) {
3331 for (i=0; i<end; i++) {
3366 if (argc == 0)
return num;
3463 #define rb_intern(str) rb_intern_const(str)
3465 #if defined(__FreeBSD__) && __FreeBSD__ < 4
3467 fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL));
3468 #elif defined(_UNICOSMP)
3471 #elif defined(__BORLANDC__)
3473 _control87(MCW_EM, MCW_EM);
3474 _control87(_control87(0,0),0x1FFF);