Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

numeric.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 numeric.c -
4
5 $Author: nagachika $
6 created at: Fri Aug 13 18:33:09 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10 **********************************************************************/
11
12 #include "ruby/ruby.h"
13 #include "ruby/encoding.h"
14 #include "ruby/util.h"
15 #include "internal.h"
16 #include "id.h"
17 #include <ctype.h>
18 #include <math.h>
19 #include <stdio.h>
20
21 #if defined(__FreeBSD__) && __FreeBSD__ < 4
22 #include <floatingpoint.h>
23 #endif
24
25 #ifdef HAVE_FLOAT_H
26 #include <float.h>
27 #endif
28
29 #ifdef HAVE_IEEEFP_H
30 #include <ieeefp.h>
31 #endif
32
33 /* use IEEE 64bit values if not defined */
34 #ifndef FLT_RADIX
35 #define FLT_RADIX 2
36 #endif
37 #ifndef FLT_ROUNDS
38 #define FLT_ROUNDS 1
39 #endif
40 #ifndef DBL_MIN
41 #define DBL_MIN 2.2250738585072014e-308
42 #endif
43 #ifndef DBL_MAX
44 #define DBL_MAX 1.7976931348623157e+308
45 #endif
46 #ifndef DBL_MIN_EXP
47 #define DBL_MIN_EXP (-1021)
48 #endif
49 #ifndef DBL_MAX_EXP
50 #define DBL_MAX_EXP 1024
51 #endif
52 #ifndef DBL_MIN_10_EXP
53 #define DBL_MIN_10_EXP (-307)
54 #endif
55 #ifndef DBL_MAX_10_EXP
56 #define DBL_MAX_10_EXP 308
57 #endif
58 #ifndef DBL_DIG
59 #define DBL_DIG 15
60 #endif
61 #ifndef DBL_MANT_DIG
62 #define DBL_MANT_DIG 53
63 #endif
64 #ifndef DBL_EPSILON
65 #define DBL_EPSILON 2.2204460492503131e-16
66 #endif
67
68 #ifdef HAVE_INFINITY
69 #elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
70 const union bytesequence4_or_float rb_infinity = {{0x00, 0x00, 0x80, 0x7f}};
71 #else
72 const union bytesequence4_or_float rb_infinity = {{0x7f, 0x80, 0x00, 0x00}};
73 #endif
74
75 #ifdef HAVE_NAN
76 #elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
77 const union bytesequence4_or_float rb_nan = {{0x00, 0x00, 0xc0, 0x7f}};
78 #else
79 const union bytesequence4_or_float rb_nan = {{0x7f, 0xc0, 0x00, 0x00}};
80 #endif
81
82 #ifndef HAVE_ROUND
83 double
84 round(double x)
85 {
86 double f;
87
88 if (x > 0.0) {
89 f = floor(x);
90 x = f + (x - f >= 0.5);
91 }
92 else if (x < 0.0) {
93 f = ceil(x);
94 x = f - (f - x >= 0.5);
95 }
96 return x;
97 }
98 #endif
99
100 static VALUE fix_uminus(VALUE num);
101 static VALUE fix_mul(VALUE x, VALUE y);
102 static VALUE int_pow(long x, unsigned long y);
103
104 static ID id_coerce, id_to_i, id_eq, id_div;
105
106 VALUE rb_cNumeric;
107 VALUE rb_cFloat;
108 VALUE rb_cInteger;
109 VALUE rb_cFixnum;
110
111 VALUE rb_eZeroDivError;
112 VALUE rb_eFloatDomainError;
113
114 void
115 rb_num_zerodiv(void)
116 {
117 rb_raise(rb_eZeroDivError, "divided by 0");
118 }
119
120 /* experimental API */
121 int
122 rb_num_to_uint(VALUE val, unsigned int *ret)
123 {
124 #define NUMERR_TYPE 1
125 #define NUMERR_NEGATIVE 2
126 #define NUMERR_TOOLARGE 3
127 if (FIXNUM_P(val)) {
128 long v = FIX2LONG(val);
129 #if SIZEOF_INT < SIZEOF_LONG
130 if (v > (long)UINT_MAX) return NUMERR_TOOLARGE;
131 #endif
132 if (v < 0) return NUMERR_NEGATIVE;
133 *ret = (unsigned int)v;
134 return 0;
135 }
136
137 switch (TYPE(val)) {
138 case T_BIGNUM:
139 if (RBIGNUM_NEGATIVE_P(val)) return NUMERR_NEGATIVE;
140 #if SIZEOF_INT < SIZEOF_LONG
141 /* long is 64bit */
142 return NUMERR_TOOLARGE;
143 #else
144 /* long is 32bit */
145 #define DIGSPERLONG (SIZEOF_LONG/SIZEOF_BDIGITS)
146 if (RBIGNUM_LEN(val) > DIGSPERLONG) return NUMERR_TOOLARGE;
147 *ret = (unsigned int)rb_big2ulong((VALUE)val);
148 return 0;
149 #endif
150 }
151 return NUMERR_TYPE;
152 }
153
154 #define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
155
156 static inline int
157 positive_int_p(VALUE num)
158 {
159 const ID mid = '>';
160
161 if (FIXNUM_P(num)) {
162 if (method_basic_p(rb_cFixnum))
163 return (SIGNED_VALUE)num > 0;
164 }
165 else if (RB_TYPE_P(num, T_BIGNUM)) {
166 if (method_basic_p(rb_cBignum))
167 return RBIGNUM_POSITIVE_P(num);
168 }
169 return RTEST(rb_funcall(num, mid, 1, INT2FIX(0)));
170 }
171
172 static inline int
173 negative_int_p(VALUE num)
174 {
175 const ID mid = '<';
176
177 if (FIXNUM_P(num)) {
178 if (method_basic_p(rb_cFixnum))
179 return (SIGNED_VALUE)num < 0;
180 }
181 else if (RB_TYPE_P(num, T_BIGNUM)) {
182 if (method_basic_p(rb_cBignum))
183 return RBIGNUM_NEGATIVE_P(num);
184 }
185 return RTEST(rb_funcall(num, mid, 1, INT2FIX(0)));
186 }
187
188 int
189 rb_num_negative_p(VALUE num)
190 {
191 return negative_int_p(num);
192 }
193
194 /*
195 * call-seq:
196 * num.coerce(numeric) -> array
197 *
198 * If <i>aNumeric</i> is the same type as <i>num</i>, returns an array
199 * containing <i>aNumeric</i> and <i>num</i>. Otherwise, returns an
200 * array with both <i>aNumeric</i> and <i>num</i> represented as
201 * <code>Float</code> objects. This coercion mechanism is used by
202 * Ruby to handle mixed-type numeric operations: it is intended to
203 * find a compatible common type between the two operands of the operator.
204 *
205 * 1.coerce(2.5) #=> [2.5, 1.0]
206 * 1.2.coerce(3) #=> [3.0, 1.2]
207 * 1.coerce(2) #=> [2, 1]
208 */
209
210 static VALUE
211 num_coerce(VALUE x, VALUE y)
212 {
213 if (CLASS_OF(x) == CLASS_OF(y))
214 return rb_assoc_new(y, x);
215 x = rb_Float(x);
216 y = rb_Float(y);
217 return rb_assoc_new(y, x);
218 }
219
220 static VALUE
221 coerce_body(VALUE *x)
222 {
223 return rb_funcall(x[1], id_coerce, 1, x[0]);
224 }
225
226 static VALUE
227 coerce_rescue(VALUE *x)
228 {
229 volatile VALUE v = rb_inspect(x[1]);
230
231 rb_raise(rb_eTypeError, "%s can't be coerced into %s",
232 rb_special_const_p(x[1])?
233 RSTRING_PTR(v):
234 rb_obj_classname(x[1]),
235 rb_obj_classname(x[0]));
236
237 return Qnil; /* dummy */
238 }
239
240 static int
241 do_coerce(VALUE *x, VALUE *y, int err)
242 {
243 VALUE ary;
244 VALUE a[2];
245
246 a[0] = *x; a[1] = *y;
247
248 if (!rb_respond_to(*y, id_coerce)) {
249 if (err) {
250 coerce_rescue(a);
251 }
252 return FALSE;
253 }
254
255 ary = rb_rescue(coerce_body, (VALUE)a, err ? coerce_rescue : 0, (VALUE)a);
256 if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) {
257 if (err) {
258 rb_raise(rb_eTypeError, "coerce must return [x, y]");
259 }
260 return FALSE;
261 }
262
263 *x = RARRAY_PTR(ary)[0];
264 *y = RARRAY_PTR(ary)[1];
265 return TRUE;
266 }
267
268 VALUE
269 rb_num_coerce_bin(VALUE x, VALUE y, ID func)
270 {
271 do_coerce(&x, &y, TRUE);
272 return rb_funcall(x, func, 1, y);
273 }
274
275 VALUE
276 rb_num_coerce_cmp(VALUE x, VALUE y, ID func)
277 {
278 if (do_coerce(&x, &y, FALSE))
279 return rb_funcall(x, func, 1, y);
280 return Qnil;
281 }
282
283 VALUE
284 rb_num_coerce_relop(VALUE x, VALUE y, ID func)
285 {
286 VALUE c, x0 = x, y0 = y;
287
288 if (!do_coerce(&x, &y, FALSE) ||
289 NIL_P(c = rb_funcall(x, func, 1, y))) {
290 rb_cmperr(x0, y0);
291 return Qnil; /* not reached */
292 }
293 return c;
294 }
295
296 /*
297 * Trap attempts to add methods to <code>Numeric</code> objects. Always
298 * raises a <code>TypeError</code>
299 */
300
301 static VALUE
302 num_sadded(VALUE x, VALUE name)
303 {
304 ID mid = rb_to_id(name);
305 /* ruby_frame = ruby_frame->prev; */ /* pop frame for "singleton_method_added" */
306 /* Numerics should be values; singleton_methods should not be added to them */
307 rb_remove_method_id(rb_singleton_class(x), mid);
308 rb_raise(rb_eTypeError,
309 "can't define singleton method \"%s\" for %s",
310 rb_id2name(mid),
311 rb_obj_classname(x));
312
313 UNREACHABLE;
314 }
315
316 /* :nodoc: */
317 static VALUE
318 num_init_copy(VALUE x, VALUE y)
319 {
320 /* Numerics are immutable values, which should not be copied */
321 rb_raise(rb_eTypeError, "can't copy %s", rb_obj_classname(x));
322
323 UNREACHABLE;
324 }
325
326 /*
327 * call-seq:
328 * +num -> num
329 *
330 * Unary Plus---Returns the receiver's value.
331 */
332
333 static VALUE
334 num_uplus(VALUE num)
335 {
336 return num;
337 }
338
339 /*
340 * call-seq:
341 * num.i -> Complex(0,num)
342 *
343 * Returns the corresponding imaginary number.
344 * Not available for complex numbers.
345 */
346
347 static VALUE
348 num_imaginary(VALUE num)
349 {
350 return rb_complex_new(INT2FIX(0), num);
351 }
352
353
354 /*
355 * call-seq:
356 * -num -> numeric
357 *
358 * Unary Minus---Returns the receiver's value, negated.
359 */
360
361 static VALUE
362 num_uminus(VALUE num)
363 {
364 VALUE zero;
365
366 zero = INT2FIX(0);
367 do_coerce(&zero, &num, TRUE);
368
369 return rb_funcall(zero, '-', 1, num);
370 }
371
372 /*
373 * call-seq:
374 * num.quo(numeric) -> real
375 *
376 * Returns most exact division (rational for integers, float for floats).
377 */
378
379 static VALUE
380 num_quo(VALUE x, VALUE y)
381 {
382 return rb_funcall(rb_rational_raw1(x), '/', 1, y);
383 }
384
385
386 /*
387 * call-seq:
388 * num.fdiv(numeric) -> float
389 *
390 * Returns float division.
391 */
392
393 static VALUE
394 num_fdiv(VALUE x, VALUE y)
395 {
396 return rb_funcall(rb_Float(x), '/', 1, y);
397 }
398
399
400 /*
401 * call-seq:
402 * num.div(numeric) -> integer
403 *
404 * Uses <code>/</code> to perform division, then converts the result to
405 * an integer. <code>numeric</code> does not define the <code>/</code>
406 * operator; this is left to subclasses.
407 *
408 * Equivalent to
409 * <i>num</i>.<code>divmod(</code><i>aNumeric</i><code>)[0]</code>.
410 *
411 * See <code>Numeric#divmod</code>.
412 */
413
414 static VALUE
415 num_div(VALUE x, VALUE y)
416 {
417 if (rb_equal(INT2FIX(0), y)) rb_num_zerodiv();
418 return rb_funcall(rb_funcall(x, '/', 1, y), rb_intern("floor"), 0);
419 }
420
421
422 /*
423 * call-seq:
424 * num.modulo(numeric) -> real
425 *
426 * x.modulo(y) means x-y*(x/y).floor
427 *
428 * Equivalent to
429 * <i>num</i>.<code>divmod(</code><i>aNumeric</i><code>)[1]</code>.
430 *
431 * See <code>Numeric#divmod</code>.
432 */
433
434 static VALUE
435 num_modulo(VALUE x, VALUE y)
436 {
437 return rb_funcall(x, '-', 1,
438 rb_funcall(y, '*', 1,
439 rb_funcall(x, rb_intern("div"), 1, y)));
440 }
441
442 /*
443 * call-seq:
444 * num.remainder(numeric) -> real
445 *
446 * x.remainder(y) means x-y*(x/y).truncate
447 *
448 * See <code>Numeric#divmod</code>.
449 */
450
451 static VALUE
452 num_remainder(VALUE x, VALUE y)
453 {
454 VALUE z = rb_funcall(x, '%', 1, y);
455
456 if ((!rb_equal(z, INT2FIX(0))) &&
457 ((negative_int_p(x) &&
458 positive_int_p(y)) ||
459 (positive_int_p(x) &&
460 negative_int_p(y)))) {
461 return rb_funcall(z, '-', 1, y);
462 }
463 return z;
464 }
465
466 /*
467 * call-seq:
468 * num.divmod(numeric) -> array
469 *
470 * Returns an array containing the quotient and modulus obtained by
471 * dividing <i>num</i> by <i>numeric</i>. If <code>q, r =
472 * x.divmod(y)</code>, then
473 *
474 * q = floor(x/y)
475 * x = q*y+r
476 *
477 * The quotient is rounded toward -infinity, as shown in the following table:
478 *
479 * a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b)
480 * ------+-----+---------------+---------+-------------+---------------
481 * 13 | 4 | 3, 1 | 3 | 1 | 1
482 * ------+-----+---------------+---------+-------------+---------------
483 * 13 | -4 | -4, -3 | -4 | -3 | 1
484 * ------+-----+---------------+---------+-------------+---------------
485 * -13 | 4 | -4, 3 | -4 | 3 | -1
486 * ------+-----+---------------+---------+-------------+---------------
487 * -13 | -4 | 3, -1 | 3 | -1 | -1
488 * ------+-----+---------------+---------+-------------+---------------
489 * 11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5
490 * ------+-----+---------------+---------+-------------+---------------
491 * 11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5
492 * ------+-----+---------------+---------+-------------+---------------
493 * -11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5
494 * ------+-----+---------------+---------+-------------+---------------
495 * -11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5
496 *
497 *
498 * Examples
499 *
500 * 11.divmod(3) #=> [3, 2]
501 * 11.divmod(-3) #=> [-4, -1]
502 * 11.divmod(3.5) #=> [3, 0.5]
503 * (-11).divmod(3.5) #=> [-4, 3.0]
504 * (11.5).divmod(3.5) #=> [3, 1.0]
505 */
506
507 static VALUE
508 num_divmod(VALUE x, VALUE y)
509 {
510 return rb_assoc_new(num_div(x, y), num_modulo(x, y));
511 }
512
513 /*
514 * call-seq:
515 * num.real? -> true or false
516 *
517 * Returns <code>true</code> if <i>num</i> is a <code>Real</code>
518 * (i.e. non <code>Complex</code>).
519 */
520
521 static VALUE
522 num_real_p(VALUE num)
523 {
524 return Qtrue;
525 }
526
527 /*
528 * call-seq:
529 * num.integer? -> true or false
530 *
531 * Returns +true+ if +num+ is an Integer (including Fixnum and Bignum).
532 *
533 * (1.0).integer? #=> false
534 * (1).integer? #=> true
535 */
536
537 static VALUE
538 num_int_p(VALUE num)
539 {
540 return Qfalse;
541 }
542
543 /*
544 * call-seq:
545 * num.abs -> numeric
546 * num.magnitude -> numeric
547 *
548 * Returns the absolute value of <i>num</i>.
549 *
550 * 12.abs #=> 12
551 * (-34.56).abs #=> 34.56
552 * -34.56.abs #=> 34.56
553 */
554
555 static VALUE
556 num_abs(VALUE num)
557 {
558 if (negative_int_p(num)) {
559 return rb_funcall(num, rb_intern("-@"), 0);
560 }
561 return num;
562 }
563
564
565 /*
566 * call-seq:
567 * num.zero? -> true or false
568 *
569 * Returns <code>true</code> if <i>num</i> has a zero value.
570 */
571
572 static VALUE
573 num_zero_p(VALUE num)
574 {
575 if (rb_equal(num, INT2FIX(0))) {
576 return Qtrue;
577 }
578 return Qfalse;
579 }
580
581
582 /*
583 * call-seq:
584 * num.nonzero? -> self or nil
585 *
586 * Returns +self+ if <i>num</i> is not zero, <code>nil</code>
587 * otherwise. This behavior is useful when chaining comparisons:
588 *
589 * a = %w( z Bb bB bb BB a aA Aa AA A )
590 * b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
591 * b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
592 */
593
594 static VALUE
595 num_nonzero_p(VALUE num)
596 {
597 if (RTEST(rb_funcall(num, rb_intern("zero?"), 0, 0))) {
598 return Qnil;
599 }
600 return num;
601 }
602
603 /*
604 * call-seq:
605 * num.to_int -> integer
606 *
607 * Invokes the child class's +to_i+ method to convert +num+ to an integer.
608 *
609 * 1.0.class => Float
610 * 1.0.to_int.class => Fixnum
611 * 1.0.to_i.class => Fixnum
612 */
613
614 static VALUE
615 num_to_int(VALUE num)
616 {
617 return rb_funcall(num, id_to_i, 0, 0);
618 }
619
620
621 /********************************************************************
622 *
623 * Document-class: Float
624 *
625 * <code>Float</code> objects represent inexact real numbers using
626 * the native architecture's double-precision floating point
627 * representation.
628 *
629 * Floating point has a different arithmetic and is a inexact number.
630 * So you should know its esoteric system. see following:
631 *
632 * - http://docs.sun.com/source/806-3568/ncg_goldberg.html
633 * - http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#wiki-floats_imprecise
634 * - http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
635 */
636
637 VALUE
638 rb_float_new_in_heap(double d)
639 {
640 NEWOBJ_OF(flt, struct RFloat, rb_cFloat, T_FLOAT);
641
642 flt->float_value = d;
643 OBJ_FREEZE(flt);
644 return (VALUE)flt;
645 }
646
647 /*
648 * call-seq:
649 * flt.to_s -> string
650 *
651 * Returns a string containing a representation of self. As well as a
652 * fixed or exponential form of the number, the call may return
653 * ``<code>NaN</code>'', ``<code>Infinity</code>'', and
654 * ``<code>-Infinity</code>''.
655 */
656
657 static VALUE
658 flo_to_s(VALUE flt)
659 {
660 char *ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve);
661 enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
662 enum {float_dig = DBL_DIG+1};
663 char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
664 double value = RFLOAT_VALUE(flt);
665 VALUE s;
666 char *p, *e;
667 int sign, decpt, digs;
668
669 if (isinf(value))
670 return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
671 else if (isnan(value))
672 return rb_usascii_str_new2("NaN");
673
674 p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
675 s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0);
676 if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1;
677 memcpy(buf, p, digs);
678 xfree(p);
679 if (decpt > 0) {
680 if (decpt < digs) {
681 memmove(buf + decpt + 1, buf + decpt, digs - decpt);
682 buf[decpt] = '.';
683 rb_str_cat(s, buf, digs + 1);
684 }
685 else if (decpt <= DBL_DIG) {
686 long len;
687 char *ptr;
688 rb_str_cat(s, buf, digs);
689 rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2);
690 ptr = RSTRING_PTR(s) + len;
691 if (decpt > digs) {
692 memset(ptr, '0', decpt - digs);
693 ptr += decpt - digs;
694 }
695 memcpy(ptr, ".0", 2);
696 }
697 else {
698 goto exp;
699 }
700 }
701 else if (decpt > -4) {
702 long len;
703 char *ptr;
704 rb_str_cat(s, "0.", 2);
705 rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs);
706 ptr = RSTRING_PTR(s);
707 memset(ptr += len, '0', -decpt);
708 memcpy(ptr -= decpt, buf, digs);
709 }
710 else {
711 exp:
712 if (digs > 1) {
713 memmove(buf + 2, buf + 1, digs - 1);
714 }
715 else {
716 buf[2] = '0';
717 digs++;
718 }
719 buf[1] = '.';
720 rb_str_cat(s, buf, digs + 1);
721 rb_str_catf(s, "e%+03d", decpt - 1);
722 }
723 return s;
724 }
725
726 /*
727 * call-seq:
728 * flt.coerce(numeric) -> array
729 *
730 * Returns an array with both <i>aNumeric</i> and <i>flt</i> represented
731 * as <code>Float</code> objects.
732 * This is achieved by converting <i>aNumeric</i> to a <code>Float</code>.
733 *
734 * 1.2.coerce(3) #=> [3.0, 1.2]
735 * 2.5.coerce(1.1) #=> [1.1, 2.5]
736 */
737
738 static VALUE
739 flo_coerce(VALUE x, VALUE y)
740 {
741 return rb_assoc_new(rb_Float(y), x);
742 }
743
744 /*
745 * call-seq:
746 * -float -> float
747 *
748 * Returns float, negated.
749 */
750
751 static VALUE
752 flo_uminus(VALUE flt)
753 {
754 return DBL2NUM(-RFLOAT_VALUE(flt));
755 }
756
757 /*
758 * call-seq:
759 * float + other -> float
760 *
761 * Returns a new float which is the sum of <code>float</code>
762 * and <code>other</code>.
763 */
764
765 static VALUE
766 flo_plus(VALUE x, VALUE y)
767 {
768 switch (TYPE(y)) {
769 case T_FIXNUM:
770 return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
771 case T_BIGNUM:
772 return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
773 case T_FLOAT:
774 return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
775 default:
776 return rb_num_coerce_bin(x, y, '+');
777 }
778 }
779
780 /*
781 * call-seq:
782 * float - other -> float
783 *
784 * Returns a new float which is the difference of <code>float</code>
785 * and <code>other</code>.
786 */
787
788 static VALUE
789 flo_minus(VALUE x, VALUE y)
790 {
791 switch (TYPE(y)) {
792 case T_FIXNUM:
793 return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
794 case T_BIGNUM:
795 return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
796 case T_FLOAT:
797 return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
798 default:
799 return rb_num_coerce_bin(x, y, '-');
800 }
801 }
802
803 /*
804 * call-seq:
805 * float * other -> float
806 *
807 * Returns a new float which is the product of <code>float</code>
808 * and <code>other</code>.
809 */
810
811 static VALUE
812 flo_mul(VALUE x, VALUE y)
813 {
814 switch (TYPE(y)) {
815 case T_FIXNUM:
816 return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
817 case T_BIGNUM:
818 return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
819 case T_FLOAT:
820 return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
821 default:
822 return rb_num_coerce_bin(x, y, '*');
823 }
824 }
825
826 /*
827 * call-seq:
828 * float / other -> float
829 *
830 * Returns a new float which is the result of dividing
831 * <code>float</code> by <code>other</code>.
832 */
833
834 static VALUE
835 flo_div(VALUE x, VALUE y)
836 {
837 long f_y;
838 double d;
839
840 switch (TYPE(y)) {
841 case T_FIXNUM:
842 f_y = FIX2LONG(y);
843 return DBL2NUM(RFLOAT_VALUE(x) / (double)f_y);
844 case T_BIGNUM:
845 d = rb_big2dbl(y);
846 return DBL2NUM(RFLOAT_VALUE(x) / d);
847 case T_FLOAT:
848 return DBL2NUM(RFLOAT_VALUE(x) / RFLOAT_VALUE(y));
849 default:
850 return rb_num_coerce_bin(x, y, '/');
851 }
852 }
853
854 /*
855 * call-seq:
856 * float.quo(numeric) -> float
857 *
858 * Returns float / numeric.
859 */
860
861 static VALUE
862 flo_quo(VALUE x, VALUE y)
863 {
864 return rb_funcall(x, '/', 1, y);
865 }
866
867 static void
868 flodivmod(double x, double y, double *divp, double *modp)
869 {
870 double div, mod;
871
872 if (y == 0.0) rb_num_zerodiv();
873 if ((x == 0.0) || (isinf(y) && !isinf(x)))
874 mod = x;
875 else {
876 #ifdef HAVE_FMOD
877 mod = fmod(x, y);
878 #else
879 double z;
880
881 modf(x/y, &z);
882 mod = x - z * y;
883 #endif
884 }
885 if (isinf(x) && !isinf(y) && !isnan(y))
886 div = x;
887 else
888 div = (x - mod) / y;
889 if (y*mod < 0) {
890 mod += y;
891 div -= 1.0;
892 }
893 if (modp) *modp = mod;
894 if (divp) *divp = div;
895 }
896
897 /*
898 * Returns the modulo of division of x by y.
899 * An error will be raised if y == 0.
900 */
901
902 double
903 ruby_float_mod(double x, double y)
904 {
905 double mod;
906 flodivmod(x, y, 0, &mod);
907 return mod;
908 }
909
910
911 /*
912 * call-seq:
913 * float % other -> float
914 * float.modulo(other) -> float
915 *
916 * Return the modulo after division of +float+ by +other+.
917 *
918 * 6543.21.modulo(137) #=> 104.21
919 * 6543.21.modulo(137.24) #=> 92.9299999999996
920 */
921
922 static VALUE
923 flo_mod(VALUE x, VALUE y)
924 {
925 double fy;
926
927 switch (TYPE(y)) {
928 case T_FIXNUM:
929 fy = (double)FIX2LONG(y);
930 break;
931 case T_BIGNUM:
932 fy = rb_big2dbl(y);
933 break;
934 case T_FLOAT:
935 fy = RFLOAT_VALUE(y);
936 break;
937 default:
938 return rb_num_coerce_bin(x, y, '%');
939 }
940 return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
941 }
942
943 static VALUE
944 dbl2ival(double d)
945 {
946 d = round(d);
947 if (FIXABLE(d)) {
948 return LONG2FIX((long)d);
949 }
950 return rb_dbl2big(d);
951 }
952
953 /*
954 * call-seq:
955 * float.divmod(numeric) -> array
956 *
957 * See Numeric#divmod.
958 *
959 * 42.0.divmod 6 #=> [7, 0.0]
960 * 42.0.divmod 5 #=> [8, 2.0]
961 */
962
963 static VALUE
964 flo_divmod(VALUE x, VALUE y)
965 {
966 double fy, div, mod;
967 volatile VALUE a, b;
968
969 switch (TYPE(y)) {
970 case T_FIXNUM:
971 fy = (double)FIX2LONG(y);
972 break;
973 case T_BIGNUM:
974 fy = rb_big2dbl(y);
975 break;
976 case T_FLOAT:
977 fy = RFLOAT_VALUE(y);
978 break;
979 default:
980 return rb_num_coerce_bin(x, y, rb_intern("divmod"));
981 }
982 flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
983 a = dbl2ival(div);
984 b = DBL2NUM(mod);
985 return rb_assoc_new(a, b);
986 }
987
988 /*
989 * call-seq:
990 *
991 * flt ** other -> float
992 *
993 * Raises <code>float</code> the <code>other</code> power.
994 *
995 * 2.0**3 #=> 8.0
996 */
997
998 static VALUE
999 flo_pow(VALUE x, VALUE y)
1000 {
1001 switch (TYPE(y)) {
1002 case T_FIXNUM:
1003 return DBL2NUM(pow(RFLOAT_VALUE(x), (double)FIX2LONG(y)));
1004 case T_BIGNUM:
1005 return DBL2NUM(pow(RFLOAT_VALUE(x), rb_big2dbl(y)));
1006 case T_FLOAT:
1007 {
1008 double dx = RFLOAT_VALUE(x);
1009 double dy = RFLOAT_VALUE(y);
1010 if (dx < 0 && dy != round(dy))
1011 return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
1012 return DBL2NUM(pow(dx, dy));
1013 }
1014 default:
1015 return rb_num_coerce_bin(x, y, rb_intern("**"));
1016 }
1017 }
1018
1019 /*
1020 * call-seq:
1021 * num.eql?(numeric) -> true or false
1022 *
1023 * Returns <code>true</code> if <i>num</i> and <i>numeric</i> are the
1024 * same type and have equal values.
1025 *
1026 * 1 == 1.0 #=> true
1027 * 1.eql?(1.0) #=> false
1028 * (1.0).eql?(1.0) #=> true
1029 */
1030
1031 static VALUE
1032 num_eql(VALUE x, VALUE y)
1033 {
1034 if (TYPE(x) != TYPE(y)) return Qfalse;
1035
1036 return rb_equal(x, y);
1037 }
1038
1039 /*
1040 * call-seq:
1041 * number <=> other -> 0 or nil
1042 *
1043 * Returns zero if +number+ equals +other+, otherwise +nil+ is returned if the
1044 * two values are incomparable.
1045 */
1046
1047 static VALUE
1048 num_cmp(VALUE x, VALUE y)
1049 {
1050 if (x == y) return INT2FIX(0);
1051 return Qnil;
1052 }
1053
1054 static VALUE
1055 num_equal(VALUE x, VALUE y)
1056 {
1057 if (x == y) return Qtrue;
1058 return rb_funcall(y, id_eq, 1, x);
1059 }
1060
1061 /*
1062 * call-seq:
1063 * flt == obj -> true or false
1064 *
1065 * Returns <code>true</code> only if <i>obj</i> has the same value
1066 * as <i>flt</i>. Contrast this with <code>Float#eql?</code>, which
1067 * requires <i>obj</i> to be a <code>Float</code>.
1068 * The result of <code>NaN == NaN</code> is undefined, so the
1069 * implementation-dependent value is returned.
1070 *
1071 * 1.0 == 1 #=> true
1072 *
1073 */
1074
1075 static VALUE
1076 flo_eq(VALUE x, VALUE y)
1077 {
1078 volatile double a, b;
1079
1080 switch (TYPE(y)) {
1081 case T_FIXNUM:
1082 case T_BIGNUM:
1083 return rb_integer_float_eq(y, x);
1084 case T_FLOAT:
1085 b = RFLOAT_VALUE(y);
1086 #if defined(_MSC_VER) && _MSC_VER < 1300
1087 if (isnan(b)) return Qfalse;
1088 #endif
1089 break;
1090 default:
1091 return num_equal(x, y);
1092 }
1093 a = RFLOAT_VALUE(x);
1094 #if defined(_MSC_VER) && _MSC_VER < 1300
1095 if (isnan(a)) return Qfalse;
1096 #endif
1097 return (a == b)?Qtrue:Qfalse;
1098 }
1099
1100 /*
1101 * call-seq:
1102 * flt.hash -> integer
1103 *
1104 * Returns a hash code for this float.
1105 */
1106
1107 static VALUE
1108 flo_hash(VALUE num)
1109 {
1110 double d;
1111 st_index_t hash;
1112
1113 d = RFLOAT_VALUE(num);
1114 /* normalize -0.0 to 0.0 */
1115 if (d == 0.0) d = 0.0;
1116 hash = rb_memhash(&d, sizeof(d));
1117 return LONG2FIX(hash);
1118 }
1119
1120 VALUE
1121 rb_dbl_cmp(double a, double b)
1122 {
1123 if (isnan(a) || isnan(b)) return Qnil;
1124 if (a == b) return INT2FIX(0);
1125 if (a > b) return INT2FIX(1);
1126 if (a < b) return INT2FIX(-1);
1127 return Qnil;
1128 }
1129
1130 /*
1131 * call-seq:
1132 * float <=> real -> -1, 0, +1 or nil
1133 *
1134 * Returns -1, 0, +1 or nil depending on whether +float+ is less than, equal
1135 * to, or greater than +real+. This is the basis for the tests in Comparable.
1136 *
1137 * The result of <code>NaN <=> NaN</code> is undefined, so the
1138 * implementation-dependent value is returned.
1139 *
1140 * +nil+ is returned if the two values are incomparable.
1141 */
1142
1143 static VALUE
1144 flo_cmp(VALUE x, VALUE y)
1145 {
1146 double a, b;
1147 VALUE i;
1148
1149 a = RFLOAT_VALUE(x);
1150 if (isnan(a)) return Qnil;
1151 switch (TYPE(y)) {
1152 case T_FIXNUM:
1153 case T_BIGNUM:
1154 {
1155 VALUE rel = rb_integer_float_cmp(y, x);
1156 if (FIXNUM_P(rel))
1157 return INT2FIX(-FIX2INT(rel));
1158 return rel;
1159 }
1160
1161 case T_FLOAT:
1162 b = RFLOAT_VALUE(y);
1163 break;
1164
1165 default:
1166 if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) {
1167 if (RTEST(i)) {
1168 int j = rb_cmpint(i, x, y);
1169 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1170 return INT2FIX(j);
1171 }
1172 if (a > 0.0) return INT2FIX(1);
1173 return INT2FIX(-1);
1174 }
1175 return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
1176 }
1177 return rb_dbl_cmp(a, b);
1178 }
1179
1180 /*
1181 * call-seq:
1182 * flt > real -> true or false
1183 *
1184 * <code>true</code> if <code>flt</code> is greater than <code>real</code>.
1185 * The result of <code>NaN > NaN</code> is undefined, so the
1186 * implementation-dependent value is returned.
1187 */
1188
1189 static VALUE
1190 flo_gt(VALUE x, VALUE y)
1191 {
1192 double a, b;
1193
1194 a = RFLOAT_VALUE(x);
1195 switch (TYPE(y)) {
1196 case T_FIXNUM:
1197 case T_BIGNUM:
1198 {
1199 VALUE rel = rb_integer_float_cmp(y, x);
1200 if (FIXNUM_P(rel))
1201 return -FIX2INT(rel) > 0 ? Qtrue : Qfalse;
1202 return Qfalse;
1203 }
1204
1205 case T_FLOAT:
1206 b = RFLOAT_VALUE(y);
1207 #if defined(_MSC_VER) && _MSC_VER < 1300
1208 if (isnan(b)) return Qfalse;
1209 #endif
1210 break;
1211
1212 default:
1213 return rb_num_coerce_relop(x, y, '>');
1214 }
1215 #if defined(_MSC_VER) && _MSC_VER < 1300
1216 if (isnan(a)) return Qfalse;
1217 #endif
1218 return (a > b)?Qtrue:Qfalse;
1219 }
1220
1221 /*
1222 * call-seq:
1223 * flt >= real -> true or false
1224 *
1225 * <code>true</code> if <code>flt</code> is greater than
1226 * or equal to <code>real</code>.
1227 * The result of <code>NaN >= NaN</code> is undefined, so the
1228 * implementation-dependent value is returned.
1229 */
1230
1231 static VALUE
1232 flo_ge(VALUE x, VALUE y)
1233 {
1234 double a, b;
1235
1236 a = RFLOAT_VALUE(x);
1237 switch (TYPE(y)) {
1238 case T_FIXNUM:
1239 case T_BIGNUM:
1240 {
1241 VALUE rel = rb_integer_float_cmp(y, x);
1242 if (FIXNUM_P(rel))
1243 return -FIX2INT(rel) >= 0 ? Qtrue : Qfalse;
1244 return Qfalse;
1245 }
1246
1247 case T_FLOAT:
1248 b = RFLOAT_VALUE(y);
1249 #if defined(_MSC_VER) && _MSC_VER < 1300
1250 if (isnan(b)) return Qfalse;
1251 #endif
1252 break;
1253
1254 default:
1255 return rb_num_coerce_relop(x, y, rb_intern(">="));
1256 }
1257 #if defined(_MSC_VER) && _MSC_VER < 1300
1258 if (isnan(a)) return Qfalse;
1259 #endif
1260 return (a >= b)?Qtrue:Qfalse;
1261 }
1262
1263 /*
1264 * call-seq:
1265 * flt < real -> true or false
1266 *
1267 * <code>true</code> if <code>flt</code> is less than <code>real</code>.
1268 * The result of <code>NaN < NaN</code> is undefined, so the
1269 * implementation-dependent value is returned.
1270 */
1271
1272 static VALUE
1273 flo_lt(VALUE x, VALUE y)
1274 {
1275 double a, b;
1276
1277 a = RFLOAT_VALUE(x);
1278 switch (TYPE(y)) {
1279 case T_FIXNUM:
1280 case T_BIGNUM:
1281 {
1282 VALUE rel = rb_integer_float_cmp(y, x);
1283 if (FIXNUM_P(rel))
1284 return -FIX2INT(rel) < 0 ? Qtrue : Qfalse;
1285 return Qfalse;
1286 }
1287
1288 case T_FLOAT:
1289 b = RFLOAT_VALUE(y);
1290 #if defined(_MSC_VER) && _MSC_VER < 1300
1291 if (isnan(b)) return Qfalse;
1292 #endif
1293 break;
1294
1295 default:
1296 return rb_num_coerce_relop(x, y, '<');
1297 }
1298 #if defined(_MSC_VER) && _MSC_VER < 1300
1299 if (isnan(a)) return Qfalse;
1300 #endif
1301 return (a < b)?Qtrue:Qfalse;
1302 }
1303
1304 /*
1305 * call-seq:
1306 * flt <= real -> true or false
1307 *
1308 * <code>true</code> if <code>flt</code> is less than
1309 * or equal to <code>real</code>.
1310 * The result of <code>NaN <= NaN</code> is undefined, so the
1311 * implementation-dependent value is returned.
1312 */
1313
1314 static VALUE
1315 flo_le(VALUE x, VALUE y)
1316 {
1317 double a, b;
1318
1319 a = RFLOAT_VALUE(x);
1320 switch (TYPE(y)) {
1321 case T_FIXNUM:
1322 case T_BIGNUM:
1323 {
1324 VALUE rel = rb_integer_float_cmp(y, x);
1325 if (FIXNUM_P(rel))
1326 return -FIX2INT(rel) <= 0 ? Qtrue : Qfalse;
1327 return Qfalse;
1328 }
1329
1330 case T_FLOAT:
1331 b = RFLOAT_VALUE(y);
1332 #if defined(_MSC_VER) && _MSC_VER < 1300
1333 if (isnan(b)) return Qfalse;
1334 #endif
1335 break;
1336
1337 default:
1338 return rb_num_coerce_relop(x, y, rb_intern("<="));
1339 }
1340 #if defined(_MSC_VER) && _MSC_VER < 1300
1341 if (isnan(a)) return Qfalse;
1342 #endif
1343 return (a <= b)?Qtrue:Qfalse;
1344 }
1345
1346 /*
1347 * call-seq:
1348 * flt.eql?(obj) -> true or false
1349 *
1350 * Returns <code>true</code> only if <i>obj</i> is a
1351 * <code>Float</code> with the same value as <i>flt</i>. Contrast this
1352 * with <code>Float#==</code>, which performs type conversions.
1353 * The result of <code>NaN.eql?(NaN)</code> is undefined, so the
1354 * implementation-dependent value is returned.
1355 *
1356 * 1.0.eql?(1) #=> false
1357 */
1358
1359 static VALUE
1360 flo_eql(VALUE x, VALUE y)
1361 {
1362 if (RB_TYPE_P(y, T_FLOAT)) {
1363 double a = RFLOAT_VALUE(x);
1364 double b = RFLOAT_VALUE(y);
1365 #if defined(_MSC_VER) && _MSC_VER < 1300
1366 if (isnan(a) || isnan(b)) return Qfalse;
1367 #endif
1368 if (a == b)
1369 return Qtrue;
1370 }
1371 return Qfalse;
1372 }
1373
1374 /*
1375 * call-seq:
1376 * flt.to_f -> self
1377 *
1378 * As <code>flt</code> is already a float, returns +self+.
1379 */
1380
1381 static VALUE
1382 flo_to_f(VALUE num)
1383 {
1384 return num;
1385 }
1386
1387 /*
1388 * call-seq:
1389 * flt.abs -> float
1390 * flt.magnitude -> float
1391 *
1392 * Returns the absolute value of <i>flt</i>.
1393 *
1394 * (-34.56).abs #=> 34.56
1395 * -34.56.abs #=> 34.56
1396 *
1397 */
1398
1399 static VALUE
1400 flo_abs(VALUE flt)
1401 {
1402 double val = fabs(RFLOAT_VALUE(flt));
1403 return DBL2NUM(val);
1404 }
1405
1406 /*
1407 * call-seq:
1408 * flt.zero? -> true or false
1409 *
1410 * Returns <code>true</code> if <i>flt</i> is 0.0.
1411 *
1412 */
1413
1414 static VALUE
1415 flo_zero_p(VALUE num)
1416 {
1417 if (RFLOAT_VALUE(num) == 0.0) {
1418 return Qtrue;
1419 }
1420 return Qfalse;
1421 }
1422
1423 /*
1424 * call-seq:
1425 * flt.nan? -> true or false
1426 *
1427 * Returns <code>true</code> if <i>flt</i> is an invalid IEEE floating
1428 * point number.
1429 *
1430 * a = -1.0 #=> -1.0
1431 * a.nan? #=> false
1432 * a = 0.0/0.0 #=> NaN
1433 * a.nan? #=> true
1434 */
1435
1436 static VALUE
1437 flo_is_nan_p(VALUE num)
1438 {
1439 double value = RFLOAT_VALUE(num);
1440
1441 return isnan(value) ? Qtrue : Qfalse;
1442 }
1443
1444 /*
1445 * call-seq:
1446 * flt.infinite? -> nil, -1, +1
1447 *
1448 * Returns <code>nil</code>, -1, or +1 depending on whether <i>flt</i>
1449 * is finite, -infinity, or +infinity.
1450 *
1451 * (0.0).infinite? #=> nil
1452 * (-1.0/0.0).infinite? #=> -1
1453 * (+1.0/0.0).infinite? #=> 1
1454 */
1455
1456 static VALUE
1457 flo_is_infinite_p(VALUE num)
1458 {
1459 double value = RFLOAT_VALUE(num);
1460
1461 if (isinf(value)) {
1462 return INT2FIX( value < 0 ? -1 : 1 );
1463 }
1464
1465 return Qnil;
1466 }
1467
1468 /*
1469 * call-seq:
1470 * flt.finite? -> true or false
1471 *
1472 * Returns <code>true</code> if <i>flt</i> is a valid IEEE floating
1473 * point number (it is not infinite, and <code>nan?</code> is
1474 * <code>false</code>).
1475 *
1476 */
1477
1478 static VALUE
1479 flo_is_finite_p(VALUE num)
1480 {
1481 double value = RFLOAT_VALUE(num);
1482
1483 #if HAVE_FINITE
1484 if (!finite(value))
1485 return Qfalse;
1486 #else
1487 if (isinf(value) || isnan(value))
1488 return Qfalse;
1489 #endif
1490
1491 return Qtrue;
1492 }
1493
1494 /*
1495 * call-seq:
1496 * flt.floor -> integer
1497 *
1498 * Returns the largest integer less than or equal to <i>flt</i>.
1499 *
1500 * 1.2.floor #=> 1
1501 * 2.0.floor #=> 2
1502 * (-1.2).floor #=> -2
1503 * (-2.0).floor #=> -2
1504 */
1505
1506 static VALUE
1507 flo_floor(VALUE num)
1508 {
1509 double f = floor(RFLOAT_VALUE(num));
1510 long val;
1511
1512 if (!FIXABLE(f)) {
1513 return rb_dbl2big(f);
1514 }
1515 val = (long)f;
1516 return LONG2FIX(val);
1517 }
1518
1519 /*
1520 * call-seq:
1521 * flt.ceil -> integer
1522 *
1523 * Returns the smallest <code>Integer</code> greater than or equal to
1524 * <i>flt</i>.
1525 *
1526 * 1.2.ceil #=> 2
1527 * 2.0.ceil #=> 2
1528 * (-1.2).ceil #=> -1
1529 * (-2.0).ceil #=> -2
1530 */
1531
1532 static VALUE
1533 flo_ceil(VALUE num)
1534 {
1535 double f = ceil(RFLOAT_VALUE(num));
1536 long val;
1537
1538 if (!FIXABLE(f)) {
1539 return rb_dbl2big(f);
1540 }
1541 val = (long)f;
1542 return LONG2FIX(val);
1543 }
1544
1545 /*
1546 * Assumes num is an Integer, ndigits <= 0
1547 */
1548 static VALUE
1549 int_round_0(VALUE num, int ndigits)
1550 {
1551 VALUE n, f, h, r;
1552 long bytes;
1553 ID op;
1554 /* If 10**N / 2 > num, then return 0 */
1555 /* We have log_256(10) > 0.415241 and log_256(1/2) = -0.125, so */
1556 bytes = FIXNUM_P(num) ? sizeof(long) : rb_funcall(num, idSize, 0);
1557 if (-0.415241 * ndigits - 0.125 > bytes ) {
1558 return INT2FIX(0);
1559 }
1560
1561 f = int_pow(10, -ndigits);
1562 if (FIXNUM_P(num) && FIXNUM_P(f)) {
1563 SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
1564 int neg = x < 0;
1565 if (neg) x = -x;
1566 x = (x + y / 2) / y * y;
1567 if (neg) x = -x;
1568 return LONG2NUM(x);
1569 }
1570 if (RB_TYPE_P(f, T_FLOAT)) {
1571 /* then int_pow overflow */
1572 return INT2FIX(0);
1573 }
1574 h = rb_funcall(f, '/', 1, INT2FIX(2));
1575 r = rb_funcall(num, '%', 1, f);
1576 n = rb_funcall(num, '-', 1, r);
1577 op = negative_int_p(num) ? rb_intern("<=") : '<';
1578 if (!RTEST(rb_funcall(r, op, 1, h))) {
1579 n = rb_funcall(n, '+', 1, f);
1580 }
1581 return n;
1582 }
1583
1584 static VALUE
1585 flo_truncate(VALUE num);
1586
1587 /*
1588 * call-seq:
1589 * flt.round([ndigits]) -> integer or float
1590 *
1591 * Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits).
1592 * Precision may be negative. Returns a floating point number when ndigits
1593 * is more than zero.
1594 *
1595 * 1.4.round #=> 1
1596 * 1.5.round #=> 2
1597 * 1.6.round #=> 2
1598 * (-1.5).round #=> -2
1599 *
1600 * 1.234567.round(2) #=> 1.23
1601 * 1.234567.round(3) #=> 1.235
1602 * 1.234567.round(4) #=> 1.2346
1603 * 1.234567.round(5) #=> 1.23457
1604 *
1605 * 34567.89.round(-5) #=> 0
1606 * 34567.89.round(-4) #=> 30000
1607 * 34567.89.round(-3) #=> 35000
1608 * 34567.89.round(-2) #=> 34600
1609 * 34567.89.round(-1) #=> 34570
1610 * 34567.89.round(0) #=> 34568
1611 * 34567.89.round(1) #=> 34567.9
1612 * 34567.89.round(2) #=> 34567.89
1613 * 34567.89.round(3) #=> 34567.89
1614 *
1615 */
1616
1617 static VALUE
1618 flo_round(int argc, VALUE *argv, VALUE num)
1619 {
1620 VALUE nd;
1621 double number, f;
1622 int ndigits = 0;
1623 int binexp;
1624 enum {float_dig = DBL_DIG+2};
1625
1626 if (argc > 0 && rb_scan_args(argc, argv, "01", &nd) == 1) {
1627 ndigits = NUM2INT(nd);
1628 }
1629 if (ndigits < 0) {
1630 return int_round_0(flo_truncate(num), ndigits);
1631 }
1632 number = RFLOAT_VALUE(num);
1633 if (ndigits == 0) {
1634 return dbl2ival(number);
1635 }
1636 frexp(number, &binexp);
1637
1638 /* Let `exp` be such that `number` is written as:"0.#{digits}e#{exp}",
1639 i.e. such that 10 ** (exp - 1) <= |number| < 10 ** exp
1640 Recall that up to float_dig digits can be needed to represent a double,
1641 so if ndigits + exp >= float_dig, the intermediate value (number * 10 ** ndigits)
1642 will be an integer and thus the result is the original number.
1643 If ndigits + exp <= 0, the result is 0 or "1e#{exp}", so
1644 if ndigits + exp < 0, the result is 0.
1645 We have:
1646 2 ** (binexp-1) <= |number| < 2 ** binexp
1647 10 ** ((binexp-1)/log_2(10)) <= |number| < 10 ** (binexp/log_2(10))
1648 If binexp >= 0, and since log_2(10) = 3.322259:
1649 10 ** (binexp/4 - 1) < |number| < 10 ** (binexp/3)
1650 floor(binexp/4) <= exp <= ceil(binexp/3)
1651 If binexp <= 0, swap the /4 and the /3
1652 So if ndigits + floor(binexp/(4 or 3)) >= float_dig, the result is number
1653 If ndigits + ceil(binexp/(3 or 4)) < 0 the result is 0
1654 */
1655 if (isinf(number) || isnan(number) ||
1656 (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1))) {
1657 return num;
1658 }
1659 if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
1660 return DBL2NUM(0);
1661 }
1662 f = pow(10, ndigits);
1663 return DBL2NUM(round(number * f) / f);
1664 }
1665
1666 /*
1667 * call-seq:
1668 * flt.to_i -> integer
1669 * flt.to_int -> integer
1670 * flt.truncate -> integer
1671 *
1672 * Returns <i>flt</i> truncated to an <code>Integer</code>.
1673 */
1674
1675 static VALUE
1676 flo_truncate(VALUE num)
1677 {
1678 double f = RFLOAT_VALUE(num);
1679 long val;
1680
1681 if (f > 0.0) f = floor(f);
1682 if (f < 0.0) f = ceil(f);
1683
1684 if (!FIXABLE(f)) {
1685 return rb_dbl2big(f);
1686 }
1687 val = (long)f;
1688 return LONG2FIX(val);
1689 }
1690
1691 /*
1692 * call-seq:
1693 * num.floor -> integer
1694 *
1695 * Returns the largest integer less than or equal to <i>num</i>.
1696 * <code>Numeric</code> implements this by converting <i>anInteger</i>
1697 * to a <code>Float</code> and invoking <code>Float#floor</code>.
1698 *
1699 * 1.floor #=> 1
1700 * (-1).floor #=> -1
1701 */
1702
1703 static VALUE
1704 num_floor(VALUE num)
1705 {
1706 return flo_floor(rb_Float(num));
1707 }
1708
1709
1710 /*
1711 * call-seq:
1712 * num.ceil -> integer
1713 *
1714 * Returns the smallest <code>Integer</code> greater than or equal to
1715 * <i>num</i>. Class <code>Numeric</code> achieves this by converting
1716 * itself to a <code>Float</code> then invoking
1717 * <code>Float#ceil</code>.
1718 *
1719 * 1.ceil #=> 1
1720 * 1.2.ceil #=> 2
1721 * (-1.2).ceil #=> -1
1722 * (-1.0).ceil #=> -1
1723 */
1724
1725 static VALUE
1726 num_ceil(VALUE num)
1727 {
1728 return flo_ceil(rb_Float(num));
1729 }
1730
1731 /*
1732 * call-seq:
1733 * num.round([ndigits]) -> integer or float
1734 *
1735 * Rounds <i>num</i> to a given precision in decimal digits (default 0 digits).
1736 * Precision may be negative. Returns a floating point number when <i>ndigits</i>
1737 * is more than zero. <code>Numeric</code> implements this by converting itself
1738 * to a <code>Float</code> and invoking <code>Float#round</code>.
1739 */
1740
1741 static VALUE
1742 num_round(int argc, VALUE* argv, VALUE num)
1743 {
1744 return flo_round(argc, argv, rb_Float(num));
1745 }
1746
1747 /*
1748 * call-seq:
1749 * num.truncate -> integer
1750 *
1751 * Returns <i>num</i> truncated to an integer. <code>Numeric</code>
1752 * implements this by converting its value to a float and invoking
1753 * <code>Float#truncate</code>.
1754 */
1755
1756 static VALUE
1757 num_truncate(VALUE num)
1758 {
1759 return flo_truncate(rb_Float(num));
1760 }
1761
1762 static double
1763 ruby_float_step_size(double beg, double end, double unit, int excl)
1764 {
1765 const double epsilon = DBL_EPSILON;
1766 double n = (end - beg)/unit;
1767 double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
1768
1769 if (isinf(unit)) {
1770 return unit > 0 ? beg <= end : beg >= end;
1771 }
1772 if (err>0.5) err=0.5;
1773 if (excl) {
1774 if (n<=0) return 0;
1775 if (n<1)
1776 n = 0;
1777 else
1778 n = floor(n - err);
1779 }
1780 else {
1781 if (n<0) return 0;
1782 n = floor(n + err);
1783 }
1784 return n+1;
1785 }
1786
1787 int
1788 ruby_float_step(VALUE from, VALUE to, VALUE step, int excl)
1789 {
1790 if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
1791 double beg = NUM2DBL(from);
1792 double end = NUM2DBL(to);
1793 double unit = NUM2DBL(step);
1794 double n = ruby_float_step_size(beg, end, unit, excl);
1795 long i;
1796
1797 if (isinf(unit)) {
1798 /* if unit is infinity, i*unit+beg is NaN */
1799 if (n) rb_yield(DBL2NUM(beg));
1800 }
1801 else {
1802 for (i=0; i<n; i++) {
1803 double d = i*unit+beg;
1804 if (unit >= 0 ? end < d : d < end) d = end;
1805 rb_yield(DBL2NUM(d));
1806 }
1807 }
1808 return TRUE;
1809 }
1810 return FALSE;
1811 }
1812
1813 VALUE
1814 num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
1815 {
1816 if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
1817 long delta, diff, result;
1818
1819 diff = FIX2LONG(step);
1820 delta = FIX2LONG(to) - FIX2LONG(from);
1821 if (excl) {
1822 delta += (diff > 0 ? -1 : +1);
1823 }
1824 result = delta / diff;
1825 return LONG2FIX(result >= 0 ? result + 1 : 0);
1826 }
1827 else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
1828 double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
1829
1830 if (isinf(n)) return DBL2NUM(n);
1831 return LONG2FIX(n);
1832 }
1833 else {
1834 VALUE result;
1835 ID cmp = RTEST(rb_funcall(step, '>', 1, INT2FIX(0))) ? '>' : '<';
1836 if (RTEST(rb_funcall(from, cmp, 1, to))) return INT2FIX(0);
1837 result = rb_funcall(rb_funcall(to, '-', 1, from), id_div, 1, step);
1838 if (!excl || RTEST(rb_funcall(rb_funcall(from, '+', 1, rb_funcall(result, '*', 1, step)), cmp, 1, to))) {
1839 result = rb_funcall(result, '+', 1, INT2FIX(1));
1840 }
1841 return result;
1842 }
1843 }
1844
1845 static VALUE
1846 num_step_size(VALUE from, VALUE args)
1847 {
1848 VALUE to = RARRAY_PTR(args)[0];
1849 VALUE step = (RARRAY_LEN(args) > 1) ? RARRAY_PTR(args)[1] : INT2FIX(1);
1850 return num_interval_step_size(from, to, step, FALSE);
1851 }
1852 /*
1853 * call-seq:
1854 * num.step(limit[, step]) {|i| block } -> self
1855 * num.step(limit[, step]) -> an_enumerator
1856 *
1857 * Invokes <em>block</em> with the sequence of numbers starting at
1858 * <i>num</i>, incremented by <i>step</i> (default 1) on each
1859 * call. The loop finishes when the value to be passed to the block
1860 * is greater than <i>limit</i> (if <i>step</i> is positive) or less
1861 * than <i>limit</i> (if <i>step</i> is negative). If all the
1862 * arguments are integers, the loop operates using an integer
1863 * counter. If any of the arguments are floating point numbers, all
1864 * are converted to floats, and the loop is executed <i>floor(n +
1865 * n*epsilon)+ 1</i> times, where <i>n = (limit -
1866 * num)/step</i>. Otherwise, the loop starts at <i>num</i>, uses
1867 * either the <code><</code> or <code>></code> operator to compare
1868 * the counter against <i>limit</i>, and increments itself using the
1869 * <code>+</code> operator.
1870 *
1871 * If no block is given, an enumerator is returned instead.
1872 *
1873 * 1.step(10, 2) { |i| print i, " " }
1874 * Math::E.step(Math::PI, 0.2) { |f| print f, " " }
1875 *
1876 * <em>produces:</em>
1877 *
1878 * 1 3 5 7 9
1879 * 2.71828182845905 2.91828182845905 3.11828182845905
1880 */
1881
1882 static VALUE
1883 num_step(int argc, VALUE *argv, VALUE from)
1884 {
1885 VALUE to, step;
1886
1887 RETURN_SIZED_ENUMERATOR(from, argc, argv, num_step_size);
1888 if (argc == 1) {
1889 to = argv[0];
1890 step = INT2FIX(1);
1891 }
1892 else {
1893 rb_check_arity(argc, 1, 2);
1894 to = argv[0];
1895 step = argv[1];
1896 if (rb_equal(step, INT2FIX(0))) {
1897 rb_raise(rb_eArgError, "step can't be 0");
1898 }
1899 }
1900
1901 if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
1902 long i, end, diff;
1903
1904 i = FIX2LONG(from);
1905 end = FIX2LONG(to);
1906 diff = FIX2LONG(step);
1907
1908 if (diff > 0) {
1909 while (i <= end) {
1910 rb_yield(LONG2FIX(i));
1911 i += diff;
1912 }
1913 }
1914 else {
1915 while (i >= end) {
1916 rb_yield(LONG2FIX(i));
1917 i += diff;
1918 }
1919 }
1920 }
1921 else if (!ruby_float_step(from, to, step, FALSE)) {
1922 VALUE i = from;
1923 ID cmp;
1924
1925 if (positive_int_p(step)) {
1926 cmp = '>';
1927 }
1928 else {
1929 cmp = '<';
1930 }
1931 for (;;) {
1932 if (RTEST(rb_funcall(i, cmp, 1, to))) break;
1933 rb_yield(i);
1934 i = rb_funcall(i, '+', 1, step);
1935 }
1936 }
1937 return from;
1938 }
1939
1940 #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
1941 #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
1942 #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
1943
1944 SIGNED_VALUE
1945 rb_num2long(VALUE val)
1946 {
1947 again:
1948 if (NIL_P(val)) {
1949 rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
1950 }
1951
1952 if (FIXNUM_P(val)) return FIX2LONG(val);
1953
1954 switch (TYPE(val)) {
1955 case T_FLOAT:
1956 if (RFLOAT_VALUE(val) < LONG_MAX_PLUS_ONE
1957 && RFLOAT_VALUE(val) > LONG_MIN_MINUS_ONE) {
1958 return (SIGNED_VALUE)(RFLOAT_VALUE(val));
1959 }
1960 else {
1961 char buf[24];
1962 char *s;
1963
1964 snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
1965 if ((s = strchr(buf, ' ')) != 0) *s = '\0';
1966 rb_raise(rb_eRangeError, "float %s out of range of integer", buf);
1967 }
1968
1969 case T_BIGNUM:
1970 return rb_big2long(val);
1971
1972 default:
1973 val = rb_to_int(val);
1974 goto again;
1975 }
1976 }
1977
1978 VALUE
1979 rb_num2ulong(VALUE val)
1980 {
1981 again:
1982 if (NIL_P(val)) {
1983 rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
1984 }
1985
1986 if (FIXNUM_P(val)) return FIX2LONG(val); /* this is FIX2LONG, inteneded */
1987
1988 switch (TYPE(val)) {
1989 case T_FLOAT:
1990 if (RFLOAT_VALUE(val) < ULONG_MAX_PLUS_ONE
1991 && RFLOAT_VALUE(val) > LONG_MIN_MINUS_ONE) {
1992 return (VALUE)RFLOAT_VALUE(val);
1993 }
1994 else {
1995 char buf[24];
1996 char *s;
1997
1998 snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
1999 if ((s = strchr(buf, ' ')) != 0) *s = '\0';
2000 rb_raise(rb_eRangeError, "float %s out of range of integer", buf);
2001 }
2002
2003 case T_BIGNUM:
2004 return rb_big2ulong(val);
2005
2006 default:
2007 val = rb_to_int(val);
2008 goto again;
2009 }
2010 }
2011
2012 #if SIZEOF_INT < SIZEOF_VALUE
2013 void
2014 rb_out_of_int(SIGNED_VALUE num)
2015 {
2016 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `int'",
2017 num, num < 0 ? "small" : "big");
2018 }
2019
2020 static void
2021 check_int(SIGNED_VALUE num)
2022 {
2023 if ((SIGNED_VALUE)(int)num != num) {
2024 rb_out_of_int(num);
2025 }
2026 }
2027
2028 static void
2029 check_uint(VALUE num, int sign)
2030 {
2031 static const VALUE mask = ~(VALUE)UINT_MAX;
2032
2033 if (sign) {
2034 /* minus */
2035 if ((num & mask) != mask || (num & ~mask) <= INT_MAX)
2036 #define VALUE_MSBMASK ((VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1))
2037 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too small to convert to `unsigned int'", num|VALUE_MSBMASK);
2038 }
2039 else {
2040 /* plus */
2041 if ((num & mask) != 0)
2042 rb_raise(rb_eRangeError, "integer %"PRIuVALUE " too big to convert to `unsigned int'", num);
2043 }
2044 }
2045
2046 long
2047 rb_num2int(VALUE val)
2048 {
2049 long num = rb_num2long(val);
2050
2051 check_int(num);
2052 return num;
2053 }
2054
2055 long
2056 rb_fix2int(VALUE val)
2057 {
2058 long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
2059
2060 check_int(num);
2061 return num;
2062 }
2063
2064 unsigned long
2065 rb_num2uint(VALUE val)
2066 {
2067 VALUE num = rb_num2ulong(val);
2068
2069 check_uint(num, negative_int_p(val));
2070 return (unsigned long)num;
2071 }
2072
2073 unsigned long
2074 rb_fix2uint(VALUE val)
2075 {
2076 unsigned long num;
2077
2078 if (!FIXNUM_P(val)) {
2079 return rb_num2uint(val);
2080 }
2081 num = FIX2ULONG(val);
2082
2083 check_uint(num, negative_int_p(val));
2084 return num;
2085 }
2086 #else
2087 long
2088 rb_num2int(VALUE val)
2089 {
2090 return rb_num2long(val);
2091 }
2092
2093 long
2094 rb_fix2int(VALUE val)
2095 {
2096 return FIX2INT(val);
2097 }
2098 #endif
2099
2100 void
2101 rb_out_of_short(SIGNED_VALUE num)
2102 {
2103 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `short'",
2104 num, num < 0 ? "small" : "big");
2105 }
2106
2107 static void
2108 check_short(SIGNED_VALUE num)
2109 {
2110 if ((SIGNED_VALUE)(short)num != num) {
2111 rb_out_of_short(num);
2112 }
2113 }
2114
2115 static void
2116 check_ushort(VALUE num, int sign)
2117 {
2118 static const VALUE mask = ~(VALUE)USHRT_MAX;
2119
2120 if (sign) {
2121 /* minus */
2122 if ((num & mask) != mask || (num & ~mask) <= SHRT_MAX)
2123 #define VALUE_MSBMASK ((VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1))
2124 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too small to convert to `unsigned short'", num|VALUE_MSBMASK);
2125 }
2126 else {
2127 /* plus */
2128 if ((num & mask) != 0)
2129 rb_raise(rb_eRangeError, "integer %"PRIuVALUE " too big to convert to `unsigned short'", num);
2130 }
2131 }
2132
2133 short
2134 rb_num2short(VALUE val)
2135 {
2136 long num = rb_num2long(val);
2137
2138 check_short(num);
2139 return num;
2140 }
2141
2142 short
2143 rb_fix2short(VALUE val)
2144 {
2145 long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
2146
2147 check_short(num);
2148 return num;
2149 }
2150
2151 unsigned short
2152 rb_num2ushort(VALUE val)
2153 {
2154 VALUE num = rb_num2ulong(val);
2155
2156 check_ushort(num, negative_int_p(val));
2157 return (unsigned long)num;
2158 }
2159
2160 unsigned short
2161 rb_fix2ushort(VALUE val)
2162 {
2163 unsigned long num;
2164
2165 if (!FIXNUM_P(val)) {
2166 return rb_num2ushort(val);
2167 }
2168 num = FIX2ULONG(val);
2169
2170 check_ushort(num, negative_int_p(val));
2171 return num;
2172 }
2173
2174 VALUE
2175 rb_num2fix(VALUE val)
2176 {
2177 SIGNED_VALUE v;
2178
2179 if (FIXNUM_P(val)) return val;
2180
2181 v = rb_num2long(val);
2182 if (!FIXABLE(v))
2183 rb_raise(rb_eRangeError, "integer %"PRIdVALUE " out of range of fixnum", v);
2184 return LONG2FIX(v);
2185 }
2186
2187 #if HAVE_LONG_LONG
2188
2189 #define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
2190 #define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
2191 #define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
2192 #ifndef ULLONG_MAX
2193 #define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
2194 #endif
2195
2196 LONG_LONG
2197 rb_num2ll(VALUE val)
2198 {
2199 if (NIL_P(val)) {
2200 rb_raise(rb_eTypeError, "no implicit conversion from nil");
2201 }
2202
2203 if (FIXNUM_P(val)) return (LONG_LONG)FIX2LONG(val);
2204
2205 switch (TYPE(val)) {
2206 case T_FLOAT:
2207 if (RFLOAT_VALUE(val) < LLONG_MAX_PLUS_ONE
2208 && RFLOAT_VALUE(val) > LLONG_MIN_MINUS_ONE) {
2209 return (LONG_LONG)(RFLOAT_VALUE(val));
2210 }
2211 else {
2212 char buf[24];
2213 char *s;
2214
2215 snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
2216 if ((s = strchr(buf, ' ')) != 0) *s = '\0';
2217 rb_raise(rb_eRangeError, "float %s out of range of long long", buf);
2218 }
2219
2220 case T_BIGNUM:
2221 return rb_big2ll(val);
2222
2223 case T_STRING:
2224 rb_raise(rb_eTypeError, "no implicit conversion from string");
2225 break;
2226
2227 case T_TRUE:
2228 case T_FALSE:
2229 rb_raise(rb_eTypeError, "no implicit conversion from boolean");
2230 break;
2231
2232 default:
2233 break;
2234 }
2235
2236 val = rb_to_int(val);
2237 return NUM2LL(val);
2238 }
2239
2240 unsigned LONG_LONG
2241 rb_num2ull(VALUE val)
2242 {
2243 switch (TYPE(val)) {
2244 case T_NIL:
2245 rb_raise(rb_eTypeError, "no implicit conversion from nil");
2246
2247 case T_FIXNUM:
2248 return (LONG_LONG)FIX2LONG(val); /* this is FIX2LONG, inteneded */
2249
2250 case T_FLOAT:
2251 if (RFLOAT_VALUE(val) < ULLONG_MAX_PLUS_ONE
2252 && RFLOAT_VALUE(val) > 0) {
2253 return (unsigned LONG_LONG)(RFLOAT_VALUE(val));
2254 }
2255 else {
2256 char buf[24];
2257 char *s;
2258
2259 snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
2260 if ((s = strchr(buf, ' ')) != 0) *s = '\0';
2261 rb_raise(rb_eRangeError, "float %s out of range of unsgined long long", buf);
2262 }
2263
2264 case T_BIGNUM:
2265 return rb_big2ull(val);
2266
2267 case T_STRING:
2268 rb_raise(rb_eTypeError, "no implicit conversion from string");
2269 break;
2270
2271 case T_TRUE:
2272 case T_FALSE:
2273 rb_raise(rb_eTypeError, "no implicit conversion from boolean");
2274 break;
2275
2276 default:
2277 break;
2278 }
2279
2280 val = rb_to_int(val);
2281 return NUM2ULL(val);
2282 }
2283
2284 #endif /* HAVE_LONG_LONG */
2285
2286 /*
2287 * Document-class: Integer
2288 *
2289 * <code>Integer</code> is the basis for the two concrete classes that
2290 * hold whole numbers, <code>Bignum</code> and <code>Fixnum</code>.
2291 *
2292 */
2293
2294 /*
2295 * call-seq:
2296 * int.to_i -> integer
2297 * int.to_int -> integer
2298 * int.floor -> integer
2299 * int.ceil -> integer
2300 * int.truncate -> integer
2301 *
2302 * As <i>int</i> is already an <code>Integer</code>, all these
2303 * methods simply return the receiver.
2304 */
2305
2306 static VALUE
2307 int_to_i(VALUE num)
2308 {
2309 return num;
2310 }
2311
2312 /*
2313 * call-seq:
2314 * int.integer? -> true
2315 *
2316 * Always returns <code>true</code>.
2317 */
2318
2319 static VALUE
2320 int_int_p(VALUE num)
2321 {
2322 return Qtrue;
2323 }
2324
2325 /*
2326 * call-seq:
2327 * int.odd? -> true or false
2328 *
2329 * Returns <code>true</code> if <i>int</i> is an odd number.
2330 */
2331
2332 static VALUE
2333 int_odd_p(VALUE num)
2334 {
2335 if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
2336 return Qtrue;
2337 }
2338 return Qfalse;
2339 }
2340
2341 /*
2342 * call-seq:
2343 * int.even? -> true or false
2344 *
2345 * Returns <code>true</code> if <i>int</i> is an even number.
2346 */
2347
2348 static VALUE
2349 int_even_p(VALUE num)
2350 {
2351 if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
2352 return Qtrue;
2353 }
2354 return Qfalse;
2355 }
2356
2357 /*
2358 * call-seq:
2359 * fixnum.next -> integer
2360 * fixnum.succ -> integer
2361 *
2362 * Returns the <code>Integer</code> equal to <i>int</i> + 1.
2363 *
2364 * 1.next #=> 2
2365 * (-1).next #=> 0
2366 */
2367
2368 static VALUE
2369 fix_succ(VALUE num)
2370 {
2371 long i = FIX2LONG(num) + 1;
2372 return LONG2NUM(i);
2373 }
2374
2375 /*
2376 * call-seq:
2377 * int.next -> integer
2378 * int.succ -> integer
2379 *
2380 * Returns the <code>Integer</code> equal to <i>int</i> + 1.
2381 *
2382 * 1.next #=> 2
2383 * (-1).next #=> 0
2384 */
2385
2386 static VALUE
2387 int_succ(VALUE num)
2388 {
2389 if (FIXNUM_P(num)) {
2390 long i = FIX2LONG(num) + 1;
2391 return LONG2NUM(i);
2392 }
2393 return rb_funcall(num, '+', 1, INT2FIX(1));
2394 }
2395
2396 /*
2397 * call-seq:
2398 * int.pred -> integer
2399 *
2400 * Returns the <code>Integer</code> equal to <i>int</i> - 1.
2401 *
2402 * 1.pred #=> 0
2403 * (-1).pred #=> -2
2404 */
2405
2406 static VALUE
2407 int_pred(VALUE num)
2408 {
2409 if (FIXNUM_P(num)) {
2410 long i = FIX2LONG(num) - 1;
2411 return LONG2NUM(i);
2412 }
2413 return rb_funcall(num, '-', 1, INT2FIX(1));
2414 }
2415
2416 VALUE
2417 rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
2418 {
2419 int n;
2420 VALUE str;
2421 switch (n = rb_enc_codelen(code, enc)) {
2422 case ONIGERR_INVALID_CODE_POINT_VALUE:
2423 rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
2424 break;
2425 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
2426 case 0:
2427 rb_raise(rb_eRangeError, "%u out of char range", code);
2428 break;
2429 }
2430 str = rb_enc_str_new(0, n, enc);
2431 rb_enc_mbcput(code, RSTRING_PTR(str), enc);
2432 if (rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_END(str), enc) != n) {
2433 rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
2434 }
2435 return str;
2436 }
2437
2438 /*
2439 * call-seq:
2440 * int.chr([encoding]) -> string
2441 *
2442 * Returns a string containing the character represented by the
2443 * receiver's value according to +encoding+.
2444 *
2445 * 65.chr #=> "A"
2446 * 230.chr #=> "\346"
2447 * 255.chr(Encoding::UTF_8) #=> "\303\277"
2448 */
2449
2450 static VALUE
2451 int_chr(int argc, VALUE *argv, VALUE num)
2452 {
2453 char c;
2454 unsigned int i;
2455 rb_encoding *enc;
2456
2457 if (rb_num_to_uint(num, &i) == 0) {
2458 }
2459 else if (FIXNUM_P(num)) {
2460 rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(num));
2461 }
2462 else {
2463 rb_raise(rb_eRangeError, "bignum out of char range");
2464 }
2465
2466 switch (argc) {
2467 case 0:
2468 if (0xff < i) {
2469 enc = rb_default_internal_encoding();
2470 if (!enc) {
2471 rb_raise(rb_eRangeError, "%d out of char range", i);
2472 }
2473 goto decode;
2474 }
2475 c = (char)i;
2476 if (i < 0x80) {
2477 return rb_usascii_str_new(&c, 1);
2478 }
2479 else {
2480 return rb_str_new(&c, 1);
2481 }
2482 case 1:
2483 break;
2484 default:
2485 rb_check_arity(argc, 0, 1);
2486 break;
2487 }
2488 enc = rb_to_encoding(argv[0]);
2489 if (!enc) enc = rb_ascii8bit_encoding();
2490 decode:
2491 return rb_enc_uint_chr(i, enc);
2492 }
2493
2494 /*
2495 * call-seq:
2496 * int.ord -> self
2497 *
2498 * Returns the int itself.
2499 *
2500 * ?a.ord #=> 97
2501 *
2502 * This method is intended for compatibility to
2503 * character constant in Ruby 1.9.
2504 * For example, ?a.ord returns 97 both in 1.8 and 1.9.
2505 */
2506
2507 static VALUE
2508 int_ord(VALUE num)
2509 {
2510 return num;
2511 }
2512
2513 /********************************************************************
2514 *
2515 * Document-class: Fixnum
2516 *
2517 * A <code>Fixnum</code> holds <code>Integer</code> values that can be
2518 * represented in a native machine word (minus 1 bit). If any operation
2519 * on a <code>Fixnum</code> exceeds this range, the value is
2520 * automatically converted to a <code>Bignum</code>.
2521 *
2522 * <code>Fixnum</code> objects have immediate value. This means that
2523 * when they are assigned or passed as parameters, the actual object is
2524 * passed, rather than a reference to that object. Assignment does not
2525 * alias <code>Fixnum</code> objects. There is effectively only one
2526 * <code>Fixnum</code> object instance for any given integer value, so,
2527 * for example, you cannot add a singleton method to a
2528 * <code>Fixnum</code>.
2529 */
2530
2531
2532 /*
2533 * call-seq:
2534 * -fix -> integer
2535 *
2536 * Negates <code>fix</code> (which might return a Bignum).
2537 */
2538
2539 static VALUE
2540 fix_uminus(VALUE num)
2541 {
2542 return LONG2NUM(-FIX2LONG(num));
2543 }
2544
2545 VALUE
2546 rb_fix2str(VALUE x, int base)
2547 {
2548 extern const char ruby_digitmap[];
2549 char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf;
2550 long val = FIX2LONG(x);
2551 int neg = 0;
2552
2553 if (base < 2 || 36 < base) {
2554 rb_raise(rb_eArgError, "invalid radix %d", base);
2555 }
2556 if (val == 0) {
2557 return rb_usascii_str_new2("0");
2558 }
2559 if (val < 0) {
2560 val = -val;
2561 neg = 1;
2562 }
2563 *--b = '\0';
2564 do {
2565 *--b = ruby_digitmap[(int)(val % base)];
2566 } while (val /= base);
2567 if (neg) {
2568 *--b = '-';
2569 }
2570
2571 return rb_usascii_str_new2(b);
2572 }
2573
2574 /*
2575 * call-seq:
2576 * fix.to_s(base=10) -> string
2577 *
2578 * Returns a string containing the representation of <i>fix</i> radix
2579 * <i>base</i> (between 2 and 36).
2580 *
2581 * 12345.to_s #=> "12345"
2582 * 12345.to_s(2) #=> "11000000111001"
2583 * 12345.to_s(8) #=> "30071"
2584 * 12345.to_s(10) #=> "12345"
2585 * 12345.to_s(16) #=> "3039"
2586 * 12345.to_s(36) #=> "9ix"
2587 *
2588 */
2589 static VALUE
2590 fix_to_s(int argc, VALUE *argv, VALUE x)
2591 {
2592 int base;
2593
2594 if (argc == 0) base = 10;
2595 else {
2596 VALUE b;
2597
2598 rb_scan_args(argc, argv, "01", &b);
2599 base = NUM2INT(b);
2600 }
2601
2602 return rb_fix2str(x, base);
2603 }
2604
2605 /*
2606 * call-seq:
2607 * fix + numeric -> numeric_result
2608 *
2609 * Performs addition: the class of the resulting object depends on
2610 * the class of <code>numeric</code> and on the magnitude of the
2611 * result.
2612 */
2613
2614 static VALUE
2615 fix_plus(VALUE x, VALUE y)
2616 {
2617 if (FIXNUM_P(y)) {
2618 long a, b, c;
2619 VALUE r;
2620
2621 a = FIX2LONG(x);
2622 b = FIX2LONG(y);
2623 c = a + b;
2624 r = LONG2NUM(c);
2625
2626 return r;
2627 }
2628 switch (TYPE(y)) {
2629 case T_BIGNUM:
2630 return rb_big_plus(y, x);
2631 case T_FLOAT:
2632 return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));
2633 default:
2634 return rb_num_coerce_bin(x, y, '+');
2635 }
2636 }
2637
2638 /*
2639 * call-seq:
2640 * fix - numeric -> numeric_result
2641 *
2642 * Performs subtraction: the class of the resulting object depends on
2643 * the class of <code>numeric</code> and on the magnitude of the
2644 * result.
2645 */
2646
2647 static VALUE
2648 fix_minus(VALUE x, VALUE y)
2649 {
2650 if (FIXNUM_P(y)) {
2651 long a, b, c;
2652 VALUE r;
2653
2654 a = FIX2LONG(x);
2655 b = FIX2LONG(y);
2656 c = a - b;
2657 r = LONG2NUM(c);
2658
2659 return r;
2660 }
2661 switch (TYPE(y)) {
2662 case T_BIGNUM:
2663 x = rb_int2big(FIX2LONG(x));
2664 return rb_big_minus(x, y);
2665 case T_FLOAT:
2666 return DBL2NUM((double)FIX2LONG(x) - RFLOAT_VALUE(y));
2667 default:
2668 return rb_num_coerce_bin(x, y, '-');
2669 }
2670 }
2671
2672 #define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
2673 /*tests if N*N would overflow*/
2674 #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
2675
2676 /*
2677 * call-seq:
2678 * fix * numeric -> numeric_result
2679 *
2680 * Performs multiplication: the class of the resulting object depends on
2681 * the class of <code>numeric</code> and on the magnitude of the
2682 * result.
2683 */
2684
2685 static VALUE
2686 fix_mul(VALUE x, VALUE y)
2687 {
2688 if (FIXNUM_P(y)) {
2689 #ifdef __HP_cc
2690 /* avoids an optimization bug of HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005] */
2691 volatile
2692 #endif
2693 long a, b;
2694 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
2695 LONG_LONG d;
2696 #else
2697 VALUE r;
2698 #endif
2699
2700 a = FIX2LONG(x);
2701 b = FIX2LONG(y);
2702
2703 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
2704 d = (LONG_LONG)a * b;
2705 if (FIXABLE(d)) return LONG2FIX(d);
2706 return rb_ll2inum(d);
2707 #else
2708 if (FIT_SQRT_LONG(a) && FIT_SQRT_LONG(b))
2709 return LONG2FIX(a*b);
2710 if (a == 0) return x;
2711 if (MUL_OVERFLOW_FIXNUM_P(a, b))
2712 r = rb_big_mul(rb_int2big(a), rb_int2big(b));
2713 else
2714 r = LONG2FIX(a * b);
2715 return r;
2716 #endif
2717 }
2718 switch (TYPE(y)) {
2719 case T_BIGNUM:
2720 return rb_big_mul(y, x);
2721 case T_FLOAT:
2722 return DBL2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y));
2723 default:
2724 return rb_num_coerce_bin(x, y, '*');
2725 }
2726 }
2727
2728 static void
2729 fixdivmod(long x, long y, long *divp, long *modp)
2730 {
2731 long div, mod;
2732
2733 if (y == 0) rb_num_zerodiv();
2734 if (y < 0) {
2735 if (x < 0)
2736 div = -x / -y;
2737 else
2738 div = - (x / -y);
2739 }
2740 else {
2741 if (x < 0)
2742 div = - (-x / y);
2743 else
2744 div = x / y;
2745 }
2746 mod = x - div*y;
2747 if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
2748 mod += y;
2749 div -= 1;
2750 }
2751 if (divp) *divp = div;
2752 if (modp) *modp = mod;
2753 }
2754
2755 /*
2756 * call-seq:
2757 * fix.fdiv(numeric) -> float
2758 *
2759 * Returns the floating point result of dividing <i>fix</i> by
2760 * <i>numeric</i>.
2761 *
2762 * 654321.fdiv(13731) #=> 47.6528293642124
2763 * 654321.fdiv(13731.24) #=> 47.6519964693647
2764 *
2765 */
2766
2767 static VALUE
2768 fix_fdiv(VALUE x, VALUE y)
2769 {
2770 if (FIXNUM_P(y)) {
2771 return DBL2NUM((double)FIX2LONG(x) / (double)FIX2LONG(y));
2772 }
2773 switch (TYPE(y)) {
2774 case T_BIGNUM:
2775 return rb_big_fdiv(rb_int2big(FIX2LONG(x)), y);
2776 case T_FLOAT:
2777 return DBL2NUM((double)FIX2LONG(x) / RFLOAT_VALUE(y));
2778 default:
2779 return rb_num_coerce_bin(x, y, rb_intern("fdiv"));
2780 }
2781 }
2782
2783 static VALUE
2784 fix_divide(VALUE x, VALUE y, ID op)
2785 {
2786 if (FIXNUM_P(y)) {
2787 long div;
2788
2789 fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0);
2790 return LONG2NUM(div);
2791 }
2792 switch (TYPE(y)) {
2793 case T_BIGNUM:
2794 x = rb_int2big(FIX2LONG(x));
2795 return rb_big_div(x, y);
2796 case T_FLOAT:
2797 {
2798 double div;
2799
2800 if (op == '/') {
2801 div = (double)FIX2LONG(x) / RFLOAT_VALUE(y);
2802 return DBL2NUM(div);
2803 }
2804 else {
2805 if (RFLOAT_VALUE(y) == 0) rb_num_zerodiv();
2806 div = (double)FIX2LONG(x) / RFLOAT_VALUE(y);
2807 return rb_dbl2big(floor(div));
2808 }
2809 }
2810 case T_RATIONAL:
2811 if (op == '/' && FIX2LONG(x) == 1)
2812 return rb_rational_reciprocal(y);
2813 /* fall through */
2814 default:
2815 return rb_num_coerce_bin(x, y, op);
2816 }
2817 }
2818
2819 /*
2820 * call-seq:
2821 * fix / numeric -> numeric_result
2822 *
2823 * Performs division: the class of the resulting object depends on
2824 * the class of <code>numeric</code> and on the magnitude of the
2825 * result.
2826 */
2827
2828 static VALUE
2829 fix_div(VALUE x, VALUE y)
2830 {
2831 return fix_divide(x, y, '/');
2832 }
2833
2834 /*
2835 * call-seq:
2836 * fix.div(numeric) -> integer
2837 *
2838 * Performs integer division: returns integer value.
2839 */
2840
2841 static VALUE
2842 fix_idiv(VALUE x, VALUE y)
2843 {
2844 return fix_divide(x, y, rb_intern("div"));
2845 }
2846
2847 /*
2848 * call-seq:
2849 * fix % other -> real
2850 * fix.modulo(other) -> real
2851 *
2852 * Returns <code>fix</code> modulo <code>other</code>.
2853 * See <code>numeric.divmod</code> for more information.
2854 */
2855
2856 static VALUE
2857 fix_mod(VALUE x, VALUE y)
2858 {
2859 if (FIXNUM_P(y)) {
2860 long mod;
2861
2862 fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod);
2863 return LONG2NUM(mod);
2864 }
2865 switch (TYPE(y)) {
2866 case T_BIGNUM:
2867 x = rb_int2big(FIX2LONG(x));
2868 return rb_big_modulo(x, y);
2869 case T_FLOAT:
2870 return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y)));
2871 default:
2872 return rb_num_coerce_bin(x, y, '%');
2873 }
2874 }
2875
2876 /*
2877 * call-seq:
2878 * fix.divmod(numeric) -> array
2879 *
2880 * See <code>Numeric#divmod</code>.
2881 */
2882 static VALUE
2883 fix_divmod(VALUE x, VALUE y)
2884 {
2885 if (FIXNUM_P(y)) {
2886 long div, mod;
2887
2888 fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod);
2889
2890 return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod));
2891 }
2892 switch (TYPE(y)) {
2893 case T_BIGNUM:
2894 x = rb_int2big(FIX2LONG(x));
2895 return rb_big_divmod(x, y);
2896 case T_FLOAT:
2897 {
2898 double div, mod;
2899 volatile VALUE a, b;
2900
2901 flodivmod((double)FIX2LONG(x), RFLOAT_VALUE(y), &div, &mod);
2902 a = dbl2ival(div);
2903 b = DBL2NUM(mod);
2904 return rb_assoc_new(a, b);
2905 }
2906 default:
2907 return rb_num_coerce_bin(x, y, rb_intern("divmod"));
2908 }
2909 }
2910
2911 static VALUE
2912 int_pow(long x, unsigned long y)
2913 {
2914 int neg = x < 0;
2915 long z = 1;
2916
2917 if (neg) x = -x;
2918 if (y & 1)
2919 z = x;
2920 else
2921 neg = 0;
2922 y &= ~1;
2923 do {
2924 while (y % 2 == 0) {
2925 if (!FIT_SQRT_LONG(x)) {
2926 VALUE v;
2927 bignum:
2928 v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
2929 if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
2930 return v;
2931 }
2932 x = x * x;
2933 y >>= 1;
2934 }
2935 {
2936 if (MUL_OVERFLOW_FIXNUM_P(x, z)) {
2937 goto bignum;
2938 }
2939 z = x * z;
2940 }
2941 } while (--y);
2942 if (neg) z = -z;
2943 return LONG2NUM(z);
2944 }
2945
2946 /*
2947 * call-seq:
2948 * fix ** numeric -> numeric_result
2949 *
2950 * Raises <code>fix</code> to the <code>numeric</code> power, which may
2951 * be negative or fractional.
2952 *
2953 * 2 ** 3 #=> 8
2954 * 2 ** -1 #=> (1/2)
2955 * 2 ** 0.5 #=> 1.4142135623731
2956 */
2957
2958 static VALUE
2959 fix_pow(VALUE x, VALUE y)
2960 {
2961 long a = FIX2LONG(x);
2962
2963 if (FIXNUM_P(y)) {
2964 long b = FIX2LONG(y);
2965
2966 if (a == 1) return INT2FIX(1);
2967 if (a == -1) {
2968 if (b % 2 == 0)
2969 return INT2FIX(1);
2970 else
2971 return INT2FIX(-1);
2972 }
2973 if (b < 0)
2974 return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
2975
2976 if (b == 0) return INT2FIX(1);
2977 if (b == 1) return x;
2978 if (a == 0) {
2979 if (b > 0) return INT2FIX(0);
2980 return DBL2NUM(INFINITY);
2981 }
2982 return int_pow(a, b);
2983 }
2984 switch (TYPE(y)) {
2985 case T_BIGNUM:
2986 if (a == 1) return INT2FIX(1);
2987 if (a == -1) {
2988 if (int_even_p(y)) return INT2FIX(1);
2989 else return INT2FIX(-1);
2990 }
2991 if (negative_int_p(y))
2992 return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
2993 if (a == 0) return INT2FIX(0);
2994 x = rb_int2big(FIX2LONG(x));
2995 return rb_big_pow(x, y);
2996 case T_FLOAT:
2997 if (RFLOAT_VALUE(y) == 0.0) return DBL2NUM(1.0);
2998 if (a == 0) {
2999 return DBL2NUM(RFLOAT_VALUE(y) < 0 ? INFINITY : 0.0);
3000 }
3001 if (a == 1) return DBL2NUM(1.0);
3002 {
3003 double dy = RFLOAT_VALUE(y);
3004 if (a < 0 && dy != round(dy))
3005 return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
3006 return DBL2NUM(pow((double)a, dy));
3007 }
3008 default:
3009 return rb_num_coerce_bin(x, y, rb_intern("**"));
3010 }
3011 }
3012
3013 /*
3014 * call-seq:
3015 * fix == other -> true or false
3016 *
3017 * Return <code>true</code> if <code>fix</code> equals <code>other</code>
3018 * numerically.
3019 *
3020 * 1 == 2 #=> false
3021 * 1 == 1.0 #=> true
3022 */
3023
3024 static VALUE
3025 fix_equal(VALUE x, VALUE y)
3026 {
3027 if (x == y) return Qtrue;
3028 if (FIXNUM_P(y)) return Qfalse;
3029 switch (TYPE(y)) {
3030 case T_BIGNUM:
3031 return rb_big_eq(y, x);
3032 case T_FLOAT:
3033 return rb_integer_float_eq(x, y);
3034 default:
3035 return num_equal(x, y);
3036 }
3037 }
3038
3039 /*
3040 * call-seq:
3041 * fix <=> numeric -> -1, 0, +1 or nil
3042 *
3043 * Comparison---Returns -1, 0, +1 or nil depending on whether +fix+ is less
3044 * than, equal to, or greater than +numeric+. This is the basis for the tests
3045 * in Comparable.
3046 *
3047 * +nil+ is returned if the two values are incomparable.
3048 */
3049
3050 static VALUE
3051 fix_cmp(VALUE x, VALUE y)
3052 {
3053 if (x == y) return INT2FIX(0);
3054 if (FIXNUM_P(y)) {
3055 if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
3056 return INT2FIX(-1);
3057 }
3058 switch (TYPE(y)) {
3059 case T_BIGNUM:
3060 return rb_big_cmp(rb_int2big(FIX2LONG(x)), y);
3061 case T_FLOAT:
3062 return rb_integer_float_cmp(x, y);
3063 default:
3064 return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
3065 }
3066 }
3067
3068 /*
3069 * call-seq:
3070 * fix > real -> true or false
3071 *
3072 * Returns <code>true</code> if the value of <code>fix</code> is
3073 * greater than that of <code>real</code>.
3074 */
3075
3076 static VALUE
3077 fix_gt(VALUE x, VALUE y)
3078 {
3079 if (FIXNUM_P(y)) {
3080 if (FIX2LONG(x) > FIX2LONG(y)) return Qtrue;
3081 return Qfalse;
3082 }
3083 switch (TYPE(y)) {
3084 case T_BIGNUM:
3085 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) > 0 ? Qtrue : Qfalse;
3086 case T_FLOAT:
3087 return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse;
3088 default:
3089 return rb_num_coerce_relop(x, y, '>');
3090 }
3091 }
3092
3093 /*
3094 * call-seq:
3095 * fix >= real -> true or false
3096 *
3097 * Returns <code>true</code> if the value of <code>fix</code> is
3098 * greater than or equal to that of <code>real</code>.
3099 */
3100
3101 static VALUE
3102 fix_ge(VALUE x, VALUE y)
3103 {
3104 if (FIXNUM_P(y)) {
3105 if (FIX2LONG(x) >= FIX2LONG(y)) return Qtrue;
3106 return Qfalse;
3107 }
3108 switch (TYPE(y)) {
3109 case T_BIGNUM:
3110 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) >= 0 ? Qtrue : Qfalse;
3111 case T_FLOAT:
3112 {
3113 VALUE rel = rb_integer_float_cmp(x, y);
3114 return rel == INT2FIX(1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
3115 }
3116 default:
3117 return rb_num_coerce_relop(x, y, rb_intern(">="));
3118 }
3119 }
3120
3121 /*
3122 * call-seq:
3123 * fix < real -> true or false
3124 *
3125 * Returns <code>true</code> if the value of <code>fix</code> is
3126 * less than that of <code>real</code>.
3127 */
3128
3129 static VALUE
3130 fix_lt(VALUE x, VALUE y)
3131 {
3132 if (FIXNUM_P(y)) {
3133 if (FIX2LONG(x) < FIX2LONG(y)) return Qtrue;
3134 return Qfalse;
3135 }
3136 switch (TYPE(y)) {
3137 case T_BIGNUM:
3138 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) < 0 ? Qtrue : Qfalse;
3139 case T_FLOAT:
3140 return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse;
3141 default:
3142 return rb_num_coerce_relop(x, y, '<');
3143 }
3144 }
3145
3146 /*
3147 * call-seq:
3148 * fix <= real -> true or false
3149 *
3150 * Returns <code>true</code> if the value of <code>fix</code> is
3151 * less than or equal to that of <code>real</code>.
3152 */
3153
3154 static VALUE
3155 fix_le(VALUE x, VALUE y)
3156 {
3157 if (FIXNUM_P(y)) {
3158 if (FIX2LONG(x) <= FIX2LONG(y)) return Qtrue;
3159 return Qfalse;
3160 }
3161 switch (TYPE(y)) {
3162 case T_BIGNUM:
3163 return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) <= 0 ? Qtrue : Qfalse;
3164 case T_FLOAT:
3165 {
3166 VALUE rel = rb_integer_float_cmp(x, y);
3167 return rel == INT2FIX(-1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
3168 }
3169 default:
3170 return rb_num_coerce_relop(x, y, rb_intern("<="));
3171 }
3172 }
3173
3174 /*
3175 * call-seq:
3176 * ~fix -> integer
3177 *
3178 * One's complement: returns a number where each bit is flipped.
3179 */
3180
3181 static VALUE
3182 fix_rev(VALUE num)
3183 {
3184 return ~num | FIXNUM_FLAG;
3185 }
3186
3187 static int
3188 bit_coerce(VALUE *x, VALUE *y, int err)
3189 {
3190 if (!FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) {
3191 do_coerce(x, y, err);
3192 if (!FIXNUM_P(*x) && !RB_TYPE_P(*x, T_BIGNUM)
3193 && !FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) {
3194 if (!err) return FALSE;
3195 rb_raise(rb_eTypeError,
3196 "%s can't be coerced into %s for bitwise arithmetic",
3197 rb_special_const_p(*y) ?
3198 RSTRING_PTR(rb_inspect(*y)) : rb_obj_classname(*y),
3199 rb_obj_classname(*x));
3200 }
3201 }
3202 return TRUE;
3203 }
3204
3205 VALUE
3206 rb_num_coerce_bit(VALUE x, VALUE y, ID func)
3207 {
3208 bit_coerce(&x, &y, TRUE);
3209 return rb_funcall(x, func, 1, y);
3210 }
3211
3212 /*
3213 * call-seq:
3214 * fix & integer -> integer_result
3215 *
3216 * Bitwise AND.
3217 */
3218
3219 static VALUE
3220 fix_and(VALUE x, VALUE y)
3221 {
3222 if (FIXNUM_P(y)) {
3223 long val = FIX2LONG(x) & FIX2LONG(y);
3224 return LONG2NUM(val);
3225 }
3226
3227 if (RB_TYPE_P(y, T_BIGNUM)) {
3228 return rb_big_and(y, x);
3229 }
3230
3231 bit_coerce(&x, &y, TRUE);
3232 return rb_funcall(x, rb_intern("&"), 1, y);
3233 }
3234
3235 /*
3236 * call-seq:
3237 * fix | integer -> integer_result
3238 *
3239 * Bitwise OR.
3240 */
3241
3242 static VALUE
3243 fix_or(VALUE x, VALUE y)
3244 {
3245 if (FIXNUM_P(y)) {
3246 long val = FIX2LONG(x) | FIX2LONG(y);
3247 return LONG2NUM(val);
3248 }
3249
3250 if (RB_TYPE_P(y, T_BIGNUM)) {
3251 return rb_big_or(y, x);
3252 }
3253
3254 bit_coerce(&x, &y, TRUE);
3255 return rb_funcall(x, rb_intern("|"), 1, y);
3256 }
3257
3258 /*
3259 * call-seq:
3260 * fix ^ integer -> integer_result
3261 *
3262 * Bitwise EXCLUSIVE OR.
3263 */
3264
3265 static VALUE
3266 fix_xor(VALUE x, VALUE y)
3267 {
3268 if (FIXNUM_P(y)) {
3269 long val = FIX2LONG(x) ^ FIX2LONG(y);
3270 return LONG2NUM(val);
3271 }
3272
3273 if (RB_TYPE_P(y, T_BIGNUM)) {
3274 return rb_big_xor(y, x);
3275 }
3276
3277 bit_coerce(&x, &y, TRUE);
3278 return rb_funcall(x, rb_intern("^"), 1, y);
3279 }
3280
3281 static VALUE fix_lshift(long, unsigned long);
3282 static VALUE fix_rshift(long, unsigned long);
3283
3284 /*
3285 * call-seq:
3286 * fix << count -> integer
3287 *
3288 * Shifts _fix_ left _count_ positions (right if _count_ is negative).
3289 */
3290
3291 static VALUE
3292 rb_fix_lshift(VALUE x, VALUE y)
3293 {
3294 long val, width;
3295
3296 val = NUM2LONG(x);
3297 if (!FIXNUM_P(y))
3298 return rb_big_lshift(rb_int2big(val), y);
3299 width = FIX2LONG(y);
3300 if (width < 0)
3301 return fix_rshift(val, (unsigned long)-width);
3302 return fix_lshift(val, width);
3303 }
3304
3305 static VALUE
3306 fix_lshift(long val, unsigned long width)
3307 {
3308 if (width > (SIZEOF_LONG*CHAR_BIT-1)
3309 || ((unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) {
3310 return rb_big_lshift(rb_int2big(val), ULONG2NUM(width));
3311 }
3312 val = val << width;
3313 return LONG2NUM(val);
3314 }
3315
3316 /*
3317 * call-seq:
3318 * fix >> count -> integer
3319 *
3320 * Shifts _fix_ right _count_ positions (left if _count_ is negative).
3321 */
3322
3323 static VALUE
3324 rb_fix_rshift(VALUE x, VALUE y)
3325 {
3326 long i, val;
3327
3328 val = FIX2LONG(x);
3329 if (!FIXNUM_P(y))
3330 return rb_big_rshift(rb_int2big(val), y);
3331 i = FIX2LONG(y);
3332 if (i == 0) return x;
3333 if (i < 0)
3334 return fix_lshift(val, (unsigned long)-i);
3335 return fix_rshift(val, i);
3336 }
3337
3338 static VALUE
3339 fix_rshift(long val, unsigned long i)
3340 {
3341 if (i >= sizeof(long)*CHAR_BIT-1) {
3342 if (val < 0) return INT2FIX(-1);
3343 return INT2FIX(0);
3344 }
3345 val = RSHIFT(val, i);
3346 return LONG2FIX(val);
3347 }
3348
3349 /*
3350 * call-seq:
3351 * fix[n] -> 0, 1
3352 *
3353 * Bit Reference---Returns the <em>n</em>th bit in the binary
3354 * representation of <i>fix</i>, where <i>fix</i>[0] is the least
3355 * significant bit.
3356 *
3357 * a = 0b11001100101010
3358 * 30.downto(0) do |n| print a[n] end
3359 *
3360 * <em>produces:</em>
3361 *
3362 * 0000000000000000011001100101010
3363 */
3364
3365 static VALUE
3366 fix_aref(VALUE fix, VALUE idx)
3367 {
3368 long val = FIX2LONG(fix);
3369 long i;
3370
3371 idx = rb_to_int(idx);
3372 if (!FIXNUM_P(idx)) {
3373 idx = rb_big_norm(idx);
3374 if (!FIXNUM_P(idx)) {
3375 if (!RBIGNUM_SIGN(idx) || val >= 0)
3376 return INT2FIX(0);
3377 return INT2FIX(1);
3378 }
3379 }
3380 i = FIX2LONG(idx);
3381
3382 if (i < 0) return INT2FIX(0);
3383 if (SIZEOF_LONG*CHAR_BIT-1 < i) {
3384 if (val < 0) return INT2FIX(1);
3385 return INT2FIX(0);
3386 }
3387 if (val & (1L<<i))
3388 return INT2FIX(1);
3389 return INT2FIX(0);
3390 }
3391
3392 /*
3393 * call-seq:
3394 * fix.to_f -> float
3395 *
3396 * Converts <i>fix</i> to a <code>Float</code>.
3397 *
3398 */
3399
3400 static VALUE
3401 fix_to_f(VALUE num)
3402 {
3403 double val;
3404
3405 val = (double)FIX2LONG(num);
3406
3407 return DBL2NUM(val);
3408 }
3409
3410 /*
3411 * call-seq:
3412 * fix.abs -> integer
3413 * fix.magnitude -> integer
3414 *
3415 * Returns the absolute value of <i>fix</i>.
3416 *
3417 * -12345.abs #=> 12345
3418 * 12345.abs #=> 12345
3419 *
3420 */
3421
3422 static VALUE
3423 fix_abs(VALUE fix)
3424 {
3425 long i = FIX2LONG(fix);
3426
3427 if (i < 0) i = -i;
3428
3429 return LONG2NUM(i);
3430 }
3431
3432
3433
3434 /*
3435 * call-seq:
3436 * fix.size -> fixnum
3437 *
3438 * Returns the number of <em>bytes</em> in the machine representation
3439 * of a <code>Fixnum</code>.
3440 *
3441 * 1.size #=> 4
3442 * -1.size #=> 4
3443 * 2147483647.size #=> 4
3444 */
3445
3446 static VALUE
3447 fix_size(VALUE fix)
3448 {
3449 return INT2FIX(sizeof(long));
3450 }
3451
3452 static VALUE
3453 int_upto_size(VALUE from, VALUE args)
3454 {
3455 return num_interval_step_size(from, RARRAY_PTR(args)[0], INT2FIX(1), FALSE);
3456 }
3457
3458 /*
3459 * call-seq:
3460 * int.upto(limit) {|i| block } -> self
3461 * int.upto(limit) -> an_enumerator
3462 *
3463 * Iterates <em>block</em>, passing in integer values from <i>int</i>
3464 * up to and including <i>limit</i>.
3465 *
3466 * If no block is given, an enumerator is returned instead.
3467 *
3468 * 5.upto(10) { |i| print i, " " }
3469 *
3470 * <em>produces:</em>
3471 *
3472 * 5 6 7 8 9 10
3473 */
3474
3475 static VALUE
3476 int_upto(VALUE from, VALUE to)
3477 {
3478 RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size);
3479 if (FIXNUM_P(from) && FIXNUM_P(to)) {
3480 long i, end;
3481
3482 end = FIX2LONG(to);
3483 for (i = FIX2LONG(from); i <= end; i++) {
3484 rb_yield(LONG2FIX(i));
3485 }
3486 }
3487 else {
3488 VALUE i = from, c;
3489
3490 while (!(c = rb_funcall(i, '>', 1, to))) {
3491 rb_yield(i);
3492 i = rb_funcall(i, '+', 1, INT2FIX(1));
3493 }
3494 if (NIL_P(c)) rb_cmperr(i, to);
3495 }
3496 return from;
3497 }
3498
3499 static VALUE
3500 int_downto_size(VALUE from, VALUE args)
3501 {
3502 return num_interval_step_size(from, RARRAY_PTR(args)[0], INT2FIX(-1), FALSE);
3503 }
3504
3505 /*
3506 * call-seq:
3507 * int.downto(limit) {|i| block } -> self
3508 * int.downto(limit) -> an_enumerator
3509 *
3510 * Iterates <em>block</em>, passing decreasing values from <i>int</i>
3511 * down to and including <i>limit</i>.
3512 *
3513 * If no block is given, an enumerator is returned instead.
3514 *
3515 * 5.downto(1) { |n| print n, ".. " }
3516 * print " Liftoff!\n"
3517 *
3518 * <em>produces:</em>
3519 *
3520 * 5.. 4.. 3.. 2.. 1.. Liftoff!
3521 */
3522
3523 static VALUE
3524 int_downto(VALUE from, VALUE to)
3525 {
3526 RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size);
3527 if (FIXNUM_P(from) && FIXNUM_P(to)) {
3528 long i, end;
3529
3530 end = FIX2LONG(to);
3531 for (i=FIX2LONG(from); i >= end; i--) {
3532 rb_yield(LONG2FIX(i));
3533 }
3534 }
3535 else {
3536 VALUE i = from, c;
3537
3538 while (!(c = rb_funcall(i, '<', 1, to))) {
3539 rb_yield(i);
3540 i = rb_funcall(i, '-', 1, INT2FIX(1));
3541 }
3542 if (NIL_P(c)) rb_cmperr(i, to);
3543 }
3544 return from;
3545 }
3546
3547 static VALUE
3548 int_dotimes_size(VALUE num)
3549 {
3550 if (FIXNUM_P(num)) {
3551 if (NUM2LONG(num) <= 0) return INT2FIX(0);
3552 }
3553 else {
3554 if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) return INT2FIX(0);
3555 }
3556 return num;
3557 }
3558
3559 /*
3560 * call-seq:
3561 * int.times {|i| block } -> self
3562 * int.times -> an_enumerator
3563 *
3564 * Iterates block <i>int</i> times, passing in values from zero to
3565 * <i>int</i> - 1.
3566 *
3567 * If no block is given, an enumerator is returned instead.
3568 *
3569 * 5.times do |i|
3570 * print i, " "
3571 * end
3572 *
3573 * <em>produces:</em>
3574 *
3575 * 0 1 2 3 4
3576 */
3577
3578 static VALUE
3579 int_dotimes(VALUE num)
3580 {
3581 RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size);
3582
3583 if (FIXNUM_P(num)) {
3584 long i, end;
3585
3586 end = FIX2LONG(num);
3587 for (i=0; i<end; i++) {
3588 rb_yield(LONG2FIX(i));
3589 }
3590 }
3591 else {
3592 VALUE i = INT2FIX(0);
3593
3594 for (;;) {
3595 if (!RTEST(rb_funcall(i, '<', 1, num))) break;
3596 rb_yield(i);
3597 i = rb_funcall(i, '+', 1, INT2FIX(1));
3598 }
3599 }
3600 return num;
3601 }
3602
3603 /*
3604 * call-seq:
3605 * int.round([ndigits]) -> integer or float
3606 *
3607 * Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits).
3608 * Precision may be negative. Returns a floating point number when +ndigits+
3609 * is positive, +self+ for zero, and round down for negative.
3610 *
3611 * 1.round #=> 1
3612 * 1.round(2) #=> 1.0
3613 * 15.round(-1) #=> 20
3614 */
3615
3616 static VALUE
3617 int_round(int argc, VALUE* argv, VALUE num)
3618 {
3619 VALUE n;
3620 int ndigits;
3621
3622 if (argc == 0) return num;
3623 rb_scan_args(argc, argv, "1", &n);
3624 ndigits = NUM2INT(n);
3625 if (ndigits > 0) {
3626 return rb_Float(num);
3627 }
3628 if (ndigits == 0) {
3629 return num;
3630 }
3631 return int_round_0(num, ndigits);
3632 }
3633
3634 /*
3635 * call-seq:
3636 * fix.zero? -> true or false
3637 *
3638 * Returns <code>true</code> if <i>fix</i> is zero.
3639 *
3640 */
3641
3642 static VALUE
3643 fix_zero_p(VALUE num)
3644 {
3645 if (FIX2LONG(num) == 0) {
3646 return Qtrue;
3647 }
3648 return Qfalse;
3649 }
3650
3651 /*
3652 * call-seq:
3653 * fix.odd? -> true or false
3654 *
3655 * Returns <code>true</code> if <i>fix</i> is an odd number.
3656 */
3657
3658 static VALUE
3659 fix_odd_p(VALUE num)
3660 {
3661 if (num & 2) {
3662 return Qtrue;
3663 }
3664 return Qfalse;
3665 }
3666
3667 /*
3668 * call-seq:
3669 * fix.even? -> true or false
3670 *
3671 * Returns <code>true</code> if <i>fix</i> is an even number.
3672 */
3673
3674 static VALUE
3675 fix_even_p(VALUE num)
3676 {
3677 if (num & 2) {
3678 return Qfalse;
3679 }
3680 return Qtrue;
3681 }
3682
3683 /*
3684 * Document-class: ZeroDivisionError
3685 *
3686 * Raised when attempting to divide an integer by 0.
3687 *
3688 * 42 / 0
3689 *
3690 * <em>raises the exception:</em>
3691 *
3692 * ZeroDivisionError: divided by 0
3693 *
3694 * Note that only division by an exact 0 will raise that exception:
3695 *
3696 * 42 / 0.0 #=> Float::INFINITY
3697 * 42 / -0.0 #=> -Float::INFINITY
3698 * 0 / 0.0 #=> NaN
3699 */
3700
3701 /*
3702 * Document-class: FloatDomainError
3703 *
3704 * Raised when attempting to convert special float values
3705 * (in particular infinite or NaN)
3706 * to numerical classes which don't support them.
3707 *
3708 * Float::INFINITY.to_r
3709 *
3710 * <em>raises the exception:</em>
3711 *
3712 * FloatDomainError: Infinity
3713 */
3714
3715 void
3716 Init_Numeric(void)
3717 {
3718 #undef rb_intern
3719 #define rb_intern(str) rb_intern_const(str)
3720
3721 #if defined(__FreeBSD__) && __FreeBSD__ < 4
3722 /* allow divide by zero -- Inf */
3723 fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL));
3724 #elif defined(_UNICOSMP)
3725 /* Turn off floating point exceptions for divide by zero, etc. */
3726 _set_Creg(0, 0);
3727 #elif defined(__BORLANDC__)
3728 /* Turn off floating point exceptions for overflow, etc. */
3729 _control87(MCW_EM, MCW_EM);
3730 _control87(_control87(0,0),0x1FFF);
3731 #endif
3732 id_coerce = rb_intern("coerce");
3733 id_to_i = rb_intern("to_i");
3734 id_eq = rb_intern("==");
3735 id_div = rb_intern("div");
3736
3737 rb_eZeroDivError = rb_define_class("ZeroDivisionError", rb_eStandardError);
3738 rb_eFloatDomainError = rb_define_class("FloatDomainError", rb_eRangeError);
3739 rb_cNumeric = rb_define_class("Numeric", rb_cObject);
3740
3741 rb_define_method(rb_cNumeric, "singleton_method_added", num_sadded, 1);
3742 rb_include_module(rb_cNumeric, rb_mComparable);
3743 rb_define_method(rb_cNumeric, "initialize_copy", num_init_copy, 1);
3744 rb_define_method(rb_cNumeric, "coerce", num_coerce, 1);
3745
3746 rb_define_method(rb_cNumeric, "i", num_imaginary, 0);
3747 rb_define_method(rb_cNumeric, "+@", num_uplus, 0);
3748 rb_define_method(rb_cNumeric, "-@", num_uminus, 0);
3749 rb_define_method(rb_cNumeric, "<=>", num_cmp, 1);
3750 rb_define_method(rb_cNumeric, "eql?", num_eql, 1);
3751 rb_define_method(rb_cNumeric, "quo", num_quo, 1);
3752 rb_define_method(rb_cNumeric, "fdiv", num_fdiv, 1);
3753 rb_define_method(rb_cNumeric, "div", num_div, 1);
3754 rb_define_method(rb_cNumeric, "divmod", num_divmod, 1);
3755 rb_define_method(rb_cNumeric, "%", num_modulo, 1);
3756 rb_define_method(rb_cNumeric, "modulo", num_modulo, 1);
3757 rb_define_method(rb_cNumeric, "remainder", num_remainder, 1);
3758 rb_define_method(rb_cNumeric, "abs", num_abs, 0);
3759 rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
3760 rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
3761
3762 rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
3763 rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
3764 rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
3765 rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
3766
3767 rb_define_method(rb_cNumeric, "floor", num_floor, 0);
3768 rb_define_method(rb_cNumeric, "ceil", num_ceil, 0);
3769 rb_define_method(rb_cNumeric, "round", num_round, -1);
3770 rb_define_method(rb_cNumeric, "truncate", num_truncate, 0);
3771 rb_define_method(rb_cNumeric, "step", num_step, -1);
3772
3773 rb_cInteger = rb_define_class("Integer", rb_cNumeric);
3774 rb_undef_alloc_func(rb_cInteger);
3775 rb_undef_method(CLASS_OF(rb_cInteger), "new");
3776
3777 rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
3778 rb_define_method(rb_cInteger, "odd?", int_odd_p, 0);
3779 rb_define_method(rb_cInteger, "even?", int_even_p, 0);
3780 rb_define_method(rb_cInteger, "upto", int_upto, 1);
3781 rb_define_method(rb_cInteger, "downto", int_downto, 1);
3782 rb_define_method(rb_cInteger, "times", int_dotimes, 0);
3783 rb_define_method(rb_cInteger, "succ", int_succ, 0);
3784 rb_define_method(rb_cInteger, "next", int_succ, 0);
3785 rb_define_method(rb_cInteger, "pred", int_pred, 0);
3786 rb_define_method(rb_cInteger, "chr", int_chr, -1);
3787 rb_define_method(rb_cInteger, "ord", int_ord, 0);
3788 rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
3789 rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
3790 rb_define_method(rb_cInteger, "floor", int_to_i, 0);
3791 rb_define_method(rb_cInteger, "ceil", int_to_i, 0);
3792 rb_define_method(rb_cInteger, "truncate", int_to_i, 0);
3793 rb_define_method(rb_cInteger, "round", int_round, -1);
3794
3795 rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
3796
3797 rb_define_method(rb_cFixnum, "to_s", fix_to_s, -1);
3798 rb_define_alias(rb_cFixnum, "inspect", "to_s");
3799
3800 rb_define_method(rb_cFixnum, "-@", fix_uminus, 0);
3801 rb_define_method(rb_cFixnum, "+", fix_plus, 1);
3802 rb_define_method(rb_cFixnum, "-", fix_minus, 1);
3803 rb_define_method(rb_cFixnum, "*", fix_mul, 1);
3804 rb_define_method(rb_cFixnum, "/", fix_div, 1);
3805 rb_define_method(rb_cFixnum, "div", fix_idiv, 1);
3806 rb_define_method(rb_cFixnum, "%", fix_mod, 1);
3807 rb_define_method(rb_cFixnum, "modulo", fix_mod, 1);
3808 rb_define_method(rb_cFixnum, "divmod", fix_divmod, 1);
3809 rb_define_method(rb_cFixnum, "fdiv", fix_fdiv, 1);
3810 rb_define_method(rb_cFixnum, "**", fix_pow, 1);
3811
3812 rb_define_method(rb_cFixnum, "abs", fix_abs, 0);
3813 rb_define_method(rb_cFixnum, "magnitude", fix_abs, 0);
3814
3815 rb_define_method(rb_cFixnum, "==", fix_equal, 1);
3816 rb_define_method(rb_cFixnum, "===", fix_equal, 1);
3817 rb_define_method(rb_cFixnum, "<=>", fix_cmp, 1);
3818 rb_define_method(rb_cFixnum, ">", fix_gt, 1);
3819 rb_define_method(rb_cFixnum, ">=", fix_ge, 1);
3820 rb_define_method(rb_cFixnum, "<", fix_lt, 1);
3821 rb_define_method(rb_cFixnum, "<=", fix_le, 1);
3822
3823 rb_define_method(rb_cFixnum, "~", fix_rev, 0);
3824 rb_define_method(rb_cFixnum, "&", fix_and, 1);
3825 rb_define_method(rb_cFixnum, "|", fix_or, 1);
3826 rb_define_method(rb_cFixnum, "^", fix_xor, 1);
3827 rb_define_method(rb_cFixnum, "[]", fix_aref, 1);
3828
3829 rb_define_method(rb_cFixnum, "<<", rb_fix_lshift, 1);
3830 rb_define_method(rb_cFixnum, ">>", rb_fix_rshift, 1);
3831
3832 rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0);
3833 rb_define_method(rb_cFixnum, "size", fix_size, 0);
3834 rb_define_method(rb_cFixnum, "zero?", fix_zero_p, 0);
3835 rb_define_method(rb_cFixnum, "odd?", fix_odd_p, 0);
3836 rb_define_method(rb_cFixnum, "even?", fix_even_p, 0);
3837 rb_define_method(rb_cFixnum, "succ", fix_succ, 0);
3838
3839 rb_cFloat = rb_define_class("Float", rb_cNumeric);
3840
3841 rb_undef_alloc_func(rb_cFloat);
3842 rb_undef_method(CLASS_OF(rb_cFloat), "new");
3843
3844 /*
3845 * Represents the rounding mode for floating point addition.
3846 *
3847 * Usually defaults to 1, rounding to the nearest number.
3848 *
3849 * Other modes include:
3850 *
3851 * -1:: Indeterminable
3852 * 0:: Rounding towards zero
3853 * 1:: Rounding to the nearest number
3854 * 2:: Rounding towards positive infinity
3855 * 3:: Rounding towards negative infinity
3856 */
3857 rb_define_const(rb_cFloat, "ROUNDS", INT2FIX(FLT_ROUNDS));
3858 /*
3859 * The base of the floating point, or number of unique digits used to
3860 * represent the number.
3861 *
3862 * Usually defaults to 2 on most systems, which would represent a base-10 decimal.
3863 */
3864 rb_define_const(rb_cFloat, "RADIX", INT2FIX(FLT_RADIX));
3865 /*
3866 * The number of base digits for the +double+ data type.
3867 *
3868 * Usually defaults to 53.
3869 */
3870 rb_define_const(rb_cFloat, "MANT_DIG", INT2FIX(DBL_MANT_DIG));
3871 /*
3872 * The number of decimal digits in a double-precision floating point.
3873 *
3874 * Usually defaults to 15.
3875 */
3876 rb_define_const(rb_cFloat, "DIG", INT2FIX(DBL_DIG));
3877 /*
3878 * The smallest posable exponent value in a double-precision floating
3879 * point.
3880 *
3881 * Usually defaults to -1021.
3882 */
3883 rb_define_const(rb_cFloat, "MIN_EXP", INT2FIX(DBL_MIN_EXP));
3884 /*
3885 * The largest possible exponent value in a double-precision floating
3886 * point.
3887 *
3888 * Usually defaults to 1024.
3889 */
3890 rb_define_const(rb_cFloat, "MAX_EXP", INT2FIX(DBL_MAX_EXP));
3891 /*
3892 * The smallest negative exponent in a double-precision floating point
3893 * where 10 raised to this power minus 1.
3894 *
3895 * Usually defaults to -307.
3896 */
3897 rb_define_const(rb_cFloat, "MIN_10_EXP", INT2FIX(DBL_MIN_10_EXP));
3898 /*
3899 * The largest positive exponent in a double-precision floating point where
3900 * 10 raised to this power minus 1.
3901 *
3902 * Usually defaults to 308.
3903 */
3904 rb_define_const(rb_cFloat, "MAX_10_EXP", INT2FIX(DBL_MAX_10_EXP));
3905 /*
3906 * The smallest positive integer in a double-precision floating point.
3907 *
3908 * Usually defaults to 2.2250738585072014e-308.
3909 */
3910 rb_define_const(rb_cFloat, "MIN", DBL2NUM(DBL_MIN));
3911 /*
3912 * The largest possible integer in a double-precision floating point number.
3913 *
3914 * Usually defaults to 1.7976931348623157e+308.
3915 */
3916 rb_define_const(rb_cFloat, "MAX", DBL2NUM(DBL_MAX));
3917 /*
3918 * The difference between 1 and the smallest double-precision floating
3919 * point number.
3920 *
3921 * Usually defaults to 2.2204460492503131e-16.
3922 */
3923 rb_define_const(rb_cFloat, "EPSILON", DBL2NUM(DBL_EPSILON));
3924 /*
3925 * An expression representing positive infinity.
3926 */
3927 rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(INFINITY));
3928 /*
3929 * An expression representing a value which is "not a number".
3930 */
3931 rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
3932
3933 rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
3934 rb_define_alias(rb_cFloat, "inspect", "to_s");
3935 rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
3936 rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
3937 rb_define_method(rb_cFloat, "+", flo_plus, 1);
3938 rb_define_method(rb_cFloat, "-", flo_minus, 1);
3939 rb_define_method(rb_cFloat, "*", flo_mul, 1);
3940 rb_define_method(rb_cFloat, "/", flo_div, 1);
3941 rb_define_method(rb_cFloat, "quo", flo_quo, 1);
3942 rb_define_method(rb_cFloat, "fdiv", flo_quo, 1);
3943 rb_define_method(rb_cFloat, "%", flo_mod, 1);
3944 rb_define_method(rb_cFloat, "modulo", flo_mod, 1);
3945 rb_define_method(rb_cFloat, "divmod", flo_divmod, 1);
3946 rb_define_method(rb_cFloat, "**", flo_pow, 1);
3947 rb_define_method(rb_cFloat, "==", flo_eq, 1);
3948 rb_define_method(rb_cFloat, "===", flo_eq, 1);
3949 rb_define_method(rb_cFloat, "<=>", flo_cmp, 1);
3950 rb_define_method(rb_cFloat, ">", flo_gt, 1);
3951 rb_define_method(rb_cFloat, ">=", flo_ge, 1);
3952 rb_define_method(rb_cFloat, "<", flo_lt, 1);
3953 rb_define_method(rb_cFloat, "<=", flo_le, 1);
3954 rb_define_method(rb_cFloat, "eql?", flo_eql, 1);
3955 rb_define_method(rb_cFloat, "hash", flo_hash, 0);
3956 rb_define_method(rb_cFloat, "to_f", flo_to_f, 0);
3957 rb_define_method(rb_cFloat, "abs", flo_abs, 0);
3958 rb_define_method(rb_cFloat, "magnitude", flo_abs, 0);
3959 rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0);
3960
3961 rb_define_method(rb_cFloat, "to_i", flo_truncate, 0);
3962 rb_define_method(rb_cFloat, "to_int", flo_truncate, 0);
3963 rb_define_method(rb_cFloat, "floor", flo_floor, 0);
3964 rb_define_method(rb_cFloat, "ceil", flo_ceil, 0);
3965 rb_define_method(rb_cFloat, "round", flo_round, -1);
3966 rb_define_method(rb_cFloat, "truncate", flo_truncate, 0);
3967
3968 rb_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0);
3969 rb_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0);
3970 rb_define_method(rb_cFloat, "finite?", flo_is_finite_p, 0);
3971 }
3972