2 ** numeric.c - Numeric, Integer, Float, Fixnum class
4 ** See Copyright Notice in mruby.h
7 #ifndef MRB_WITHOUT_FLOAT
16 #include <mruby/array.h>
17 #include <mruby/numeric.h>
18 #include <mruby/string.h>
19 #include <mruby/class.h>
21 #ifndef MRB_WITHOUT_FLOAT
23 #define trunc(f) truncf(f)
24 #define floor(f) floorf(f)
25 #define ceil(f) ceilf(f)
26 #define fmod(x,y) fmodf(x,y)
27 #define FLO_TO_STR_PREC 8
29 #define FLO_TO_STR_PREC 16
33 #ifndef MRB_WITHOUT_FLOAT
35 mrb_to_flo(mrb_state *mrb, mrb_value val)
37 switch (mrb_type(val)) {
39 return (mrb_float)mrb_fixnum(val);
43 mrb_raise(mrb, E_TYPE_ERROR, "non float value");
45 return mrb_float(val);
54 * Raises <code>num</code> the <code>other</code> power.
59 num_pow(mrb_state *mrb, mrb_value x)
62 #ifndef MRB_WITHOUT_FLOAT
66 mrb_get_args(mrb, "o", &y);
67 if (mrb_fixnum_p(x) && mrb_fixnum_p(y)) {
69 mrb_int base = mrb_fixnum(x);
70 mrb_int exp = mrb_fixnum(y);
74 #ifdef MRB_WITHOUT_FLOAT
75 return mrb_fixnum_value(0);
81 if (mrb_int_mul_overflow(result, base, &result)) {
82 #ifndef MRB_WITHOUT_FLOAT
89 if (mrb_int_mul_overflow(base, base, &base)) {
90 #ifndef MRB_WITHOUT_FLOAT
95 return mrb_fixnum_value(result);
97 #ifdef MRB_WITHOUT_FLOAT
98 mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
101 d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y));
102 return mrb_float_value(mrb, d);
112 * Performs division: the class of the resulting object depends on
113 * the class of <code>num</code> and on the magnitude of the
118 mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y)
120 #ifdef MRB_WITHOUT_FLOAT
121 if (!mrb_fixnum_p(y)) {
122 mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
124 return mrb_fixnum_value(mrb_fixnum(x) / mrb_fixnum(y));
126 return mrb_float_value(mrb, mrb_to_flo(mrb, x) / mrb_to_flo(mrb, y));
133 * num.quo(numeric) -> real
135 * Returns most exact division.
139 num_div(mrb_state *mrb, mrb_value x)
141 #ifdef MRB_WITHOUT_FLOAT
144 mrb_get_args(mrb, "o", &y);
145 if (!mrb_fixnum_p(y)) {
146 mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
148 return mrb_fixnum_value(mrb_fixnum(x) / mrb_fixnum(y));
152 mrb_get_args(mrb, "f", &y);
153 return mrb_float_value(mrb, mrb_to_flo(mrb, x) / y);
158 num_coerce_step_counter(mrb_state *mrb, mrb_value self)
160 mrb_value counter = self, num, step;
162 mrb_get_args(mrb, "oo", &num, &step);
164 #ifndef MRB_WITHOUT_FLOAT
165 if (mrb_float_p(self) || mrb_float_p(num) || mrb_float_p(step)) {
166 counter = mrb_funcall(mrb, counter, "to_f", 0);
173 #ifndef MRB_WITHOUT_FLOAT
174 /********************************************************************
176 * Document-class: Float
178 * <code>Float</code> objects represent inexact real numbers using
179 * the native architecture's double-precision floating point
188 * Returns a string containing a representation of self. As well as a
189 * fixed or exponential form of the number, the call may return
190 * "<code>NaN</code>", "<code>Infinity</code>", and
191 * "<code>-Infinity</code>".
195 flo_to_s(mrb_state *mrb, mrb_value flt)
197 mrb_float f = mrb_float(flt);
200 return f < 0 ? mrb_str_new_lit(mrb, "-Infinity")
201 : mrb_str_new_lit(mrb, "Infinity");
204 return mrb_str_new_lit(mrb, "NaN");
207 char fmt[] = "%." MRB_STRINGIZE(FLO_TO_STR_PREC) "g";
208 mrb_value str = mrb_float_to_str(mrb, flt, fmt);
213 begp = RSTRING_PTR(str);
214 len = RSTRING_LEN(str);
215 for (char *p = begp, *endp = p + len; p < endp; ++p) {
219 else if (*p == 'e') {
220 ptrdiff_t e_pos = p - begp;
221 mrb_str_cat(mrb, str, ".0", 2);
222 p = RSTRING_PTR(str) + e_pos;
223 memmove(p + 2, p, len - e_pos);
229 if (FLO_TO_STR_PREC + (begp[0] == '-') <= len) {
230 --fmt[sizeof(fmt) - 3]; /* %.16g(%.8g) -> %.15g(%.7g) */
231 str = mrb_float_to_str(mrb, flt, fmt);
232 goto insert_dot_zero;
235 mrb_str_cat(mrb, str, ".0", 2);
245 * float - other -> float
247 * Returns a new float which is the difference of <code>float</code>
248 * and <code>other</code>.
252 flo_minus(mrb_state *mrb, mrb_value x)
256 mrb_get_args(mrb, "o", &y);
257 return mrb_float_value(mrb, mrb_float(x) - mrb_to_flo(mrb, y));
263 * float * other -> float
265 * Returns a new float which is the product of <code>float</code>
266 * and <code>other</code>.
270 flo_mul(mrb_state *mrb, mrb_value x)
274 mrb_get_args(mrb, "o", &y);
275 return mrb_float_value(mrb, mrb_float(x) * mrb_to_flo(mrb, y));
279 flodivmod(mrb_state *mrb, double x, double y, mrb_float *divp, mrb_float *modp)
284 /* y is NaN so all results are NaN */
289 if (x == 0) div = NAN;
290 else if (x > 0.0) div = INFINITY;
291 else div = -INFINITY; /* x < 0.0 */
295 if ((x == 0.0) || (isinf(y) && !isinf(x))) {
301 if (isinf(x) && !isinf(y)) {
306 if (modp && divp) div = round(div);
313 if (modp) *modp = mod;
314 if (divp) *divp = div;
320 * flt % other -> float
321 * flt.modulo(other) -> float
323 * Return the modulo after division of <code>flt</code> by <code>other</code>.
325 * 6543.21.modulo(137) #=> 104.21
326 * 6543.21.modulo(137.24) #=> 92.9299999999996
330 flo_mod(mrb_state *mrb, mrb_value x)
335 mrb_get_args(mrb, "o", &y);
337 flodivmod(mrb, mrb_float(x), mrb_to_flo(mrb, y), 0, &mod);
338 return mrb_float_value(mrb, mod);
345 * num.eql?(numeric) -> true or false
347 * Returns <code>true</code> if <i>num</i> and <i>numeric</i> are the
348 * same type and have equal values.
351 * 1.eql?(1.0) #=> false
352 * (1.0).eql?(1.0) #=> true
355 fix_eql(mrb_state *mrb, mrb_value x)
359 mrb_get_args(mrb, "o", &y);
360 if (!mrb_fixnum_p(y)) return mrb_false_value();
361 return mrb_bool_value(mrb_fixnum(x) == mrb_fixnum(y));
364 #ifndef MRB_WITHOUT_FLOAT
366 flo_eql(mrb_state *mrb, mrb_value x)
370 mrb_get_args(mrb, "o", &y);
371 if (!mrb_float_p(y)) return mrb_false_value();
372 return mrb_bool_value(mrb_float(x) == mrb_float(y));
378 * flt == obj -> true or false
380 * Returns <code>true</code> only if <i>obj</i> has the same value
381 * as <i>flt</i>. Contrast this with <code>Float#eql?</code>, which
382 * requires <i>obj</i> to be a <code>Float</code>.
389 flo_eq(mrb_state *mrb, mrb_value x)
392 mrb_get_args(mrb, "o", &y);
394 switch (mrb_type(y)) {
396 return mrb_bool_value(mrb_float(x) == (mrb_float)mrb_fixnum(y));
398 return mrb_bool_value(mrb_float(x) == mrb_float(y));
400 return mrb_false_value();
405 value_int64(mrb_state *mrb, mrb_value x)
407 switch (mrb_type(x)) {
409 return (int64_t)mrb_fixnum(x);
412 return (int64_t)mrb_float(x);
414 mrb_raise(mrb, E_TYPE_ERROR, "cannot convert to Integer");
422 int64_value(mrb_state *mrb, int64_t v)
425 return mrb_fixnum_value((mrb_int)v);
427 return mrb_float_value(mrb, (mrb_float)v);
431 flo_rev(mrb_state *mrb, mrb_value x)
434 mrb_get_args(mrb, "");
435 v1 = (int64_t)mrb_float(x);
436 return int64_value(mrb, ~v1);
440 flo_and(mrb_state *mrb, mrb_value x)
444 mrb_get_args(mrb, "o", &y);
446 v1 = (int64_t)mrb_float(x);
447 v2 = value_int64(mrb, y);
448 return int64_value(mrb, v1 & v2);
452 flo_or(mrb_state *mrb, mrb_value x)
456 mrb_get_args(mrb, "o", &y);
458 v1 = (int64_t)mrb_float(x);
459 v2 = value_int64(mrb, y);
460 return int64_value(mrb, v1 | v2);
464 flo_xor(mrb_state *mrb, mrb_value x)
468 mrb_get_args(mrb, "o", &y);
470 v1 = (int64_t)mrb_float(x);
471 v2 = value_int64(mrb, y);
472 return int64_value(mrb, v1 ^ v2);
476 flo_shift(mrb_state *mrb, mrb_value x, mrb_int width)
488 #if defined(_ISOC99_SOURCE)
497 if (val == 0 && mrb_float(x) < 0) {
498 return mrb_fixnum_value(-1);
506 if (FIXABLE_FLOAT(val)) {
507 return mrb_fixnum_value((mrb_int)val);
509 return mrb_float_value(mrb, val);
513 flo_lshift(mrb_state *mrb, mrb_value x)
517 mrb_get_args(mrb, "i", &width);
518 return flo_shift(mrb, x, -width);
522 flo_rshift(mrb_state *mrb, mrb_value x)
526 mrb_get_args(mrb, "i", &width);
527 return flo_shift(mrb, x, width);
535 * As <code>flt</code> is already a float, returns +self+.
539 flo_to_f(mrb_state *mrb, mrb_value num)
547 * flt.infinite? -> nil, -1, +1
549 * Returns <code>nil</code>, -1, or +1 depending on whether <i>flt</i>
550 * is finite, -infinity, or +infinity.
552 * (0.0).infinite? #=> nil
553 * (-1.0/0.0).infinite? #=> -1
554 * (+1.0/0.0).infinite? #=> 1
558 flo_infinite_p(mrb_state *mrb, mrb_value num)
560 mrb_float value = mrb_float(num);
563 return mrb_fixnum_value(value < 0 ? -1 : 1);
565 return mrb_nil_value();
571 * flt.finite? -> true or false
573 * Returns <code>true</code> if <i>flt</i> is a valid IEEE floating
574 * point number (it is not infinite, and <code>nan?</code> is
575 * <code>false</code>).
580 flo_finite_p(mrb_state *mrb, mrb_value num)
582 return mrb_bool_value(isfinite(mrb_float(num)));
586 mrb_check_num_exact(mrb_state *mrb, mrb_float num)
589 mrb_raise(mrb, E_FLOATDOMAIN_ERROR, num < 0 ? "-Infinity" : "Infinity");
592 mrb_raise(mrb, E_FLOATDOMAIN_ERROR, "NaN");
599 * flt.floor -> integer
601 * Returns the largest integer less than or equal to <i>flt</i>.
605 * (-1.2).floor #=> -2
606 * (-2.0).floor #=> -2
610 flo_floor(mrb_state *mrb, mrb_value num)
612 mrb_float f = floor(mrb_float(num));
614 mrb_check_num_exact(mrb, f);
615 if (!FIXABLE_FLOAT(f)) {
616 return mrb_float_value(mrb, f);
618 return mrb_fixnum_value((mrb_int)f);
624 * flt.ceil -> integer
626 * Returns the smallest <code>Integer</code> greater than or equal to
636 flo_ceil(mrb_state *mrb, mrb_value num)
638 mrb_float f = ceil(mrb_float(num));
640 mrb_check_num_exact(mrb, f);
641 if (!FIXABLE_FLOAT(f)) {
642 return mrb_float_value(mrb, f);
644 return mrb_fixnum_value((mrb_int)f);
650 * flt.round([ndigits]) -> integer or float
652 * Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits).
653 * Precision may be negative. Returns a floating point number when ndigits
659 * (-1.5).round #=> -2
661 * 1.234567.round(2) #=> 1.23
662 * 1.234567.round(3) #=> 1.235
663 * 1.234567.round(4) #=> 1.2346
664 * 1.234567.round(5) #=> 1.23457
666 * 34567.89.round(-5) #=> 0
667 * 34567.89.round(-4) #=> 30000
668 * 34567.89.round(-3) #=> 35000
669 * 34567.89.round(-2) #=> 34600
670 * 34567.89.round(-1) #=> 34570
671 * 34567.89.round(0) #=> 34568
672 * 34567.89.round(1) #=> 34567.9
673 * 34567.89.round(2) #=> 34567.89
674 * 34567.89.round(3) #=> 34567.89
679 flo_round(mrb_state *mrb, mrb_value num)
685 mrb_get_args(mrb, "|i", &ndigits);
686 number = mrb_float(num);
688 if (0 < ndigits && (isinf(number) || isnan(number))) {
691 mrb_check_num_exact(mrb, number);
694 i = ndigits >= 0 ? ndigits : -ndigits;
699 if (ndigits < 0) number = 0;
704 if (ndigits < 0) number /= f;
707 /* home-made inline implementation of round(3) */
710 number = d + (number - d >= 0.5);
712 else if (number < 0.0) {
714 number = d - (d - number >= 0.5);
717 if (ndigits < 0) number *= f;
722 if (!isfinite(number)) return num;
723 return mrb_float_value(mrb, number);
725 return mrb_fixnum_value((mrb_int)number);
732 * flt.to_i -> integer
733 * flt.truncate -> integer
735 * Returns <i>flt</i> truncated to an <code>Integer</code>.
739 flo_truncate(mrb_state *mrb, mrb_value num)
741 mrb_float f = mrb_float(num);
743 if (f > 0.0) f = floor(f);
744 if (f < 0.0) f = ceil(f);
746 mrb_check_num_exact(mrb, f);
747 if (!FIXABLE_FLOAT(f)) {
748 return mrb_float_value(mrb, f);
750 return mrb_fixnum_value((mrb_int)f);
754 flo_nan_p(mrb_state *mrb, mrb_value num)
756 return mrb_bool_value(isnan(mrb_float(num)));
761 * Document-class: Integer
763 * <code>Integer</code> is the basis for the two concrete classes that
764 * hold whole numbers, <code>Bignum</code> and <code>Fixnum</code>.
771 * int.to_i -> integer
773 * As <i>int</i> is already an <code>Integer</code>, all these
774 * methods simply return the receiver.
778 int_to_i(mrb_state *mrb, mrb_value num)
784 mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y)
789 if (mrb_fixnum_p(y)) {
792 if (a == 0) return x;
794 if (mrb_int_mul_overflow(a, b, &c)) {
795 #ifndef MRB_WITHOUT_FLOAT
796 return mrb_float_value(mrb, (mrb_float)a * (mrb_float)b);
799 return mrb_fixnum_value(c);
801 #ifdef MRB_WITHOUT_FLOAT
802 mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
804 return mrb_float_value(mrb, (mrb_float)a * mrb_to_flo(mrb, y));
811 * fix * numeric -> numeric_result
813 * Performs multiplication: the class of the resulting object depends on
814 * the class of <code>numeric</code> and on the magnitude of the
819 fix_mul(mrb_state *mrb, mrb_value x)
823 mrb_get_args(mrb, "o", &y);
824 return mrb_fixnum_mul(mrb, x, y);
828 fixdivmod(mrb_state *mrb, mrb_int x, mrb_int y, mrb_int *divp, mrb_int *modp)
832 /* TODO: add mrb_assert(y != 0) to make sure */
847 if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
851 if (divp) *divp = div;
852 if (modp) *modp = mod;
858 * fix % other -> real
859 * fix.modulo(other) -> real
861 * Returns <code>fix</code> modulo <code>other</code>.
862 * See <code>numeric.divmod</code> for more information.
866 fix_mod(mrb_state *mrb, mrb_value x)
871 mrb_get_args(mrb, "o", &y);
873 if (mrb_fixnum_p(y)) {
876 if ((b=mrb_fixnum(y)) == 0) {
877 #ifdef MRB_WITHOUT_FLOAT
878 /* ZeroDivisionError */
879 return mrb_fixnum_value(0);
881 return mrb_float_value(mrb, NAN);
884 fixdivmod(mrb, a, b, 0, &mod);
885 return mrb_fixnum_value(mod);
887 #ifdef MRB_WITHOUT_FLOAT
888 mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
893 flodivmod(mrb, (mrb_float)a, mrb_to_flo(mrb, y), 0, &mod);
894 return mrb_float_value(mrb, mod);
901 * fix.divmod(numeric) -> array
903 * See <code>Numeric#divmod</code>.
906 fix_divmod(mrb_state *mrb, mrb_value x)
910 mrb_get_args(mrb, "o", &y);
912 if (mrb_fixnum_p(y)) {
915 if (mrb_fixnum(y) == 0) {
916 #ifdef MRB_WITHOUT_FLOAT
917 return mrb_assoc_new(mrb, mrb_fixnum_value(0), mrb_fixnum_value(0));
919 return mrb_assoc_new(mrb, ((mrb_fixnum(x) == 0) ?
920 mrb_float_value(mrb, NAN):
921 mrb_float_value(mrb, INFINITY)),
922 mrb_float_value(mrb, NAN));
925 fixdivmod(mrb, mrb_fixnum(x), mrb_fixnum(y), &div, &mod);
926 return mrb_assoc_new(mrb, mrb_fixnum_value(div), mrb_fixnum_value(mod));
928 #ifdef MRB_WITHOUT_FLOAT
929 mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
935 flodivmod(mrb, (mrb_float)mrb_fixnum(x), mrb_to_flo(mrb, y), &div, &mod);
936 a = mrb_float_value(mrb, div);
937 b = mrb_float_value(mrb, mod);
938 return mrb_assoc_new(mrb, a, b);
943 #ifndef MRB_WITHOUT_FLOAT
945 flo_divmod(mrb_state *mrb, mrb_value x)
951 mrb_get_args(mrb, "o", &y);
953 flodivmod(mrb, mrb_float(x), mrb_to_flo(mrb, y), &div, &mod);
954 a = mrb_float_value(mrb, div);
955 b = mrb_float_value(mrb, mod);
956 return mrb_assoc_new(mrb, a, b);
963 * fix == other -> true or false
965 * Return <code>true</code> if <code>fix</code> equals <code>other</code>
973 fix_equal(mrb_state *mrb, mrb_value x)
977 mrb_get_args(mrb, "o", &y);
978 switch (mrb_type(y)) {
980 return mrb_bool_value(mrb_fixnum(x) == mrb_fixnum(y));
981 #ifndef MRB_WITHOUT_FLOAT
983 return mrb_bool_value((mrb_float)mrb_fixnum(x) == mrb_float(y));
986 return mrb_false_value();
995 * One's complement: returns a number where each bit is flipped.
996 * ex.0---00001 (1)-> 1---11110 (-2)
997 * ex.0---00010 (2)-> 1---11101 (-3)
998 * ex.0---00100 (4)-> 1---11011 (-5)
1002 fix_rev(mrb_state *mrb, mrb_value num)
1004 mrb_int val = mrb_fixnum(num);
1006 return mrb_fixnum_value(~val);
1009 #ifdef MRB_WITHOUT_FLOAT
1010 #define bit_op(x,y,op1,op2) do {\
1011 return mrb_fixnum_value(mrb_fixnum(x) op2 mrb_fixnum(y));\
1014 static mrb_value flo_and(mrb_state *mrb, mrb_value x);
1015 static mrb_value flo_or(mrb_state *mrb, mrb_value x);
1016 static mrb_value flo_xor(mrb_state *mrb, mrb_value x);
1017 #define bit_op(x,y,op1,op2) do {\
1018 if (mrb_fixnum_p(y)) return mrb_fixnum_value(mrb_fixnum(x) op2 mrb_fixnum(y));\
1019 return flo_ ## op1(mrb, mrb_float_value(mrb, (mrb_float)mrb_fixnum(x)));\
1026 * fix & integer -> integer_result
1032 fix_and(mrb_state *mrb, mrb_value x)
1036 mrb_get_args(mrb, "o", &y);
1037 bit_op(x, y, and, &);
1043 * fix | integer -> integer_result
1049 fix_or(mrb_state *mrb, mrb_value x)
1053 mrb_get_args(mrb, "o", &y);
1054 bit_op(x, y, or, |);
1060 * fix ^ integer -> integer_result
1062 * Bitwise EXCLUSIVE OR.
1066 fix_xor(mrb_state *mrb, mrb_value x)
1070 mrb_get_args(mrb, "o", &y);
1071 bit_op(x, y, or, ^);
1074 #define NUMERIC_SHIFT_WIDTH_MAX (MRB_INT_BIT-1)
1077 lshift(mrb_state *mrb, mrb_int val, mrb_int width)
1079 if (width < 0) { /* mrb_int overflow */
1080 #ifdef MRB_WITHOUT_FLOAT
1081 return mrb_fixnum_value(0);
1083 return mrb_float_value(mrb, INFINITY);
1087 if ((width > NUMERIC_SHIFT_WIDTH_MAX) ||
1088 (val > (MRB_INT_MAX >> width))) {
1089 #ifdef MRB_WITHOUT_FLOAT
1090 return mrb_fixnum_value(-1);
1095 return mrb_fixnum_value(val << width);
1098 if ((width > NUMERIC_SHIFT_WIDTH_MAX) ||
1099 (val < (MRB_INT_MIN >> width))) {
1100 #ifdef MRB_WITHOUT_FLOAT
1101 return mrb_fixnum_value(0);
1106 return mrb_fixnum_value(val * ((mrb_int)1 << width));
1109 #ifndef MRB_WITHOUT_FLOAT
1112 mrb_float f = (mrb_float)val;
1116 return mrb_float_value(mrb, f);
1122 rshift(mrb_int val, mrb_int width)
1124 if (width < 0) { /* mrb_int overflow */
1125 return mrb_fixnum_value(0);
1127 if (width >= NUMERIC_SHIFT_WIDTH_MAX) {
1129 return mrb_fixnum_value(-1);
1131 return mrb_fixnum_value(0);
1133 return mrb_fixnum_value(val >> width);
1139 * fix << count -> integer or float
1141 * Shifts _fix_ left _count_ positions (right if _count_ is negative).
1145 fix_lshift(mrb_state *mrb, mrb_value x)
1149 mrb_get_args(mrb, "i", &width);
1153 val = mrb_fixnum(x);
1154 if (val == 0) return x;
1156 return rshift(val, -width);
1158 return lshift(mrb, val, width);
1164 * fix >> count -> integer or float
1166 * Shifts _fix_ right _count_ positions (left if _count_ is negative).
1170 fix_rshift(mrb_state *mrb, mrb_value x)
1174 mrb_get_args(mrb, "i", &width);
1178 val = mrb_fixnum(x);
1179 if (val == 0) return x;
1181 return lshift(mrb, val, -width);
1183 return rshift(val, width);
1191 * Converts <i>fix</i> to a <code>Float</code>.
1195 #ifndef MRB_WITHOUT_FLOAT
1197 fix_to_f(mrb_state *mrb, mrb_value num)
1199 return mrb_float_value(mrb, (mrb_float)mrb_fixnum(num));
1203 * Document-class: FloatDomainError
1205 * Raised when attempting to convert special float values
1206 * (in particular infinite or NaN)
1207 * to numerical classes which don't support them.
1209 * Float::INFINITY.to_i
1211 * <em>raises the exception:</em>
1213 * FloatDomainError: Infinity
1215 /* ------------------------------------------------------------------------*/
1217 mrb_flo_to_fixnum(mrb_state *mrb, mrb_value x)
1221 if (!mrb_float_p(x)) {
1222 mrb_raise(mrb, E_TYPE_ERROR, "non float value");
1223 z = 0; /* not reached. just suppress warnings. */
1226 mrb_float d = mrb_float(x);
1228 mrb_check_num_exact(mrb, d);
1229 if (FIXABLE_FLOAT(d)) {
1233 mrb_raisef(mrb, E_ARGUMENT_ERROR, "number (%S) too big for integer", x);
1236 return mrb_fixnum_value(z);
1241 mrb_fixnum_plus(mrb_state *mrb, mrb_value x, mrb_value y)
1246 if (mrb_fixnum_p(y)) {
1249 if (a == 0) return y;
1251 if (mrb_int_add_overflow(a, b, &c)) {
1252 #ifndef MRB_WITHOUT_FLOAT
1253 return mrb_float_value(mrb, (mrb_float)a + (mrb_float)b);
1256 return mrb_fixnum_value(c);
1258 #ifdef MRB_WITHOUT_FLOAT
1259 mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
1261 return mrb_float_value(mrb, (mrb_float)a + mrb_to_flo(mrb, y));
1268 * fix + numeric -> numeric_result
1270 * Performs addition: the class of the resulting object depends on
1271 * the class of <code>numeric</code> and on the magnitude of the
1275 fix_plus(mrb_state *mrb, mrb_value self)
1279 mrb_get_args(mrb, "o", &other);
1280 return mrb_fixnum_plus(mrb, self, other);
1284 mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y)
1289 if (mrb_fixnum_p(y)) {
1293 if (mrb_int_sub_overflow(a, b, &c)) {
1294 #ifndef MRB_WITHOUT_FLOAT
1295 return mrb_float_value(mrb, (mrb_float)a - (mrb_float)b);
1298 return mrb_fixnum_value(c);
1300 #ifdef MRB_WITHOUT_FLOAT
1301 mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value");
1303 return mrb_float_value(mrb, (mrb_float)a - mrb_to_flo(mrb, y));
1311 * fix - numeric -> numeric_result
1313 * Performs subtraction: the class of the resulting object depends on
1314 * the class of <code>numeric</code> and on the magnitude of the
1318 fix_minus(mrb_state *mrb, mrb_value self)
1322 mrb_get_args(mrb, "o", &other);
1323 return mrb_fixnum_minus(mrb, self, other);
1328 mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, mrb_int base)
1330 char buf[MRB_INT_BIT+1];
1331 char *b = buf + sizeof buf;
1332 mrb_int val = mrb_fixnum(x);
1334 if (base < 2 || 36 < base) {
1335 mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %S", mrb_fixnum_value(base));
1343 *--b = mrb_digitmap[-(val % base)];
1344 } while (val /= base);
1349 *--b = mrb_digitmap[(int)(val % base)];
1350 } while (val /= base);
1353 return mrb_str_new(mrb, b, buf + sizeof(buf) - b);
1359 * fix.to_s(base=10) -> string
1361 * Returns a string containing the representation of <i>fix</i> radix
1362 * <i>base</i> (between 2 and 36).
1364 * 12345.to_s #=> "12345"
1365 * 12345.to_s(2) #=> "11000000111001"
1366 * 12345.to_s(8) #=> "30071"
1367 * 12345.to_s(10) #=> "12345"
1368 * 12345.to_s(16) #=> "3039"
1369 * 12345.to_s(36) #=> "9ix"
1373 fix_to_s(mrb_state *mrb, mrb_value self)
1377 mrb_get_args(mrb, "|i", &base);
1378 return mrb_fixnum_to_str(mrb, self, base);
1381 /* compare two numbers: (1:0:-1; -2 for error) */
1383 cmpnum(mrb_state *mrb, mrb_value v1, mrb_value v2)
1385 #ifdef MRB_WITHOUT_FLOAT
1391 #ifdef MRB_WITHOUT_FLOAT
1394 x = mrb_to_flo(mrb, v1);
1396 switch (mrb_type(v2)) {
1398 #ifdef MRB_WITHOUT_FLOAT
1401 y = (mrb_float)mrb_fixnum(v2);
1404 #ifndef MRB_WITHOUT_FLOAT
1424 * self.f <=> other.f => -1, 0, +1
1428 * Comparison---Returns -1, 0, or +1 depending on whether <i>fix</i> is
1429 * less than, equal to, or greater than <i>numeric</i>. This is the
1430 * basis for the tests in <code>Comparable</code>.
1433 num_cmp(mrb_state *mrb, mrb_value self)
1438 mrb_get_args(mrb, "o", &other);
1439 n = cmpnum(mrb, self, other);
1440 if (n == -2) return mrb_nil_value();
1441 return mrb_fixnum_value(n);
1445 cmperr(mrb_state *mrb, mrb_value v1, mrb_value v2)
1447 mrb_raisef(mrb, E_ARGUMENT_ERROR, "comparison of %S with %S failed",
1448 mrb_obj_value(mrb_class(mrb, v1)),
1449 mrb_obj_value(mrb_class(mrb, v2)));
1453 num_lt(mrb_state *mrb, mrb_value self)
1458 mrb_get_args(mrb, "o", &other);
1459 n = cmpnum(mrb, self, other);
1460 if (n == -2) cmperr(mrb, self, other);
1461 if (n < 0) return mrb_true_value();
1462 return mrb_false_value();
1466 num_le(mrb_state *mrb, mrb_value self)
1471 mrb_get_args(mrb, "o", &other);
1472 n = cmpnum(mrb, self, other);
1473 if (n == -2) cmperr(mrb, self, other);
1474 if (n <= 0) return mrb_true_value();
1475 return mrb_false_value();
1479 num_gt(mrb_state *mrb, mrb_value self)
1484 mrb_get_args(mrb, "o", &other);
1485 n = cmpnum(mrb, self, other);
1486 if (n == -2) cmperr(mrb, self, other);
1487 if (n > 0) return mrb_true_value();
1488 return mrb_false_value();
1492 num_ge(mrb_state *mrb, mrb_value self)
1497 mrb_get_args(mrb, "o", &other);
1498 n = cmpnum(mrb, self, other);
1499 if (n == -2) cmperr(mrb, self, other);
1500 if (n >= 0) return mrb_true_value();
1501 return mrb_false_value();
1505 num_finite_p(mrb_state *mrb, mrb_value self)
1507 mrb_get_args(mrb, "");
1508 return mrb_true_value();
1512 num_infinite_p(mrb_state *mrb, mrb_value self)
1514 mrb_get_args(mrb, "");
1515 return mrb_false_value();
1521 * float + other -> float
1523 * Returns a new float which is the sum of <code>float</code>
1524 * and <code>other</code>.
1526 #ifndef MRB_WITHOUT_FLOAT
1528 flo_plus(mrb_state *mrb, mrb_value x)
1532 mrb_get_args(mrb, "o", &y);
1533 return mrb_float_value(mrb, mrb_float(x) + mrb_to_flo(mrb, y));
1537 /* ------------------------------------------------------------------------*/
1539 mrb_init_numeric(mrb_state *mrb)
1541 struct RClass *numeric, *integer, *fixnum, *integral;
1542 #ifndef MRB_WITHOUT_FLOAT
1546 integral = mrb_define_module(mrb, "Integral");
1549 numeric = mrb_define_class(mrb, "Numeric", mrb->object_class); /* 15.2.7 */
1551 mrb_define_method(mrb, numeric, "**", num_pow, MRB_ARGS_REQ(1));
1552 mrb_define_method(mrb, numeric, "/", num_div, MRB_ARGS_REQ(1)); /* 15.2.8.3.4 */
1553 mrb_define_method(mrb, numeric, "quo", num_div, MRB_ARGS_REQ(1)); /* 15.2.7.4.5 (x) */
1554 mrb_define_method(mrb, numeric, "<=>", num_cmp, MRB_ARGS_REQ(1)); /* 15.2.9.3.6 */
1555 mrb_define_method(mrb, numeric, "<", num_lt, MRB_ARGS_REQ(1));
1556 mrb_define_method(mrb, numeric, "<=", num_le, MRB_ARGS_REQ(1));
1557 mrb_define_method(mrb, numeric, ">", num_gt, MRB_ARGS_REQ(1));
1558 mrb_define_method(mrb, numeric, ">=", num_ge, MRB_ARGS_REQ(1));
1559 mrb_define_method(mrb, numeric, "finite?", num_finite_p, MRB_ARGS_NONE());
1560 mrb_define_method(mrb, numeric, "infinite?",num_infinite_p, MRB_ARGS_NONE());
1561 mrb_define_method(mrb, numeric, "__coerce_step_counter", num_coerce_step_counter, MRB_ARGS_REQ(2));
1564 integer = mrb_define_class(mrb, "Integer", numeric); /* 15.2.8 */
1565 MRB_SET_INSTANCE_TT(integer, MRB_TT_FIXNUM);
1566 mrb_undef_class_method(mrb, integer, "new");
1567 mrb_define_method(mrb, integer, "to_i", int_to_i, MRB_ARGS_NONE()); /* 15.2.8.3.24 */
1568 mrb_define_method(mrb, integer, "to_int", int_to_i, MRB_ARGS_NONE());
1569 #ifndef MRB_WITHOUT_FLOAT
1570 mrb_define_method(mrb, integer, "ceil", int_to_i, MRB_ARGS_REQ(1)); /* 15.2.8.3.8 (x) */
1571 mrb_define_method(mrb, integer, "floor", int_to_i, MRB_ARGS_REQ(1)); /* 15.2.8.3.10 (x) */
1572 mrb_define_method(mrb, integer, "round", int_to_i, MRB_ARGS_REQ(1)); /* 15.2.8.3.12 (x) */
1573 mrb_define_method(mrb, integer, "truncate", int_to_i, MRB_ARGS_REQ(1)); /* 15.2.8.3.15 (x) */
1577 mrb->fixnum_class = fixnum = mrb_define_class(mrb, "Fixnum", integer);
1578 mrb_define_method(mrb, fixnum, "+", fix_plus, MRB_ARGS_REQ(1)); /* 15.2.8.3.1 */
1579 mrb_define_method(mrb, fixnum, "-", fix_minus, MRB_ARGS_REQ(1)); /* 15.2.8.3.2 */
1580 mrb_define_method(mrb, fixnum, "*", fix_mul, MRB_ARGS_REQ(1)); /* 15.2.8.3.3 */
1581 mrb_define_method(mrb, fixnum, "%", fix_mod, MRB_ARGS_REQ(1)); /* 15.2.8.3.5 */
1582 mrb_define_method(mrb, fixnum, "==", fix_equal, MRB_ARGS_REQ(1)); /* 15.2.8.3.7 */
1583 mrb_define_method(mrb, fixnum, "~", fix_rev, MRB_ARGS_NONE()); /* 15.2.8.3.8 */
1584 mrb_define_method(mrb, fixnum, "&", fix_and, MRB_ARGS_REQ(1)); /* 15.2.8.3.9 */
1585 mrb_define_method(mrb, fixnum, "|", fix_or, MRB_ARGS_REQ(1)); /* 15.2.8.3.10 */
1586 mrb_define_method(mrb, fixnum, "^", fix_xor, MRB_ARGS_REQ(1)); /* 15.2.8.3.11 */
1587 mrb_define_method(mrb, fixnum, "<<", fix_lshift, MRB_ARGS_REQ(1)); /* 15.2.8.3.12 */
1588 mrb_define_method(mrb, fixnum, ">>", fix_rshift, MRB_ARGS_REQ(1)); /* 15.2.8.3.13 */
1589 mrb_define_method(mrb, fixnum, "eql?", fix_eql, MRB_ARGS_REQ(1)); /* 15.2.8.3.16 */
1590 #ifndef MRB_WITHOUT_FLOAT
1591 mrb_define_method(mrb, fixnum, "to_f", fix_to_f, MRB_ARGS_NONE()); /* 15.2.8.3.23 */
1593 mrb_define_method(mrb, fixnum, "to_s", fix_to_s, MRB_ARGS_NONE()); /* 15.2.8.3.25 */
1594 mrb_define_method(mrb, fixnum, "inspect", fix_to_s, MRB_ARGS_NONE());
1595 mrb_define_method(mrb, fixnum, "divmod", fix_divmod, MRB_ARGS_REQ(1)); /* 15.2.8.3.30 (x) */
1597 #ifndef MRB_WITHOUT_FLOAT
1599 mrb->float_class = fl = mrb_define_class(mrb, "Float", numeric); /* 15.2.9 */
1600 MRB_SET_INSTANCE_TT(fl, MRB_TT_FLOAT);
1601 mrb_undef_class_method(mrb, fl, "new");
1602 mrb_define_method(mrb, fl, "+", flo_plus, MRB_ARGS_REQ(1)); /* 15.2.9.3.1 */
1603 mrb_define_method(mrb, fl, "-", flo_minus, MRB_ARGS_REQ(1)); /* 15.2.9.3.2 */
1604 mrb_define_method(mrb, fl, "*", flo_mul, MRB_ARGS_REQ(1)); /* 15.2.9.3.3 */
1605 mrb_define_method(mrb, fl, "%", flo_mod, MRB_ARGS_REQ(1)); /* 15.2.9.3.5 */
1606 mrb_define_method(mrb, fl, "==", flo_eq, MRB_ARGS_REQ(1)); /* 15.2.9.3.7 */
1607 mrb_define_method(mrb, fl, "~", flo_rev, MRB_ARGS_NONE());
1608 mrb_define_method(mrb, fl, "&", flo_and, MRB_ARGS_REQ(1));
1609 mrb_define_method(mrb, fl, "|", flo_or, MRB_ARGS_REQ(1));
1610 mrb_define_method(mrb, fl, "^", flo_xor, MRB_ARGS_REQ(1));
1611 mrb_define_method(mrb, fl, ">>", flo_lshift, MRB_ARGS_REQ(1));
1612 mrb_define_method(mrb, fl, "<<", flo_rshift, MRB_ARGS_REQ(1));
1613 mrb_define_method(mrb, fl, "ceil", flo_ceil, MRB_ARGS_NONE()); /* 15.2.9.3.8 */
1614 mrb_define_method(mrb, fl, "finite?", flo_finite_p, MRB_ARGS_NONE()); /* 15.2.9.3.9 */
1615 mrb_define_method(mrb, fl, "floor", flo_floor, MRB_ARGS_NONE()); /* 15.2.9.3.10 */
1616 mrb_define_method(mrb, fl, "infinite?", flo_infinite_p, MRB_ARGS_NONE()); /* 15.2.9.3.11 */
1617 mrb_define_method(mrb, fl, "round", flo_round, MRB_ARGS_OPT(1)); /* 15.2.9.3.12 */
1618 mrb_define_method(mrb, fl, "to_f", flo_to_f, MRB_ARGS_NONE()); /* 15.2.9.3.13 */
1619 mrb_define_method(mrb, fl, "to_i", flo_truncate, MRB_ARGS_NONE()); /* 15.2.9.3.14 */
1620 mrb_define_method(mrb, fl, "to_int", flo_truncate, MRB_ARGS_NONE());
1621 mrb_define_method(mrb, fl, "truncate", flo_truncate, MRB_ARGS_NONE()); /* 15.2.9.3.15 */
1622 mrb_define_method(mrb, fl, "divmod", flo_divmod, MRB_ARGS_REQ(1));
1623 mrb_define_method(mrb, fl, "eql?", flo_eql, MRB_ARGS_REQ(1)); /* 15.2.8.3.16 */
1625 mrb_define_method(mrb, fl, "to_s", flo_to_s, MRB_ARGS_NONE()); /* 15.2.9.3.16(x) */
1626 mrb_define_method(mrb, fl, "inspect", flo_to_s, MRB_ARGS_NONE());
1627 mrb_define_method(mrb, fl, "nan?", flo_nan_p, MRB_ARGS_NONE());
1630 mrb_define_const(mrb, fl, "INFINITY", mrb_float_value(mrb, INFINITY));
1633 mrb_define_const(mrb, fl, "NAN", mrb_float_value(mrb, NAN));
1636 mrb_include_module(mrb, fl, integral);