8 #if defined(__FreeBSD__) && __FreeBSD__ < 4
9 # include <floatingpoint.h>
24 #define floor(f) floorf(f)
25 #define ceil(f) ceilf(f)
26 #define floor(f) floorf(f)
27 #define fmod(x,y) fmodf(x,y)
67 return mrb_float_value(mrb, (
mrb_float)0 - mrb_to_flo(mrb, num));
94 d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y));
96 return mrb_fixnum_value((
mrb_int)d);
97 return mrb_float_value(mrb, d);
114 return mrb_float_value(mrb, mrb_to_flo(mrb, x) / mrb_to_flo(mrb, y));
131 return mrb_float_value(mrb, mrb_to_flo(mrb, x) / y);
149 if (mrb_to_flo(mrb, num) < 0) {
150 return num_uminus(mrb, num);
170 if (max_digit > 40) {
205 if ((exp < 0 ? -exp : exp) > max_digit) {
212 n = n / pow(10.0, m);
224 while (max_digit >= 0) {
226 digit = (int)floor(n / weight + FLT_EPSILON);
227 *(c++) =
'0' + digit;
228 n -= (digit * weight);
233 else if (m < -1 && n < FLT_EPSILON) {
252 *(c++) =
'0' + exp / 10;
253 *(c++) =
'0' + exp % 10;
300 return mrb_float_value(mrb,
mrb_float(x) - mrb_to_flo(mrb, y));
318 return mrb_float_value(mrb,
mrb_float(x) * mrb_to_flo(mrb, y));
333 if (isinf(x) && !isinf(y) && !isnan(y))
343 if (modp) *modp = mod;
344 if (divp) *divp = div;
367 fy = mrb_to_flo(mrb, y);
368 flodivmod(mrb,
mrb_float(x), fy, 0, &mod);
369 return mrb_float_value(mrb, mod);
398 return mrb_bool_value(eql_p);
437 return num_equal(mrb, x, y);
440 return mrb_bool_value(a == b);
462 for (hash=0, i=0; i<
sizeof(
mrb_float);i++) {
463 hash = (hash * 971) ^ (
unsigned char)c[
i];
465 if (hash < 0) hash = -hash;
466 return mrb_fixnum_value(hash);
502 return mrb_fixnum_value( value < 0 ? -1 : 1 );
504 return mrb_nil_value();
524 finite_p = !(isinf(value) || isnan(value));
526 return mrb_bool_value(finite_p);
548 return mrb_float_value(mrb, f);
550 return mrb_fixnum_value((
mrb_int)f);
573 return mrb_float_value(mrb, f);
575 return mrb_fixnum_value((
mrb_int)f);
624 if (ndigits < 0) number = 0;
629 if (ndigits < 0) number /= f;
635 number = d + (number - d >= 0.5);
637 else if (number < 0.0) {
639 number = d - (d - number >= 0.5);
642 if (ndigits < 0) number *= f;
645 if (ndigits > 0)
return mrb_float_value(mrb, number);
646 return mrb_fixnum_value((
mrb_int)number);
665 if (f > 0.0) f = floor(f);
666 if (f < 0.0) f = ceil(f);
669 return mrb_float_value(mrb, f);
671 return mrb_fixnum_value((
mrb_int)f);
731 return mrb_funcall(mrb, num,
"+", 1, mrb_fixnum_value(1));
734 #define SQRT_INT_MAX ((mrb_int)1<<((sizeof(mrb_int)*CHAR_BIT-1)/2))
736 #define FIT_SQRT_INT(n) (((n)<SQRT_INT_MAX)&&((n)>=-SQRT_INT_MAX))
744 if (a == 0)
return x;
750 return mrb_fixnum_value(a*b);
752 if (a != 0 && c/a != b) {
755 return mrb_fixnum_value(c);;
757 return mrb_float_value(mrb, (
mrb_float)a * mrb_to_flo(mrb, y));
799 if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
803 if (divp) *divp = div;
804 if (modp) *modp = mod;
832 return mrb_fixnum_value(mod);
837 flodivmod(mrb, (
mrb_float)a, mrb_to_flo(mrb, y), 0, &mod);
838 return mrb_float_value(mrb, mod);
863 return mrb_assoc_new(mrb, mrb_fixnum_value(div), mrb_fixnum_value(mod));
870 a = mrb_float_value(mrb, (
mrb_int)div);
871 b = mrb_float_value(mrb, mod);
900 return mrb_bool_value(equal_p);
920 return mrb_fixnum_value(val);
951 y = bit_coerce(mrb, y);
953 return mrb_fixnum_value(val);
972 y = bit_coerce(mrb, y);
974 return mrb_fixnum_value(val);
993 y = bit_coerce(mrb, y);
995 return mrb_fixnum_value(val);
998 #define NUMERIC_SHIFT_WIDTH_MAX (sizeof(mrb_int)*CHAR_BIT-1)
1005 mrb_fixnum_value(width),
1009 return mrb_fixnum_value(val);
1013 rshift(
mrb_int val,
size_t width)
1027 return mrb_fixnum_value(val);
1036 y = bit_coerce(mrb, y);
1054 fix_shift_get_width(mrb, &width);
1064 result = rshift(val, -width);
1067 result = lshift(mrb, val, width);
1088 fix_shift_get_width(mrb, &width);
1098 result = lshift(mrb, val, -width);
1101 result = rshift(val, width);
1124 return mrb_float_value(mrb, val);
1161 return mrb_fixnum_value(z);
1170 if (a == 0)
return y;
1176 if (((a < 0) ^ (b < 0)) == 0 && (a < 0) != (c < 0)) {
1180 return mrb_fixnum_value(c);
1182 return mrb_float_value(mrb, (
mrb_float)a + mrb_to_flo(mrb, y));
1214 if (((a < 0) ^ (b < 0)) != 0 && (a < 0) != (c < 0)) {
1218 return mrb_fixnum_value(c);
1220 return mrb_float_value(mrb, (
mrb_float)a - mrb_to_flo(mrb, y));
1246 char buf[
sizeof(
mrb_int)*CHAR_BIT+1];
1247 char *b = buf +
sizeof buf;
1250 if (base < 2 || 36 < base) {
1260 }
while (val /= base);
1266 }
while (val /= base);
1269 return mrb_str_new(mrb, b, buf +
sizeof(buf) - b);
1316 x = mrb_to_flo(mrb,
self);
1325 return mrb_nil_value();
1328 return mrb_fixnum_value(1);
1331 return mrb_fixnum_value(-1);
1332 return mrb_fixnum_value(0);
1352 return mrb_float_value(mrb, x + y);
1358 struct RClass *numeric, *integer, *fixnum, *fl;