1 /**********************************************************************
6 created at: Fri Aug 13 18:33:09 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
13 #include "ruby/encoding.h"
18 #if defined(__FreeBSD__) && __FreeBSD__ < 4
19 #include <floatingpoint.h>
30 /* use IEEE 64bit values if not defined */
38 #define DBL_MIN 2.2250738585072014e-308
41 #define DBL_MAX 1.7976931348623157e+308
44 #define DBL_MIN_EXP (-1021)
47 #define DBL_MAX_EXP 1024
49 #ifndef DBL_MIN_10_EXP
50 #define DBL_MIN_10_EXP (-307)
52 #ifndef DBL_MAX_10_EXP
53 #define DBL_MAX_10_EXP 308
59 #define DBL_MANT_DIG 53
62 #define DBL_EPSILON 2.2204460492503131e-16
73 x
= f
+ (x
- f
>= 0.5);
77 x
= f
- (f
- x
>= 0.5);
83 static ID id_coerce
, id_to_i
, id_eq
;
90 VALUE rb_eZeroDivError
;
91 VALUE rb_eFloatDomainError
;
96 rb_raise(rb_eZeroDivError
, "divided by 0");
102 * num.coerce(numeric) => array
104 * If <i>aNumeric</i> is the same type as <i>num</i>, returns an array
105 * containing <i>aNumeric</i> and <i>num</i>. Otherwise, returns an
106 * array with both <i>aNumeric</i> and <i>num</i> represented as
107 * <code>Float</code> objects. This coercion mechanism is used by
108 * Ruby to handle mixed-type numeric operations: it is intended to
109 * find a compatible common type between the two operands of the operator.
111 * 1.coerce(2.5) #=> [2.5, 1.0]
112 * 1.2.coerce(3) #=> [3.0, 1.2]
113 * 1.coerce(2) #=> [2, 1]
117 num_coerce(VALUE x
, VALUE y
)
119 if (CLASS_OF(x
) == CLASS_OF(y
))
120 return rb_assoc_new(y
, x
);
121 return rb_assoc_new(rb_Float(y
), rb_Float(x
));
125 coerce_body(VALUE
*x
)
127 return rb_funcall(x
[1], id_coerce
, 1, x
[0]);
131 coerce_rescue(VALUE
*x
)
133 volatile VALUE v
= rb_inspect(x
[1]);
135 rb_raise(rb_eTypeError
, "%s can't be coerced into %s",
136 rb_special_const_p(x
[1])?
138 rb_obj_classname(x
[1]),
139 rb_obj_classname(x
[0]));
140 return Qnil
; /* dummy */
144 do_coerce(VALUE
*x
, VALUE
*y
, int err
)
149 a
[0] = *x
; a
[1] = *y
;
151 ary
= rb_rescue(coerce_body
, (VALUE
)a
, err
?coerce_rescue
:0, (VALUE
)a
);
152 if (TYPE(ary
) != T_ARRAY
|| RARRAY_LEN(ary
) != 2) {
154 rb_raise(rb_eTypeError
, "coerce must return [x, y]");
159 *x
= RARRAY_PTR(ary
)[0];
160 *y
= RARRAY_PTR(ary
)[1];
165 rb_num_coerce_bin(VALUE x
, VALUE y
, ID func
)
167 do_coerce(&x
, &y
, Qtrue
);
168 return rb_funcall(x
, func
, 1, y
);
172 rb_num_coerce_cmp(VALUE x
, VALUE y
, ID func
)
174 if (do_coerce(&x
, &y
, Qfalse
))
175 return rb_funcall(x
, func
, 1, y
);
180 rb_num_coerce_relop(VALUE x
, VALUE y
, ID func
)
182 VALUE c
, x0
= x
, y0
= y
;
184 if (!do_coerce(&x
, &y
, Qfalse
) ||
185 NIL_P(c
= rb_funcall(x
, func
, 1, y
))) {
187 return Qnil
; /* not reached */
193 * Trap attempts to add methods to <code>Numeric</code> objects. Always
194 * raises a <code>TypeError</code>
198 num_sadded(VALUE x
, VALUE name
)
200 /* ruby_frame = ruby_frame->prev; */ /* pop frame for "singleton_method_added" */
201 /* Numerics should be values; singleton_methods should not be added to them */
202 rb_raise(rb_eTypeError
,
203 "can't define singleton method \"%s\" for %s",
204 rb_id2name(rb_to_id(name
)),
205 rb_obj_classname(x
));
206 return Qnil
; /* not reached */
211 num_init_copy(VALUE x
, VALUE y
)
213 /* Numerics are immutable values, which should not be copied */
214 rb_raise(rb_eTypeError
, "can't copy %s", rb_obj_classname(x
));
215 return Qnil
; /* not reached */
222 * Unary Plus---Returns the receiver's value.
235 * Unary Minus---Returns the receiver's value, negated.
239 num_uminus(VALUE num
)
244 do_coerce(&zero
, &num
, Qtrue
);
246 return rb_funcall(zero
, '-', 1, num
);
251 * num.quo(numeric) => result
252 * num.fdiv(numeric) => result
254 * Equivalent to <code>Numeric#/</code>, but overridden in subclasses.
258 num_quo(VALUE x
, VALUE y
)
260 return rb_funcall(x
, '/', 1, y
);
264 static VALUE
num_floor(VALUE num
);
268 * num.div(numeric) => integer
270 * Uses <code>/</code> to perform division, then converts the result to
271 * an integer. <code>Numeric</code> does not define the <code>/</code>
272 * operator; this is left to subclasses.
276 num_div(VALUE x
, VALUE y
)
278 return num_floor(rb_funcall(x
, '/', 1, y
));
284 * num.divmod( aNumeric ) -> anArray
286 * Returns an array containing the quotient and modulus obtained by
287 * dividing <i>num</i> by <i>aNumeric</i>. If <code>q, r =
288 * x.divmod(y)</code>, then
290 * q = floor(float(x)/float(y))
293 * The quotient is rounded toward -infinity, as shown in the following table:
295 * a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b)
296 * ------+-----+---------------+---------+-------------+---------------
297 * 13 | 4 | 3, 1 | 3 | 1 | 1
298 * ------+-----+---------------+---------+-------------+---------------
299 * 13 | -4 | -4, -3 | -3 | -3 | 1
300 * ------+-----+---------------+---------+-------------+---------------
301 * -13 | 4 | -4, 3 | -4 | 3 | -1
302 * ------+-----+---------------+---------+-------------+---------------
303 * -13 | -4 | 3, -1 | 3 | -1 | -1
304 * ------+-----+---------------+---------+-------------+---------------
305 * 11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5
306 * ------+-----+---------------+---------+-------------+---------------
307 * 11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5
308 * ------+-----+---------------+---------+-------------+---------------
309 * -11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5
310 * ------+-----+---------------+---------+-------------+---------------
311 * -11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5
316 * 11.divmod(3) #=> [3, 2]
317 * 11.divmod(-3) #=> [-4, -1]
318 * 11.divmod(3.5) #=> [3, 0.5]
319 * (-11).divmod(3.5) #=> [-4, 3.0]
320 * (11.5).divmod(3.5) #=> [3, 1.0]
324 num_divmod(VALUE x
, VALUE y
)
326 return rb_assoc_new(num_div(x
, y
), rb_funcall(x
, '%', 1, y
));
331 * num.modulo(numeric) => result
334 * <i>num</i>.<code>divmod(</code><i>aNumeric</i><code>)[1]</code>.
338 num_modulo(VALUE x
, VALUE y
)
340 return rb_funcall(x
, '%', 1, y
);
345 * num.remainder(numeric) => result
347 * If <i>num</i> and <i>numeric</i> have different signs, returns
348 * <em>mod</em>-<i>numeric</i>; otherwise, returns <em>mod</em>. In
349 * both cases <em>mod</em> is the value
350 * <i>num</i>.<code>modulo(</code><i>numeric</i><code>)</code>. The
351 * differences between <code>remainder</code> and modulo
352 * (<code>%</code>) are shown in the table under <code>Numeric#divmod</code>.
356 num_remainder(VALUE x
, VALUE y
)
358 VALUE z
= rb_funcall(x
, '%', 1, y
);
360 if ((!rb_equal(z
, INT2FIX(0))) &&
361 ((RTEST(rb_funcall(x
, '<', 1, INT2FIX(0))) &&
362 RTEST(rb_funcall(y
, '>', 1, INT2FIX(0)))) ||
363 (RTEST(rb_funcall(x
, '>', 1, INT2FIX(0))) &&
364 RTEST(rb_funcall(y
, '<', 1, INT2FIX(0)))))) {
365 return rb_funcall(z
, '-', 1, y
);
372 * num.scalar? -> true or false
374 * Returns <code>true</code> if <i>num</i> is an <code>Scalar</code>
375 * (i.e. non <code>Complex</code>).
379 num_scalar_p(VALUE num
)
386 * num.integer? -> true or false
388 * Returns <code>true</code> if <i>num</i> is an <code>Integer</code>
389 * (including <code>Fixnum</code> and <code>Bignum</code>).
400 * num.abs => num or numeric
402 * Returns the absolute value of <i>num</i>.
405 * (-34.56).abs #=> 34.56
406 * -34.56.abs #=> 34.56
412 if (RTEST(rb_funcall(num
, '<', 1, INT2FIX(0)))) {
413 return rb_funcall(num
, rb_intern("-@"), 0);
421 * num.zero? => true or false
423 * Returns <code>true</code> if <i>num</i> has a zero value.
427 num_zero_p(VALUE num
)
429 if (rb_equal(num
, INT2FIX(0))) {
438 * num.nonzero? => num or nil
440 * Returns <i>num</i> if <i>num</i> is not zero, <code>nil</code>
441 * otherwise. This behavior is useful when chaining comparisons:
443 * a = %w( z Bb bB bb BB a aA Aa AA A )
444 * b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
445 * b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
449 num_nonzero_p(VALUE num
)
451 if (RTEST(rb_funcall(num
, rb_intern("zero?"), 0, 0))) {
459 * num.to_int => integer
461 * Invokes the child class's <code>to_i</code> method to convert
462 * <i>num</i> to an integer.
466 num_to_int(VALUE num
)
468 return rb_funcall(num
, id_to_i
, 0, 0);
472 /********************************************************************
474 * Document-class: Float
476 * <code>Float</code> objects represent real numbers using the native
477 * architecture's double-precision floating point representation.
481 rb_float_new(double d
)
483 NEWOBJ(flt
, struct RFloat
);
484 OBJSETUP(flt
, rb_cFloat
, T_FLOAT
);
486 flt
->float_value
= d
;
494 * Returns a string containing a representation of self. As well as a
495 * fixed or exponential form of the number, the call may return
496 * ``<code>NaN</code>'', ``<code>Infinity</code>'', and
497 * ``<code>-Infinity</code>''.
504 double value
= RFLOAT_VALUE(flt
);
508 return rb_usascii_str_new2(value
< 0 ? "-Infinity" : "Infinity");
509 else if(isnan(value
))
510 return rb_usascii_str_new2("NaN");
512 sprintf(buf
, "%#.15g", value
); /* ensure to print decimal point */
513 if (!(e
= strchr(buf
, 'e'))) {
514 e
= buf
+ strlen(buf
);
516 if (!ISDIGIT(e
[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
517 sprintf(buf
, "%#.14e", value
);
518 if (!(e
= strchr(buf
, 'e'))) {
519 e
= buf
+ strlen(buf
);
523 while (p
[-1]=='0' && ISDIGIT(p
[-2]))
525 memmove(p
, e
, strlen(e
)+1);
526 return rb_usascii_str_new2(buf
);
530 * MISSING: documentation
534 flo_coerce(VALUE x
, VALUE y
)
536 return rb_assoc_new(rb_Float(y
), x
);
543 * Returns float, negated.
547 flo_uminus(VALUE flt
)
549 return DOUBLE2NUM(-RFLOAT_VALUE(flt
));
554 * float + other => float
556 * Returns a new float which is the sum of <code>float</code>
557 * and <code>other</code>.
561 flo_plus(VALUE x
, VALUE y
)
565 return DOUBLE2NUM(RFLOAT_VALUE(x
) + (double)FIX2LONG(y
));
567 return DOUBLE2NUM(RFLOAT_VALUE(x
) + rb_big2dbl(y
));
569 return DOUBLE2NUM(RFLOAT_VALUE(x
) + RFLOAT_VALUE(y
));
571 return rb_num_coerce_bin(x
, y
, '+');
577 * float + other => float
579 * Returns a new float which is the difference of <code>float</code>
580 * and <code>other</code>.
584 flo_minus(VALUE x
, VALUE y
)
588 return DOUBLE2NUM(RFLOAT_VALUE(x
) - (double)FIX2LONG(y
));
590 return DOUBLE2NUM(RFLOAT_VALUE(x
) - rb_big2dbl(y
));
592 return DOUBLE2NUM(RFLOAT_VALUE(x
) - RFLOAT_VALUE(y
));
594 return rb_num_coerce_bin(x
, y
, '-');
600 * float * other => float
602 * Returns a new float which is the product of <code>float</code>
603 * and <code>other</code>.
607 flo_mul(VALUE x
, VALUE y
)
611 return DOUBLE2NUM(RFLOAT_VALUE(x
) * (double)FIX2LONG(y
));
613 return DOUBLE2NUM(RFLOAT_VALUE(x
) * rb_big2dbl(y
));
615 return DOUBLE2NUM(RFLOAT_VALUE(x
) * RFLOAT_VALUE(y
));
617 return rb_num_coerce_bin(x
, y
, '*');
623 * float / other => float
625 * Returns a new float which is the result of dividing
626 * <code>float</code> by <code>other</code>.
630 flo_div(VALUE x
, VALUE y
)
638 return DOUBLE2NUM(RFLOAT_VALUE(x
) / (double)f_y
);
641 return DOUBLE2NUM(RFLOAT_VALUE(x
) / d
);
643 return DOUBLE2NUM(RFLOAT_VALUE(x
) / RFLOAT_VALUE(y
));
645 return rb_num_coerce_bin(x
, y
, '/');
650 flo_quo(VALUE x
, VALUE y
)
652 return rb_funcall(x
, '/', 1, y
);
656 flodivmod(double x
, double y
, double *divp
, double *modp
)
670 if (isinf(x
) && !isinf(y
) && !isnan(y
))
678 if (modp
) *modp
= mod
;
679 if (divp
) *divp
= div
;
685 * flt % other => float
686 * flt.modulo(other) => float
688 * Return the modulo after division of <code>flt</code> by <code>other</code>.
690 * 6543.21.modulo(137) #=> 104.21
691 * 6543.21.modulo(137.24) #=> 92.9299999999996
695 flo_mod(VALUE x
, VALUE y
)
701 fy
= (double)FIX2LONG(y
);
707 fy
= RFLOAT_VALUE(y
);
710 return rb_num_coerce_bin(x
, y
, '%');
712 flodivmod(RFLOAT_VALUE(x
), fy
, 0, &mod
);
713 return DOUBLE2NUM(mod
);
721 return LONG2FIX((long)d
);
723 else if (isnan(d
) || isinf(d
)) {
724 /* special case: cannot return integer value */
725 return rb_float_new(d
);
728 return rb_dbl2big(d
);
734 * flt.divmod(numeric) => array
736 * See <code>Numeric#divmod</code>.
740 flo_divmod(VALUE x
, VALUE y
)
747 fy
= (double)FIX2LONG(y
);
753 fy
= RFLOAT_VALUE(y
);
756 return rb_num_coerce_bin(x
, y
, rb_intern("divmod"));
758 flodivmod(RFLOAT_VALUE(x
), fy
, &div
, &mod
);
761 return rb_assoc_new(a
, b
);
767 * flt ** other => float
769 * Raises <code>float</code> the <code>other</code> power.
773 flo_pow(VALUE x
, VALUE y
)
777 return DOUBLE2NUM(pow(RFLOAT_VALUE(x
), (double)FIX2LONG(y
)));
779 return DOUBLE2NUM(pow(RFLOAT_VALUE(x
), rb_big2dbl(y
)));
781 return DOUBLE2NUM(pow(RFLOAT_VALUE(x
), RFLOAT_VALUE(y
)));
783 return rb_num_coerce_bin(x
, y
, rb_intern("**"));
789 * num.eql?(numeric) => true or false
791 * Returns <code>true</code> if <i>num</i> and <i>numeric</i> are the
792 * same type and have equal values.
795 * 1.eql?(1.0) #=> false
796 * (1.0).eql?(1.0) #=> true
800 num_eql(VALUE x
, VALUE y
)
802 if (TYPE(x
) != TYPE(y
)) return Qfalse
;
804 return rb_equal(x
, y
);
809 * num <=> other -> 0 or nil
811 * Returns zero if <i>num</i> equals <i>other</i>, <code>nil</code>
816 num_cmp(VALUE x
, VALUE y
)
818 if (x
== y
) return INT2FIX(0);
823 num_equal(VALUE x
, VALUE y
)
825 if (x
== y
) return Qtrue
;
826 return rb_funcall(y
, id_eq
, 1, x
);
831 * flt == obj => true or false
833 * Returns <code>true</code> only if <i>obj</i> has the same value
834 * as <i>flt</i>. Contrast this with <code>Float#eql?</code>, which
835 * requires <i>obj</i> to be a <code>Float</code>.
842 flo_eq(VALUE x
, VALUE y
)
844 volatile double a
, b
;
855 if (isnan(b
)) return Qfalse
;
858 return num_equal(x
, y
);
861 if (isnan(a
)) return Qfalse
;
862 return (a
== b
)?Qtrue
:Qfalse
;
867 * flt.hash => integer
869 * Returns a hash code for this float.
878 d
= RFLOAT_VALUE(num
);
879 hash
= rb_memhash(&d
, sizeof(d
));
880 return INT2FIX(hash
);
884 rb_dbl_cmp(double a
, double b
)
886 if (isnan(a
) || isnan(b
)) return Qnil
;
887 if (a
== b
) return INT2FIX(0);
888 if (a
> b
) return INT2FIX(1);
889 if (a
< b
) return INT2FIX(-1);
895 * flt <=> numeric => -1, 0, +1
897 * Returns -1, 0, or +1 depending on whether <i>flt</i> is less than,
898 * equal to, or greater than <i>numeric</i>. This is the basis for the
899 * tests in <code>Comparable</code>.
903 flo_cmp(VALUE x
, VALUE y
)
910 b
= (double)FIX2LONG(y
);
922 return rb_num_coerce_cmp(x
, y
, rb_intern("<=>"));
924 return rb_dbl_cmp(a
, b
);
929 * flt > other => true or false
931 * <code>true</code> if <code>flt</code> is greater than <code>other</code>.
935 flo_gt(VALUE x
, VALUE y
)
942 b
= (double)FIX2LONG(y
);
951 if (isnan(b
)) return Qfalse
;
955 return rb_num_coerce_relop(x
, y
, '>');
957 if (isnan(a
)) return Qfalse
;
958 return (a
> b
)?Qtrue
:Qfalse
;
963 * flt >= other => true or false
965 * <code>true</code> if <code>flt</code> is greater than
966 * or equal to <code>other</code>.
970 flo_ge(VALUE x
, VALUE y
)
977 b
= (double)FIX2LONG(y
);
986 if (isnan(b
)) return Qfalse
;
990 return rb_num_coerce_relop(x
, y
, rb_intern(">="));
992 if (isnan(a
)) return Qfalse
;
993 return (a
>= b
)?Qtrue
:Qfalse
;
998 * flt < other => true or false
1000 * <code>true</code> if <code>flt</code> is less than <code>other</code>.
1004 flo_lt(VALUE x
, VALUE y
)
1008 a
= RFLOAT_VALUE(x
);
1011 b
= (double)FIX2LONG(y
);
1019 b
= RFLOAT_VALUE(y
);
1020 if (isnan(b
)) return Qfalse
;
1024 return rb_num_coerce_relop(x
, y
, '<');
1026 if (isnan(a
)) return Qfalse
;
1027 return (a
< b
)?Qtrue
:Qfalse
;
1032 * flt <= other => true or false
1034 * <code>true</code> if <code>flt</code> is less than
1035 * or equal to <code>other</code>.
1039 flo_le(VALUE x
, VALUE y
)
1043 a
= RFLOAT_VALUE(x
);
1046 b
= (double)FIX2LONG(y
);
1054 b
= RFLOAT_VALUE(y
);
1055 if (isnan(b
)) return Qfalse
;
1059 return rb_num_coerce_relop(x
, y
, rb_intern("<="));
1061 if (isnan(a
)) return Qfalse
;
1062 return (a
<= b
)?Qtrue
:Qfalse
;
1067 * flt.eql?(obj) => true or false
1069 * Returns <code>true</code> only if <i>obj</i> is a
1070 * <code>Float</code> with the same value as <i>flt</i>. Contrast this
1071 * with <code>Float#==</code>, which performs type conversions.
1073 * 1.0.eql?(1) #=> false
1077 flo_eql(VALUE x
, VALUE y
)
1079 if (TYPE(y
) == T_FLOAT
) {
1080 double a
= RFLOAT_VALUE(x
);
1081 double b
= RFLOAT_VALUE(y
);
1083 if (isnan(a
) || isnan(b
)) return Qfalse
;
1084 if (a
== b
) return Qtrue
;
1093 * As <code>flt</code> is already a float, returns <i>self</i>.
1106 * Returns the absolute value of <i>flt</i>.
1108 * (-34.56).abs #=> 34.56
1109 * -34.56.abs #=> 34.56
1116 double val
= fabs(RFLOAT_VALUE(flt
));
1117 return DOUBLE2NUM(val
);
1122 * flt.zero? -> true or false
1124 * Returns <code>true</code> if <i>flt</i> is 0.0.
1129 flo_zero_p(VALUE num
)
1131 if (RFLOAT_VALUE(num
) == 0.0) {
1139 * flt.nan? -> true or false
1141 * Returns <code>true</code> if <i>flt</i> is an invalid IEEE floating
1146 * a = 0.0/0.0 #=> NaN
1151 flo_is_nan_p(VALUE num
)
1153 double value
= RFLOAT_VALUE(num
);
1155 return isnan(value
) ? Qtrue
: Qfalse
;
1160 * flt.infinite? -> nil, -1, +1
1162 * Returns <code>nil</code>, -1, or +1 depending on whether <i>flt</i>
1163 * is finite, -infinity, or +infinity.
1165 * (0.0).infinite? #=> nil
1166 * (-1.0/0.0).infinite? #=> -1
1167 * (+1.0/0.0).infinite? #=> 1
1171 flo_is_infinite_p(VALUE num
)
1173 double value
= RFLOAT_VALUE(num
);
1176 return INT2FIX( value
< 0 ? -1 : 1 );
1184 * flt.finite? -> true or false
1186 * Returns <code>true</code> if <i>flt</i> is a valid IEEE floating
1187 * point number (it is not infinite, and <code>nan?</code> is
1188 * <code>false</code>).
1193 flo_is_finite_p(VALUE num
)
1195 double value
= RFLOAT_VALUE(num
);
1201 if (isinf(value
) || isnan(value
))
1210 * flt.floor => integer
1212 * Returns the largest integer less than or equal to <i>flt</i>.
1216 * (-1.2).floor #=> -2
1217 * (-2.0).floor #=> -2
1221 flo_floor(VALUE num
)
1223 double f
= floor(RFLOAT_VALUE(num
));
1227 return rb_dbl2big(f
);
1230 return LONG2FIX(val
);
1235 * flt.ceil => integer
1237 * Returns the smallest <code>Integer</code> greater than or equal to
1242 * (-1.2).ceil #=> -1
1243 * (-2.0).ceil #=> -2
1249 double f
= ceil(RFLOAT_VALUE(num
));
1253 return rb_dbl2big(f
);
1256 return LONG2FIX(val
);
1261 * flt.round([ndigits]) => integer or float
1263 * Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits).
1264 * Precision may be negative. Returns a a floating point number when ndigits
1268 * (-1.5).round #=> -2
1272 flo_round(int argc
, VALUE
*argv
, VALUE num
)
1279 if (argc
> 0 && rb_scan_args(argc
, argv
, "01", &nd
) == 1) {
1280 ndigits
= NUM2INT(nd
);
1282 number
= RFLOAT_VALUE(num
);
1288 if (ndigits
< 0) number
/= f
;
1290 number
= round(number
);
1291 if (ndigits
< 0) number
*= f
;
1294 if (ndigits
> 0) return DOUBLE2NUM(number
);
1296 if (!FIXABLE(number
)) {
1297 return rb_dbl2big(number
);
1300 return LONG2FIX(val
);
1305 * flt.to_i => integer
1306 * flt.to_int => integer
1307 * flt.truncate => integer
1309 * Returns <i>flt</i> truncated to an <code>Integer</code>.
1313 flo_truncate(VALUE num
)
1315 double f
= RFLOAT_VALUE(num
);
1318 if (f
> 0.0) f
= floor(f
);
1319 if (f
< 0.0) f
= ceil(f
);
1322 return rb_dbl2big(f
);
1325 return LONG2FIX(val
);
1331 * num.floor => integer
1333 * Returns the largest integer less than or equal to <i>num</i>.
1334 * <code>Numeric</code> implements this by converting <i>anInteger</i>
1335 * to a <code>Float</code> and invoking <code>Float#floor</code>.
1342 num_floor(VALUE num
)
1344 return flo_floor(rb_Float(num
));
1350 * num.ceil => integer
1352 * Returns the smallest <code>Integer</code> greater than or equal to
1353 * <i>num</i>. Class <code>Numeric</code> achieves this by converting
1354 * itself to a <code>Float</code> then invoking
1355 * <code>Float#ceil</code>.
1359 * (-1.2).ceil #=> -1
1360 * (-1.0).ceil #=> -1
1366 return flo_ceil(rb_Float(num
));
1371 * num.round([ndigits]) => integer or float
1373 * Rounds <i>num</i> to a given precision in decimal digits (default 0 digits).
1374 * Precision may be negative. Returns a a floating point number when ndigits
1375 * is more than one. <code>Numeric</code> implements this by converting itself
1376 * to a <code>Float</code> and invoking <code>Float#round</code>.
1380 num_round(int argc
, VALUE
* argv
, VALUE num
)
1382 return flo_round(argc
, argv
, rb_Float(num
));
1387 * num.truncate => integer
1389 * Returns <i>num</i> truncated to an integer. <code>Numeric</code>
1390 * implements this by converting its value to a float and invoking
1391 * <code>Float#truncate</code>.
1395 num_truncate(VALUE num
)
1397 return flo_truncate(rb_Float(num
));
1403 * num.step(limit, step ) {|i| block } => num
1405 * Invokes <em>block</em> with the sequence of numbers starting at
1406 * <i>num</i>, incremented by <i>step</i> on each call. The loop
1407 * finishes when the value to be passed to the block is greater than
1408 * <i>limit</i> (if <i>step</i> is positive) or less than
1409 * <i>limit</i> (if <i>step</i> is negative). If all the arguments are
1410 * integers, the loop operates using an integer counter. If any of the
1411 * arguments are floating point numbers, all are converted to floats,
1412 * and the loop is executed <i>floor(n + n*epsilon)+ 1</i> times,
1413 * where <i>n = (limit - num)/step</i>. Otherwise, the loop
1414 * starts at <i>num</i>, uses either the <code><</code> or
1415 * <code>></code> operator to compare the counter against
1416 * <i>limit</i>, and increments itself using the <code>+</code>
1419 * 1.step(10, 2) { |i| print i, " " }
1420 * Math::E.step(Math::PI, 0.2) { |f| print f, " " }
1422 * <em>produces:</em>
1425 * 2.71828182845905 2.91828182845905 3.11828182845905
1429 num_step(int argc
, VALUE
*argv
, VALUE from
)
1433 RETURN_ENUMERATOR(from
, argc
, argv
);
1444 rb_raise(rb_eArgError
, "wrong number of arguments");
1446 if (rb_equal(step
, INT2FIX(0))) {
1447 rb_raise(rb_eArgError
, "step can't be 0");
1451 if (FIXNUM_P(from
) && FIXNUM_P(to
) && FIXNUM_P(step
)) {
1456 diff
= FIX2LONG(step
);
1460 rb_yield(LONG2FIX(i
));
1466 rb_yield(LONG2FIX(i
));
1471 else if (TYPE(from
) == T_FLOAT
|| TYPE(to
) == T_FLOAT
|| TYPE(step
) == T_FLOAT
) {
1472 const double epsilon
= DBL_EPSILON
;
1473 double beg
= NUM2DBL(from
);
1474 double end
= NUM2DBL(to
);
1475 double unit
= NUM2DBL(step
);
1476 double n
= (end
- beg
)/unit
;
1477 double err
= (fabs(beg
) + fabs(end
) + fabs(end
-beg
)) / fabs(unit
) * epsilon
;
1480 if (err
>0.5) err
=0.5;
1481 n
= floor(n
+ err
) + 1;
1482 for (i
=0; i
<n
; i
++) {
1483 rb_yield(DOUBLE2NUM(i
*unit
+beg
));
1490 if (RTEST(rb_funcall(step
, '>', 1, INT2FIX(0)))) {
1497 if (RTEST(rb_funcall(i
, cmp
, 1, to
))) break;
1499 i
= rb_funcall(i
, '+', 1, step
);
1506 rb_num2long(VALUE val
)
1510 rb_raise(rb_eTypeError
, "no implicit conversion from nil to integer");
1513 if (FIXNUM_P(val
)) return FIX2LONG(val
);
1515 switch (TYPE(val
)) {
1517 if (RFLOAT_VALUE(val
) <= (double)LONG_MAX
1518 && RFLOAT_VALUE(val
) >= (double)LONG_MIN
) {
1519 return (SIGNED_VALUE
)(RFLOAT_VALUE(val
));
1525 sprintf(buf
, "%-.10g", RFLOAT_VALUE(val
));
1526 if ((s
= strchr(buf
, ' ')) != 0) *s
= '\0';
1527 rb_raise(rb_eRangeError
, "float %s out of range of integer", buf
);
1531 return rb_big2long(val
);
1534 val
= rb_to_int(val
);
1540 rb_num2ulong(VALUE val
)
1542 if (TYPE(val
) == T_BIGNUM
) {
1543 return rb_big2ulong(val
);
1545 return (VALUE
)rb_num2long(val
);
1548 #if SIZEOF_INT < SIZEOF_VALUE
1550 check_int(SIGNED_VALUE num
)
1554 if (num
< INT_MIN
) {
1557 else if (num
> INT_MAX
) {
1563 #ifdef LONG_LONG_VALUE
1564 rb_raise(rb_eRangeError
, "integer %lld too %s to convert to `int'", num
, s
);
1566 rb_raise(rb_eRangeError
, "integer %ld too %s to convert to `int'", num
, s
);
1571 check_uint(VALUE num
)
1573 if (num
> UINT_MAX
) {
1574 #ifdef LONG_LONG_VALUE
1575 rb_raise(rb_eRangeError
, "integer %llu too big to convert to `unsigned int'", num
);
1577 rb_raise(rb_eRangeError
, "integer %lu too big to convert to `unsigned int'", num
);
1583 rb_num2int(VALUE val
)
1585 long num
= rb_num2long(val
);
1592 rb_fix2int(VALUE val
)
1594 long num
= FIXNUM_P(val
)?FIX2LONG(val
):rb_num2long(val
);
1601 rb_num2uint(VALUE val
)
1603 unsigned long num
= rb_num2ulong(val
);
1605 if (RTEST(rb_funcall(INT2FIX(0), '<', 1, val
))) {
1612 rb_fix2uint(VALUE val
)
1616 if (!FIXNUM_P(val
)) {
1617 return rb_num2uint(val
);
1619 num
= FIX2ULONG(val
);
1620 if (FIX2LONG(val
) > 0) {
1627 rb_num2int(VALUE val
)
1629 return rb_num2long(val
);
1633 rb_fix2int(VALUE val
)
1635 return FIX2INT(val
);
1640 rb_num2fix(VALUE val
)
1644 if (FIXNUM_P(val
)) return val
;
1646 v
= rb_num2long(val
);
1648 rb_raise(rb_eRangeError
, "integer %ld out of range of fixnum", v
);
1655 rb_num2ll(VALUE val
)
1658 rb_raise(rb_eTypeError
, "no implicit conversion from nil");
1661 if (FIXNUM_P(val
)) return (LONG_LONG
)FIX2LONG(val
);
1663 switch (TYPE(val
)) {
1665 if (RFLOAT_VALUE(val
) <= (double)LLONG_MAX
1666 && RFLOAT_VALUE(val
) >= (double)LLONG_MIN
) {
1667 return (LONG_LONG
)(RFLOAT_VALUE(val
));
1673 sprintf(buf
, "%-.10g", RFLOAT_VALUE(val
));
1674 if ((s
= strchr(buf
, ' ')) != 0) *s
= '\0';
1675 rb_raise(rb_eRangeError
, "float %s out of range of long long", buf
);
1679 return rb_big2ll(val
);
1682 rb_raise(rb_eTypeError
, "no implicit conversion from string");
1683 return Qnil
; /* not reached */
1687 rb_raise(rb_eTypeError
, "no implicit conversion from boolean");
1688 return Qnil
; /* not reached */
1691 val
= rb_to_int(val
);
1697 rb_num2ull(VALUE val
)
1699 if (TYPE(val
) == T_BIGNUM
) {
1700 return rb_big2ull(val
);
1702 return (unsigned LONG_LONG
)rb_num2ll(val
);
1705 #endif /* HAVE_LONG_LONG */
1708 num_numerator(VALUE num
)
1710 return rb_funcall(rb_Rational1(num
), rb_intern("numerator"), 0);
1714 num_denominator(VALUE num
)
1716 return rb_funcall(rb_Rational1(num
), rb_intern("denominator"), 0);
1720 * Document-class: Integer
1722 * <code>Integer</code> is the basis for the two concrete classes that
1723 * hold whole numbers, <code>Bignum</code> and <code>Fixnum</code>.
1735 * int.truncate => int
1737 * As <i>int</i> is already an <code>Integer</code>, all these
1738 * methods simply return the receiver.
1749 * int.integer? -> true
1751 * Always returns <code>true</code>.
1755 int_int_p(VALUE num
)
1762 * int.odd? -> true or false
1764 * Returns <code>true</code> if <i>int</i> is an odd number.
1768 int_odd_p(VALUE num
)
1770 if (rb_funcall(num
, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
1778 * int.even? -> true or false
1780 * Returns <code>true</code> if <i>int</i> is an even number.
1784 int_even_p(VALUE num
)
1786 if (rb_funcall(num
, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
1794 * fixnum.next => integer
1795 * fixnum.succ => integer
1797 * Returns the <code>Integer</code> equal to <i>int</i> + 1.
1806 long i
= FIX2LONG(num
) + 1;
1812 * int.next => integer
1813 * int.succ => integer
1815 * Returns the <code>Integer</code> equal to <i>int</i> + 1.
1824 if (FIXNUM_P(num
)) {
1825 long i
= FIX2LONG(num
) + 1;
1828 return rb_funcall(num
, '+', 1, INT2FIX(1));
1833 * int.pred => integer
1835 * Returns the <code>Integer</code> equal to <i>int</i> - 1.
1844 if (FIXNUM_P(num
)) {
1845 long i
= FIX2LONG(num
) - 1;
1848 return rb_funcall(num
, '-', 1, INT2FIX(1));
1853 * int.chr([encoding]) => string
1855 * Returns a string containing the character represented by the
1856 * receiver's value according to +encoding+.
1859 * 230.chr #=> "\346"
1860 * 255.chr(Encoding::UTF_8) #=> "\303\277"
1864 int_chr(int argc
, VALUE
*argv
, VALUE num
)
1868 long i
= NUM2LONG(num
);
1874 if (i
< 0 || 0xff < i
) {
1876 rb_raise(rb_eRangeError
, "%ld out of char range", i
);
1880 return rb_usascii_str_new(&c
, 1);
1883 return rb_str_new(&c
, 1);
1888 rb_raise(rb_eArgError
, "wrong number of arguments (%d for 0 or 1)", argc
);
1891 enc
= rb_to_encoding(argv
[0]);
1892 if (!enc
) enc
= rb_ascii8bit_encoding();
1893 if (i
< 0 || (n
= rb_enc_codelen(i
, enc
)) <= 0) goto out_of_range
;
1894 str
= rb_enc_str_new(0, n
, enc
);
1895 rb_enc_mbcput(i
, RSTRING_PTR(str
), enc
);
1900 int_numerator(VALUE num
)
1906 int_denominator(VALUE num
)
1911 /********************************************************************
1913 * Document-class: Fixnum
1915 * A <code>Fixnum</code> holds <code>Integer</code> values that can be
1916 * represented in a native machine word (minus 1 bit). If any operation
1917 * on a <code>Fixnum</code> exceeds this range, the value is
1918 * automatically converted to a <code>Bignum</code>.
1920 * <code>Fixnum</code> objects have immediate value. This means that
1921 * when they are assigned or passed as parameters, the actual object is
1922 * passed, rather than a reference to that object. Assignment does not
1923 * alias <code>Fixnum</code> objects. There is effectively only one
1924 * <code>Fixnum</code> object instance for any given integer value, so,
1925 * for example, you cannot add a singleton method to a
1926 * <code>Fixnum</code>.
1932 * Fixnum.induced_from(obj) => fixnum
1934 * Convert <code>obj</code> to a Fixnum. Works with numeric parameters.
1935 * Also works with Symbols, but this is deprecated.
1939 rb_fix_induced_from(VALUE klass
, VALUE x
)
1941 return rb_num2fix(x
);
1946 * Integer.induced_from(obj) => fixnum, bignum
1948 * Convert <code>obj</code> to an Integer.
1952 rb_int_induced_from(VALUE klass
, VALUE x
)
1960 return rb_funcall(x
, id_to_i
, 0);
1962 rb_raise(rb_eTypeError
, "failed to convert %s into Integer",
1963 rb_obj_classname(x
));
1969 * Float.induced_from(obj) => float
1971 * Convert <code>obj</code> to a float.
1975 rb_flo_induced_from(VALUE klass
, VALUE x
)
1981 return rb_funcall(x
, rb_intern("to_f"), 0);
1985 rb_raise(rb_eTypeError
, "failed to convert %s into Float",
1986 rb_obj_classname(x
));
1994 * Negates <code>fix</code> (which might return a Bignum).
1998 fix_uminus(VALUE num
)
2000 return LONG2NUM(-FIX2LONG(num
));
2004 rb_fix2str(VALUE x
, int base
)
2006 extern const char ruby_digitmap
[];
2007 char buf
[SIZEOF_VALUE
*CHAR_BIT
+ 2], *b
= buf
+ sizeof buf
;
2008 long val
= FIX2LONG(x
);
2011 if (base
< 2 || 36 < base
) {
2012 rb_raise(rb_eArgError
, "invalid radix %d", base
);
2015 return rb_usascii_str_new2("0");
2023 *--b
= ruby_digitmap
[(int)(val
% base
)];
2024 } while (val
/= base
);
2029 return rb_usascii_str_new2(b
);
2034 * fix.to_s( base=10 ) -> aString
2036 * Returns a string containing the representation of <i>fix</i> radix
2037 * <i>base</i> (between 2 and 36).
2039 * 12345.to_s #=> "12345"
2040 * 12345.to_s(2) #=> "11000000111001"
2041 * 12345.to_s(8) #=> "30071"
2042 * 12345.to_s(10) #=> "12345"
2043 * 12345.to_s(16) #=> "3039"
2044 * 12345.to_s(36) #=> "9ix"
2048 fix_to_s(int argc
, VALUE
*argv
, VALUE x
)
2052 if (argc
== 0) base
= 10;
2056 rb_scan_args(argc
, argv
, "01", &b
);
2060 return rb_fix2str(x
, base
);
2065 * fix + numeric => numeric_result
2067 * Performs addition: the class of the resulting object depends on
2068 * the class of <code>numeric</code> and on the magnitude of the
2073 fix_plus(VALUE x
, VALUE y
)
2088 return rb_big_plus(y
, x
);
2090 return DOUBLE2NUM((double)FIX2LONG(x
) + RFLOAT_VALUE(y
));
2092 return rb_num_coerce_bin(x
, y
, '+');
2098 * fix - numeric => numeric_result
2100 * Performs subtraction: the class of the resulting object depends on
2101 * the class of <code>numeric</code> and on the magnitude of the
2106 fix_minus(VALUE x
, VALUE y
)
2121 x
= rb_int2big(FIX2LONG(x
));
2122 return rb_big_minus(x
, y
);
2124 return DOUBLE2NUM((double)FIX2LONG(x
) - RFLOAT_VALUE(y
));
2126 return rb_num_coerce_bin(x
, y
, '-');
2130 #define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
2131 /*tests if N*N would overflow*/
2132 #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
2136 * fix * numeric => numeric_result
2138 * Performs multiplication: the class of the resulting object depends on
2139 * the class of <code>numeric</code> and on the magnitude of the
2144 fix_mul(VALUE x
, VALUE y
)
2148 /* avoids an optimization bug of HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005] */
2152 #if SIZEOF_VALUE * 2 <= SIZEOF_LONG_LONG
2162 #if SIZEOF_VALUE * 2 <= SIZEOF_LONG_LONG
2163 d
= (LONG_LONG
)a
* b
;
2164 if (FIXABLE(d
)) return LONG2FIX(d
);
2165 return rb_ll2inum(d
);
2167 if (FIT_SQRT_LONG(a
) && FIT_SQRT_LONG(b
))
2168 return LONG2FIX(a
*b
);
2172 if (a
== 0) return x
;
2173 if (FIX2LONG(r
) != c
|| c
/a
!= b
) {
2174 r
= rb_big_mul(rb_int2big(a
), rb_int2big(b
));
2181 return rb_big_mul(y
, x
);
2183 return DOUBLE2NUM((double)FIX2LONG(x
) * RFLOAT_VALUE(y
));
2185 return rb_num_coerce_bin(x
, y
, '*');
2190 fixdivmod(long x
, long y
, long *divp
, long *modp
)
2194 if (y
== 0) rb_num_zerodiv();
2208 if ((mod
< 0 && y
> 0) || (mod
> 0 && y
< 0)) {
2212 if (divp
) *divp
= div
;
2213 if (modp
) *modp
= mod
;
2218 * fix.quo(numeric) => float
2219 * fix.fdiv(numeric) => float
2221 * Returns the floating point result of dividing <i>fix</i> by
2224 * 654321.quo(13731) #=> 47.6528293642124
2225 * 654321.quo(13731.24) #=> 47.6519964693647
2230 fix_quo(VALUE x
, VALUE y
)
2232 return rb_funcall(rb_rational_raw1(x
), '/', 1, y
);
2236 fix_fdiv(VALUE x
, VALUE y
)
2239 return DOUBLE2NUM((double)FIX2LONG(x
) / (double)FIX2LONG(y
));
2243 return DOUBLE2NUM((double)FIX2LONG(x
) / rb_big2dbl(y
));
2245 return DOUBLE2NUM((double)FIX2LONG(x
) / RFLOAT_VALUE(y
));
2247 return rb_num_coerce_bin(x
, y
, rb_intern("fdiv"));
2252 fix_divide(VALUE x
, VALUE y
, ID op
)
2257 fixdivmod(FIX2LONG(x
), FIX2LONG(y
), &div
, 0);
2258 return LONG2NUM(div
);
2262 x
= rb_int2big(FIX2LONG(x
));
2263 return rb_big_div(x
, y
);
2266 double div
= (double)FIX2LONG(x
) / RFLOAT_VALUE(y
);
2268 return DOUBLE2NUM(div
);
2271 return rb_dbl2big(div
);
2275 return rb_num_coerce_bin(x
, y
, op
);
2281 * fix / numeric => numeric_result
2283 * Performs division: the class of the resulting object depends on
2284 * the class of <code>numeric</code> and on the magnitude of the
2289 fix_div(VALUE x
, VALUE y
)
2291 return fix_divide(x
, y
, '/');
2296 * fix.div(numeric) => numeric_result
2298 * Performs integer division: returns integer value.
2302 fix_idiv(VALUE x
, VALUE y
)
2304 return fix_divide(x
, y
, rb_intern("div"));
2309 * fix % other => Numeric
2310 * fix.modulo(other) => Numeric
2312 * Returns <code>fix</code> modulo <code>other</code>.
2313 * See <code>Numeric.divmod</code> for more information.
2317 fix_mod(VALUE x
, VALUE y
)
2322 fixdivmod(FIX2LONG(x
), FIX2LONG(y
), 0, &mod
);
2323 return LONG2NUM(mod
);
2327 x
= rb_int2big(FIX2LONG(x
));
2328 return rb_big_modulo(x
, y
);
2333 flodivmod((double)FIX2LONG(x
), RFLOAT_VALUE(y
), 0, &mod
);
2334 return DOUBLE2NUM(mod
);
2337 return rb_num_coerce_bin(x
, y
, '%');
2343 * fix.divmod(numeric) => array
2345 * See <code>Numeric#divmod</code>.
2348 fix_divmod(VALUE x
, VALUE y
)
2353 fixdivmod(FIX2LONG(x
), FIX2LONG(y
), &div
, &mod
);
2355 return rb_assoc_new(LONG2NUM(div
), LONG2NUM(mod
));
2359 x
= rb_int2big(FIX2LONG(x
));
2360 return rb_big_divmod(x
, y
);
2364 volatile VALUE a
, b
;
2366 flodivmod((double)FIX2LONG(x
), RFLOAT_VALUE(y
), &div
, &mod
);
2368 b
= DOUBLE2NUM(mod
);
2369 return rb_assoc_new(a
, b
);
2372 return rb_num_coerce_bin(x
, y
, rb_intern("divmod"));
2377 int_pow(long x
, unsigned long y
)
2389 while (y
% 2 == 0) {
2390 if (!FIT_SQRT_LONG(x
)) {
2393 v
= rb_big_pow(rb_int2big(x
), LONG2NUM(y
));
2394 if (z
!= 1) v
= rb_big_mul(rb_int2big(neg
? -z
: z
), v
);
2402 if (!POSFIXABLE(xz
) || xz
/ x
!= z
) {
2414 * fix ** other => Numeric
2416 * Raises <code>fix</code> to the <code>other</code> power, which may
2417 * be negative or fractional.
2421 * 2 ** 0.5 #=> 1.4142135623731
2425 fix_pow(VALUE x
, VALUE y
)
2427 static const double zero
= 0.0;
2428 long a
= FIX2LONG(x
);
2431 long b
= FIX2LONG(y
);
2434 return rb_funcall(rb_rational_raw1(x
), rb_intern("**"), 1, y
);
2436 if (b
== 0) return INT2FIX(1);
2437 if (b
== 1) return x
;
2439 if (b
> 0) return INT2FIX(0);
2440 return DOUBLE2NUM(1.0 / zero
);
2442 if (a
== 1) return INT2FIX(1);
2449 return int_pow(a
, b
);
2454 if (rb_funcall(y
, '<', 1, INT2FIX(0)))
2455 return rb_funcall(rb_rational_raw1(x
), rb_intern("**"), 1, y
);
2457 if (a
== 0) return INT2FIX(0);
2458 if (a
== 1) return INT2FIX(1);
2460 if (int_even_p(y
)) return INT2FIX(1);
2461 else return INT2FIX(-1);
2463 x
= rb_int2big(FIX2LONG(x
));
2464 return rb_big_pow(x
, y
);
2466 if (RFLOAT_VALUE(y
) == 0.0) return DOUBLE2NUM(1.0);
2468 return DOUBLE2NUM(RFLOAT_VALUE(y
) < 0 ? (1.0 / zero
) : 0.0);
2470 if (a
== 1) return DOUBLE2NUM(1.0);
2471 return DOUBLE2NUM(pow((double)a
, RFLOAT_VALUE(y
)));
2473 return rb_num_coerce_bin(x
, y
, rb_intern("**"));
2481 * Return <code>true</code> if <code>fix</code> equals <code>other</code>
2489 fix_equal(VALUE x
, VALUE y
)
2491 if (x
== y
) return Qtrue
;
2492 if (FIXNUM_P(y
)) return Qfalse
;
2495 return rb_big_eq(y
, x
);
2497 return (double)FIX2LONG(x
) == RFLOAT_VALUE(y
) ? Qtrue
: Qfalse
;
2499 return num_equal(x
, y
);
2505 * fix <=> numeric => -1, 0, +1
2507 * Comparison---Returns -1, 0, or +1 depending on whether <i>fix</i> is
2508 * less than, equal to, or greater than <i>numeric</i>. This is the
2509 * basis for the tests in <code>Comparable</code>.
2513 fix_cmp(VALUE x
, VALUE y
)
2515 if (x
== y
) return INT2FIX(0);
2517 if (FIX2LONG(x
) > FIX2LONG(y
)) return INT2FIX(1);
2522 return rb_big_cmp(rb_int2big(FIX2LONG(x
)), y
);
2524 return rb_dbl_cmp((double)FIX2LONG(x
), RFLOAT_VALUE(y
));
2526 return rb_num_coerce_cmp(x
, y
, rb_intern("<=>"));
2532 * fix > other => true or false
2534 * Returns <code>true</code> if the value of <code>fix</code> is
2535 * greater than that of <code>other</code>.
2539 fix_gt(VALUE x
, VALUE y
)
2542 if (FIX2LONG(x
) > FIX2LONG(y
)) return Qtrue
;
2547 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x
)), y
)) > 0 ? Qtrue
: Qfalse
;
2549 return (double)FIX2LONG(x
) > RFLOAT_VALUE(y
) ? Qtrue
: Qfalse
;
2551 return rb_num_coerce_relop(x
, y
, '>');
2557 * fix >= other => true or false
2559 * Returns <code>true</code> if the value of <code>fix</code> is
2560 * greater than or equal to that of <code>other</code>.
2564 fix_ge(VALUE x
, VALUE y
)
2567 if (FIX2LONG(x
) >= FIX2LONG(y
)) return Qtrue
;
2572 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x
)), y
)) >= 0 ? Qtrue
: Qfalse
;
2574 return (double)FIX2LONG(x
) >= RFLOAT_VALUE(y
) ? Qtrue
: Qfalse
;
2576 return rb_num_coerce_relop(x
, y
, rb_intern(">="));
2582 * fix < other => true or false
2584 * Returns <code>true</code> if the value of <code>fix</code> is
2585 * less than that of <code>other</code>.
2589 fix_lt(VALUE x
, VALUE y
)
2592 if (FIX2LONG(x
) < FIX2LONG(y
)) return Qtrue
;
2597 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x
)), y
)) < 0 ? Qtrue
: Qfalse
;
2599 return (double)FIX2LONG(x
) < RFLOAT_VALUE(y
) ? Qtrue
: Qfalse
;
2601 return rb_num_coerce_relop(x
, y
, '<');
2607 * fix <= other => true or false
2609 * Returns <code>true</code> if the value of <code>fix</code> is
2610 * less than or equal to that of <code>other</code>.
2614 fix_le(VALUE x
, VALUE y
)
2617 if (FIX2LONG(x
) <= FIX2LONG(y
)) return Qtrue
;
2622 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x
)), y
)) <= 0 ? Qtrue
: Qfalse
;
2624 return (double)FIX2LONG(x
) <= RFLOAT_VALUE(y
) ? Qtrue
: Qfalse
;
2626 return rb_num_coerce_relop(x
, y
, rb_intern("<="));
2634 * One's complement: returns a number where each bit is flipped.
2640 long val
= FIX2LONG(num
);
2643 return LONG2NUM(val
);
2649 while (!FIXNUM_P(x
) && TYPE(x
) != T_BIGNUM
) {
2657 * fix & other => integer
2663 fix_and(VALUE x
, VALUE y
)
2667 if (!FIXNUM_P(y
= fix_coerce(y
))) {
2668 return rb_big_and(y
, x
);
2670 val
= FIX2LONG(x
) & FIX2LONG(y
);
2671 return LONG2NUM(val
);
2676 * fix | other => integer
2682 fix_or(VALUE x
, VALUE y
)
2686 if (!FIXNUM_P(y
= fix_coerce(y
))) {
2687 return rb_big_or(y
, x
);
2689 val
= FIX2LONG(x
) | FIX2LONG(y
);
2690 return LONG2NUM(val
);
2695 * fix ^ other => integer
2697 * Bitwise EXCLUSIVE OR.
2701 fix_xor(VALUE x
, VALUE y
)
2705 if (!FIXNUM_P(y
= fix_coerce(y
))) {
2706 return rb_big_xor(y
, x
);
2708 val
= FIX2LONG(x
) ^ FIX2LONG(y
);
2709 return LONG2NUM(val
);
2712 static VALUE
fix_lshift(long, unsigned long);
2713 static VALUE
fix_rshift(long, unsigned long);
2717 * fix << count => integer
2719 * Shifts _fix_ left _count_ positions (right if _count_ is negative).
2723 rb_fix_lshift(VALUE x
, VALUE y
)
2729 return rb_big_lshift(rb_int2big(val
), y
);
2730 width
= FIX2LONG(y
);
2732 return fix_rshift(val
, (unsigned long)-width
);
2733 return fix_lshift(val
, width
);
2737 fix_lshift(long val
, unsigned long width
)
2739 if (width
> (SIZEOF_LONG
*CHAR_BIT
-1)
2740 || ((unsigned long)val
)>>(SIZEOF_LONG
*CHAR_BIT
-1-width
) > 0) {
2741 return rb_big_lshift(rb_int2big(val
), ULONG2NUM(width
));
2744 return LONG2NUM(val
);
2749 * fix >> count => integer
2751 * Shifts _fix_ right _count_ positions (left if _count_ is negative).
2755 rb_fix_rshift(VALUE x
, VALUE y
)
2761 return rb_big_rshift(rb_int2big(val
), y
);
2763 if (i
== 0) return x
;
2765 return fix_lshift(val
, (unsigned long)-i
);
2766 return fix_rshift(val
, i
);
2770 fix_rshift(long val
, unsigned long i
)
2772 if (i
>= sizeof(long)*CHAR_BIT
-1) {
2773 if (val
< 0) return INT2FIX(-1);
2776 val
= RSHIFT(val
, i
);
2777 return LONG2FIX(val
);
2784 * Bit Reference---Returns the <em>n</em>th bit in the binary
2785 * representation of <i>fix</i>, where <i>fix</i>[0] is the least
2788 * a = 0b11001100101010
2789 * 30.downto(0) do |n| print a[n] end
2791 * <em>produces:</em>
2793 * 0000000000000000011001100101010
2797 fix_aref(VALUE fix
, VALUE idx
)
2799 long val
= FIX2LONG(fix
);
2802 if (!FIXNUM_P(idx
= fix_coerce(idx
))) {
2803 idx
= rb_big_norm(idx
);
2804 if (!FIXNUM_P(idx
)) {
2805 if (!RBIGNUM_SIGN(idx
) || val
>= 0)
2812 if (i
< 0) return INT2FIX(0);
2813 if (SIZEOF_LONG
*CHAR_BIT
-1 < i
) {
2814 if (val
< 0) return INT2FIX(1);
2826 * Converts <i>fix</i> to a <code>Float</code>.
2835 val
= (double)FIX2LONG(num
);
2837 return DOUBLE2NUM(val
);
2842 * fix.abs -> aFixnum
2844 * Returns the absolute value of <i>fix</i>.
2846 * -12345.abs #=> 12345
2847 * 12345.abs #=> 12345
2854 long i
= FIX2LONG(fix
);
2863 * fix.id2name -> string or nil
2865 * Returns the name of the object whose symbol id is <i>fix</i>. If
2866 * there is no symbol in the symbol table with this value, returns
2867 * <code>nil</code>. <code>id2name</code> has nothing to do with the
2868 * <code>Object.id</code> method. See also <code>Fixnum#to_sym</code>,
2869 * <code>String#intern</code>, and class <code>Symbol</code>.
2871 * symbol = :@inst_var #=> :@inst_var
2872 * id = symbol.to_i #=> 9818
2873 * id.id2name #=> "@inst_var"
2877 fix_id2name(VALUE fix
)
2879 VALUE name
= rb_id2str(FIX2UINT(fix
));
2880 if (name
) return rb_str_dup(name
);
2887 * fix.to_sym -> aSymbol
2889 * Returns the symbol whose integer value is <i>fix</i>. See also
2890 * <code>Fixnum#id2name</code>.
2893 * fred.id2name #=> "fred"
2894 * fred.to_sym #=> :fred
2898 fix_to_sym(VALUE fix
)
2900 ID id
= FIX2UINT(fix
);
2902 if (rb_id2name(id
)) {
2911 * fix.size -> fixnum
2913 * Returns the number of <em>bytes</em> in the machine representation
2914 * of a <code>Fixnum</code>.
2918 * 2147483647.size #=> 4
2924 return INT2FIX(sizeof(long));
2929 * int.upto(limit) {|i| block } => int
2931 * Iterates <em>block</em>, passing in integer values from <i>int</i>
2932 * up to and including <i>limit</i>.
2934 * 5.upto(10) { |i| print i, " " }
2936 * <em>produces:</em>
2942 int_upto(VALUE from
, VALUE to
)
2944 RETURN_ENUMERATOR(from
, 1, &to
);
2945 if (FIXNUM_P(from
) && FIXNUM_P(to
)) {
2949 for (i
= FIX2LONG(from
); i
<= end
; i
++) {
2950 rb_yield(LONG2FIX(i
));
2956 while (!(c
= rb_funcall(i
, '>', 1, to
))) {
2958 i
= rb_funcall(i
, '+', 1, INT2FIX(1));
2960 if (NIL_P(c
)) rb_cmperr(i
, to
);
2967 * int.downto(limit) {|i| block } => int
2969 * Iterates <em>block</em>, passing decreasing values from <i>int</i>
2970 * down to and including <i>limit</i>.
2972 * 5.downto(1) { |n| print n, ".. " }
2973 * print " Liftoff!\n"
2975 * <em>produces:</em>
2977 * 5.. 4.. 3.. 2.. 1.. Liftoff!
2981 int_downto(VALUE from
, VALUE to
)
2983 RETURN_ENUMERATOR(from
, 1, &to
);
2984 if (FIXNUM_P(from
) && FIXNUM_P(to
)) {
2988 for (i
=FIX2LONG(from
); i
>= end
; i
--) {
2989 rb_yield(LONG2FIX(i
));
2995 while (!(c
= rb_funcall(i
, '<', 1, to
))) {
2997 i
= rb_funcall(i
, '-', 1, INT2FIX(1));
2999 if (NIL_P(c
)) rb_cmperr(i
, to
);
3006 * int.times {|i| block } => int
3008 * Iterates block <i>int</i> times, passing in values from zero to
3015 * <em>produces:</em>
3021 int_dotimes(VALUE num
)
3023 RETURN_ENUMERATOR(num
, 0, 0);
3025 if (FIXNUM_P(num
)) {
3028 end
= FIX2LONG(num
);
3029 for (i
=0; i
<end
; i
++) {
3030 rb_yield(LONG2FIX(i
));
3034 VALUE i
= INT2FIX(0);
3037 if (!RTEST(rb_funcall(i
, '<', 1, num
))) break;
3039 i
= rb_funcall(i
, '+', 1, INT2FIX(1));
3046 int_round(int argc
, VALUE
* argv
, VALUE num
)
3051 if (argc
== 0) return num
;
3052 rb_scan_args(argc
, argv
, "1", &n
);
3053 ndigits
= NUM2INT(n
);
3055 return rb_Float(num
);
3062 rb_raise(rb_eArgError
, "ndigits out of range");
3064 f
= int_pow(10, ndigits
);
3065 if (FIXNUM_P(num
) && FIXNUM_P(f
)) {
3066 SIGNED_VALUE x
= FIX2LONG(num
), y
= FIX2LONG(f
);
3069 x
= (x
+ y
/ 2) / y
* y
;
3073 h
= rb_funcall(f
, '/', 1, INT2FIX(2));
3074 r
= rb_funcall(num
, '%', 1, f
);
3075 n
= rb_funcall(num
, '-', 1, r
);
3076 if (!RTEST(rb_funcall(r
, '<', 1, h
))) {
3077 n
= rb_funcall(n
, '+', 1, f
);
3084 * fix.zero? => true or false
3086 * Returns <code>true</code> if <i>fix</i> is zero.
3091 fix_zero_p(VALUE num
)
3093 if (FIX2LONG(num
) == 0) {
3101 * fix.odd? -> true or false
3103 * Returns <code>true</code> if <i>fix</i> is an odd number.
3107 fix_odd_p(VALUE num
)
3117 * fix.even? -> true or false
3119 * Returns <code>true</code> if <i>fix</i> is an even number.
3123 fix_even_p(VALUE num
)
3134 #if defined(__FreeBSD__) && __FreeBSD__ < 4
3135 /* allow divide by zero -- Inf */
3136 fpsetmask(fpgetmask() & ~(FP_X_DZ
|FP_X_INV
|FP_X_OFL
));
3137 #elif defined(_UNICOSMP)
3138 /* Turn off floating point exceptions for divide by zero, etc. */
3140 #elif defined(__BORLANDC__)
3141 /* Turn off floating point exceptions for overflow, etc. */
3142 _control87(MCW_EM
, MCW_EM
);
3144 id_coerce
= rb_intern("coerce");
3145 id_to_i
= rb_intern("to_i");
3146 id_eq
= rb_intern("==");
3148 rb_eZeroDivError
= rb_define_class("ZeroDivisionError", rb_eStandardError
);
3149 rb_eFloatDomainError
= rb_define_class("FloatDomainError", rb_eRangeError
);
3150 rb_cNumeric
= rb_define_class("Numeric", rb_cObject
);
3152 rb_define_method(rb_cNumeric
, "singleton_method_added", num_sadded
, 1);
3153 rb_include_module(rb_cNumeric
, rb_mComparable
);
3154 rb_define_method(rb_cNumeric
, "initialize_copy", num_init_copy
, 1);
3155 rb_define_method(rb_cNumeric
, "coerce", num_coerce
, 1);
3157 rb_define_method(rb_cNumeric
, "+@", num_uplus
, 0);
3158 rb_define_method(rb_cNumeric
, "-@", num_uminus
, 0);
3159 rb_define_method(rb_cNumeric
, "<=>", num_cmp
, 1);
3160 rb_define_method(rb_cNumeric
, "eql?", num_eql
, 1);
3161 rb_define_method(rb_cNumeric
, "quo", num_quo
, 1);
3162 rb_define_method(rb_cNumeric
, "fdiv", num_quo
, 1);
3163 rb_define_method(rb_cNumeric
, "div", num_div
, 1);
3164 rb_define_method(rb_cNumeric
, "divmod", num_divmod
, 1);
3165 rb_define_method(rb_cNumeric
, "modulo", num_modulo
, 1);
3166 rb_define_method(rb_cNumeric
, "remainder", num_remainder
, 1);
3167 rb_define_method(rb_cNumeric
, "abs", num_abs
, 0);
3168 rb_define_method(rb_cNumeric
, "to_int", num_to_int
, 0);
3170 rb_define_method(rb_cNumeric
, "scalar?", num_scalar_p
, 0);
3171 rb_define_method(rb_cNumeric
, "integer?", num_int_p
, 0);
3172 rb_define_method(rb_cNumeric
, "zero?", num_zero_p
, 0);
3173 rb_define_method(rb_cNumeric
, "nonzero?", num_nonzero_p
, 0);
3175 rb_define_method(rb_cNumeric
, "floor", num_floor
, 0);
3176 rb_define_method(rb_cNumeric
, "ceil", num_ceil
, 0);
3177 rb_define_method(rb_cNumeric
, "round", num_round
, -1);
3178 rb_define_method(rb_cNumeric
, "truncate", num_truncate
, 0);
3179 rb_define_method(rb_cNumeric
, "step", num_step
, -1);
3181 rb_define_method(rb_cNumeric
, "numerator", num_numerator
, 0);
3182 rb_define_method(rb_cNumeric
, "denominator", num_denominator
, 0);
3184 rb_cInteger
= rb_define_class("Integer", rb_cNumeric
);
3185 rb_undef_alloc_func(rb_cInteger
);
3186 rb_undef_method(CLASS_OF(rb_cInteger
), "new");
3188 rb_define_method(rb_cInteger
, "integer?", int_int_p
, 0);
3189 rb_define_method(rb_cInteger
, "odd?", int_odd_p
, 0);
3190 rb_define_method(rb_cInteger
, "even?", int_even_p
, 0);
3191 rb_define_method(rb_cInteger
, "upto", int_upto
, 1);
3192 rb_define_method(rb_cInteger
, "downto", int_downto
, 1);
3193 rb_define_method(rb_cInteger
, "times", int_dotimes
, 0);
3194 rb_include_module(rb_cInteger
, rb_mPrecision
);
3195 rb_define_method(rb_cInteger
, "succ", int_succ
, 0);
3196 rb_define_method(rb_cInteger
, "next", int_succ
, 0);
3197 rb_define_method(rb_cInteger
, "pred", int_pred
, 0);
3198 rb_define_method(rb_cInteger
, "chr", int_chr
, -1);
3199 rb_define_method(rb_cInteger
, "to_i", int_to_i
, 0);
3200 rb_define_method(rb_cInteger
, "to_int", int_to_i
, 0);
3201 rb_define_method(rb_cInteger
, "floor", int_to_i
, 0);
3202 rb_define_method(rb_cInteger
, "ceil", int_to_i
, 0);
3203 rb_define_method(rb_cInteger
, "truncate", int_to_i
, 0);
3204 rb_define_method(rb_cInteger
, "round", int_round
, -1);
3206 rb_cFixnum
= rb_define_class("Fixnum", rb_cInteger
);
3207 rb_include_module(rb_cFixnum
, rb_mPrecision
);
3208 rb_define_singleton_method(rb_cFixnum
, "induced_from", rb_fix_induced_from
, 1);
3209 rb_define_singleton_method(rb_cInteger
, "induced_from", rb_int_induced_from
, 1);
3211 rb_define_method(rb_cInteger
, "numerator", int_numerator
, 0);
3212 rb_define_method(rb_cInteger
, "denominator", int_denominator
, 0);
3214 rb_define_method(rb_cFixnum
, "to_s", fix_to_s
, -1);
3216 rb_define_method(rb_cFixnum
, "id2name", fix_id2name
, 0);
3217 rb_define_method(rb_cFixnum
, "to_sym", fix_to_sym
, 0);
3219 rb_define_method(rb_cFixnum
, "-@", fix_uminus
, 0);
3220 rb_define_method(rb_cFixnum
, "+", fix_plus
, 1);
3221 rb_define_method(rb_cFixnum
, "-", fix_minus
, 1);
3222 rb_define_method(rb_cFixnum
, "*", fix_mul
, 1);
3223 rb_define_method(rb_cFixnum
, "/", fix_div
, 1);
3224 rb_define_method(rb_cFixnum
, "div", fix_idiv
, 1);
3225 rb_define_method(rb_cFixnum
, "%", fix_mod
, 1);
3226 rb_define_method(rb_cFixnum
, "modulo", fix_mod
, 1);
3227 rb_define_method(rb_cFixnum
, "divmod", fix_divmod
, 1);
3228 rb_define_method(rb_cFixnum
, "quo", fix_quo
, 1);
3229 rb_define_method(rb_cFixnum
, "fdiv", fix_fdiv
, 1);
3230 rb_define_method(rb_cFixnum
, "**", fix_pow
, 1);
3232 rb_define_method(rb_cFixnum
, "abs", fix_abs
, 0);
3234 rb_define_method(rb_cFixnum
, "==", fix_equal
, 1);
3235 rb_define_method(rb_cFixnum
, "<=>", fix_cmp
, 1);
3236 rb_define_method(rb_cFixnum
, ">", fix_gt
, 1);
3237 rb_define_method(rb_cFixnum
, ">=", fix_ge
, 1);
3238 rb_define_method(rb_cFixnum
, "<", fix_lt
, 1);
3239 rb_define_method(rb_cFixnum
, "<=", fix_le
, 1);
3241 rb_define_method(rb_cFixnum
, "~", fix_rev
, 0);
3242 rb_define_method(rb_cFixnum
, "&", fix_and
, 1);
3243 rb_define_method(rb_cFixnum
, "|", fix_or
, 1);
3244 rb_define_method(rb_cFixnum
, "^", fix_xor
, 1);
3245 rb_define_method(rb_cFixnum
, "[]", fix_aref
, 1);
3247 rb_define_method(rb_cFixnum
, "<<", rb_fix_lshift
, 1);
3248 rb_define_method(rb_cFixnum
, ">>", rb_fix_rshift
, 1);
3250 rb_define_method(rb_cFixnum
, "to_f", fix_to_f
, 0);
3251 rb_define_method(rb_cFixnum
, "size", fix_size
, 0);
3252 rb_define_method(rb_cFixnum
, "zero?", fix_zero_p
, 0);
3253 rb_define_method(rb_cFixnum
, "odd?", fix_odd_p
, 0);
3254 rb_define_method(rb_cFixnum
, "even?", fix_even_p
, 0);
3255 rb_define_method(rb_cFixnum
, "succ", fix_succ
, 0);
3257 rb_cFloat
= rb_define_class("Float", rb_cNumeric
);
3259 rb_undef_alloc_func(rb_cFloat
);
3260 rb_undef_method(CLASS_OF(rb_cFloat
), "new");
3262 rb_define_singleton_method(rb_cFloat
, "induced_from", rb_flo_induced_from
, 1);
3263 rb_include_module(rb_cFloat
, rb_mPrecision
);
3265 rb_define_const(rb_cFloat
, "ROUNDS", INT2FIX(FLT_ROUNDS
));
3266 rb_define_const(rb_cFloat
, "RADIX", INT2FIX(FLT_RADIX
));
3267 rb_define_const(rb_cFloat
, "MANT_DIG", INT2FIX(DBL_MANT_DIG
));
3268 rb_define_const(rb_cFloat
, "DIG", INT2FIX(DBL_DIG
));
3269 rb_define_const(rb_cFloat
, "MIN_EXP", INT2FIX(DBL_MIN_EXP
));
3270 rb_define_const(rb_cFloat
, "MAX_EXP", INT2FIX(DBL_MAX_EXP
));
3271 rb_define_const(rb_cFloat
, "MIN_10_EXP", INT2FIX(DBL_MIN_10_EXP
));
3272 rb_define_const(rb_cFloat
, "MAX_10_EXP", INT2FIX(DBL_MAX_10_EXP
));
3273 rb_define_const(rb_cFloat
, "MIN", DOUBLE2NUM(DBL_MIN
));
3274 rb_define_const(rb_cFloat
, "MAX", DOUBLE2NUM(DBL_MAX
));
3275 rb_define_const(rb_cFloat
, "EPSILON", DOUBLE2NUM(DBL_EPSILON
));
3277 rb_define_method(rb_cFloat
, "to_s", flo_to_s
, 0);
3278 rb_define_method(rb_cFloat
, "coerce", flo_coerce
, 1);
3279 rb_define_method(rb_cFloat
, "-@", flo_uminus
, 0);
3280 rb_define_method(rb_cFloat
, "+", flo_plus
, 1);
3281 rb_define_method(rb_cFloat
, "-", flo_minus
, 1);
3282 rb_define_method(rb_cFloat
, "*", flo_mul
, 1);
3283 rb_define_method(rb_cFloat
, "/", flo_div
, 1);
3284 rb_define_method(rb_cFloat
, "quo", flo_quo
, 1);
3285 rb_define_method(rb_cFloat
, "fdiv", flo_quo
, 1);
3286 rb_define_method(rb_cFloat
, "%", flo_mod
, 1);
3287 rb_define_method(rb_cFloat
, "modulo", flo_mod
, 1);
3288 rb_define_method(rb_cFloat
, "divmod", flo_divmod
, 1);
3289 rb_define_method(rb_cFloat
, "**", flo_pow
, 1);
3290 rb_define_method(rb_cFloat
, "==", flo_eq
, 1);
3291 rb_define_method(rb_cFloat
, "<=>", flo_cmp
, 1);
3292 rb_define_method(rb_cFloat
, ">", flo_gt
, 1);
3293 rb_define_method(rb_cFloat
, ">=", flo_ge
, 1);
3294 rb_define_method(rb_cFloat
, "<", flo_lt
, 1);
3295 rb_define_method(rb_cFloat
, "<=", flo_le
, 1);
3296 rb_define_method(rb_cFloat
, "eql?", flo_eql
, 1);
3297 rb_define_method(rb_cFloat
, "hash", flo_hash
, 0);
3298 rb_define_method(rb_cFloat
, "to_f", flo_to_f
, 0);
3299 rb_define_method(rb_cFloat
, "abs", flo_abs
, 0);
3300 rb_define_method(rb_cFloat
, "zero?", flo_zero_p
, 0);
3302 rb_define_method(rb_cFloat
, "to_i", flo_truncate
, 0);
3303 rb_define_method(rb_cFloat
, "to_int", flo_truncate
, 0);
3304 rb_define_method(rb_cFloat
, "floor", flo_floor
, 0);
3305 rb_define_method(rb_cFloat
, "ceil", flo_ceil
, 0);
3306 rb_define_method(rb_cFloat
, "round", flo_round
, -1);
3307 rb_define_method(rb_cFloat
, "truncate", flo_truncate
, 0);
3309 rb_define_method(rb_cFloat
, "nan?", flo_is_nan_p
, 0);
3310 rb_define_method(rb_cFloat
, "infinite?", flo_is_infinite_p
, 0);
3311 rb_define_method(rb_cFloat
, "finite?", flo_is_finite_p
, 0);