Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
numeric.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  numeric.c -
4 
5  $Author$
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/encoding.h"
13 #include "ruby/util.h"
14 #include "internal.h"
15 #include "id.h"
16 #include <assert.h>
17 #include <ctype.h>
18 #include <math.h>
19 #include <stdio.h>
20 
21 #ifdef HAVE_FLOAT_H
22 #include <float.h>
23 #endif
24 
25 #ifdef HAVE_IEEEFP_H
26 #include <ieeefp.h>
27 #endif
28 
29 /* use IEEE 64bit values if not defined */
30 #ifndef FLT_RADIX
31 #define FLT_RADIX 2
32 #endif
33 #ifndef FLT_ROUNDS
34 #define FLT_ROUNDS 1
35 #endif
36 #ifndef DBL_MIN
37 #define DBL_MIN 2.2250738585072014e-308
38 #endif
39 #ifndef DBL_MAX
40 #define DBL_MAX 1.7976931348623157e+308
41 #endif
42 #ifndef DBL_MIN_EXP
43 #define DBL_MIN_EXP (-1021)
44 #endif
45 #ifndef DBL_MAX_EXP
46 #define DBL_MAX_EXP 1024
47 #endif
48 #ifndef DBL_MIN_10_EXP
49 #define DBL_MIN_10_EXP (-307)
50 #endif
51 #ifndef DBL_MAX_10_EXP
52 #define DBL_MAX_10_EXP 308
53 #endif
54 #ifndef DBL_DIG
55 #define DBL_DIG 15
56 #endif
57 #ifndef DBL_MANT_DIG
58 #define DBL_MANT_DIG 53
59 #endif
60 #ifndef DBL_EPSILON
61 #define DBL_EPSILON 2.2204460492503131e-16
62 #endif
63 
64 #ifndef USE_RB_INFINITY
65 #elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
66 const union bytesequence4_or_float rb_infinity = {{0x00, 0x00, 0x80, 0x7f}};
67 #else
68 const union bytesequence4_or_float rb_infinity = {{0x7f, 0x80, 0x00, 0x00}};
69 #endif
70 
71 #ifndef USE_RB_NAN
72 #elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
73 const union bytesequence4_or_float rb_nan = {{0x00, 0x00, 0xc0, 0x7f}};
74 #else
75 const union bytesequence4_or_float rb_nan = {{0x7f, 0xc0, 0x00, 0x00}};
76 #endif
77 
78 #ifndef HAVE_ROUND
79 double
80 round(double x)
81 {
82  double f;
83 
84  if (x > 0.0) {
85  f = floor(x);
86  x = f + (x - f >= 0.5);
87  }
88  else if (x < 0.0) {
89  f = ceil(x);
90  x = f - (f - x >= 0.5);
91  }
92  return x;
93 }
94 #endif
95 
96 static double
97 round_half_up(double x, double s)
98 {
99  double f, xs = x * s;
100 
101  f = round(xs);
102  if (s == 1.0) return f;
103  if (x > 0) {
104  if ((double)((f + 0.5) / s) <= x) f += 1;
105  x = f;
106  }
107  else {
108  if ((double)((f - 0.5) / s) >= x) f -= 1;
109  x = f;
110  }
111  return x;
112 }
113 
114 static double
115 round_half_down(double x, double s)
116 {
117  double f, xs = x * s;
118 
119  f = round(xs);
120  if (x > 0) {
121  if ((double)((f - 0.5) / s) >= x) f -= 1;
122  x = f;
123  }
124  else {
125  if ((double)((f + 0.5) / s) <= x) f += 1;
126  x = f;
127  }
128  return x;
129 }
130 
131 static double
132 round_half_even(double x, double s)
133 {
134  double f, d, xs = x * s;
135 
136  if (x > 0.0) {
137  f = floor(xs);
138  d = xs - f;
139  if (d > 0.5)
140  d = 1.0;
141  else if (d == 0.5 || ((double)((f + 0.5) / s) <= x))
142  d = fmod(f, 2.0);
143  else
144  d = 0.0;
145  x = f + d;
146  }
147  else if (x < 0.0) {
148  f = ceil(xs);
149  d = f - xs;
150  if (d > 0.5)
151  d = 1.0;
152  else if (d == 0.5 || ((double)((f - 0.5) / s) >= x))
153  d = fmod(-f, 2.0);
154  else
155  d = 0.0;
156  x = f - d;
157  }
158  return x;
159 }
160 
161 static VALUE fix_uminus(VALUE num);
162 static VALUE fix_mul(VALUE x, VALUE y);
163 static VALUE fix_lshift(long, unsigned long);
164 static VALUE fix_rshift(long, unsigned long);
165 static VALUE int_pow(long x, unsigned long y);
166 static VALUE int_even_p(VALUE x);
167 static int int_round_zero_p(VALUE num, int ndigits);
168 VALUE rb_int_floor(VALUE num, int ndigits);
169 VALUE rb_int_ceil(VALUE num, int ndigits);
170 static VALUE flo_to_i(VALUE num);
171 static int float_round_overflow(int ndigits, int binexp);
172 static int float_round_underflow(int ndigits, int binexp);
173 
174 static ID id_coerce;
175 #define id_div idDiv
176 #define id_divmod idDivmod
177 #define id_to_i idTo_i
178 #define id_eq idEq
179 #define id_cmp idCmp
180 
184 #ifndef RUBY_INTEGER_UNIFICATION
186 #endif
187 
190 
191 static ID id_to, id_by;
192 
193 void
195 {
196  rb_raise(rb_eZeroDivError, "divided by 0");
197 }
198 
201 {
202  static ID round_kwds[1];
203  VALUE rounding;
204  VALUE str;
205  const char *s;
206 
207  if (!NIL_P(opts)) {
208  if (!round_kwds[0]) {
209  round_kwds[0] = rb_intern_const("half");
210  }
211  if (!rb_get_kwargs(opts, round_kwds, 0, 1, &rounding)) goto noopt;
212  if (SYMBOL_P(rounding)) {
213  str = rb_sym2str(rounding);
214  }
215  else if (NIL_P(rounding)) {
216  goto noopt;
217  }
218  else if (!RB_TYPE_P(str = rounding, T_STRING)) {
219  str = rb_check_string_type(rounding);
220  if (NIL_P(str)) goto invalid;
221  }
222  s = RSTRING_PTR(str);
223  switch (RSTRING_LEN(str)) {
224  case 2:
225  if (rb_memcicmp(s, "up", 2) == 0)
226  return RUBY_NUM_ROUND_HALF_UP;
227  break;
228  case 4:
229  if (rb_memcicmp(s, "even", 4) == 0)
231  if (strncasecmp(s, "down", 4) == 0)
233  break;
234  }
235  invalid:
236  rb_raise(rb_eArgError, "invalid rounding mode: % "PRIsVALUE, rounding);
237  }
238  noopt:
239  return RUBY_NUM_ROUND_DEFAULT;
240 }
241 
242 /* experimental API */
243 int
244 rb_num_to_uint(VALUE val, unsigned int *ret)
245 {
246 #define NUMERR_TYPE 1
247 #define NUMERR_NEGATIVE 2
248 #define NUMERR_TOOLARGE 3
249  if (FIXNUM_P(val)) {
250  long v = FIX2LONG(val);
251 #if SIZEOF_INT < SIZEOF_LONG
252  if (v > (long)UINT_MAX) return NUMERR_TOOLARGE;
253 #endif
254  if (v < 0) return NUMERR_NEGATIVE;
255  *ret = (unsigned int)v;
256  return 0;
257  }
258 
259  if (RB_TYPE_P(val, T_BIGNUM)) {
260  if (BIGNUM_NEGATIVE_P(val)) return NUMERR_NEGATIVE;
261 #if SIZEOF_INT < SIZEOF_LONG
262  /* long is 64bit */
263  return NUMERR_TOOLARGE;
264 #else
265  /* long is 32bit */
266  if (rb_absint_size(val, NULL) > sizeof(int)) return NUMERR_TOOLARGE;
267  *ret = (unsigned int)rb_big2ulong((VALUE)val);
268  return 0;
269 #endif
270  }
271  return NUMERR_TYPE;
272 }
273 
274 #define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
275 
276 static inline int
277 int_pos_p(VALUE num)
278 {
279  if (FIXNUM_P(num)) {
280  return FIXNUM_POSITIVE_P(num);
281  }
282  else if (RB_TYPE_P(num, T_BIGNUM)) {
283  return BIGNUM_POSITIVE_P(num);
284  }
285  rb_raise(rb_eTypeError, "not an Integer");
286 }
287 
288 static inline int
289 int_neg_p(VALUE num)
290 {
291  if (FIXNUM_P(num)) {
292  return FIXNUM_NEGATIVE_P(num);
293  }
294  else if (RB_TYPE_P(num, T_BIGNUM)) {
295  return BIGNUM_NEGATIVE_P(num);
296  }
297  rb_raise(rb_eTypeError, "not an Integer");
298 }
299 
300 int
302 {
303  return int_pos_p(num);
304 }
305 
306 int
308 {
309  return int_neg_p(num);
310 }
311 
312 int
314 {
315  return rb_num_negative_int_p(num);
316 }
317 
318 static VALUE
319 num_funcall_op_0(VALUE x, VALUE arg, int recursive)
320 {
321  ID func = (ID)arg;
322  if (recursive) {
323  const char *name = rb_id2name(func);
324  if (ISALNUM(name[0])) {
325  rb_name_error(func, "%"PRIsVALUE".%"PRIsVALUE,
326  x, ID2SYM(func));
327  }
328  else if (name[0] && name[1] == '@' && !name[2]) {
329  rb_name_error(func, "%c%"PRIsVALUE,
330  name[0], x);
331  }
332  else {
333  rb_name_error(func, "%"PRIsVALUE"%"PRIsVALUE,
334  ID2SYM(func), x);
335  }
336  }
337  return rb_funcallv(x, func, 0, 0);
338 }
339 
340 static VALUE
341 num_funcall0(VALUE x, ID func)
342 {
343  return rb_exec_recursive(num_funcall_op_0, x, (VALUE)func);
344 }
345 
346 NORETURN(static void num_funcall_op_1_recursion(VALUE x, ID func, VALUE y));
347 
348 static void
349 num_funcall_op_1_recursion(VALUE x, ID func, VALUE y)
350 {
351  const char *name = rb_id2name(func);
352  if (ISALNUM(name[0])) {
353  rb_name_error(func, "%"PRIsVALUE".%"PRIsVALUE"(%"PRIsVALUE")",
354  x, ID2SYM(func), y);
355  }
356  else {
358  x, ID2SYM(func), y);
359  }
360 }
361 
362 static VALUE
363 num_funcall_op_1(VALUE y, VALUE arg, int recursive)
364 {
365  ID func = (ID)((VALUE *)arg)[0];
366  VALUE x = ((VALUE *)arg)[1];
367  if (recursive) {
368  num_funcall_op_1_recursion(x, func, y);
369  }
370  return rb_funcall(x, func, 1, y);
371 }
372 
373 static VALUE
374 num_funcall1(VALUE x, ID func, VALUE y)
375 {
376  VALUE args[2];
377  args[0] = (VALUE)func;
378  args[1] = x;
379  return rb_exec_recursive_paired(num_funcall_op_1, y, x, (VALUE)args);
380 }
381 
382 /*
383  * call-seq:
384  * num.coerce(numeric) -> array
385  *
386  * If +numeric+ is the same type as +num+, returns an array
387  * <code>[numeric, num]</code>. Otherwise, returns an array with both
388  * +numeric+ and +num+ represented as Float objects.
389  *
390  * This coercion mechanism is used by Ruby to handle mixed-type numeric
391  * operations: it is intended to find a compatible common type between the two
392  * operands of the operator.
393  *
394  * 1.coerce(2.5) #=> [2.5, 1.0]
395  * 1.2.coerce(3) #=> [3.0, 1.2]
396  * 1.coerce(2) #=> [2, 1]
397  */
398 
399 static VALUE
400 num_coerce(VALUE x, VALUE y)
401 {
402  if (CLASS_OF(x) == CLASS_OF(y))
403  return rb_assoc_new(y, x);
404  x = rb_Float(x);
405  y = rb_Float(y);
406  return rb_assoc_new(y, x);
407 }
408 
409 NORETURN(static void coerce_failed(VALUE x, VALUE y));
410 static void
411 coerce_failed(VALUE x, VALUE y)
412 {
413  if (SPECIAL_CONST_P(y) || SYMBOL_P(y) || RB_FLOAT_TYPE_P(y)) {
414  y = rb_inspect(y);
415  }
416  else {
417  y = rb_obj_class(y);
418  }
419  rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
420  y, rb_obj_class(x));
421 }
422 
423 static int
424 do_coerce(VALUE *x, VALUE *y, int err)
425 {
426  VALUE ary = rb_check_funcall(*y, id_coerce, 1, x);
427  if (ary == Qundef) {
428  if (err) {
429  coerce_failed(*x, *y);
430  }
431  return FALSE;
432  }
433  if (!err && NIL_P(ary)) {
434  return FALSE;
435  }
436  if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) {
437  rb_raise(rb_eTypeError, "coerce must return [x, y]");
438  }
439 
440  *x = RARRAY_AREF(ary, 0);
441  *y = RARRAY_AREF(ary, 1);
442  return TRUE;
443 }
444 
445 VALUE
447 {
448  do_coerce(&x, &y, TRUE);
449  return rb_funcall(x, func, 1, y);
450 }
451 
452 VALUE
454 {
455  if (do_coerce(&x, &y, FALSE))
456  return rb_funcall(x, func, 1, y);
457  return Qnil;
458 }
459 
460 VALUE
462 {
463  VALUE c, x0 = x, y0 = y;
464 
465  if (!do_coerce(&x, &y, FALSE) ||
466  NIL_P(c = rb_funcall(x, func, 1, y))) {
467  rb_cmperr(x0, y0);
468  return Qnil; /* not reached */
469  }
470  return c;
471 }
472 
473 /*
474  * :nodoc:
475  *
476  * Trap attempts to add methods to Numeric objects. Always raises a TypeError.
477  *
478  * Numerics should be values; singleton_methods should not be added to them.
479  */
480 
481 static VALUE
482 num_sadded(VALUE x, VALUE name)
483 {
484  ID mid = rb_to_id(name);
485  /* ruby_frame = ruby_frame->prev; */ /* pop frame for "singleton_method_added" */
488  "can't define singleton method \"%"PRIsVALUE"\" for %"PRIsVALUE,
489  rb_id2str(mid),
490  rb_obj_class(x));
491 
493 }
494 
495 #if 0
496 /*
497  * call-seq:
498  * num.clone(freeze: true) -> num
499  *
500  * Returns the receiver. +freeze+ cannot be +false+.
501  */
502 static VALUE
503 num_clone(int argc, VALUE *argv, VALUE x)
504 {
505  return rb_immutable_obj_clone(argc, argv, x);
506 }
507 #else
508 # define num_clone rb_immutable_obj_clone
509 #endif
510 
511 #if 0
512 /*
513  * call-seq:
514  * num.dup -> num
515  *
516  * Returns the receiver.
517  */
518 static VALUE
519 num_dup(VALUE x)
520 {
521  return x;
522 }
523 #else
524 # define num_dup num_uplus
525 #endif
526 
527 /*
528  * call-seq:
529  * +num -> num
530  *
531  * Unary Plus---Returns the receiver.
532  */
533 
534 static VALUE
535 num_uplus(VALUE num)
536 {
537  return num;
538 }
539 
540 /*
541  * call-seq:
542  * num.i -> Complex(0, num)
543  *
544  * Returns the corresponding imaginary number.
545  * Not available for complex numbers.
546  *
547  * -42.i #=> (0-42i)
548  * 2.0.i #=> (0+2.0i)
549  */
550 
551 static VALUE
552 num_imaginary(VALUE num)
553 {
554  return rb_complex_new(INT2FIX(0), num);
555 }
556 
557 /*
558  * call-seq:
559  * -num -> numeric
560  *
561  * Unary Minus---Returns the receiver, negated.
562  */
563 
564 static VALUE
565 num_uminus(VALUE num)
566 {
567  VALUE zero;
568 
569  zero = INT2FIX(0);
570  do_coerce(&zero, &num, TRUE);
571 
572  return num_funcall1(zero, '-', num);
573 }
574 
575 /*
576  * call-seq:
577  * num.fdiv(numeric) -> float
578  *
579  * Returns float division.
580  */
581 
582 static VALUE
583 num_fdiv(VALUE x, VALUE y)
584 {
585  return rb_funcall(rb_Float(x), '/', 1, y);
586 }
587 
588 /*
589  * call-seq:
590  * num.div(numeric) -> integer
591  *
592  * Uses +/+ to perform division, then converts the result to an integer.
593  * Numeric does not define the +/+ operator; this is left to subclasses.
594  *
595  * Equivalent to <code>num.divmod(numeric)[0]</code>.
596  *
597  * See Numeric#divmod.
598  */
599 
600 static VALUE
601 num_div(VALUE x, VALUE y)
602 {
603  if (rb_equal(INT2FIX(0), y)) rb_num_zerodiv();
604  return rb_funcall(num_funcall1(x, '/', y), rb_intern("floor"), 0);
605 }
606 
607 /*
608  * call-seq:
609  * num.modulo(numeric) -> real
610  *
611  * <code>x.modulo(y)</code> means <code>x-y*(x/y).floor</code>.
612  *
613  * Equivalent to <code>num.divmod(numeric)[1]</code>.
614  *
615  * See Numeric#divmod.
616  */
617 
618 static VALUE
619 num_modulo(VALUE x, VALUE y)
620 {
621  VALUE q = num_funcall1(x, id_div, y);
622  return rb_funcall(x, '-', 1,
623  rb_funcall(y, '*', 1, q));
624 }
625 
626 /*
627  * call-seq:
628  * num.remainder(numeric) -> real
629  *
630  * <code>x.remainder(y)</code> means <code>x-y*(x/y).truncate</code>.
631  *
632  * See Numeric#divmod.
633  */
634 
635 static VALUE
636 num_remainder(VALUE x, VALUE y)
637 {
638  VALUE z = num_funcall1(x, '%', y);
639 
640  if ((!rb_equal(z, INT2FIX(0))) &&
641  ((rb_num_negative_int_p(x) &&
642  rb_num_positive_int_p(y)) ||
643  (rb_num_positive_int_p(x) &&
644  rb_num_negative_int_p(y)))) {
645  return rb_funcall(z, '-', 1, y);
646  }
647  return z;
648 }
649 
650 /*
651  * call-seq:
652  * num.divmod(numeric) -> array
653  *
654  * Returns an array containing the quotient and modulus obtained by dividing
655  * +num+ by +numeric+.
656  *
657  * If <code>q, r = x.divmod(y)</code>, then
658  *
659  * q = floor(x/y)
660  * x = q*y + r
661  *
662  * The quotient is rounded toward negative infinity, as shown in the
663  * following table:
664  *
665  * a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b)
666  * ------+-----+---------------+---------+-------------+---------------
667  * 13 | 4 | 3, 1 | 3 | 1 | 1
668  * ------+-----+---------------+---------+-------------+---------------
669  * 13 | -4 | -4, -3 | -4 | -3 | 1
670  * ------+-----+---------------+---------+-------------+---------------
671  * -13 | 4 | -4, 3 | -4 | 3 | -1
672  * ------+-----+---------------+---------+-------------+---------------
673  * -13 | -4 | 3, -1 | 3 | -1 | -1
674  * ------+-----+---------------+---------+-------------+---------------
675  * 11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5
676  * ------+-----+---------------+---------+-------------+---------------
677  * 11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5
678  * ------+-----+---------------+---------+-------------+---------------
679  * -11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5
680  * ------+-----+---------------+---------+-------------+---------------
681  * -11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5
682  *
683  *
684  * Examples
685  *
686  * 11.divmod(3) #=> [3, 2]
687  * 11.divmod(-3) #=> [-4, -1]
688  * 11.divmod(3.5) #=> [3, 0.5]
689  * (-11).divmod(3.5) #=> [-4, 3.0]
690  * 11.5.divmod(3.5) #=> [3, 1.0]
691  */
692 
693 static VALUE
694 num_divmod(VALUE x, VALUE y)
695 {
696  return rb_assoc_new(num_div(x, y), num_modulo(x, y));
697 }
698 
699 /*
700  * call-seq:
701  * num.real? -> true or false
702  *
703  * Returns +true+ if +num+ is a real number (i.e. not Complex).
704  */
705 
706 static VALUE
707 num_real_p(VALUE num)
708 {
709  return Qtrue;
710 }
711 
712 /*
713  * call-seq:
714  * num.integer? -> true or false
715  *
716  * Returns +true+ if +num+ is an Integer.
717  *
718  * 1.0.integer? #=> false
719  * 1.integer? #=> true
720  */
721 
722 static VALUE
723 num_int_p(VALUE num)
724 {
725  return Qfalse;
726 }
727 
728 /*
729  * call-seq:
730  * num.abs -> numeric
731  * num.magnitude -> numeric
732  *
733  * Returns the absolute value of +num+.
734  *
735  * 12.abs #=> 12
736  * (-34.56).abs #=> 34.56
737  * -34.56.abs #=> 34.56
738  *
739  * Numeric#magnitude is an alias for Numeric#abs.
740  */
741 
742 static VALUE
743 num_abs(VALUE num)
744 {
745  if (rb_num_negative_int_p(num)) {
746  return num_funcall0(num, idUMinus);
747  }
748  return num;
749 }
750 
751 /*
752  * call-seq:
753  * num.zero? -> true or false
754  *
755  * Returns +true+ if +num+ has a zero value.
756  */
757 
758 static VALUE
759 num_zero_p(VALUE num)
760 {
761  if (FIXNUM_P(num)) {
762  if (FIXNUM_ZERO_P(num)) {
763  return Qtrue;
764  }
765  }
766  else if (RB_TYPE_P(num, T_BIGNUM)) {
767  if (rb_bigzero_p(num)) {
768  /* this should not happen usually */
769  return Qtrue;
770  }
771  }
772  else if (rb_equal(num, INT2FIX(0))) {
773  return Qtrue;
774  }
775  return Qfalse;
776 }
777 
778 /*
779  * call-seq:
780  * num.nonzero? -> self or nil
781  *
782  * Returns +self+ if +num+ is not zero, +nil+ otherwise.
783  *
784  * This behavior is useful when chaining comparisons:
785  *
786  * a = %w( z Bb bB bb BB a aA Aa AA A )
787  * b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
788  * b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
789  */
790 
791 static VALUE
792 num_nonzero_p(VALUE num)
793 {
794  if (RTEST(num_funcall0(num, rb_intern("zero?")))) {
795  return Qnil;
796  }
797  return num;
798 }
799 
800 /*
801  * call-seq:
802  * num.finite? -> true or false
803  *
804  * Returns +true+ if +num+ is a finite number, otherwise returns +false+.
805  */
806 static VALUE
807 num_finite_p(VALUE num)
808 {
809  return Qtrue;
810 }
811 
812 /*
813  * call-seq:
814  * num.infinite? -> -1, 1, or nil
815  *
816  * Returns +nil+, -1, or 1 depending on whether the value is
817  * finite, <code>-Infinity</code>, or <code>+Infinity</code>.
818  */
819 static VALUE
820 num_infinite_p(VALUE num)
821 {
822  return Qnil;
823 }
824 
825 /*
826  * call-seq:
827  * num.to_int -> integer
828  *
829  * Invokes the child class's +to_i+ method to convert +num+ to an integer.
830  *
831  * 1.0.class #=> Float
832  * 1.0.to_int.class #=> Integer
833  * 1.0.to_i.class #=> Integer
834  */
835 
836 static VALUE
837 num_to_int(VALUE num)
838 {
839  return num_funcall0(num, id_to_i);
840 }
841 
842 /*
843  * call-seq:
844  * num.positive? -> true or false
845  *
846  * Returns +true+ if +num+ is greater than 0.
847  */
848 
849 static VALUE
850 num_positive_p(VALUE num)
851 {
852  const ID mid = '>';
853 
854  if (FIXNUM_P(num)) {
856  return (SIGNED_VALUE)num > (SIGNED_VALUE)INT2FIX(0) ? Qtrue : Qfalse;
857  }
858  else if (RB_TYPE_P(num, T_BIGNUM)) {
860  return BIGNUM_POSITIVE_P(num) && !rb_bigzero_p(num) ? Qtrue : Qfalse;
861  }
862  return rb_num_compare_with_zero(num, mid);
863 }
864 
865 /*
866  * call-seq:
867  * num.negative? -> true or false
868  *
869  * Returns +true+ if +num+ is less than 0.
870  */
871 
872 static VALUE
873 num_negative_p(VALUE num)
874 {
875  return rb_num_negative_int_p(num) ? Qtrue : Qfalse;
876 }
877 
878 
879 /********************************************************************
880  *
881  * Document-class: Float
882  *
883  * Float objects represent inexact real numbers using the native
884  * architecture's double-precision floating point representation.
885  *
886  * Floating point has a different arithmetic and is an inexact number.
887  * So you should know its esoteric system. See following:
888  *
889  * - http://docs.sun.com/source/806-3568/ncg_goldberg.html
890  * - https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#floats_imprecise
891  * - http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
892  */
893 
894 VALUE
896 {
898 
899  flt->float_value = d;
900  OBJ_FREEZE(flt);
901  return (VALUE)flt;
902 }
903 
904 /*
905  * call-seq:
906  * float.to_s -> string
907  *
908  * Returns a string containing a representation of +self+.
909  * As well as a fixed or exponential form of the +float+,
910  * the call may return +NaN+, +Infinity+, and +-Infinity+.
911  */
912 
913 static VALUE
914 flo_to_s(VALUE flt)
915 {
916  enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
917  enum {float_dig = DBL_DIG+1};
918  char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
919  double value = RFLOAT_VALUE(flt);
920  VALUE s;
921  char *p, *e;
922  int sign, decpt, digs;
923 
924  if (isinf(value)) {
925  static const char minf[] = "-Infinity";
926  const int pos = (value > 0); /* skip "-" */
927  return rb_usascii_str_new(minf+pos, strlen(minf)-pos);
928  }
929  else if (isnan(value))
930  return rb_usascii_str_new2("NaN");
931 
932  p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
933  s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0);
934  if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1;
935  memcpy(buf, p, digs);
936  xfree(p);
937  if (decpt > 0) {
938  if (decpt < digs) {
939  memmove(buf + decpt + 1, buf + decpt, digs - decpt);
940  buf[decpt] = '.';
941  rb_str_cat(s, buf, digs + 1);
942  }
943  else if (decpt <= DBL_DIG) {
944  long len;
945  char *ptr;
946  rb_str_cat(s, buf, digs);
947  rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2);
948  ptr = RSTRING_PTR(s) + len;
949  if (decpt > digs) {
950  memset(ptr, '0', decpt - digs);
951  ptr += decpt - digs;
952  }
953  memcpy(ptr, ".0", 2);
954  }
955  else {
956  goto exp;
957  }
958  }
959  else if (decpt > -4) {
960  long len;
961  char *ptr;
962  rb_str_cat(s, "0.", 2);
963  rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs);
964  ptr = RSTRING_PTR(s);
965  memset(ptr += len, '0', -decpt);
966  memcpy(ptr -= decpt, buf, digs);
967  }
968  else {
969  exp:
970  if (digs > 1) {
971  memmove(buf + 2, buf + 1, digs - 1);
972  }
973  else {
974  buf[2] = '0';
975  digs++;
976  }
977  buf[1] = '.';
978  rb_str_cat(s, buf, digs + 1);
979  rb_str_catf(s, "e%+03d", decpt - 1);
980  }
981  return s;
982 }
983 
984 /*
985  * call-seq:
986  * float.coerce(numeric) -> array
987  *
988  * Returns an array with both +numeric+ and +float+ represented as Float
989  * objects.
990  *
991  * This is achieved by converting +numeric+ to a Float.
992  *
993  * 1.2.coerce(3) #=> [3.0, 1.2]
994  * 2.5.coerce(1.1) #=> [1.1, 2.5]
995  */
996 
997 static VALUE
998 flo_coerce(VALUE x, VALUE y)
999 {
1000  return rb_assoc_new(rb_Float(y), x);
1001 }
1002 
1003 /*
1004  * call-seq:
1005  * -float -> float
1006  *
1007  * Returns +float+, negated.
1008  */
1009 
1010 VALUE
1012 {
1013  return DBL2NUM(-RFLOAT_VALUE(flt));
1014 }
1015 
1016 /*
1017  * call-seq:
1018  * float + other -> float
1019  *
1020  * Returns a new Float which is the sum of +float+ and +other+.
1021  */
1022 
1023 VALUE
1025 {
1026  if (RB_TYPE_P(y, T_FIXNUM)) {
1027  return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
1028  }
1029  else if (RB_TYPE_P(y, T_BIGNUM)) {
1030  return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
1031  }
1032  else if (RB_TYPE_P(y, T_FLOAT)) {
1033  return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
1034  }
1035  else {
1036  return rb_num_coerce_bin(x, y, '+');
1037  }
1038 }
1039 
1040 /*
1041  * call-seq:
1042  * float - other -> float
1043  *
1044  * Returns a new Float which is the difference of +float+ and +other+.
1045  */
1046 
1047 static VALUE
1048 flo_minus(VALUE x, VALUE y)
1049 {
1050  if (RB_TYPE_P(y, T_FIXNUM)) {
1051  return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
1052  }
1053  else if (RB_TYPE_P(y, T_BIGNUM)) {
1054  return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
1055  }
1056  else if (RB_TYPE_P(y, T_FLOAT)) {
1057  return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
1058  }
1059  else {
1060  return rb_num_coerce_bin(x, y, '-');
1061  }
1062 }
1063 
1064 /*
1065  * call-seq:
1066  * float * other -> float
1067  *
1068  * Returns a new Float which is the product of +float+ and +other+.
1069  */
1070 
1071 VALUE
1073 {
1074  if (RB_TYPE_P(y, T_FIXNUM)) {
1075  return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
1076  }
1077  else if (RB_TYPE_P(y, T_BIGNUM)) {
1078  return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
1079  }
1080  else if (RB_TYPE_P(y, T_FLOAT)) {
1081  return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
1082  }
1083  else {
1084  return rb_num_coerce_bin(x, y, '*');
1085  }
1086 }
1087 
1088 static bool
1089 flo_iszero(VALUE f)
1090 {
1091  return FLOAT_ZERO_P(f);
1092 }
1093 
1094 static double
1095 double_div_double(double x, double y)
1096 {
1097  if (LIKELY(y != 0.0)) {
1098  return x / y;
1099  }
1100  else if (x == 0.0) {
1101  return nan("");
1102  }
1103  else {
1104  double z = signbit(y) ? -1.0 : 1.0;
1105  return x * z * HUGE_VAL;
1106  }
1107 }
1108 
1111 {
1112  double num = RFLOAT_VALUE(x);
1113  double den = RFLOAT_VALUE(y);
1114  double ret = double_div_double(num, den);
1115  return DBL2NUM(ret);
1116 }
1117 
1118 /*
1119  * call-seq:
1120  * float / other -> float
1121  *
1122  * Returns a new Float which is the result of dividing +float+ by +other+.
1123  */
1124 
1125 VALUE
1127 {
1128  double num = RFLOAT_VALUE(x);
1129  double den;
1130  double ret;
1131 
1132  if (RB_TYPE_P(y, T_FIXNUM)) {
1133  den = FIX2LONG(y);
1134  }
1135  else if (RB_TYPE_P(y, T_BIGNUM)) {
1136  den = rb_big2dbl(y);
1137  }
1138  else if (RB_TYPE_P(y, T_FLOAT)) {
1139  den = RFLOAT_VALUE(y);
1140  }
1141  else {
1142  return rb_num_coerce_bin(x, y, '/');
1143  }
1144 
1145  ret = double_div_double(num, den);
1146  return DBL2NUM(ret);
1147 }
1148 
1149 /*
1150  * call-seq:
1151  * float.fdiv(numeric) -> float
1152  * float.quo(numeric) -> float
1153  *
1154  * Returns <code>float / numeric</code>, same as Float#/.
1155  */
1156 
1157 static VALUE
1158 flo_quo(VALUE x, VALUE y)
1159 {
1160  return num_funcall1(x, '/', y);
1161 }
1162 
1163 static void
1164 flodivmod(double x, double y, double *divp, double *modp)
1165 {
1166  double div, mod;
1167 
1168  if (isnan(y)) {
1169  /* y is NaN so all results are NaN */
1170  if (modp) *modp = y;
1171  if (divp) *divp = y;
1172  return;
1173  }
1174  if (y == 0.0) rb_num_zerodiv();
1175  if ((x == 0.0) || (isinf(y) && !isinf(x)))
1176  mod = x;
1177  else {
1178 #ifdef HAVE_FMOD
1179  mod = fmod(x, y);
1180 #else
1181  double z;
1182 
1183  modf(x/y, &z);
1184  mod = x - z * y;
1185 #endif
1186  }
1187  if (isinf(x) && !isinf(y))
1188  div = x;
1189  else {
1190  div = (x - mod) / y;
1191  if (modp && divp) div = round(div);
1192  }
1193  if (y*mod < 0) {
1194  mod += y;
1195  div -= 1.0;
1196  }
1197  if (modp) *modp = mod;
1198  if (divp) *divp = div;
1199 }
1200 
1201 /*
1202  * Returns the modulo of division of x by y.
1203  * An error will be raised if y == 0.
1204  */
1205 
1206 MJIT_FUNC_EXPORTED double
1207 ruby_float_mod(double x, double y)
1208 {
1209  double mod;
1210  flodivmod(x, y, 0, &mod);
1211  return mod;
1212 }
1213 
1214 /*
1215  * call-seq:
1216  * float % other -> float
1217  * float.modulo(other) -> float
1218  *
1219  * Returns the modulo after division of +float+ by +other+.
1220  *
1221  * 6543.21.modulo(137) #=> 104.21000000000004
1222  * 6543.21.modulo(137.24) #=> 92.92999999999961
1223  */
1224 
1225 static VALUE
1226 flo_mod(VALUE x, VALUE y)
1227 {
1228  double fy;
1229 
1230  if (RB_TYPE_P(y, T_FIXNUM)) {
1231  fy = (double)FIX2LONG(y);
1232  }
1233  else if (RB_TYPE_P(y, T_BIGNUM)) {
1234  fy = rb_big2dbl(y);
1235  }
1236  else if (RB_TYPE_P(y, T_FLOAT)) {
1237  fy = RFLOAT_VALUE(y);
1238  }
1239  else {
1240  return rb_num_coerce_bin(x, y, '%');
1241  }
1242  return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
1243 }
1244 
1245 static VALUE
1246 dbl2ival(double d)
1247 {
1248  if (FIXABLE(d)) {
1249  return LONG2FIX((long)d);
1250  }
1251  return rb_dbl2big(d);
1252 }
1253 
1254 /*
1255  * call-seq:
1256  * float.divmod(numeric) -> array
1257  *
1258  * See Numeric#divmod.
1259  *
1260  * 42.0.divmod(6) #=> [7, 0.0]
1261  * 42.0.divmod(5) #=> [8, 2.0]
1262  */
1263 
1264 static VALUE
1265 flo_divmod(VALUE x, VALUE y)
1266 {
1267  double fy, div, mod;
1268  volatile VALUE a, b;
1269 
1270  if (RB_TYPE_P(y, T_FIXNUM)) {
1271  fy = (double)FIX2LONG(y);
1272  }
1273  else if (RB_TYPE_P(y, T_BIGNUM)) {
1274  fy = rb_big2dbl(y);
1275  }
1276  else if (RB_TYPE_P(y, T_FLOAT)) {
1277  fy = RFLOAT_VALUE(y);
1278  }
1279  else {
1280  return rb_num_coerce_bin(x, y, id_divmod);
1281  }
1282  flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
1283  a = dbl2ival(div);
1284  b = DBL2NUM(mod);
1285  return rb_assoc_new(a, b);
1286 }
1287 
1288 /*
1289  * call-seq:
1290  * float ** other -> float
1291  *
1292  * Raises +float+ to the power of +other+.
1293  *
1294  * 2.0**3 #=> 8.0
1295  */
1296 
1297 VALUE
1299 {
1300  double dx, dy;
1301  if (RB_TYPE_P(y, T_FIXNUM)) {
1302  dx = RFLOAT_VALUE(x);
1303  dy = (double)FIX2LONG(y);
1304  }
1305  else if (RB_TYPE_P(y, T_BIGNUM)) {
1306  dx = RFLOAT_VALUE(x);
1307  dy = rb_big2dbl(y);
1308  }
1309  else if (RB_TYPE_P(y, T_FLOAT)) {
1310  dx = RFLOAT_VALUE(x);
1311  dy = RFLOAT_VALUE(y);
1312  if (dx < 0 && dy != round(dy))
1313  return rb_dbl_complex_new_polar_pi(pow(-dx, dy), dy);
1314  }
1315  else {
1316  return rb_num_coerce_bin(x, y, idPow);
1317  }
1318  return DBL2NUM(pow(dx, dy));
1319 }
1320 
1321 /*
1322  * call-seq:
1323  * num.eql?(numeric) -> true or false
1324  *
1325  * Returns +true+ if +num+ and +numeric+ are the same type and have equal
1326  * values. Contrast this with Numeric#==, which performs type conversions.
1327  *
1328  * 1 == 1.0 #=> true
1329  * 1.eql?(1.0) #=> false
1330  * 1.0.eql?(1.0) #=> true
1331  */
1332 
1333 static VALUE
1334 num_eql(VALUE x, VALUE y)
1335 {
1336  if (TYPE(x) != TYPE(y)) return Qfalse;
1337 
1338  if (RB_TYPE_P(x, T_BIGNUM)) {
1339  return rb_big_eql(x, y);
1340  }
1341 
1342  return rb_equal(x, y);
1343 }
1344 
1345 /*
1346  * call-seq:
1347  * number <=> other -> 0 or nil
1348  *
1349  * Returns zero if +number+ equals +other+, otherwise returns +nil+.
1350  */
1351 
1352 static VALUE
1353 num_cmp(VALUE x, VALUE y)
1354 {
1355  if (x == y) return INT2FIX(0);
1356  return Qnil;
1357 }
1358 
1359 static VALUE
1360 num_equal(VALUE x, VALUE y)
1361 {
1362  VALUE result;
1363  if (x == y) return Qtrue;
1364  result = num_funcall1(y, id_eq, x);
1365  if (RTEST(result)) return Qtrue;
1366  return Qfalse;
1367 }
1368 
1369 /*
1370  * call-seq:
1371  * float == obj -> true or false
1372  *
1373  * Returns +true+ only if +obj+ has the same value as +float+.
1374  * Contrast this with Float#eql?, which requires +obj+ to be a Float.
1375  *
1376  * 1.0 == 1 #=> true
1377  *
1378  * The result of <code>NaN == NaN</code> is undefined,
1379  * so an implementation-dependent value is returned.
1380  */
1381 
1384 {
1385  volatile double a, b;
1386 
1387  if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
1388  return rb_integer_float_eq(y, x);
1389  }
1390  else if (RB_TYPE_P(y, T_FLOAT)) {
1391  b = RFLOAT_VALUE(y);
1392 #if defined(_MSC_VER) && _MSC_VER < 1300
1393  if (isnan(b)) return Qfalse;
1394 #endif
1395  }
1396  else {
1397  return num_equal(x, y);
1398  }
1399  a = RFLOAT_VALUE(x);
1400 #if defined(_MSC_VER) && _MSC_VER < 1300
1401  if (isnan(a)) return Qfalse;
1402 #endif
1403  return (a == b)?Qtrue:Qfalse;
1404 }
1405 
1406 #define flo_eq rb_float_equal
1407 static VALUE rb_dbl_hash(double d);
1408 
1409 /*
1410  * call-seq:
1411  * float.hash -> integer
1412  *
1413  * Returns a hash code for this float.
1414  *
1415  * See also Object#hash.
1416  */
1417 
1418 static VALUE
1419 flo_hash(VALUE num)
1420 {
1421  return rb_dbl_hash(RFLOAT_VALUE(num));
1422 }
1423 
1424 static VALUE
1425 rb_dbl_hash(double d)
1426 {
1427  return LONG2FIX(rb_dbl_long_hash(d));
1428 }
1429 
1430 VALUE
1431 rb_dbl_cmp(double a, double b)
1432 {
1433  if (isnan(a) || isnan(b)) return Qnil;
1434  if (a == b) return INT2FIX(0);
1435  if (a > b) return INT2FIX(1);
1436  if (a < b) return INT2FIX(-1);
1437  return Qnil;
1438 }
1439 
1440 /*
1441  * call-seq:
1442  * float <=> real -> -1, 0, +1, or nil
1443  *
1444  * Returns -1, 0, or +1 depending on whether +float+ is
1445  * less than, equal to, or greater than +real+.
1446  * This is the basis for the tests in the Comparable module.
1447  *
1448  * The result of <code>NaN <=> NaN</code> is undefined,
1449  * so an implementation-dependent value is returned.
1450  *
1451  * +nil+ is returned if the two values are incomparable.
1452  */
1453 
1454 static VALUE
1455 flo_cmp(VALUE x, VALUE y)
1456 {
1457  double a, b;
1458  VALUE i;
1459 
1460  a = RFLOAT_VALUE(x);
1461  if (isnan(a)) return Qnil;
1462  if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
1463  VALUE rel = rb_integer_float_cmp(y, x);
1464  if (FIXNUM_P(rel))
1465  return LONG2FIX(-FIX2LONG(rel));
1466  return rel;
1467  }
1468  else if (RB_TYPE_P(y, T_FLOAT)) {
1469  b = RFLOAT_VALUE(y);
1470  }
1471  else {
1472  if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) {
1473  if (RTEST(i)) {
1474  int j = rb_cmpint(i, x, y);
1475  j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
1476  return INT2FIX(j);
1477  }
1478  if (a > 0.0) return INT2FIX(1);
1479  return INT2FIX(-1);
1480  }
1481  return rb_num_coerce_cmp(x, y, id_cmp);
1482  }
1483  return rb_dbl_cmp(a, b);
1484 }
1485 
1488 {
1489  return NUM2INT(flo_cmp(x, y));
1490 }
1491 
1492 /*
1493  * call-seq:
1494  * float > real -> true or false
1495  *
1496  * Returns +true+ if +float+ is greater than +real+.
1497  *
1498  * The result of <code>NaN > NaN</code> is undefined,
1499  * so an implementation-dependent value is returned.
1500  */
1501 
1502 VALUE
1504 {
1505  double a, b;
1506 
1507  a = RFLOAT_VALUE(x);
1508  if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
1509  VALUE rel = rb_integer_float_cmp(y, x);
1510  if (FIXNUM_P(rel))
1511  return -FIX2LONG(rel) > 0 ? Qtrue : Qfalse;
1512  return Qfalse;
1513  }
1514  else if (RB_TYPE_P(y, T_FLOAT)) {
1515  b = RFLOAT_VALUE(y);
1516 #if defined(_MSC_VER) && _MSC_VER < 1300
1517  if (isnan(b)) return Qfalse;
1518 #endif
1519  }
1520  else {
1521  return rb_num_coerce_relop(x, y, '>');
1522  }
1523 #if defined(_MSC_VER) && _MSC_VER < 1300
1524  if (isnan(a)) return Qfalse;
1525 #endif
1526  return (a > b)?Qtrue:Qfalse;
1527 }
1528 
1529 /*
1530  * call-seq:
1531  * float >= real -> true or false
1532  *
1533  * Returns +true+ if +float+ is greater than or equal to +real+.
1534  *
1535  * The result of <code>NaN >= NaN</code> is undefined,
1536  * so an implementation-dependent value is returned.
1537  */
1538 
1539 static VALUE
1540 flo_ge(VALUE x, VALUE y)
1541 {
1542  double a, b;
1543 
1544  a = RFLOAT_VALUE(x);
1545  if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
1546  VALUE rel = rb_integer_float_cmp(y, x);
1547  if (FIXNUM_P(rel))
1548  return -FIX2LONG(rel) >= 0 ? Qtrue : Qfalse;
1549  return Qfalse;
1550  }
1551  else if (RB_TYPE_P(y, T_FLOAT)) {
1552  b = RFLOAT_VALUE(y);
1553 #if defined(_MSC_VER) && _MSC_VER < 1300
1554  if (isnan(b)) return Qfalse;
1555 #endif
1556  }
1557  else {
1558  return rb_num_coerce_relop(x, y, idGE);
1559  }
1560 #if defined(_MSC_VER) && _MSC_VER < 1300
1561  if (isnan(a)) return Qfalse;
1562 #endif
1563  return (a >= b)?Qtrue:Qfalse;
1564 }
1565 
1566 /*
1567  * call-seq:
1568  * float < real -> true or false
1569  *
1570  * Returns +true+ if +float+ is less than +real+.
1571  *
1572  * The result of <code>NaN < NaN</code> is undefined,
1573  * so an implementation-dependent value is returned.
1574  */
1575 
1576 static VALUE
1577 flo_lt(VALUE x, VALUE y)
1578 {
1579  double a, b;
1580 
1581  a = RFLOAT_VALUE(x);
1582  if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
1583  VALUE rel = rb_integer_float_cmp(y, x);
1584  if (FIXNUM_P(rel))
1585  return -FIX2LONG(rel) < 0 ? Qtrue : Qfalse;
1586  return Qfalse;
1587  }
1588  else if (RB_TYPE_P(y, T_FLOAT)) {
1589  b = RFLOAT_VALUE(y);
1590 #if defined(_MSC_VER) && _MSC_VER < 1300
1591  if (isnan(b)) return Qfalse;
1592 #endif
1593  }
1594  else {
1595  return rb_num_coerce_relop(x, y, '<');
1596  }
1597 #if defined(_MSC_VER) && _MSC_VER < 1300
1598  if (isnan(a)) return Qfalse;
1599 #endif
1600  return (a < b)?Qtrue:Qfalse;
1601 }
1602 
1603 /*
1604  * call-seq:
1605  * float <= real -> true or false
1606  *
1607  * Returns +true+ if +float+ is less than or equal to +real+.
1608  *
1609  * The result of <code>NaN <= NaN</code> is undefined,
1610  * so an implementation-dependent value is returned.
1611  */
1612 
1613 static VALUE
1614 flo_le(VALUE x, VALUE y)
1615 {
1616  double a, b;
1617 
1618  a = RFLOAT_VALUE(x);
1619  if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
1620  VALUE rel = rb_integer_float_cmp(y, x);
1621  if (FIXNUM_P(rel))
1622  return -FIX2LONG(rel) <= 0 ? Qtrue : Qfalse;
1623  return Qfalse;
1624  }
1625  else if (RB_TYPE_P(y, T_FLOAT)) {
1626  b = RFLOAT_VALUE(y);
1627 #if defined(_MSC_VER) && _MSC_VER < 1300
1628  if (isnan(b)) return Qfalse;
1629 #endif
1630  }
1631  else {
1632  return rb_num_coerce_relop(x, y, idLE);
1633  }
1634 #if defined(_MSC_VER) && _MSC_VER < 1300
1635  if (isnan(a)) return Qfalse;
1636 #endif
1637  return (a <= b)?Qtrue:Qfalse;
1638 }
1639 
1640 /*
1641  * call-seq:
1642  * float.eql?(obj) -> true or false
1643  *
1644  * Returns +true+ only if +obj+ is a Float with the same value as +float+.
1645  * Contrast this with Float#==, which performs type conversions.
1646  *
1647  * 1.0.eql?(1) #=> false
1648  *
1649  * The result of <code>NaN.eql?(NaN)</code> is undefined,
1650  * so an implementation-dependent value is returned.
1651  */
1652 
1655 {
1656  if (RB_TYPE_P(y, T_FLOAT)) {
1657  double a = RFLOAT_VALUE(x);
1658  double b = RFLOAT_VALUE(y);
1659 #if defined(_MSC_VER) && _MSC_VER < 1300
1660  if (isnan(a) || isnan(b)) return Qfalse;
1661 #endif
1662  if (a == b)
1663  return Qtrue;
1664  }
1665  return Qfalse;
1666 }
1667 
1668 #define flo_eql rb_float_eql
1669 
1670 /*
1671  * call-seq:
1672  * float.to_f -> self
1673  *
1674  * Since +float+ is already a Float, returns +self+.
1675  */
1676 
1677 static VALUE
1678 flo_to_f(VALUE num)
1679 {
1680  return num;
1681 }
1682 
1683 /*
1684  * call-seq:
1685  * float.abs -> float
1686  * float.magnitude -> float
1687  *
1688  * Returns the absolute value of +float+.
1689  *
1690  * (-34.56).abs #=> 34.56
1691  * -34.56.abs #=> 34.56
1692  * 34.56.abs #=> 34.56
1693  *
1694  * Float#magnitude is an alias for Float#abs.
1695  */
1696 
1697 VALUE
1699 {
1700  double val = fabs(RFLOAT_VALUE(flt));
1701  return DBL2NUM(val);
1702 }
1703 
1704 /*
1705  * call-seq:
1706  * float.zero? -> true or false
1707  *
1708  * Returns +true+ if +float+ is 0.0.
1709  */
1710 
1711 static VALUE
1712 flo_zero_p(VALUE num)
1713 {
1714  return flo_iszero(num) ? Qtrue : Qfalse;
1715 }
1716 
1717 /*
1718  * call-seq:
1719  * float.nan? -> true or false
1720  *
1721  * Returns +true+ if +float+ is an invalid IEEE floating point number.
1722  *
1723  * a = -1.0 #=> -1.0
1724  * a.nan? #=> false
1725  * a = 0.0/0.0 #=> NaN
1726  * a.nan? #=> true
1727  */
1728 
1729 static VALUE
1730 flo_is_nan_p(VALUE num)
1731 {
1732  double value = RFLOAT_VALUE(num);
1733 
1734  return isnan(value) ? Qtrue : Qfalse;
1735 }
1736 
1737 /*
1738  * call-seq:
1739  * float.infinite? -> -1, 1, or nil
1740  *
1741  * Returns +nil+, -1, or 1 depending on whether the value is
1742  * finite, <code>-Infinity</code>, or <code>+Infinity</code>.
1743  *
1744  * (0.0).infinite? #=> nil
1745  * (-1.0/0.0).infinite? #=> -1
1746  * (+1.0/0.0).infinite? #=> 1
1747  */
1748 
1749 VALUE
1751 {
1752  double value = RFLOAT_VALUE(num);
1753 
1754  if (isinf(value)) {
1755  return INT2FIX( value < 0 ? -1 : 1 );
1756  }
1757 
1758  return Qnil;
1759 }
1760 
1761 /*
1762  * call-seq:
1763  * float.finite? -> true or false
1764  *
1765  * Returns +true+ if +float+ is a valid IEEE floating point number,
1766  * i.e. it is not infinite and Float#nan? is +false+.
1767  */
1768 
1769 VALUE
1771 {
1772  double value = RFLOAT_VALUE(num);
1773 
1774 #ifdef HAVE_ISFINITE
1775  if (!isfinite(value))
1776  return Qfalse;
1777 #else
1778  if (isinf(value) || isnan(value))
1779  return Qfalse;
1780 #endif
1781 
1782  return Qtrue;
1783 }
1784 
1785 /*
1786  * call-seq:
1787  * float.next_float -> float
1788  *
1789  * Returns the next representable floating point number.
1790  *
1791  * Float::MAX.next_float and Float::INFINITY.next_float is Float::INFINITY.
1792  *
1793  * Float::NAN.next_float is Float::NAN.
1794  *
1795  * For example:
1796  *
1797  * 0.01.next_float #=> 0.010000000000000002
1798  * 1.0.next_float #=> 1.0000000000000002
1799  * 100.0.next_float #=> 100.00000000000001
1800  *
1801  * 0.01.next_float - 0.01 #=> 1.734723475976807e-18
1802  * 1.0.next_float - 1.0 #=> 2.220446049250313e-16
1803  * 100.0.next_float - 100.0 #=> 1.4210854715202004e-14
1804  *
1805  * f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.next_float }
1806  * #=> 0x1.47ae147ae147bp-7 0.01
1807  * # 0x1.47ae147ae147cp-7 0.010000000000000002
1808  * # 0x1.47ae147ae147dp-7 0.010000000000000004
1809  * # 0x1.47ae147ae147ep-7 0.010000000000000005
1810  * # 0x1.47ae147ae147fp-7 0.010000000000000007
1811  * # 0x1.47ae147ae148p-7 0.010000000000000009
1812  * # 0x1.47ae147ae1481p-7 0.01000000000000001
1813  * # 0x1.47ae147ae1482p-7 0.010000000000000012
1814  * # 0x1.47ae147ae1483p-7 0.010000000000000014
1815  * # 0x1.47ae147ae1484p-7 0.010000000000000016
1816  * # 0x1.47ae147ae1485p-7 0.010000000000000018
1817  * # 0x1.47ae147ae1486p-7 0.01000000000000002
1818  * # 0x1.47ae147ae1487p-7 0.010000000000000021
1819  * # 0x1.47ae147ae1488p-7 0.010000000000000023
1820  * # 0x1.47ae147ae1489p-7 0.010000000000000024
1821  * # 0x1.47ae147ae148ap-7 0.010000000000000026
1822  * # 0x1.47ae147ae148bp-7 0.010000000000000028
1823  * # 0x1.47ae147ae148cp-7 0.01000000000000003
1824  * # 0x1.47ae147ae148dp-7 0.010000000000000031
1825  * # 0x1.47ae147ae148ep-7 0.010000000000000033
1826  *
1827  * f = 0.0
1828  * 100.times { f += 0.1 }
1829  * f #=> 9.99999999999998 # should be 10.0 in the ideal world.
1830  * 10-f #=> 1.9539925233402755e-14 # the floating point error.
1831  * 10.0.next_float-10 #=> 1.7763568394002505e-15 # 1 ulp (unit in the last place).
1832  * (10-f)/(10.0.next_float-10) #=> 11.0 # the error is 11 ulp.
1833  * (10-f)/(10*Float::EPSILON) #=> 8.8 # approximation of the above.
1834  * "%a" % 10 #=> "0x1.4p+3"
1835  * "%a" % f #=> "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
1836  */
1837 static VALUE
1838 flo_next_float(VALUE vx)
1839 {
1840  double x, y;
1841  x = NUM2DBL(vx);
1842  y = nextafter(x, HUGE_VAL);
1843  return DBL2NUM(y);
1844 }
1845 
1846 /*
1847  * call-seq:
1848  * float.prev_float -> float
1849  *
1850  * Returns the previous representable floating point number.
1851  *
1852  * (-Float::MAX).prev_float and (-Float::INFINITY).prev_float is -Float::INFINITY.
1853  *
1854  * Float::NAN.prev_float is Float::NAN.
1855  *
1856  * For example:
1857  *
1858  * 0.01.prev_float #=> 0.009999999999999998
1859  * 1.0.prev_float #=> 0.9999999999999999
1860  * 100.0.prev_float #=> 99.99999999999999
1861  *
1862  * 0.01 - 0.01.prev_float #=> 1.734723475976807e-18
1863  * 1.0 - 1.0.prev_float #=> 1.1102230246251565e-16
1864  * 100.0 - 100.0.prev_float #=> 1.4210854715202004e-14
1865  *
1866  * f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float }
1867  * #=> 0x1.47ae147ae147bp-7 0.01
1868  * # 0x1.47ae147ae147ap-7 0.009999999999999998
1869  * # 0x1.47ae147ae1479p-7 0.009999999999999997
1870  * # 0x1.47ae147ae1478p-7 0.009999999999999995
1871  * # 0x1.47ae147ae1477p-7 0.009999999999999993
1872  * # 0x1.47ae147ae1476p-7 0.009999999999999992
1873  * # 0x1.47ae147ae1475p-7 0.00999999999999999
1874  * # 0x1.47ae147ae1474p-7 0.009999999999999988
1875  * # 0x1.47ae147ae1473p-7 0.009999999999999986
1876  * # 0x1.47ae147ae1472p-7 0.009999999999999985
1877  * # 0x1.47ae147ae1471p-7 0.009999999999999983
1878  * # 0x1.47ae147ae147p-7 0.009999999999999981
1879  * # 0x1.47ae147ae146fp-7 0.00999999999999998
1880  * # 0x1.47ae147ae146ep-7 0.009999999999999978
1881  * # 0x1.47ae147ae146dp-7 0.009999999999999976
1882  * # 0x1.47ae147ae146cp-7 0.009999999999999974
1883  * # 0x1.47ae147ae146bp-7 0.009999999999999972
1884  * # 0x1.47ae147ae146ap-7 0.00999999999999997
1885  * # 0x1.47ae147ae1469p-7 0.009999999999999969
1886  * # 0x1.47ae147ae1468p-7 0.009999999999999967
1887  */
1888 static VALUE
1889 flo_prev_float(VALUE vx)
1890 {
1891  double x, y;
1892  x = NUM2DBL(vx);
1893  y = nextafter(x, -HUGE_VAL);
1894  return DBL2NUM(y);
1895 }
1896 
1897 /*
1898  * call-seq:
1899  * float.floor([ndigits]) -> integer or float
1900  *
1901  * Returns the largest number less than or equal to +float+ with
1902  * a precision of +ndigits+ decimal digits (default: 0).
1903  *
1904  * When the precision is negative, the returned value is an integer
1905  * with at least <code>ndigits.abs</code> trailing zeros.
1906  *
1907  * Returns a floating point number when +ndigits+ is positive,
1908  * otherwise returns an integer.
1909  *
1910  * 1.2.floor #=> 1
1911  * 2.0.floor #=> 2
1912  * (-1.2).floor #=> -2
1913  * (-2.0).floor #=> -2
1914  *
1915  * 1.234567.floor(2) #=> 1.23
1916  * 1.234567.floor(3) #=> 1.234
1917  * 1.234567.floor(4) #=> 1.2345
1918  * 1.234567.floor(5) #=> 1.23456
1919  *
1920  * 34567.89.floor(-5) #=> 0
1921  * 34567.89.floor(-4) #=> 30000
1922  * 34567.89.floor(-3) #=> 34000
1923  * 34567.89.floor(-2) #=> 34500
1924  * 34567.89.floor(-1) #=> 34560
1925  * 34567.89.floor(0) #=> 34567
1926  * 34567.89.floor(1) #=> 34567.8
1927  * 34567.89.floor(2) #=> 34567.89
1928  * 34567.89.floor(3) #=> 34567.89
1929  *
1930  * Note that the limited precision of floating point arithmetic
1931  * might lead to surprising results:
1932  *
1933  * (0.3 / 0.1).floor #=> 2 (!)
1934  */
1935 
1936 static VALUE
1937 flo_floor(int argc, VALUE *argv, VALUE num)
1938 {
1939  double number, f;
1940  int ndigits = 0;
1941 
1942  if (rb_check_arity(argc, 0, 1)) {
1943  ndigits = NUM2INT(argv[0]);
1944  }
1945  number = RFLOAT_VALUE(num);
1946  if (number == 0.0) {
1947  return ndigits > 0 ? DBL2NUM(number) : INT2FIX(0);
1948  }
1949  if (ndigits > 0) {
1950  int binexp;
1951  frexp(number, &binexp);
1952  if (float_round_overflow(ndigits, binexp)) return num;
1953  if (number > 0.0 && float_round_underflow(ndigits, binexp))
1954  return DBL2NUM(0.0);
1955  f = pow(10, ndigits);
1956  f = floor(number * f) / f;
1957  return DBL2NUM(f);
1958  }
1959  else {
1960  num = dbl2ival(floor(number));
1961  if (ndigits < 0) num = rb_int_floor(num, ndigits);
1962  return num;
1963  }
1964 }
1965 
1966 /*
1967  * call-seq:
1968  * float.ceil([ndigits]) -> integer or float
1969  *
1970  * Returns the smallest number greater than or equal to +float+ with
1971  * a precision of +ndigits+ decimal digits (default: 0).
1972  *
1973  * When the precision is negative, the returned value is an integer
1974  * with at least <code>ndigits.abs</code> trailing zeros.
1975  *
1976  * Returns a floating point number when +ndigits+ is positive,
1977  * otherwise returns an integer.
1978  *
1979  * 1.2.ceil #=> 2
1980  * 2.0.ceil #=> 2
1981  * (-1.2).ceil #=> -1
1982  * (-2.0).ceil #=> -2
1983  *
1984  * 1.234567.ceil(2) #=> 1.24
1985  * 1.234567.ceil(3) #=> 1.235
1986  * 1.234567.ceil(4) #=> 1.2346
1987  * 1.234567.ceil(5) #=> 1.23457
1988  *
1989  * 34567.89.ceil(-5) #=> 100000
1990  * 34567.89.ceil(-4) #=> 40000
1991  * 34567.89.ceil(-3) #=> 35000
1992  * 34567.89.ceil(-2) #=> 34600
1993  * 34567.89.ceil(-1) #=> 34570
1994  * 34567.89.ceil(0) #=> 34568
1995  * 34567.89.ceil(1) #=> 34567.9
1996  * 34567.89.ceil(2) #=> 34567.89
1997  * 34567.89.ceil(3) #=> 34567.89
1998  *
1999  * Note that the limited precision of floating point arithmetic
2000  * might lead to surprising results:
2001  *
2002  * (2.1 / 0.7).ceil #=> 4 (!)
2003  */
2004 
2005 static VALUE
2006 flo_ceil(int argc, VALUE *argv, VALUE num)
2007 {
2008  int ndigits = 0;
2009 
2010  if (rb_check_arity(argc, 0, 1)) {
2011  ndigits = NUM2INT(argv[0]);
2012  }
2013  return rb_float_ceil(num, ndigits);
2014 }
2015 
2016 VALUE
2017 rb_float_ceil(VALUE num, int ndigits)
2018 {
2019  double number, f;
2020 
2021  number = RFLOAT_VALUE(num);
2022  if (number == 0.0) {
2023  return ndigits > 0 ? DBL2NUM(number) : INT2FIX(0);
2024  }
2025  if (ndigits > 0) {
2026  int binexp;
2027  frexp(number, &binexp);
2028  if (float_round_overflow(ndigits, binexp)) return num;
2029  if (number < 0.0 && float_round_underflow(ndigits, binexp))
2030  return DBL2NUM(0.0);
2031  f = pow(10, ndigits);
2032  f = ceil(number * f) / f;
2033  return DBL2NUM(f);
2034  }
2035  else {
2036  num = dbl2ival(ceil(number));
2037  if (ndigits < 0) num = rb_int_ceil(num, ndigits);
2038  return num;
2039  }
2040 }
2041 
2042 static int
2043 int_round_zero_p(VALUE num, int ndigits)
2044 {
2045  long bytes;
2046  /* If 10**N / 2 > num, then return 0 */
2047  /* We have log_256(10) > 0.415241 and log_256(1/2) = -0.125, so */
2048  if (FIXNUM_P(num)) {
2049  bytes = sizeof(long);
2050  }
2051  else if (RB_TYPE_P(num, T_BIGNUM)) {
2052  bytes = rb_big_size(num);
2053  }
2054  else {
2055  bytes = NUM2LONG(rb_funcall(num, idSize, 0));
2056  }
2057  return (-0.415241 * ndigits - 0.125 > bytes);
2058 }
2059 
2060 static SIGNED_VALUE
2061 int_round_half_even(SIGNED_VALUE x, SIGNED_VALUE y)
2062 {
2063  SIGNED_VALUE z = +(x + y / 2) / y;
2064  if ((z * y - x) * 2 == y) {
2065  z &= ~1;
2066  }
2067  return z * y;
2068 }
2069 
2070 static SIGNED_VALUE
2071 int_round_half_up(SIGNED_VALUE x, SIGNED_VALUE y)
2072 {
2073  return (x + y / 2) / y * y;
2074 }
2075 
2076 static SIGNED_VALUE
2077 int_round_half_down(SIGNED_VALUE x, SIGNED_VALUE y)
2078 {
2079  return (x + y / 2 - 1) / y * y;
2080 }
2081 
2082 static int
2083 int_half_p_half_even(VALUE num, VALUE n, VALUE f)
2084 {
2085  return (int)rb_int_odd_p(rb_int_idiv(n, f));
2086 }
2087 
2088 static int
2089 int_half_p_half_up(VALUE num, VALUE n, VALUE f)
2090 {
2091  return int_pos_p(num);
2092 }
2093 
2094 static int
2095 int_half_p_half_down(VALUE num, VALUE n, VALUE f)
2096 {
2097  return int_neg_p(num);
2098 }
2099 
2100 /*
2101  * Assumes num is an Integer, ndigits <= 0
2102  */
2103 static VALUE
2104 rb_int_round(VALUE num, int ndigits, enum ruby_num_rounding_mode mode)
2105 {
2106  VALUE n, f, h, r;
2107 
2108  if (int_round_zero_p(num, ndigits)) {
2109  return INT2FIX(0);
2110  }
2111 
2112  f = int_pow(10, -ndigits);
2113  if (FIXNUM_P(num) && FIXNUM_P(f)) {
2114  SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
2115  int neg = x < 0;
2116  if (neg) x = -x;
2117  x = ROUND_CALL(mode, int_round, (x, y));
2118  if (neg) x = -x;
2119  return LONG2NUM(x);
2120  }
2121  if (RB_TYPE_P(f, T_FLOAT)) {
2122  /* then int_pow overflow */
2123  return INT2FIX(0);
2124  }
2125  h = rb_int_idiv(f, INT2FIX(2));
2126  r = rb_int_modulo(num, f);
2127  n = rb_int_minus(num, r);
2128  r = rb_int_cmp(r, h);
2129  if (FIXNUM_POSITIVE_P(r) ||
2130  (FIXNUM_ZERO_P(r) && ROUND_CALL(mode, int_half_p, (num, n, f)))) {
2131  n = rb_int_plus(n, f);
2132  }
2133  return n;
2134 }
2135 
2136 VALUE
2137 rb_int_floor(VALUE num, int ndigits)
2138 {
2139  VALUE f;
2140 
2141  if (int_round_zero_p(num, ndigits))
2142  return INT2FIX(0);
2143  f = int_pow(10, -ndigits);
2144  if (FIXNUM_P(num) && FIXNUM_P(f)) {
2145  SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
2146  int neg = x < 0;
2147  if (neg) x = -x + y - 1;
2148  x = x / y * y;
2149  if (neg) x = -x;
2150  return LONG2NUM(x);
2151  }
2152  if (RB_TYPE_P(f, T_FLOAT)) {
2153  /* then int_pow overflow */
2154  return INT2FIX(0);
2155  }
2156  return rb_int_minus(num, rb_int_modulo(num, f));
2157 }
2158 
2159 VALUE
2160 rb_int_ceil(VALUE num, int ndigits)
2161 {
2162  VALUE f;
2163 
2164  if (int_round_zero_p(num, ndigits))
2165  return INT2FIX(0);
2166  f = int_pow(10, -ndigits);
2167  if (FIXNUM_P(num) && FIXNUM_P(f)) {
2168  SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
2169  int neg = x < 0;
2170  if (neg) x = -x;
2171  else x += y - 1;
2172  x = (x / y) * y;
2173  if (neg) x = -x;
2174  return LONG2NUM(x);
2175  }
2176  if (RB_TYPE_P(f, T_FLOAT)) {
2177  /* then int_pow overflow */
2178  return INT2FIX(0);
2179  }
2180  return rb_int_plus(num, rb_int_minus(f, rb_int_modulo(num, f)));
2181 }
2182 
2183 VALUE
2184 rb_int_truncate(VALUE num, int ndigits)
2185 {
2186  VALUE f;
2187  VALUE m;
2188 
2189  if (int_round_zero_p(num, ndigits))
2190  return INT2FIX(0);
2191  f = int_pow(10, -ndigits);
2192  if (FIXNUM_P(num) && FIXNUM_P(f)) {
2193  SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
2194  int neg = x < 0;
2195  if (neg) x = -x;
2196  x = x / y * y;
2197  if (neg) x = -x;
2198  return LONG2NUM(x);
2199  }
2200  if (RB_TYPE_P(f, T_FLOAT)) {
2201  /* then int_pow overflow */
2202  return INT2FIX(0);
2203  }
2204  m = rb_int_modulo(num, f);
2205  if (int_neg_p(num)) {
2206  return rb_int_plus(num, rb_int_minus(f, m));
2207  }
2208  else {
2209  return rb_int_minus(num, m);
2210  }
2211 }
2212 
2213 /*
2214  * call-seq:
2215  * float.round([ndigits] [, half: mode]) -> integer or float
2216  *
2217  * Returns +float+ rounded to the nearest value with
2218  * a precision of +ndigits+ decimal digits (default: 0).
2219  *
2220  * When the precision is negative, the returned value is an integer
2221  * with at least <code>ndigits.abs</code> trailing zeros.
2222  *
2223  * Returns a floating point number when +ndigits+ is positive,
2224  * otherwise returns an integer.
2225  *
2226  * 1.4.round #=> 1
2227  * 1.5.round #=> 2
2228  * 1.6.round #=> 2
2229  * (-1.5).round #=> -2
2230  *
2231  * 1.234567.round(2) #=> 1.23
2232  * 1.234567.round(3) #=> 1.235
2233  * 1.234567.round(4) #=> 1.2346
2234  * 1.234567.round(5) #=> 1.23457
2235  *
2236  * 34567.89.round(-5) #=> 0
2237  * 34567.89.round(-4) #=> 30000
2238  * 34567.89.round(-3) #=> 35000
2239  * 34567.89.round(-2) #=> 34600
2240  * 34567.89.round(-1) #=> 34570
2241  * 34567.89.round(0) #=> 34568
2242  * 34567.89.round(1) #=> 34567.9
2243  * 34567.89.round(2) #=> 34567.89
2244  * 34567.89.round(3) #=> 34567.89
2245  *
2246  * If the optional +half+ keyword argument is given,
2247  * numbers that are half-way between two possible rounded values
2248  * will be rounded according to the specified tie-breaking +mode+:
2249  *
2250  * * <code>:up</code> or +nil+: round half away from zero (default)
2251  * * <code>:down</code>: round half toward zero
2252  * * <code>:even</code>: round half toward the nearest even number
2253  *
2254  * 2.5.round(half: :up) #=> 3
2255  * 2.5.round(half: :down) #=> 2
2256  * 2.5.round(half: :even) #=> 2
2257  * 3.5.round(half: :up) #=> 4
2258  * 3.5.round(half: :down) #=> 3
2259  * 3.5.round(half: :even) #=> 4
2260  * (-2.5).round(half: :up) #=> -3
2261  * (-2.5).round(half: :down) #=> -2
2262  * (-2.5).round(half: :even) #=> -2
2263  */
2264 
2265 static VALUE
2266 flo_round(int argc, VALUE *argv, VALUE num)
2267 {
2268  double number, f, x;
2269  VALUE nd, opt;
2270  int ndigits = 0;
2271  enum ruby_num_rounding_mode mode;
2272 
2273  if (rb_scan_args(argc, argv, "01:", &nd, &opt)) {
2274  ndigits = NUM2INT(nd);
2275  }
2276  mode = rb_num_get_rounding_option(opt);
2277  number = RFLOAT_VALUE(num);
2278  if (number == 0.0) {
2279  return ndigits > 0 ? DBL2NUM(number) : INT2FIX(0);
2280  }
2281  if (ndigits < 0) {
2282  return rb_int_round(flo_to_i(num), ndigits, mode);
2283  }
2284  if (ndigits == 0) {
2285  x = ROUND_CALL(mode, round, (number, 1.0));
2286  return dbl2ival(x);
2287  }
2288  if (isfinite(number)) {
2289  int binexp;
2290  frexp(number, &binexp);
2291  if (float_round_overflow(ndigits, binexp)) return num;
2292  if (float_round_underflow(ndigits, binexp)) return DBL2NUM(0);
2293  f = pow(10, ndigits);
2294  x = ROUND_CALL(mode, round, (number, f));
2295  return DBL2NUM(x / f);
2296  }
2297  return num;
2298 }
2299 
2300 static int
2301 float_round_overflow(int ndigits, int binexp)
2302 {
2303  enum {float_dig = DBL_DIG+2};
2304 
2305 /* Let `exp` be such that `number` is written as:"0.#{digits}e#{exp}",
2306  i.e. such that 10 ** (exp - 1) <= |number| < 10 ** exp
2307  Recall that up to float_dig digits can be needed to represent a double,
2308  so if ndigits + exp >= float_dig, the intermediate value (number * 10 ** ndigits)
2309  will be an integer and thus the result is the original number.
2310  If ndigits + exp <= 0, the result is 0 or "1e#{exp}", so
2311  if ndigits + exp < 0, the result is 0.
2312  We have:
2313  2 ** (binexp-1) <= |number| < 2 ** binexp
2314  10 ** ((binexp-1)/log_2(10)) <= |number| < 10 ** (binexp/log_2(10))
2315  If binexp >= 0, and since log_2(10) = 3.322259:
2316  10 ** (binexp/4 - 1) < |number| < 10 ** (binexp/3)
2317  floor(binexp/4) <= exp <= ceil(binexp/3)
2318  If binexp <= 0, swap the /4 and the /3
2319  So if ndigits + floor(binexp/(4 or 3)) >= float_dig, the result is number
2320  If ndigits + ceil(binexp/(3 or 4)) < 0 the result is 0
2321 */
2322  if (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1)) {
2323  return TRUE;
2324  }
2325  return FALSE;
2326 }
2327 
2328 static int
2329 float_round_underflow(int ndigits, int binexp)
2330 {
2331  if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
2332  return TRUE;
2333  }
2334  return FALSE;
2335 }
2336 
2337 /*
2338  * call-seq:
2339  * float.to_i -> integer
2340  * float.to_int -> integer
2341  *
2342  * Returns the +float+ truncated to an Integer.
2343  *
2344  * 1.2.to_i #=> 1
2345  * (-1.2).to_i #=> -1
2346  *
2347  * Note that the limited precision of floating point arithmetic
2348  * might lead to surprising results:
2349  *
2350  * (0.3 / 0.1).to_i #=> 2 (!)
2351  *
2352  * #to_int is an alias for #to_i.
2353  */
2354 
2355 static VALUE
2356 flo_to_i(VALUE num)
2357 {
2358  double f = RFLOAT_VALUE(num);
2359 
2360  if (f > 0.0) f = floor(f);
2361  if (f < 0.0) f = ceil(f);
2362 
2363  return dbl2ival(f);
2364 }
2365 
2366 /*
2367  * call-seq:
2368  * float.truncate([ndigits]) -> integer or float
2369  *
2370  * Returns +float+ truncated (toward zero) to
2371  * a precision of +ndigits+ decimal digits (default: 0).
2372  *
2373  * When the precision is negative, the returned value is an integer
2374  * with at least <code>ndigits.abs</code> trailing zeros.
2375  *
2376  * Returns a floating point number when +ndigits+ is positive,
2377  * otherwise returns an integer.
2378  *
2379  * 2.8.truncate #=> 2
2380  * (-2.8).truncate #=> -2
2381  * 1.234567.truncate(2) #=> 1.23
2382  * 34567.89.truncate(-2) #=> 34500
2383  *
2384  * Note that the limited precision of floating point arithmetic
2385  * might lead to surprising results:
2386  *
2387  * (0.3 / 0.1).truncate #=> 2 (!)
2388  */
2389 static VALUE
2390 flo_truncate(int argc, VALUE *argv, VALUE num)
2391 {
2392  if (signbit(RFLOAT_VALUE(num)))
2393  return flo_ceil(argc, argv, num);
2394  else
2395  return flo_floor(argc, argv, num);
2396 }
2397 
2398 /*
2399  * call-seq:
2400  * float.positive? -> true or false
2401  *
2402  * Returns +true+ if +float+ is greater than 0.
2403  */
2404 
2405 static VALUE
2406 flo_positive_p(VALUE num)
2407 {
2408  double f = RFLOAT_VALUE(num);
2409  return f > 0.0 ? Qtrue : Qfalse;
2410 }
2411 
2412 /*
2413  * call-seq:
2414  * float.negative? -> true or false
2415  *
2416  * Returns +true+ if +float+ is less than 0.
2417  */
2418 
2419 static VALUE
2420 flo_negative_p(VALUE num)
2421 {
2422  double f = RFLOAT_VALUE(num);
2423  return f < 0.0 ? Qtrue : Qfalse;
2424 }
2425 
2426 /*
2427  * call-seq:
2428  * num.floor([ndigits]) -> integer or float
2429  *
2430  * Returns the largest number less than or equal to +num+ with
2431  * a precision of +ndigits+ decimal digits (default: 0).
2432  *
2433  * Numeric implements this by converting its value to a Float and
2434  * invoking Float#floor.
2435  */
2436 
2437 static VALUE
2438 num_floor(int argc, VALUE *argv, VALUE num)
2439 {
2440  return flo_floor(argc, argv, rb_Float(num));
2441 }
2442 
2443 /*
2444  * call-seq:
2445  * num.ceil([ndigits]) -> integer or float
2446  *
2447  * Returns the smallest number greater than or equal to +num+ with
2448  * a precision of +ndigits+ decimal digits (default: 0).
2449  *
2450  * Numeric implements this by converting its value to a Float and
2451  * invoking Float#ceil.
2452  */
2453 
2454 static VALUE
2455 num_ceil(int argc, VALUE *argv, VALUE num)
2456 {
2457  return flo_ceil(argc, argv, rb_Float(num));
2458 }
2459 
2460 /*
2461  * call-seq:
2462  * num.round([ndigits]) -> integer or float
2463  *
2464  * Returns +num+ rounded to the nearest value with
2465  * a precision of +ndigits+ decimal digits (default: 0).
2466  *
2467  * Numeric implements this by converting its value to a Float and
2468  * invoking Float#round.
2469  */
2470 
2471 static VALUE
2472 num_round(int argc, VALUE* argv, VALUE num)
2473 {
2474  return flo_round(argc, argv, rb_Float(num));
2475 }
2476 
2477 /*
2478  * call-seq:
2479  * num.truncate([ndigits]) -> integer or float
2480  *
2481  * Returns +num+ truncated (toward zero) to
2482  * a precision of +ndigits+ decimal digits (default: 0).
2483  *
2484  * Numeric implements this by converting its value to a Float and
2485  * invoking Float#truncate.
2486  */
2487 
2488 static VALUE
2489 num_truncate(int argc, VALUE *argv, VALUE num)
2490 {
2491  return flo_truncate(argc, argv, rb_Float(num));
2492 }
2493 
2494 double
2495 ruby_float_step_size(double beg, double end, double unit, int excl)
2496 {
2497  const double epsilon = DBL_EPSILON;
2498  double n, err;
2499 
2500  if (unit == 0) {
2501  return HUGE_VAL;
2502  }
2503  n= (end - beg)/unit;
2504  err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
2505  if (isinf(unit)) {
2506  return unit > 0 ? beg <= end : beg >= end;
2507  }
2508  if (err>0.5) err=0.5;
2509  if (excl) {
2510  if (n<=0) return 0;
2511  if (n<1)
2512  n = 0;
2513  else
2514  n = floor(n - err);
2515  }
2516  else {
2517  if (n<0) return 0;
2518  n = floor(n + err);
2519  }
2520  return n+1;
2521 }
2522 
2523 int
2524 ruby_float_step(VALUE from, VALUE to, VALUE step, int excl, int allow_endless)
2525 {
2526  if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
2527  double unit = NUM2DBL(step);
2528  double beg = NUM2DBL(from);
2529  double end = (allow_endless && NIL_P(to)) ? (unit < 0 ? -1 : 1)*HUGE_VAL : NUM2DBL(to);
2530  double n = ruby_float_step_size(beg, end, unit, excl);
2531  long i;
2532 
2533  if (isinf(unit)) {
2534  /* if unit is infinity, i*unit+beg is NaN */
2535  if (n) rb_yield(DBL2NUM(beg));
2536  }
2537  else if (unit == 0) {
2538  VALUE val = DBL2NUM(beg);
2539  for (;;)
2540  rb_yield(val);
2541  }
2542  else {
2543  for (i=0; i<n; i++) {
2544  double d = i*unit+beg;
2545  if (unit >= 0 ? end < d : d < end) d = end;
2546  rb_yield(DBL2NUM(d));
2547  }
2548  }
2549  return TRUE;
2550  }
2551  return FALSE;
2552 }
2553 
2554 VALUE
2556 {
2557  if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
2558  long delta, diff;
2559 
2560  diff = FIX2LONG(step);
2561  if (diff == 0) {
2562  return DBL2NUM(HUGE_VAL);
2563  }
2564  delta = FIX2LONG(to) - FIX2LONG(from);
2565  if (diff < 0) {
2566  diff = -diff;
2567  delta = -delta;
2568  }
2569  if (excl) {
2570  delta--;
2571  }
2572  if (delta < 0) {
2573  return INT2FIX(0);
2574  }
2575  return ULONG2NUM(delta / diff + 1UL);
2576  }
2577  else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
2578  double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
2579 
2580  if (isinf(n)) return DBL2NUM(n);
2581  if (POSFIXABLE(n)) return LONG2FIX(n);
2582  return rb_dbl2big(n);
2583  }
2584  else {
2585  VALUE result;
2586  ID cmp = '>';
2587  switch (rb_cmpint(rb_num_coerce_cmp(step, INT2FIX(0), id_cmp), step, INT2FIX(0))) {
2588  case 0: return DBL2NUM(HUGE_VAL);
2589  case -1: cmp = '<'; break;
2590  }
2591  if (RTEST(rb_funcall(from, cmp, 1, to))) return INT2FIX(0);
2592  result = rb_funcall(rb_funcall(to, '-', 1, from), id_div, 1, step);
2593  if (!excl || RTEST(rb_funcall(rb_funcall(from, '+', 1, rb_funcall(result, '*', 1, step)), cmp, 1, to))) {
2594  result = rb_funcall(result, '+', 1, INT2FIX(1));
2595  }
2596  return result;
2597  }
2598 }
2599 
2600 static int
2601 num_step_negative_p(VALUE num)
2602 {
2603  const ID mid = '<';
2604  VALUE zero = INT2FIX(0);
2605  VALUE r;
2606 
2607  if (FIXNUM_P(num)) {
2609  return (SIGNED_VALUE)num < 0;
2610  }
2611  else if (RB_TYPE_P(num, T_BIGNUM)) {
2613  return BIGNUM_NEGATIVE_P(num);
2614  }
2615 
2616  r = rb_check_funcall(num, '>', 1, &zero);
2617  if (r == Qundef) {
2618  coerce_failed(num, INT2FIX(0));
2619  }
2620  return !RTEST(r);
2621 }
2622 
2623 static int
2624 num_step_extract_args(int argc, const VALUE *argv, VALUE *to, VALUE *step, VALUE *by)
2625 {
2626  VALUE hash;
2627 
2628  argc = rb_scan_args(argc, argv, "02:", to, step, &hash);
2629  if (!NIL_P(hash)) {
2630  ID keys[2];
2631  VALUE values[2];
2632  keys[0] = id_to;
2633  keys[1] = id_by;
2634  rb_get_kwargs(hash, keys, 0, 2, values);
2635  if (values[0] != Qundef) {
2636  if (argc > 0) rb_raise(rb_eArgError, "to is given twice");
2637  *to = values[0];
2638  }
2639  if (values[1] != Qundef) {
2640  if (argc > 1) rb_raise(rb_eArgError, "step is given twice");
2641  *by = values[1];
2642  }
2643  }
2644 
2645  return argc;
2646 }
2647 
2648 static int
2649 num_step_check_fix_args(int argc, VALUE *to, VALUE *step, VALUE by, int fix_nil, int allow_zero_step)
2650 {
2651  int desc;
2652  if (by != Qundef) {
2653  *step = by;
2654  }
2655  else {
2656  /* compatibility */
2657  if (argc > 1 && NIL_P(*step)) {
2658  rb_raise(rb_eTypeError, "step must be numeric");
2659  }
2660  if (!allow_zero_step && rb_equal(*step, INT2FIX(0))) {
2661  rb_raise(rb_eArgError, "step can't be 0");
2662  }
2663  }
2664  if (NIL_P(*step)) {
2665  *step = INT2FIX(1);
2666  }
2667  desc = num_step_negative_p(*step);
2668  if (fix_nil && NIL_P(*to)) {
2669  *to = desc ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
2670  }
2671  return desc;
2672 }
2673 
2674 static int
2675 num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step, int fix_nil, int allow_zero_step)
2676 {
2677  VALUE by = Qundef;
2678  argc = num_step_extract_args(argc, argv, to, step, &by);
2679  return num_step_check_fix_args(argc, to, step, by, fix_nil, allow_zero_step);
2680 }
2681 
2682 static VALUE
2683 num_step_size(VALUE from, VALUE args, VALUE eobj)
2684 {
2685  VALUE to, step;
2686  int argc = args ? RARRAY_LENINT(args) : 0;
2687  const VALUE *argv = args ? RARRAY_CONST_PTR(args) : 0;
2688 
2689  num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
2690 
2691  return ruby_num_interval_step_size(from, to, step, FALSE);
2692 }
2693 
2694 /*
2695  * call-seq:
2696  * num.step(by: step, to: limit) {|i| block } -> self
2697  * num.step(by: step, to: limit) -> an_enumerator
2698  * num.step(by: step, to: limit) -> an_arithmetic_sequence
2699  * num.step(limit=nil, step=1) {|i| block } -> self
2700  * num.step(limit=nil, step=1) -> an_enumerator
2701  * num.step(limit=nil, step=1) -> an_arithmetic_sequence
2702  *
2703  * Invokes the given block with the sequence of numbers starting at +num+,
2704  * incremented by +step+ (defaulted to +1+) on each call.
2705  *
2706  * The loop finishes when the value to be passed to the block is greater than
2707  * +limit+ (if +step+ is positive) or less than +limit+ (if +step+ is
2708  * negative), where +limit+ is defaulted to infinity.
2709  *
2710  * In the recommended keyword argument style, either or both of
2711  * +step+ and +limit+ (default infinity) can be omitted. In the
2712  * fixed position argument style, zero as a step
2713  * (i.e. <code>num.step(limit, 0)</code>) is not allowed for historical
2714  * compatibility reasons.
2715  *
2716  * If all the arguments are integers, the loop operates using an integer
2717  * counter.
2718  *
2719  * If any of the arguments are floating point numbers, all are converted
2720  * to floats, and the loop is executed
2721  * <i>floor(n + n*Float::EPSILON) + 1</i> times,
2722  * where <i>n = (limit - num)/step</i>.
2723  *
2724  * Otherwise, the loop starts at +num+, uses either the
2725  * less-than (<code><</code>) or greater-than (<code>></code>) operator
2726  * to compare the counter against +limit+,
2727  * and increments itself using the <code>+</code> operator.
2728  *
2729  * If no block is given, an Enumerator is returned instead.
2730  * Especially, the enumerator is an Enumerator::ArithmeticSequence
2731  * if both +limit+ and +step+ are kind of Numeric or <code>nil</code>.
2732  *
2733  * For example:
2734  *
2735  * p 1.step.take(4)
2736  * p 10.step(by: -1).take(4)
2737  * 3.step(to: 5) {|i| print i, " " }
2738  * 1.step(10, 2) {|i| print i, " " }
2739  * Math::E.step(to: Math::PI, by: 0.2) {|f| print f, " " }
2740  *
2741  * Will produce:
2742  *
2743  * [1, 2, 3, 4]
2744  * [10, 9, 8, 7]
2745  * 3 4 5
2746  * 1 3 5 7 9
2747  * 2.718281828459045 2.9182818284590453 3.118281828459045
2748  */
2749 
2750 static VALUE
2751 num_step(int argc, VALUE *argv, VALUE from)
2752 {
2753  VALUE to, step;
2754  int desc, inf;
2755 
2756  if (!rb_block_given_p()) {
2757  VALUE by = Qundef;
2758 
2759  num_step_extract_args(argc, argv, &to, &step, &by);
2760  if (by != Qundef) {
2761  step = by;
2762  }
2763  if (NIL_P(step)) {
2764  step = INT2FIX(1);
2765  }
2766  if ((NIL_P(to) || rb_obj_is_kind_of(to, rb_cNumeric)) &&
2767  rb_obj_is_kind_of(step, rb_cNumeric)) {
2769  num_step_size, from, to, step, FALSE);
2770  }
2771 
2772  return SIZED_ENUMERATOR(from, 2, ((VALUE [2]){to, step}), num_step_size);
2773  }
2774 
2775  desc = num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
2776  if (rb_equal(step, INT2FIX(0))) {
2777  inf = 1;
2778  }
2779  else if (RB_TYPE_P(to, T_FLOAT)) {
2780  double f = RFLOAT_VALUE(to);
2781  inf = isinf(f) && (signbit(f) ? desc : !desc);
2782  }
2783  else inf = 0;
2784 
2785  if (FIXNUM_P(from) && (inf || FIXNUM_P(to)) && FIXNUM_P(step)) {
2786  long i = FIX2LONG(from);
2787  long diff = FIX2LONG(step);
2788 
2789  if (inf) {
2790  for (;; i += diff)
2791  rb_yield(LONG2FIX(i));
2792  }
2793  else {
2794  long end = FIX2LONG(to);
2795 
2796  if (desc) {
2797  for (; i >= end; i += diff)
2798  rb_yield(LONG2FIX(i));
2799  }
2800  else {
2801  for (; i <= end; i += diff)
2802  rb_yield(LONG2FIX(i));
2803  }
2804  }
2805  }
2806  else if (!ruby_float_step(from, to, step, FALSE, FALSE)) {
2807  VALUE i = from;
2808 
2809  if (inf) {
2810  for (;; i = rb_funcall(i, '+', 1, step))
2811  rb_yield(i);
2812  }
2813  else {
2814  ID cmp = desc ? '<' : '>';
2815 
2816  for (; !RTEST(rb_funcall(i, cmp, 1, to)); i = rb_funcall(i, '+', 1, step))
2817  rb_yield(i);
2818  }
2819  }
2820  return from;
2821 }
2822 
2823 static char *
2824 out_of_range_float(char (*pbuf)[24], VALUE val)
2825 {
2826  char *const buf = *pbuf;
2827  char *s;
2828 
2829  snprintf(buf, sizeof(*pbuf), "%-.10g", RFLOAT_VALUE(val));
2830  if ((s = strchr(buf, ' ')) != 0) *s = '\0';
2831  return buf;
2832 }
2833 
2834 #define FLOAT_OUT_OF_RANGE(val, type) do { \
2835  char buf[24]; \
2836  rb_raise(rb_eRangeError, "float %s out of range of "type, \
2837  out_of_range_float(&buf, (val))); \
2838 } while (0)
2839 
2840 #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
2841 #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
2842 #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
2843 #define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
2844  (LONG_MIN_MINUS_ONE == (double)LONG_MIN ? \
2845  LONG_MIN <= (n): \
2846  LONG_MIN_MINUS_ONE < (n))
2847 
2848 long
2850 {
2851  again:
2852  if (NIL_P(val)) {
2853  rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
2854  }
2855 
2856  if (FIXNUM_P(val)) return FIX2LONG(val);
2857 
2858  else if (RB_TYPE_P(val, T_FLOAT)) {
2859  if (RFLOAT_VALUE(val) < LONG_MAX_PLUS_ONE
2861  return (long)RFLOAT_VALUE(val);
2862  }
2863  else {
2864  FLOAT_OUT_OF_RANGE(val, "integer");
2865  }
2866  }
2867  else if (RB_TYPE_P(val, T_BIGNUM)) {
2868  return rb_big2long(val);
2869  }
2870  else {
2871  val = rb_to_int(val);
2872  goto again;
2873  }
2874 }
2875 
2876 static unsigned long
2877 rb_num2ulong_internal(VALUE val, int *wrap_p)
2878 {
2879  again:
2880  if (NIL_P(val)) {
2881  rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
2882  }
2883 
2884  if (FIXNUM_P(val)) {
2885  long l = FIX2LONG(val); /* this is FIX2LONG, intended */
2886  if (wrap_p)
2887  *wrap_p = l < 0;
2888  return (unsigned long)l;
2889  }
2890  else if (RB_TYPE_P(val, T_FLOAT)) {
2891  double d = RFLOAT_VALUE(val);
2893  if (wrap_p)
2894  *wrap_p = d <= -1.0; /* NUM2ULONG(v) uses v.to_int conceptually. */
2895  if (0 <= d)
2896  return (unsigned long)d;
2897  return (unsigned long)(long)d;
2898  }
2899  else {
2900  FLOAT_OUT_OF_RANGE(val, "integer");
2901  }
2902  }
2903  else if (RB_TYPE_P(val, T_BIGNUM)) {
2904  {
2905  unsigned long ul = rb_big2ulong(val);
2906  if (wrap_p)
2907  *wrap_p = BIGNUM_NEGATIVE_P(val);
2908  return ul;
2909  }
2910  }
2911  else {
2912  val = rb_to_int(val);
2913  goto again;
2914  }
2915 }
2916 
2917 unsigned long
2919 {
2920  return rb_num2ulong_internal(val, NULL);
2921 }
2922 
2923 #if SIZEOF_INT < SIZEOF_LONG
2924 void
2925 rb_out_of_int(SIGNED_VALUE num)
2926 {
2927  rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `int'",
2928  num, num < 0 ? "small" : "big");
2929 }
2930 
2931 static void
2932 check_int(long num)
2933 {
2934  if ((long)(int)num != num) {
2935  rb_out_of_int(num);
2936  }
2937 }
2938 
2939 static void
2940 check_uint(unsigned long num, int sign)
2941 {
2942  if (sign) {
2943  /* minus */
2944  if (num < (unsigned long)INT_MIN)
2945  rb_raise(rb_eRangeError, "integer %ld too small to convert to `unsigned int'", (long)num);
2946  }
2947  else {
2948  /* plus */
2949  if (UINT_MAX < num)
2950  rb_raise(rb_eRangeError, "integer %lu too big to convert to `unsigned int'", num);
2951  }
2952 }
2953 
2954 long
2955 rb_num2int(VALUE val)
2956 {
2957  long num = rb_num2long(val);
2958 
2959  check_int(num);
2960  return num;
2961 }
2962 
2963 long
2964 rb_fix2int(VALUE val)
2965 {
2966  long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
2967 
2968  check_int(num);
2969  return num;
2970 }
2971 
2972 unsigned long
2973 rb_num2uint(VALUE val)
2974 {
2975  int wrap;
2976  unsigned long num = rb_num2ulong_internal(val, &wrap);
2977 
2978  check_uint(num, wrap);
2979  return num;
2980 }
2981 
2982 unsigned long
2983 rb_fix2uint(VALUE val)
2984 {
2985  unsigned long num;
2986 
2987  if (!FIXNUM_P(val)) {
2988  return rb_num2uint(val);
2989  }
2990  num = FIX2ULONG(val);
2991 
2992  check_uint(num, rb_num_negative_int_p(val));
2993  return num;
2994 }
2995 #else
2996 long
2998 {
2999  return rb_num2long(val);
3000 }
3001 
3002 long
3004 {
3005  return FIX2INT(val);
3006 }
3007 #endif
3008 
3009 NORETURN(static void rb_out_of_short(SIGNED_VALUE num));
3010 static void
3011 rb_out_of_short(SIGNED_VALUE num)
3012 {
3013  rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `short'",
3014  num, num < 0 ? "small" : "big");
3015 }
3016 
3017 static void
3018 check_short(long num)
3019 {
3020  if ((long)(short)num != num) {
3021  rb_out_of_short(num);
3022  }
3023 }
3024 
3025 static void
3026 check_ushort(unsigned long num, int sign)
3027 {
3028  if (sign) {
3029  /* minus */
3030  if (num < (unsigned long)SHRT_MIN)
3031  rb_raise(rb_eRangeError, "integer %ld too small to convert to `unsigned short'", (long)num);
3032  }
3033  else {
3034  /* plus */
3035  if (USHRT_MAX < num)
3036  rb_raise(rb_eRangeError, "integer %lu too big to convert to `unsigned short'", num);
3037  }
3038 }
3039 
3040 short
3042 {
3043  long num = rb_num2long(val);
3044 
3045  check_short(num);
3046  return num;
3047 }
3048 
3049 short
3051 {
3052  long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
3053 
3054  check_short(num);
3055  return num;
3056 }
3057 
3058 unsigned short
3060 {
3061  int wrap;
3062  unsigned long num = rb_num2ulong_internal(val, &wrap);
3063 
3064  check_ushort(num, wrap);
3065  return num;
3066 }
3067 
3068 unsigned short
3070 {
3071  unsigned long num;
3072 
3073  if (!FIXNUM_P(val)) {
3074  return rb_num2ushort(val);
3075  }
3076  num = FIX2ULONG(val);
3077 
3078  check_ushort(num, rb_num_negative_int_p(val));
3079  return num;
3080 }
3081 
3082 VALUE
3084 {
3085  long v;
3086 
3087  if (FIXNUM_P(val)) return val;
3088 
3089  v = rb_num2long(val);
3090  if (!FIXABLE(v))
3091  rb_raise(rb_eRangeError, "integer %ld out of range of fixnum", v);
3092  return LONG2FIX(v);
3093 }
3094 
3095 #if HAVE_LONG_LONG
3096 
3097 #define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
3098 #define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
3099 #define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
3100 #ifndef ULLONG_MAX
3101 #define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
3102 #endif
3103 #define LLONG_MIN_MINUS_ONE_IS_LESS_THAN(n) \
3104  (LLONG_MIN_MINUS_ONE == (double)LLONG_MIN ? \
3105  LLONG_MIN <= (n): \
3106  LLONG_MIN_MINUS_ONE < (n))
3107 
3108 LONG_LONG
3109 rb_num2ll(VALUE val)
3110 {
3111  if (NIL_P(val)) {
3112  rb_raise(rb_eTypeError, "no implicit conversion from nil");
3113  }
3114 
3115  if (FIXNUM_P(val)) return (LONG_LONG)FIX2LONG(val);
3116 
3117  else if (RB_TYPE_P(val, T_FLOAT)) {
3118  double d = RFLOAT_VALUE(val);
3119  if (d < LLONG_MAX_PLUS_ONE && (LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d))) {
3120  return (LONG_LONG)d;
3121  }
3122  else {
3123  FLOAT_OUT_OF_RANGE(val, "long long");
3124  }
3125  }
3126  else if (RB_TYPE_P(val, T_BIGNUM)) {
3127  return rb_big2ll(val);
3128  }
3129  else if (RB_TYPE_P(val, T_STRING)) {
3130  rb_raise(rb_eTypeError, "no implicit conversion from string");
3131  }
3132  else if (RB_TYPE_P(val, T_TRUE) || RB_TYPE_P(val, T_FALSE)) {
3133  rb_raise(rb_eTypeError, "no implicit conversion from boolean");
3134  }
3135 
3136  val = rb_to_int(val);
3137  return NUM2LL(val);
3138 }
3139 
3140 unsigned LONG_LONG
3141 rb_num2ull(VALUE val)
3142 {
3143  if (RB_TYPE_P(val, T_NIL)) {
3144  rb_raise(rb_eTypeError, "no implicit conversion from nil");
3145  }
3146  else if (RB_TYPE_P(val, T_FIXNUM)) {
3147  return (LONG_LONG)FIX2LONG(val); /* this is FIX2LONG, intended */
3148  }
3149  else if (RB_TYPE_P(val, T_FLOAT)) {
3150  double d = RFLOAT_VALUE(val);
3151  if (d < ULLONG_MAX_PLUS_ONE && LLONG_MIN_MINUS_ONE_IS_LESS_THAN(d)) {
3152  if (0 <= d)
3153  return (unsigned LONG_LONG)d;
3154  return (unsigned LONG_LONG)(LONG_LONG)d;
3155  }
3156  else {
3157  FLOAT_OUT_OF_RANGE(val, "unsigned long long");
3158  }
3159  }
3160  else if (RB_TYPE_P(val, T_BIGNUM)) {
3161  return rb_big2ull(val);
3162  }
3163  else if (RB_TYPE_P(val, T_STRING)) {
3164  rb_raise(rb_eTypeError, "no implicit conversion from string");
3165  }
3166  else if (RB_TYPE_P(val, T_TRUE) || RB_TYPE_P(val, T_FALSE)) {
3167  rb_raise(rb_eTypeError, "no implicit conversion from boolean");
3168  }
3169 
3170  val = rb_to_int(val);
3171  return NUM2ULL(val);
3172 }
3173 
3174 #endif /* HAVE_LONG_LONG */
3175 
3176 /********************************************************************
3177  *
3178  * Document-class: Integer
3179  *
3180  * Holds Integer values. You cannot add a singleton method to an
3181  * Integer object, any attempt to do so will raise a TypeError.
3182  *
3183  */
3184 
3185 /*
3186  * call-seq:
3187  * int.to_i -> integer
3188  * int.to_int -> integer
3189  *
3190  * Since +int+ is already an Integer, returns +self+.
3191  *
3192  * #to_int is an alias for #to_i.
3193  */
3194 
3195 static VALUE
3196 int_to_i(VALUE num)
3197 {
3198  return num;
3199 }
3200 
3201 /*
3202  * call-seq:
3203  * int.integer? -> true
3204  *
3205  * Since +int+ is already an Integer, this always returns +true+.
3206  */
3207 
3208 static VALUE
3209 int_int_p(VALUE num)
3210 {
3211  return Qtrue;
3212 }
3213 
3214 /*
3215  * call-seq:
3216  * int.odd? -> true or false
3217  *
3218  * Returns +true+ if +int+ is an odd number.
3219  */
3220 
3221 VALUE
3223 {
3224  if (FIXNUM_P(num)) {
3225  if (num & 2) {
3226  return Qtrue;
3227  }
3228  }
3229  else if (RB_TYPE_P(num, T_BIGNUM)) {
3230  return rb_big_odd_p(num);
3231  }
3232  else if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
3233  return Qtrue;
3234  }
3235  return Qfalse;
3236 }
3237 
3238 /*
3239  * call-seq:
3240  * int.even? -> true or false
3241  *
3242  * Returns +true+ if +int+ is an even number.
3243  */
3244 
3245 static VALUE
3246 int_even_p(VALUE num)
3247 {
3248  if (FIXNUM_P(num)) {
3249  if ((num & 2) == 0) {
3250  return Qtrue;
3251  }
3252  }
3253  else if (RB_TYPE_P(num, T_BIGNUM)) {
3254  return rb_big_even_p(num);
3255  }
3256  else if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
3257  return Qtrue;
3258  }
3259  return Qfalse;
3260 }
3261 
3262 /*
3263  * call-seq:
3264  * int.allbits?(mask) -> true or false
3265  *
3266  * Returns +true+ if all bits of <code>+int+ & +mask+</code> are 1.
3267  */
3268 
3269 static VALUE
3270 int_allbits_p(VALUE num, VALUE mask)
3271 {
3272  mask = rb_to_int(mask);
3273  return rb_int_equal(rb_int_and(num, mask), mask);
3274 }
3275 
3276 /*
3277  * call-seq:
3278  * int.anybits?(mask) -> true or false
3279  *
3280  * Returns +true+ if any bits of <code>+int+ & +mask+</code> are 1.
3281  */
3282 
3283 static VALUE
3284 int_anybits_p(VALUE num, VALUE mask)
3285 {
3286  mask = rb_to_int(mask);
3287  return num_zero_p(rb_int_and(num, mask)) ? Qfalse : Qtrue;
3288 }
3289 
3290 /*
3291  * call-seq:
3292  * int.nobits?(mask) -> true or false
3293  *
3294  * Returns +true+ if no bits of <code>+int+ & +mask+</code> are 1.
3295  */
3296 
3297 static VALUE
3298 int_nobits_p(VALUE num, VALUE mask)
3299 {
3300  mask = rb_to_int(mask);
3301  return num_zero_p(rb_int_and(num, mask));
3302 }
3303 
3304 /*
3305  * Document-method: Integer#succ
3306  * Document-method: Integer#next
3307  * call-seq:
3308  * int.next -> integer
3309  * int.succ -> integer
3310  *
3311  * Returns the successor of +int+,
3312  * i.e. the Integer equal to <code>int+1</code>.
3313  *
3314  * 1.next #=> 2
3315  * (-1).next #=> 0
3316  * 1.succ #=> 2
3317  * (-1).succ #=> 0
3318  */
3319 
3320 VALUE
3322 {
3323  if (FIXNUM_P(num)) {
3324  long i = FIX2LONG(num) + 1;
3325  return LONG2NUM(i);
3326  }
3327  if (RB_TYPE_P(num, T_BIGNUM)) {
3328  return rb_big_plus(num, INT2FIX(1));
3329  }
3330  return num_funcall1(num, '+', INT2FIX(1));
3331 }
3332 
3333 #define int_succ rb_int_succ
3334 
3335 /*
3336  * call-seq:
3337  * int.pred -> integer
3338  *
3339  * Returns the predecessor of +int+,
3340  * i.e. the Integer equal to <code>int-1</code>.
3341  *
3342  * 1.pred #=> 0
3343  * (-1).pred #=> -2
3344  */
3345 
3346 static VALUE
3347 rb_int_pred(VALUE num)
3348 {
3349  if (FIXNUM_P(num)) {
3350  long i = FIX2LONG(num) - 1;
3351  return LONG2NUM(i);
3352  }
3353  if (RB_TYPE_P(num, T_BIGNUM)) {
3354  return rb_big_minus(num, INT2FIX(1));
3355  }
3356  return num_funcall1(num, '-', INT2FIX(1));
3357 }
3358 
3359 #define int_pred rb_int_pred
3360 
3361 /*
3362  * Document-method: Integer#chr
3363  * call-seq:
3364  * int.chr([encoding]) -> string
3365  *
3366  * Returns a string containing the character represented by the +int+'s value
3367  * according to +encoding+.
3368  *
3369  * 65.chr #=> "A"
3370  * 230.chr #=> "\xE6"
3371  * 255.chr(Encoding::UTF_8) #=> "\u00FF"
3372  */
3373 
3374 VALUE
3375 rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
3376 {
3377  int n;
3378  VALUE str;
3379  switch (n = rb_enc_codelen(code, enc)) {
3381  rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
3382  break;
3384  case 0:
3385  rb_raise(rb_eRangeError, "%u out of char range", code);
3386  break;
3387  }
3388  str = rb_enc_str_new(0, n, enc);
3389  rb_enc_mbcput(code, RSTRING_PTR(str), enc);
3391  rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
3392  }
3393  return str;
3394 }
3395 
3396 static VALUE
3397 int_chr(int argc, VALUE *argv, VALUE num)
3398 {
3399  char c;
3400  unsigned int i;
3401  rb_encoding *enc;
3402 
3403  if (rb_num_to_uint(num, &i) == 0) {
3404  }
3405  else if (FIXNUM_P(num)) {
3406  rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(num));
3407  }
3408  else {
3409  rb_raise(rb_eRangeError, "bignum out of char range");
3410  }
3411 
3412  switch (argc) {
3413  case 0:
3414  if (0xff < i) {
3416  if (!enc) {
3417  rb_raise(rb_eRangeError, "%d out of char range", i);
3418  }
3419  goto decode;
3420  }
3421  c = (char)i;
3422  if (i < 0x80) {
3423  return rb_usascii_str_new(&c, 1);
3424  }
3425  else {
3426  return rb_str_new(&c, 1);
3427  }
3428  case 1:
3429  break;
3430  default:
3431  rb_error_arity(argc, 0, 1);
3432  }
3433  enc = rb_to_encoding(argv[0]);
3434  if (!enc) enc = rb_ascii8bit_encoding();
3435  decode:
3436  return rb_enc_uint_chr(i, enc);
3437 }
3438 
3439 /*
3440  * call-seq:
3441  * int.ord -> self
3442  *
3443  * Returns the +int+ itself.
3444  *
3445  * 97.ord #=> 97
3446  *
3447  * This method is intended for compatibility to character literals
3448  * in Ruby 1.9.
3449  *
3450  * For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
3451  */
3452 
3453 static VALUE
3454 int_ord(VALUE num)
3455 {
3456  return num;
3457 }
3458 
3459 /*
3460  * Fixnum
3461  */
3462 
3463 
3464 /*
3465  * Document-method: Integer#-@
3466  * call-seq:
3467  * -int -> integer
3468  *
3469  * Returns +int+, negated.
3470  */
3471 
3472 static VALUE
3473 fix_uminus(VALUE num)
3474 {
3475  return LONG2NUM(-FIX2LONG(num));
3476 }
3477 
3478 VALUE
3480 {
3481  if (FIXNUM_P(num)) {
3482  return fix_uminus(num);
3483  }
3484  else if (RB_TYPE_P(num, T_BIGNUM)) {
3485  return rb_big_uminus(num);
3486  }
3487  return num_funcall0(num, idUMinus);
3488 }
3489 
3490 /*
3491  * Document-method: Integer#to_s
3492  * call-seq:
3493  * int.to_s(base=10) -> string
3494  *
3495  * Returns a string containing the place-value representation of +int+
3496  * with radix +base+ (between 2 and 36).
3497  *
3498  * 12345.to_s #=> "12345"
3499  * 12345.to_s(2) #=> "11000000111001"
3500  * 12345.to_s(8) #=> "30071"
3501  * 12345.to_s(10) #=> "12345"
3502  * 12345.to_s(16) #=> "3039"
3503  * 12345.to_s(36) #=> "9ix"
3504  * 78546939656932.to_s(36) #=> "rubyrules"
3505  */
3506 
3507 VALUE
3508 rb_fix2str(VALUE x, int base)
3509 {
3510  char buf[SIZEOF_VALUE*CHAR_BIT + 1], *const e = buf + sizeof buf, *b = e;
3511  long val = FIX2LONG(x);
3512  unsigned long u;
3513  int neg = 0;
3514 
3515  if (base < 2 || 36 < base) {
3516  rb_raise(rb_eArgError, "invalid radix %d", base);
3517  }
3518 #if SIZEOF_LONG < SIZEOF_VOIDP
3519 # if SIZEOF_VOIDP == SIZEOF_LONG_LONG
3520  if ((val >= 0 && (x & 0xFFFFFFFF00000000ull)) ||
3521  (val < 0 && (x & 0xFFFFFFFF00000000ull) != 0xFFFFFFFF00000000ull)) {
3522  rb_bug("Unnormalized Fixnum value %p", (void *)x);
3523  }
3524 # else
3525  /* should do something like above code, but currently ruby does not know */
3526  /* such platforms */
3527 # endif
3528 #endif
3529  if (val == 0) {
3530  return rb_usascii_str_new2("0");
3531  }
3532  if (val < 0) {
3533  u = 1 + (unsigned long)(-(val + 1)); /* u = -val avoiding overflow */
3534  neg = 1;
3535  }
3536  else {
3537  u = val;
3538  }
3539  do {
3540  *--b = ruby_digitmap[(int)(u % base)];
3541  } while (u /= base);
3542  if (neg) {
3543  *--b = '-';
3544  }
3545 
3546  return rb_usascii_str_new(b, e - b);
3547 }
3548 
3549 static VALUE
3550 int_to_s(int argc, VALUE *argv, VALUE x)
3551 {
3552  int base;
3553 
3554  if (rb_check_arity(argc, 0, 1))
3555  base = NUM2INT(argv[0]);
3556  else
3557  base = 10;
3558  return rb_int2str(x, base);
3559 }
3560 
3561 VALUE
3562 rb_int2str(VALUE x, int base)
3563 {
3564  if (FIXNUM_P(x)) {
3565  return rb_fix2str(x, base);
3566  }
3567  else if (RB_TYPE_P(x, T_BIGNUM)) {
3568  return rb_big2str(x, base);
3569  }
3570 
3571  return rb_any_to_s(x);
3572 }
3573 
3574 /*
3575  * Document-method: Integer#+
3576  * call-seq:
3577  * int + numeric -> numeric_result
3578  *
3579  * Performs addition: the class of the resulting object depends on
3580  * the class of +numeric+.
3581  */
3582 
3583 static VALUE
3584 fix_plus(VALUE x, VALUE y)
3585 {
3586  if (FIXNUM_P(y)) {
3587  return rb_fix_plus_fix(x, y);
3588  }
3589  else if (RB_TYPE_P(y, T_BIGNUM)) {
3590  return rb_big_plus(y, x);
3591  }
3592  else if (RB_TYPE_P(y, T_FLOAT)) {
3593  return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));
3594  }
3595  else if (RB_TYPE_P(y, T_COMPLEX)) {
3596  return rb_complex_plus(y, x);
3597  }
3598  else {
3599  return rb_num_coerce_bin(x, y, '+');
3600  }
3601 }
3602 
3603 VALUE
3605 {
3606  return fix_plus(x, y);
3607 }
3608 
3609 VALUE
3611 {
3612  if (FIXNUM_P(x)) {
3613  return fix_plus(x, y);
3614  }
3615  else if (RB_TYPE_P(x, T_BIGNUM)) {
3616  return rb_big_plus(x, y);
3617  }
3618  return rb_num_coerce_bin(x, y, '+');
3619 }
3620 
3621 /*
3622  * Document-method: Integer#-
3623  * call-seq:
3624  * int - numeric -> numeric_result
3625  *
3626  * Performs subtraction: the class of the resulting object depends on
3627  * the class of +numeric+.
3628  */
3629 
3630 static VALUE
3631 fix_minus(VALUE x, VALUE y)
3632 {
3633  if (FIXNUM_P(y)) {
3634  return rb_fix_minus_fix(x, y);
3635  }
3636  else if (RB_TYPE_P(y, T_BIGNUM)) {
3637  x = rb_int2big(FIX2LONG(x));
3638  return rb_big_minus(x, y);
3639  }
3640  else if (RB_TYPE_P(y, T_FLOAT)) {
3641  return DBL2NUM((double)FIX2LONG(x) - RFLOAT_VALUE(y));
3642  }
3643  else {
3644  return rb_num_coerce_bin(x, y, '-');
3645  }
3646 }
3647 
3648 VALUE
3650 {
3651  if (FIXNUM_P(x)) {
3652  return fix_minus(x, y);
3653  }
3654  else if (RB_TYPE_P(x, T_BIGNUM)) {
3655  return rb_big_minus(x, y);
3656  }
3657  return rb_num_coerce_bin(x, y, '-');
3658 }
3659 
3660 
3661 #define SQRT_LONG_MAX HALF_LONG_MSB
3662 /*tests if N*N would overflow*/
3663 #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
3664 
3665 /*
3666  * Document-method: Integer#*
3667  * call-seq:
3668  * int * numeric -> numeric_result
3669  *
3670  * Performs multiplication: the class of the resulting object depends on
3671  * the class of +numeric+.
3672  */
3673 
3674 static VALUE
3675 fix_mul(VALUE x, VALUE y)
3676 {
3677  if (FIXNUM_P(y)) {
3678  return rb_fix_mul_fix(x, y);
3679  }
3680  else if (RB_TYPE_P(y, T_BIGNUM)) {
3681  switch (x) {
3682  case INT2FIX(0): return x;
3683  case INT2FIX(1): return y;
3684  }
3685  return rb_big_mul(y, x);
3686  }
3687  else if (RB_TYPE_P(y, T_FLOAT)) {
3688  return DBL2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y));
3689  }
3690  else if (RB_TYPE_P(y, T_COMPLEX)) {
3691  return rb_complex_mul(y, x);
3692  }
3693  else {
3694  return rb_num_coerce_bin(x, y, '*');
3695  }
3696 }
3697 
3698 VALUE
3700 {
3701  if (FIXNUM_P(x)) {
3702  return fix_mul(x, y);
3703  }
3704  else if (RB_TYPE_P(x, T_BIGNUM)) {
3705  return rb_big_mul(x, y);
3706  }
3707  return rb_num_coerce_bin(x, y, '*');
3708 }
3709 
3710 static double
3711 fix_fdiv_double(VALUE x, VALUE y)
3712 {
3713  if (FIXNUM_P(y)) {
3714  return double_div_double(FIX2LONG(x), FIX2LONG(y));
3715  }
3716  else if (RB_TYPE_P(y, T_BIGNUM)) {
3717  return rb_big_fdiv_double(rb_int2big(FIX2LONG(x)), y);
3718  }
3719  else if (RB_TYPE_P(y, T_FLOAT)) {
3720  return double_div_double(FIX2LONG(x), RFLOAT_VALUE(y));
3721  }
3722  else {
3723  return NUM2DBL(rb_num_coerce_bin(x, y, idFdiv));
3724  }
3725 }
3726 
3727 double
3729 {
3730  if (RB_INTEGER_TYPE_P(y) && !FIXNUM_ZERO_P(y)) {
3731  VALUE gcd = rb_gcd(x, y);
3732  if (!FIXNUM_ZERO_P(gcd)) {
3733  x = rb_int_idiv(x, gcd);
3734  y = rb_int_idiv(y, gcd);
3735  }
3736  }
3737  if (FIXNUM_P(x)) {
3738  return fix_fdiv_double(x, y);
3739  }
3740  else if (RB_TYPE_P(x, T_BIGNUM)) {
3741  return rb_big_fdiv_double(x, y);
3742  }
3743  else {
3744  return nan("");
3745  }
3746 }
3747 
3748 /*
3749  * Document-method: Integer#fdiv
3750  * call-seq:
3751  * int.fdiv(numeric) -> float
3752  *
3753  * Returns the floating point result of dividing +int+ by +numeric+.
3754  *
3755  * 654321.fdiv(13731) #=> 47.652829364212366
3756  * 654321.fdiv(13731.24) #=> 47.65199646936475
3757  * -654321.fdiv(13731) #=> -47.652829364212366
3758  */
3759 
3760 VALUE
3762 {
3763  if (RB_INTEGER_TYPE_P(x)) {
3764  return DBL2NUM(rb_int_fdiv_double(x, y));
3765  }
3766  return Qnil;
3767 }
3768 
3769 /*
3770  * Document-method: Integer#/
3771  * call-seq:
3772  * int / numeric -> numeric_result
3773  *
3774  * Performs division: the class of the resulting object depends on
3775  * the class of +numeric+.
3776  */
3777 
3778 static VALUE
3779 fix_divide(VALUE x, VALUE y, ID op)
3780 {
3781  if (FIXNUM_P(y)) {
3782  if (FIXNUM_ZERO_P(y)) rb_num_zerodiv();
3783  return rb_fix_div_fix(x, y);
3784  }
3785  else if (RB_TYPE_P(y, T_BIGNUM)) {
3786  x = rb_int2big(FIX2LONG(x));
3787  return rb_big_div(x, y);
3788  }
3789  else if (RB_TYPE_P(y, T_FLOAT)) {
3790  if (op == '/') {
3791  double d = FIX2LONG(x);
3792  return rb_flo_div_flo(DBL2NUM(d), y);
3793  }
3794  else {
3795  VALUE v;
3796  if (RFLOAT_VALUE(y) == 0) rb_num_zerodiv();
3797  v = fix_divide(x, y, '/');
3798  return flo_floor(0, 0, v);
3799  }
3800  }
3801  else {
3802  if (RB_TYPE_P(y, T_RATIONAL) &&
3803  op == '/' && FIX2LONG(x) == 1)
3804  return rb_rational_reciprocal(y);
3805  return rb_num_coerce_bin(x, y, op);
3806  }
3807 }
3808 
3809 static VALUE
3810 fix_div(VALUE x, VALUE y)
3811 {
3812  return fix_divide(x, y, '/');
3813 }
3814 
3815 VALUE
3817 {
3818  if (FIXNUM_P(x)) {
3819  return fix_div(x, y);
3820  }
3821  else if (RB_TYPE_P(x, T_BIGNUM)) {
3822  return rb_big_div(x, y);
3823  }
3824  return Qnil;
3825 }
3826 
3827 /*
3828  * Document-method: Integer#div
3829  * call-seq:
3830  * int.div(numeric) -> integer
3831  *
3832  * Performs integer division: returns the integer result of dividing +int+
3833  * by +numeric+.
3834  */
3835 
3836 static VALUE
3837 fix_idiv(VALUE x, VALUE y)
3838 {
3839  return fix_divide(x, y, id_div);
3840 }
3841 
3842 VALUE
3844 {
3845  if (FIXNUM_P(x)) {
3846  return fix_idiv(x, y);
3847  }
3848  else if (RB_TYPE_P(x, T_BIGNUM)) {
3849  return rb_big_idiv(x, y);
3850  }
3851  return num_div(x, y);
3852 }
3853 
3854 /*
3855  * Document-method: Integer#%
3856  * Document-method: Integer#modulo
3857  * call-seq:
3858  * int % other -> real
3859  * int.modulo(other) -> real
3860  *
3861  * Returns +int+ modulo +other+.
3862  *
3863  * See Numeric#divmod for more information.
3864  */
3865 
3866 static VALUE
3867 fix_mod(VALUE x, VALUE y)
3868 {
3869  if (FIXNUM_P(y)) {
3870  if (FIXNUM_ZERO_P(y)) rb_num_zerodiv();
3871  return rb_fix_mod_fix(x, y);
3872  }
3873  else if (RB_TYPE_P(y, T_BIGNUM)) {
3874  x = rb_int2big(FIX2LONG(x));
3875  return rb_big_modulo(x, y);
3876  }
3877  else if (RB_TYPE_P(y, T_FLOAT)) {
3878  return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y)));
3879  }
3880  else {
3881  return rb_num_coerce_bin(x, y, '%');
3882  }
3883 }
3884 
3885 VALUE
3887 {
3888  if (FIXNUM_P(x)) {
3889  return fix_mod(x, y);
3890  }
3891  else if (RB_TYPE_P(x, T_BIGNUM)) {
3892  return rb_big_modulo(x, y);
3893  }
3894  return num_modulo(x, y);
3895 }
3896 
3897 /*
3898  * call-seq:
3899  * int.remainder(numeric) -> real
3900  *
3901  * Returns the remainder after dividing +int+ by +numeric+.
3902  *
3903  * <code>x.remainder(y)</code> means <code>x-y*(x/y).truncate</code>.
3904  *
3905  * 5.remainder(3) #=> 2
3906  * -5.remainder(3) #=> -2
3907  * 5.remainder(-3) #=> 2
3908  * -5.remainder(-3) #=> -2
3909  * 5.remainder(1.5) #=> 0.5
3910  *
3911  * See Numeric#divmod.
3912  */
3913 
3914 static VALUE
3915 int_remainder(VALUE x, VALUE y)
3916 {
3917  if (FIXNUM_P(x)) {
3918  return num_remainder(x, y);
3919  }
3920  else if (RB_TYPE_P(x, T_BIGNUM)) {
3921  return rb_big_remainder(x, y);
3922  }
3923  return Qnil;
3924 }
3925 
3926 /*
3927  * Document-method: Integer#divmod
3928  * call-seq:
3929  * int.divmod(numeric) -> array
3930  *
3931  * See Numeric#divmod.
3932  */
3933 static VALUE
3934 fix_divmod(VALUE x, VALUE y)
3935 {
3936  if (FIXNUM_P(y)) {
3937  VALUE div, mod;
3938  if (FIXNUM_ZERO_P(y)) rb_num_zerodiv();
3939  rb_fix_divmod_fix(x, y, &div, &mod);
3940  return rb_assoc_new(div, mod);
3941  }
3942  else if (RB_TYPE_P(y, T_BIGNUM)) {
3943  x = rb_int2big(FIX2LONG(x));
3944  return rb_big_divmod(x, y);
3945  }
3946  else if (RB_TYPE_P(y, T_FLOAT)) {
3947  {
3948  double div, mod;
3949  volatile VALUE a, b;
3950 
3951  flodivmod((double)FIX2LONG(x), RFLOAT_VALUE(y), &div, &mod);
3952  a = dbl2ival(div);
3953  b = DBL2NUM(mod);
3954  return rb_assoc_new(a, b);
3955  }
3956  }
3957  else {
3958  return rb_num_coerce_bin(x, y, id_divmod);
3959  }
3960 }
3961 
3962 VALUE
3964 {
3965  if (FIXNUM_P(x)) {
3966  return fix_divmod(x, y);
3967  }
3968  else if (RB_TYPE_P(x, T_BIGNUM)) {
3969  return rb_big_divmod(x, y);
3970  }
3971  return Qnil;
3972 }
3973 
3974 /*
3975  * Document-method: Integer#**
3976  * call-seq:
3977  * int ** numeric -> numeric_result
3978  *
3979  * Raises +int+ to the power of +numeric+, which may be negative or
3980  * fractional.
3981  * The result may be an Integer, a Float, a Rational, or a complex number.
3982  *
3983  * 2 ** 3 #=> 8
3984  * 2 ** -1 #=> (1/2)
3985  * 2 ** 0.5 #=> 1.4142135623730951
3986  * (-1) ** 0.5 #=> (0.0+1.0i)
3987  *
3988  * 123456789 ** 2 #=> 15241578750190521
3989  * 123456789 ** 1.2 #=> 5126464716.0993185
3990  * 123456789 ** -2 #=> (1/15241578750190521)
3991  */
3992 
3993 static VALUE
3994 int_pow(long x, unsigned long y)
3995 {
3996  int neg = x < 0;
3997  long z = 1;
3998 
3999  if (y == 0) return INT2FIX(1);
4000  if (y == 1) return LONG2NUM(x);
4001  if (neg) x = -x;
4002  if (y & 1)
4003  z = x;
4004  else
4005  neg = 0;
4006  y &= ~1;
4007  do {
4008  while (y % 2 == 0) {
4009  if (!FIT_SQRT_LONG(x)) {
4010  VALUE v;
4011  bignum:
4012  v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
4013  if (RB_FLOAT_TYPE_P(v)) /* infinity due to overflow */
4014  return v;
4015  if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
4016  return v;
4017  }
4018  x = x * x;
4019  y >>= 1;
4020  }
4021  {
4022  if (MUL_OVERFLOW_FIXNUM_P(x, z)) {
4023  goto bignum;
4024  }
4025  z = x * z;
4026  }
4027  } while (--y);
4028  if (neg) z = -z;
4029  return LONG2NUM(z);
4030 }
4031 
4032 VALUE
4033 rb_int_positive_pow(long x, unsigned long y)
4034 {
4035  return int_pow(x, y);
4036 }
4037 
4038 static VALUE
4039 fix_pow(VALUE x, VALUE y)
4040 {
4041  long a = FIX2LONG(x);
4042 
4043  if (FIXNUM_P(y)) {
4044  long b = FIX2LONG(y);
4045 
4046  if (a == 1) return INT2FIX(1);
4047  if (a == -1) {
4048  if (b % 2 == 0)
4049  return INT2FIX(1);
4050  else
4051  return INT2FIX(-1);
4052  }
4053  if (b < 0) {
4054  if (a == 0) rb_num_zerodiv();
4055  y = rb_int_pow(x, LONG2NUM(-b));
4056  goto inverted;
4057  }
4058 
4059  if (b == 0) return INT2FIX(1);
4060  if (b == 1) return x;
4061  if (a == 0) {
4062  if (b > 0) return INT2FIX(0);
4063  return DBL2NUM(HUGE_VAL);
4064  }
4065  return int_pow(a, b);
4066  }
4067  else if (RB_TYPE_P(y, T_BIGNUM)) {
4068  if (a == 1) return INT2FIX(1);
4069  if (a == -1) {
4070  if (int_even_p(y)) return INT2FIX(1);
4071  else return INT2FIX(-1);
4072  }
4073  if (BIGNUM_NEGATIVE_P(y)) {
4074  if (a == 0) rb_num_zerodiv();
4075  y = rb_int_pow(x, rb_big_uminus(y));
4076  inverted:
4077  if (RB_FLOAT_TYPE_P(y)) {
4078  double d = pow((double)a, RFLOAT_VALUE(y));
4079  return DBL2NUM(1.0 / d);
4080  }
4081  return rb_rational_raw(INT2FIX(1), y);
4082  }
4083  if (a == 0) return INT2FIX(0);
4084  x = rb_int2big(FIX2LONG(x));
4085  return rb_big_pow(x, y);
4086  }
4087  else if (RB_TYPE_P(y, T_FLOAT)) {
4088  double dy = RFLOAT_VALUE(y);
4089  if (dy == 0.0) return DBL2NUM(1.0);
4090  if (a == 0) {
4091  return DBL2NUM(dy < 0 ? HUGE_VAL : 0.0);
4092  }
4093  if (a == 1) return DBL2NUM(1.0);
4094  {
4095  if (a < 0 && dy != round(dy))
4096  return rb_dbl_complex_new_polar_pi(pow(-(double)a, dy), dy);
4097  return DBL2NUM(pow((double)a, dy));
4098  }
4099  }
4100  else {
4101  return rb_num_coerce_bin(x, y, idPow);
4102  }
4103 }
4104 
4105 VALUE
4107 {
4108  if (FIXNUM_P(x)) {
4109  return fix_pow(x, y);
4110  }
4111  else if (RB_TYPE_P(x, T_BIGNUM)) {
4112  return rb_big_pow(x, y);
4113  }
4114  return Qnil;
4115 }
4116 
4117 VALUE
4119 {
4120  VALUE z = rb_int_pow(x, y);
4121  if (!NIL_P(z)) return z;
4122  if (RB_FLOAT_TYPE_P(x)) return rb_float_pow(x, y);
4123  if (SPECIAL_CONST_P(x)) return Qnil;
4124  switch (BUILTIN_TYPE(x)) {
4125  case T_COMPLEX:
4126  return rb_complex_pow(x, y);
4127  case T_RATIONAL:
4128  return rb_rational_pow(x, y);
4129  }
4130  return Qnil;
4131 }
4132 
4133 /*
4134  * Document-method: Integer#==
4135  * Document-method: Integer#===
4136  * call-seq:
4137  * int == other -> true or false
4138  *
4139  * Returns +true+ if +int+ equals +other+ numerically.
4140  * Contrast this with Integer#eql?, which requires +other+ to be an Integer.
4141  *
4142  * 1 == 2 #=> false
4143  * 1 == 1.0 #=> true
4144  */
4145 
4146 static VALUE
4147 fix_equal(VALUE x, VALUE y)
4148 {
4149  if (x == y) return Qtrue;
4150  if (FIXNUM_P(y)) return Qfalse;
4151  else if (RB_TYPE_P(y, T_BIGNUM)) {
4152  return rb_big_eq(y, x);
4153  }
4154  else if (RB_TYPE_P(y, T_FLOAT)) {
4155  return rb_integer_float_eq(x, y);
4156  }
4157  else {
4158  return num_equal(x, y);
4159  }
4160 }
4161 
4162 VALUE
4164 {
4165  if (FIXNUM_P(x)) {
4166  return fix_equal(x, y);
4167  }
4168  else if (RB_TYPE_P(x, T_BIGNUM)) {
4169  return rb_big_eq(x, y);
4170  }
4171  return Qnil;
4172 }
4173 
4174 /*
4175  * Document-method: Integer#<=>
4176  * call-seq:
4177  * int <=> numeric -> -1, 0, +1, or nil
4178  *
4179  * Comparison---Returns -1, 0, or +1 depending on whether +int+ is
4180  * less than, equal to, or greater than +numeric+.
4181  *
4182  * This is the basis for the tests in the Comparable module.
4183  *
4184  * +nil+ is returned if the two values are incomparable.
4185  */
4186 
4187 static VALUE
4188 fix_cmp(VALUE x, VALUE y)
4189 {
4190  if (x == y) return INT2FIX(0);
4191  if (FIXNUM_P(y)) {
4192  if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
4193  return INT2FIX(-1);
4194  }
4195  else if (RB_TYPE_P(y, T_BIGNUM)) {
4196  VALUE cmp = rb_big_cmp(y, x);
4197  switch (cmp) {
4198  case INT2FIX(+1): return INT2FIX(-1);
4199  case INT2FIX(-1): return INT2FIX(+1);
4200  }
4201  return cmp;
4202  }
4203  else if (RB_TYPE_P(y, T_FLOAT)) {
4204  return rb_integer_float_cmp(x, y);
4205  }
4206  else {
4207  return rb_num_coerce_cmp(x, y, id_cmp);
4208  }
4209 }
4210 
4211 VALUE
4213 {
4214  if (FIXNUM_P(x)) {
4215  return fix_cmp(x, y);
4216  }
4217  else if (RB_TYPE_P(x, T_BIGNUM)) {
4218  return rb_big_cmp(x, y);
4219  }
4220  else {
4221  rb_raise(rb_eNotImpError, "need to define `<=>' in %s", rb_obj_classname(x));
4222  }
4223 }
4224 
4225 /*
4226  * Document-method: Integer#>
4227  * call-seq:
4228  * int > real -> true or false
4229  *
4230  * Returns +true+ if the value of +int+ is greater than that of +real+.
4231  */
4232 
4233 static VALUE
4234 fix_gt(VALUE x, VALUE y)
4235 {
4236  if (FIXNUM_P(y)) {
4237  if (FIX2LONG(x) > FIX2LONG(y)) return Qtrue;
4238  return Qfalse;
4239  }
4240  else if (RB_TYPE_P(y, T_BIGNUM)) {
4241  return rb_big_cmp(y, x) == INT2FIX(-1) ? Qtrue : Qfalse;
4242  }
4243  else if (RB_TYPE_P(y, T_FLOAT)) {
4244  return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse;
4245  }
4246  else {
4247  return rb_num_coerce_relop(x, y, '>');
4248  }
4249 }
4250 
4251 VALUE
4253 {
4254  if (FIXNUM_P(x)) {
4255  return fix_gt(x, y);
4256  }
4257  else if (RB_TYPE_P(x, T_BIGNUM)) {
4258  return rb_big_gt(x, y);
4259  }
4260  return Qnil;
4261 }
4262 
4263 /*
4264  * Document-method: Integer#>=
4265  * call-seq:
4266  * int >= real -> true or false
4267  *
4268  * Returns +true+ if the value of +int+ is greater than or equal to that of
4269  * +real+.
4270  */
4271 
4272 static VALUE
4273 fix_ge(VALUE x, VALUE y)
4274 {
4275  if (FIXNUM_P(y)) {
4276  if (FIX2LONG(x) >= FIX2LONG(y)) return Qtrue;
4277  return Qfalse;
4278  }
4279  else if (RB_TYPE_P(y, T_BIGNUM)) {
4280  return rb_big_cmp(y, x) != INT2FIX(+1) ? Qtrue : Qfalse;
4281  }
4282  else if (RB_TYPE_P(y, T_FLOAT)) {
4283  VALUE rel = rb_integer_float_cmp(x, y);
4284  return rel == INT2FIX(1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
4285  }
4286  else {
4287  return rb_num_coerce_relop(x, y, idGE);
4288  }
4289 }
4290 
4291 VALUE
4293 {
4294  if (FIXNUM_P(x)) {
4295  return fix_ge(x, y);
4296  }
4297  else if (RB_TYPE_P(x, T_BIGNUM)) {
4298  return rb_big_ge(x, y);
4299  }
4300  return Qnil;
4301 }
4302 
4303 /*
4304  * Document-method: Integer#<
4305  * call-seq:
4306  * int < real -> true or false
4307  *
4308  * Returns +true+ if the value of +int+ is less than that of +real+.
4309  */
4310 
4311 static VALUE
4312 fix_lt(VALUE x, VALUE y)
4313 {
4314  if (FIXNUM_P(y)) {
4315  if (FIX2LONG(x) < FIX2LONG(y)) return Qtrue;
4316  return Qfalse;
4317  }
4318  else if (RB_TYPE_P(y, T_BIGNUM)) {
4319  return rb_big_cmp(y, x) == INT2FIX(+1) ? Qtrue : Qfalse;
4320  }
4321  else if (RB_TYPE_P(y, T_FLOAT)) {
4322  return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse;
4323  }
4324  else {
4325  return rb_num_coerce_relop(x, y, '<');
4326  }
4327 }
4328 
4329 static VALUE
4330 int_lt(VALUE x, VALUE y)
4331 {
4332  if (FIXNUM_P(x)) {
4333  return fix_lt(x, y);
4334  }
4335  else if (RB_TYPE_P(x, T_BIGNUM)) {
4336  return rb_big_lt(x, y);
4337  }
4338  return Qnil;
4339 }
4340 
4341 /*
4342  * Document-method: Integer#<=
4343  * call-seq:
4344  * int <= real -> true or false
4345  *
4346  * Returns +true+ if the value of +int+ is less than or equal to that of
4347  * +real+.
4348  */
4349 
4350 static VALUE
4351 fix_le(VALUE x, VALUE y)
4352 {
4353  if (FIXNUM_P(y)) {
4354  if (FIX2LONG(x) <= FIX2LONG(y)) return Qtrue;
4355  return Qfalse;
4356  }
4357  else if (RB_TYPE_P(y, T_BIGNUM)) {
4358  return rb_big_cmp(y, x) != INT2FIX(-1) ? Qtrue : Qfalse;
4359  }
4360  else if (RB_TYPE_P(y, T_FLOAT)) {
4361  VALUE rel = rb_integer_float_cmp(x, y);
4362  return rel == INT2FIX(-1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
4363  }
4364  else {
4365  return rb_num_coerce_relop(x, y, idLE);
4366  }
4367 }
4368 
4369 static VALUE
4370 int_le(VALUE x, VALUE y)
4371 {
4372  if (FIXNUM_P(x)) {
4373  return fix_le(x, y);
4374  }
4375  else if (RB_TYPE_P(x, T_BIGNUM)) {
4376  return rb_big_le(x, y);
4377  }
4378  return Qnil;
4379 }
4380 
4381 /*
4382  * Document-method: Integer#~
4383  * call-seq:
4384  * ~int -> integer
4385  *
4386  * One's complement: returns a number where each bit is flipped.
4387  *
4388  * Inverts the bits in an Integer. As integers are conceptually of
4389  * infinite length, the result acts as if it had an infinite number of
4390  * one bits to the left. In hex representations, this is displayed
4391  * as two periods to the left of the digits.
4392  *
4393  * sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"
4394  */
4395 
4396 static VALUE
4397 fix_comp(VALUE num)
4398 {
4399  return ~num | FIXNUM_FLAG;
4400 }
4401 
4402 static VALUE
4403 int_comp(VALUE num)
4404 {
4405  if (FIXNUM_P(num)) {
4406  return fix_comp(num);
4407  }
4408  else if (RB_TYPE_P(num, T_BIGNUM)) {
4409  return rb_big_comp(num);
4410  }
4411  return Qnil;
4412 }
4413 
4414 static VALUE
4415 num_funcall_bit_1(VALUE y, VALUE arg, int recursive)
4416 {
4417  ID func = (ID)((VALUE *)arg)[0];
4418  VALUE x = ((VALUE *)arg)[1];
4419  if (recursive) {
4420  num_funcall_op_1_recursion(x, func, y);
4421  }
4422  return rb_check_funcall(x, func, 1, &y);
4423 }
4424 
4425 VALUE
4427 {
4428  VALUE ret, args[3];
4429 
4430  args[0] = (VALUE)func;
4431  args[1] = x;
4432  args[2] = y;
4433  do_coerce(&args[1], &args[2], TRUE);
4434  ret = rb_exec_recursive_paired(num_funcall_bit_1,
4435  args[2], args[1], (VALUE)args);
4436  if (ret == Qundef) {
4437  /* show the original object, not coerced object */
4438  coerce_failed(x, y);
4439  }
4440  return ret;
4441 }
4442 
4443 /*
4444  * Document-method: Integer#&
4445  * call-seq:
4446  * int & other_int -> integer
4447  *
4448  * Bitwise AND.
4449  */
4450 
4451 static VALUE
4452 fix_and(VALUE x, VALUE y)
4453 {
4454  if (FIXNUM_P(y)) {
4455  long val = FIX2LONG(x) & FIX2LONG(y);
4456  return LONG2NUM(val);
4457  }
4458 
4459  if (RB_TYPE_P(y, T_BIGNUM)) {
4460  return rb_big_and(y, x);
4461  }
4462 
4463  return rb_num_coerce_bit(x, y, '&');
4464 }
4465 
4466 VALUE
4468 {
4469  if (FIXNUM_P(x)) {
4470  return fix_and(x, y);
4471  }
4472  else if (RB_TYPE_P(x, T_BIGNUM)) {
4473  return rb_big_and(x, y);
4474  }
4475  return Qnil;
4476 }
4477 
4478 /*
4479  * Document-method: Integer#|
4480  * call-seq:
4481  * int | other_int -> integer
4482  *
4483  * Bitwise OR.
4484  */
4485 
4486 static VALUE
4487 fix_or(VALUE x, VALUE y)
4488 {
4489  if (FIXNUM_P(y)) {
4490  long val = FIX2LONG(x) | FIX2LONG(y);
4491  return LONG2NUM(val);
4492  }
4493 
4494  if (RB_TYPE_P(y, T_BIGNUM)) {
4495  return rb_big_or(y, x);
4496  }
4497 
4498  return rb_num_coerce_bit(x, y, '|');
4499 }
4500 
4501 static VALUE
4502 int_or(VALUE x, VALUE y)
4503 {
4504  if (FIXNUM_P(x)) {
4505  return fix_or(x, y);
4506  }
4507  else if (RB_TYPE_P(x, T_BIGNUM)) {
4508  return rb_big_or(x, y);
4509  }
4510  return Qnil;
4511 }
4512 
4513 /*
4514  * Document-method: Integer#^
4515  * call-seq:
4516  * int ^ other_int -> integer
4517  *
4518  * Bitwise EXCLUSIVE OR.
4519  */
4520 
4521 static VALUE
4522 fix_xor(VALUE x, VALUE y)
4523 {
4524  if (FIXNUM_P(y)) {
4525  long val = FIX2LONG(x) ^ FIX2LONG(y);
4526  return LONG2NUM(val);
4527  }
4528 
4529  if (RB_TYPE_P(y, T_BIGNUM)) {
4530  return rb_big_xor(y, x);
4531  }
4532 
4533  return rb_num_coerce_bit(x, y, '^');
4534 }
4535 
4536 static VALUE
4537 int_xor(VALUE x, VALUE y)
4538 {
4539  if (FIXNUM_P(x)) {
4540  return fix_xor(x, y);
4541  }
4542  else if (RB_TYPE_P(x, T_BIGNUM)) {
4543  return rb_big_xor(x, y);
4544  }
4545  return Qnil;
4546 }
4547 
4548 /*
4549  * Document-method: Integer#<<
4550  * call-seq:
4551  * int << count -> integer
4552  *
4553  * Returns +int+ shifted left +count+ positions, or right if +count+
4554  * is negative.
4555  */
4556 
4557 static VALUE
4558 rb_fix_lshift(VALUE x, VALUE y)
4559 {
4560  long val, width;
4561 
4562  val = NUM2LONG(x);
4563  if (!FIXNUM_P(y))
4564  return rb_big_lshift(rb_int2big(val), y);
4565  width = FIX2LONG(y);
4566  if (width < 0)
4567  return fix_rshift(val, (unsigned long)-width);
4568  return fix_lshift(val, width);
4569 }
4570 
4571 static VALUE
4572 fix_lshift(long val, unsigned long width)
4573 {
4574  if (width > (SIZEOF_LONG*CHAR_BIT-1)
4575  || ((unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) {
4576  return rb_big_lshift(rb_int2big(val), ULONG2NUM(width));
4577  }
4578  val = val << width;
4579  return LONG2NUM(val);
4580 }
4581 
4582 VALUE
4584 {
4585  if (FIXNUM_P(x)) {
4586  return rb_fix_lshift(x, y);
4587  }
4588  else if (RB_TYPE_P(x, T_BIGNUM)) {
4589  return rb_big_lshift(x, y);
4590  }
4591  return Qnil;
4592 }
4593 
4594 /*
4595  * Document-method: Integer#>>
4596  * call-seq:
4597  * int >> count -> integer
4598  *
4599  * Returns +int+ shifted right +count+ positions, or left if +count+
4600  * is negative.
4601  */
4602 
4603 static VALUE
4604 rb_fix_rshift(VALUE x, VALUE y)
4605 {
4606  long i, val;
4607 
4608  val = FIX2LONG(x);
4609  if (!FIXNUM_P(y))
4610  return rb_big_rshift(rb_int2big(val), y);
4611  i = FIX2LONG(y);
4612  if (i == 0) return x;
4613  if (i < 0)
4614  return fix_lshift(val, (unsigned long)-i);
4615  return fix_rshift(val, i);
4616 }
4617 
4618 static VALUE
4619 fix_rshift(long val, unsigned long i)
4620 {
4621  if (i >= sizeof(long)*CHAR_BIT-1) {
4622  if (val < 0) return INT2FIX(-1);
4623  return INT2FIX(0);
4624  }
4625  val = RSHIFT(val, i);
4626  return LONG2FIX(val);
4627 }
4628 
4629 static VALUE
4630 rb_int_rshift(VALUE x, VALUE y)
4631 {
4632  if (FIXNUM_P(x)) {
4633  return rb_fix_rshift(x, y);
4634  }
4635  else if (RB_TYPE_P(x, T_BIGNUM)) {
4636  return rb_big_rshift(x, y);
4637  }
4638  return Qnil;
4639 }
4640 
4643 {
4644  long val = FIX2LONG(fix);
4645  long i;
4646 
4647  idx = rb_to_int(idx);
4648  if (!FIXNUM_P(idx)) {
4649  idx = rb_big_norm(idx);
4650  if (!FIXNUM_P(idx)) {
4651  if (!BIGNUM_SIGN(idx) || val >= 0)
4652  return INT2FIX(0);
4653  return INT2FIX(1);
4654  }
4655  }
4656  i = FIX2LONG(idx);
4657 
4658  if (i < 0) return INT2FIX(0);
4659  if (SIZEOF_LONG*CHAR_BIT-1 <= i) {
4660  if (val < 0) return INT2FIX(1);
4661  return INT2FIX(0);
4662  }
4663  if (val & (1L<<i))
4664  return INT2FIX(1);
4665  return INT2FIX(0);
4666 }
4667 
4668 
4669 /* copied from "r_less" in range.c */
4670 /* compares _a_ and _b_ and returns:
4671  * < 0: a < b
4672  * = 0: a = b
4673  * > 0: a > b or non-comparable
4674  */
4675 static int
4676 compare_indexes(VALUE a, VALUE b)
4677 {
4678  VALUE r = rb_funcall(a, id_cmp, 1, b);
4679 
4680  if (NIL_P(r))
4681  return INT_MAX;
4682  return rb_cmpint(r, a, b);
4683 }
4684 
4685 static VALUE
4686 generate_mask(VALUE len)
4687 {
4688  return rb_int_minus(rb_int_lshift(INT2FIX(1), len), INT2FIX(1));
4689 }
4690 
4691 static VALUE
4692 int_aref1(VALUE num, VALUE arg)
4693 {
4694  VALUE orig_num = num, beg, end;
4695  int excl;
4696 
4697  if (rb_range_values(arg, &beg, &end, &excl)) {
4698  if (NIL_P(beg)) {
4699  /* beginless range */
4700  if (!RTEST(num_negative_p(end))) {
4701  if (!excl) end = rb_int_plus(end, INT2FIX(1));
4702  VALUE mask = generate_mask(end);
4703  if (RTEST(num_zero_p(rb_int_and(num, mask)))) {
4704  return INT2FIX(0);
4705  }
4706  else {
4707  rb_raise(rb_eArgError, "The beginless range for Integer#[] results in infinity");
4708  }
4709  }
4710  else {
4711  return INT2FIX(0);
4712  }
4713  }
4714  num = rb_int_rshift(num, beg);
4715 
4716  int cmp = compare_indexes(beg, end);
4717  if (!NIL_P(end) && cmp < 0) {
4718  VALUE len = rb_int_minus(end, beg);
4719  if (!excl) len = rb_int_plus(len, INT2FIX(1));
4720  VALUE mask = generate_mask(len);
4721  num = rb_int_and(num, mask);
4722  }
4723  else if (cmp == 0) {
4724  if (excl) return INT2FIX(0);
4725  num = orig_num;
4726  arg = beg;
4727  goto one_bit;
4728  }
4729  return num;
4730  }
4731 
4732 one_bit:
4733  if (FIXNUM_P(num)) {
4734  return rb_fix_aref(num, arg);
4735  }
4736  else if (RB_TYPE_P(num, T_BIGNUM)) {
4737  return rb_big_aref(num, arg);
4738  }
4739  return Qnil;
4740 }
4741 
4742 static VALUE
4743 int_aref2(VALUE num, VALUE beg, VALUE len)
4744 {
4745  num = rb_int_rshift(num, beg);
4746  VALUE mask = generate_mask(len);
4747  num = rb_int_and(num, mask);
4748  return num;
4749 }
4750 
4751 /*
4752  * Document-method: Integer#[]
4753  * call-seq:
4754  * int[n] -> 0, 1
4755  * int[n, m] -> num
4756  * int[range] -> num
4757  *
4758  * Bit Reference---Returns the <code>n</code>th bit in the
4759  * binary representation of +int+, where <code>int[0]</code>
4760  * is the least significant bit.
4761  *
4762  * a = 0b11001100101010
4763  * 30.downto(0) {|n| print a[n] }
4764  * #=> 0000000000000000011001100101010
4765  *
4766  * a = 9**15
4767  * 50.downto(0) {|n| print a[n] }
4768  * #=> 000101110110100000111000011110010100111100010111001
4769  *
4770  * In principle, <code>n[i]</code> is equivalent to <code>(n >> i) & 1</code>.
4771  * Thus, any negative index always returns zero:
4772  *
4773  * p 255[-1] #=> 0
4774  *
4775  * Range operations <code>n[i, len]</code> and <code>n[i..j]</code>
4776  * are naturally extended.
4777  *
4778  * * <code>n[i, len]</code> equals to <code>(n >> i) & ((1 << len) - 1)</code>.
4779  * * <code>n[i..j]</code> equals to <code>(n >> i) & ((1 << (j - i + 1)) - 1)</code>.
4780  * * <code>n[i...j]</code> equals to <code>(n >> i) & ((1 << (j - i)) - 1)</code>.
4781  * * <code>n[i..]</code> equals to <code>(n >> i)</code>.
4782  * * <code>n[..j]</code> is zero if <code>n & ((1 << (j + 1)) - 1)</code> is zero. Otherwise, raises an ArgumentError.
4783  * * <code>n[...j]</code> is zero if <code>n & ((1 << j) - 1)</code> is zero. Otherwise, raises an ArgumentError.
4784  *
4785  * Note that range operation may exhaust memory.
4786  * For example, <code>-1[0, 1000000000000]</code> will raise NoMemoryError.
4787  */
4788 
4789 static VALUE
4790 int_aref(int const argc, VALUE * const argv, VALUE const num)
4791 {
4792  rb_check_arity(argc, 1, 2);
4793  if (argc == 2) {
4794  return int_aref2(num, argv[0], argv[1]);
4795  }
4796  return int_aref1(num, argv[0]);
4797 
4798  return Qnil;
4799 }
4800 
4801 /*
4802  * Document-method: Integer#to_f
4803  * call-seq:
4804  * int.to_f -> float
4805  *
4806  * Converts +int+ to a Float. If +int+ doesn't fit in a Float,
4807  * the result is infinity.
4808  */
4809 
4810 static VALUE
4811 int_to_f(VALUE num)
4812 {
4813  double val;
4814 
4815  if (FIXNUM_P(num)) {
4816  val = (double)FIX2LONG(num);
4817  }
4818  else if (RB_TYPE_P(num, T_BIGNUM)) {
4819  val = rb_big2dbl(num);
4820  }
4821  else {
4822  rb_raise(rb_eNotImpError, "Unknown subclass for to_f: %s", rb_obj_classname(num));
4823  }
4824 
4825  return DBL2NUM(val);
4826 }
4827 
4828 /*
4829  * Document-method: Integer#abs
4830  * Document-method: Integer#magnitude
4831  * call-seq:
4832  * int.abs -> integer
4833  * int.magnitude -> integer
4834  *
4835  * Returns the absolute value of +int+.
4836  *
4837  * (-12345).abs #=> 12345
4838  * -12345.abs #=> 12345
4839  * 12345.abs #=> 12345
4840  *
4841  * Integer#magnitude is an alias for Integer#abs.
4842  */
4843 
4844 static VALUE
4845 fix_abs(VALUE fix)
4846 {
4847  long i = FIX2LONG(fix);
4848 
4849  if (i < 0) i = -i;
4850 
4851  return LONG2NUM(i);
4852 }
4853 
4854 VALUE
4856 {
4857  if (FIXNUM_P(num)) {
4858  return fix_abs(num);
4859  }
4860  else if (RB_TYPE_P(num, T_BIGNUM)) {
4861  return rb_big_abs(num);
4862  }
4863  return Qnil;
4864 }
4865 
4866 /*
4867  * Document-method: Integer#size
4868  * call-seq:
4869  * int.size -> int
4870  *
4871  * Returns the number of bytes in the machine representation of +int+
4872  * (machine dependent).
4873  *
4874  * 1.size #=> 8
4875  * -1.size #=> 8
4876  * 2147483647.size #=> 8
4877  * (256**10 - 1).size #=> 10
4878  * (256**20 - 1).size #=> 20
4879  * (256**40 - 1).size #=> 40
4880  */
4881 
4882 static VALUE
4883 fix_size(VALUE fix)
4884 {
4885  return INT2FIX(sizeof(long));
4886 }
4887 
4888 static VALUE
4889 int_size(VALUE num)
4890 {
4891  if (FIXNUM_P(num)) {
4892  return fix_size(num);
4893  }
4894  else if (RB_TYPE_P(num, T_BIGNUM)) {
4895  return rb_big_size_m(num);
4896  }
4897  return Qnil;
4898 }
4899 
4900 /*
4901  * Document-method: Integer#bit_length
4902  * call-seq:
4903  * int.bit_length -> integer
4904  *
4905  * Returns the number of bits of the value of +int+.
4906  *
4907  * "Number of bits" means the bit position of the highest bit
4908  * which is different from the sign bit
4909  * (where the least significant bit has bit position 1).
4910  * If there is no such bit (zero or minus one), zero is returned.
4911  *
4912  * I.e. this method returns <i>ceil(log2(int < 0 ? -int : int+1))</i>.
4913  *
4914  * (-2**1000-1).bit_length #=> 1001
4915  * (-2**1000).bit_length #=> 1000
4916  * (-2**1000+1).bit_length #=> 1000
4917  * (-2**12-1).bit_length #=> 13
4918  * (-2**12).bit_length #=> 12
4919  * (-2**12+1).bit_length #=> 12
4920  * -0x101.bit_length #=> 9
4921  * -0x100.bit_length #=> 8
4922  * -0xff.bit_length #=> 8
4923  * -2.bit_length #=> 1
4924  * -1.bit_length #=> 0
4925  * 0.bit_length #=> 0
4926  * 1.bit_length #=> 1
4927  * 0xff.bit_length #=> 8
4928  * 0x100.bit_length #=> 9
4929  * (2**12-1).bit_length #=> 12
4930  * (2**12).bit_length #=> 13
4931  * (2**12+1).bit_length #=> 13
4932  * (2**1000-1).bit_length #=> 1000
4933  * (2**1000).bit_length #=> 1001
4934  * (2**1000+1).bit_length #=> 1001
4935  *
4936  * This method can be used to detect overflow in Array#pack as follows:
4937  *
4938  * if n.bit_length < 32
4939  * [n].pack("l") # no overflow
4940  * else
4941  * raise "overflow"
4942  * end
4943  */
4944 
4945 static VALUE
4946 rb_fix_bit_length(VALUE fix)
4947 {
4948  long v = FIX2LONG(fix);
4949  if (v < 0)
4950  v = ~v;
4951  return LONG2FIX(bit_length(v));
4952 }
4953 
4954 static VALUE
4955 rb_int_bit_length(VALUE num)
4956 {
4957  if (FIXNUM_P(num)) {
4958  return rb_fix_bit_length(num);
4959  }
4960  else if (RB_TYPE_P(num, T_BIGNUM)) {
4961  return rb_big_bit_length(num);
4962  }
4963  return Qnil;
4964 }
4965 
4966 /*
4967  * Document-method: Integer#digits
4968  * call-seq:
4969  * int.digits -> array
4970  * int.digits(base) -> array
4971  *
4972  * Returns the digits of +int+'s place-value representation
4973  * with radix +base+ (default: 10).
4974  * The digits are returned as an array with the least significant digit
4975  * as the first array element.
4976  *
4977  * +base+ must be greater than or equal to 2.
4978  *
4979  * 12345.digits #=> [5, 4, 3, 2, 1]
4980  * 12345.digits(7) #=> [4, 6, 6, 0, 5]
4981  * 12345.digits(100) #=> [45, 23, 1]
4982  *
4983  * -12345.digits(7) #=> Math::DomainError
4984  */
4985 
4986 static VALUE
4987 rb_fix_digits(VALUE fix, long base)
4988 {
4989  VALUE digits;
4990  long x = FIX2LONG(fix);
4991 
4992  assert(x >= 0);
4993 
4994  if (base < 2)
4995  rb_raise(rb_eArgError, "invalid radix %ld", base);
4996 
4997  if (x == 0)
4998  return rb_ary_new_from_args(1, INT2FIX(0));
4999 
5000  digits = rb_ary_new();
5001  while (x > 0) {
5002  long q = x % base;
5003  rb_ary_push(digits, LONG2NUM(q));
5004  x /= base;
5005  }
5006 
5007  return digits;
5008 }
5009 
5010 static VALUE
5011 rb_int_digits_bigbase(VALUE num, VALUE base)
5012 {
5013  VALUE digits;
5014 
5015  assert(!rb_num_negative_p(num));
5016 
5017  if (RB_TYPE_P(base, T_BIGNUM))
5018  base = rb_big_norm(base);
5019 
5020  if (FIXNUM_P(base) && FIX2LONG(base) < 2)
5021  rb_raise(rb_eArgError, "invalid radix %ld", FIX2LONG(base));
5022  else if (RB_TYPE_P(base, T_BIGNUM) && BIGNUM_NEGATIVE_P(base))
5023  rb_raise(rb_eArgError, "negative radix");
5024 
5025  if (FIXNUM_P(base) && FIXNUM_P(num))
5026  return rb_fix_digits(num, FIX2LONG(base));
5027 
5028  if (FIXNUM_P(num))
5029  return rb_ary_new_from_args(1, num);
5030 
5031  digits = rb_ary_new();
5032  while (!FIXNUM_P(num) || FIX2LONG(num) > 0) {
5033  VALUE qr = rb_int_divmod(num, base);
5034  rb_ary_push(digits, RARRAY_AREF(qr, 1));
5035  num = RARRAY_AREF(qr, 0);
5036  }
5037 
5038  return digits;
5039 }
5040 
5041 static VALUE
5042 rb_int_digits(int argc, VALUE *argv, VALUE num)
5043 {
5044  VALUE base_value;
5045  long base;
5046 
5047  if (rb_num_negative_p(num))
5048  rb_raise(rb_eMathDomainError, "out of domain");
5049 
5050  if (rb_check_arity(argc, 0, 1)) {
5051  base_value = rb_to_int(argv[0]);
5052  if (!RB_INTEGER_TYPE_P(base_value))
5053  rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)",
5054  rb_obj_classname(argv[0]));
5055  if (RB_TYPE_P(base_value, T_BIGNUM))
5056  return rb_int_digits_bigbase(num, base_value);
5057 
5058  base = FIX2LONG(base_value);
5059  if (base < 0)
5060  rb_raise(rb_eArgError, "negative radix");
5061  else if (base < 2)
5062  rb_raise(rb_eArgError, "invalid radix %ld", base);
5063  }
5064  else
5065  base = 10;
5066 
5067  if (FIXNUM_P(num))
5068  return rb_fix_digits(num, base);
5069  else if (RB_TYPE_P(num, T_BIGNUM))
5070  return rb_int_digits_bigbase(num, LONG2FIX(base));
5071 
5072  return Qnil;
5073 }
5074 
5075 /*
5076  * Document-method: Integer#upto
5077  * call-seq:
5078  * int.upto(limit) {|i| block } -> self
5079  * int.upto(limit) -> an_enumerator
5080  *
5081  * Iterates the given block, passing in integer values from +int+ up to and
5082  * including +limit+.
5083  *
5084  * If no block is given, an Enumerator is returned instead.
5085  *
5086  * 5.upto(10) {|i| print i, " " } #=> 5 6 7 8 9 10
5087  */
5088 
5089 static VALUE
5090 int_upto_size(VALUE from, VALUE args, VALUE eobj)
5091 {
5092  return ruby_num_interval_step_size(from, RARRAY_AREF(args, 0), INT2FIX(1), FALSE);
5093 }
5094 
5095 static VALUE
5096 int_upto(VALUE from, VALUE to)
5097 {
5098  RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size);
5099  if (FIXNUM_P(from) && FIXNUM_P(to)) {
5100  long i, end;
5101 
5102  end = FIX2LONG(to);
5103  for (i = FIX2LONG(from); i <= end; i++) {
5104  rb_yield(LONG2FIX(i));
5105  }
5106  }
5107  else {
5108  VALUE i = from, c;
5109 
5110  while (!(c = rb_funcall(i, '>', 1, to))) {
5111  rb_yield(i);
5112  i = rb_funcall(i, '+', 1, INT2FIX(1));
5113  }
5114  if (NIL_P(c)) rb_cmperr(i, to);
5115  }
5116  return from;
5117 }
5118 
5119 /*
5120  * Document-method: Integer#downto
5121  * call-seq:
5122  * int.downto(limit) {|i| block } -> self
5123  * int.downto(limit) -> an_enumerator
5124  *
5125  * Iterates the given block, passing in decreasing values from +int+ down to
5126  * and including +limit+.
5127  *
5128  * If no block is given, an Enumerator is returned instead.
5129  *
5130  * 5.downto(1) { |n| print n, ".. " }
5131  * puts "Liftoff!"
5132  * #=> "5.. 4.. 3.. 2.. 1.. Liftoff!"
5133  */
5134 
5135 static VALUE
5136 int_downto_size(VALUE from, VALUE args, VALUE eobj)
5137 {
5138  return ruby_num_interval_step_size(from, RARRAY_AREF(args, 0), INT2FIX(-1), FALSE);
5139 }
5140 
5141 static VALUE
5142 int_downto(VALUE from, VALUE to)
5143 {
5144  RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size);
5145  if (FIXNUM_P(from) && FIXNUM_P(to)) {
5146  long i, end;
5147 
5148  end = FIX2LONG(to);
5149  for (i=FIX2LONG(from); i >= end; i--) {
5150  rb_yield(LONG2FIX(i));
5151  }
5152  }
5153  else {
5154  VALUE i = from, c;
5155 
5156  while (!(c = rb_funcall(i, '<', 1, to))) {
5157  rb_yield(i);
5158  i = rb_funcall(i, '-', 1, INT2FIX(1));
5159  }
5160  if (NIL_P(c)) rb_cmperr(i, to);
5161  }
5162  return from;
5163 }
5164 
5165 /*
5166  * Document-method: Integer#times
5167  * call-seq:
5168  * int.times {|i| block } -> self
5169  * int.times -> an_enumerator
5170  *
5171  * Iterates the given block +int+ times, passing in values from zero to
5172  * <code>int - 1</code>.
5173  *
5174  * If no block is given, an Enumerator is returned instead.
5175  *
5176  * 5.times {|i| print i, " " } #=> 0 1 2 3 4
5177  */
5178 
5179 static VALUE
5180 int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
5181 {
5182  if (FIXNUM_P(num)) {
5183  if (NUM2LONG(num) <= 0) return INT2FIX(0);
5184  }
5185  else {
5186  if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) return INT2FIX(0);
5187  }
5188  return num;
5189 }
5190 
5191 static VALUE
5192 int_dotimes(VALUE num)
5193 {
5194  RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size);
5195 
5196  if (FIXNUM_P(num)) {
5197  long i, end;
5198 
5199  end = FIX2LONG(num);
5200  for (i=0; i<end; i++) {
5201  rb_yield_1(LONG2FIX(i));
5202  }
5203  }
5204  else {
5205  VALUE i = INT2FIX(0);
5206 
5207  for (;;) {
5208  if (!RTEST(rb_funcall(i, '<', 1, num))) break;
5209  rb_yield(i);
5210  i = rb_funcall(i, '+', 1, INT2FIX(1));
5211  }
5212  }
5213  return num;
5214 }
5215 
5216 /*
5217  * Document-method: Integer#round
5218  * call-seq:
5219  * int.round([ndigits] [, half: mode]) -> integer or float
5220  *
5221  * Returns +int+ rounded to the nearest value with
5222  * a precision of +ndigits+ decimal digits (default: 0).
5223  *
5224  * When the precision is negative, the returned value is an integer
5225  * with at least <code>ndigits.abs</code> trailing zeros.
5226  *
5227  * Returns +self+ when +ndigits+ is zero or positive.
5228  *
5229  * 1.round #=> 1
5230  * 1.round(2) #=> 1
5231  * 15.round(-1) #=> 20
5232  * (-15).round(-1) #=> -20
5233  *
5234  * The optional +half+ keyword argument is available
5235  * similar to Float#round.
5236  *
5237  * 25.round(-1, half: :up) #=> 30
5238  * 25.round(-1, half: :down) #=> 20
5239  * 25.round(-1, half: :even) #=> 20
5240  * 35.round(-1, half: :up) #=> 40
5241  * 35.round(-1, half: :down) #=> 30
5242  * 35.round(-1, half: :even) #=> 40
5243  * (-25).round(-1, half: :up) #=> -30
5244  * (-25).round(-1, half: :down) #=> -20
5245  * (-25).round(-1, half: :even) #=> -20
5246  */
5247 
5248 static VALUE
5249 int_round(int argc, VALUE* argv, VALUE num)
5250 {
5251  int ndigits;
5252  int mode;
5253  VALUE nd, opt;
5254 
5255  if (!rb_scan_args(argc, argv, "01:", &nd, &opt)) return num;
5256  ndigits = NUM2INT(nd);
5257  mode = rb_num_get_rounding_option(opt);
5258  if (ndigits >= 0) {
5259  return num;
5260  }
5261  return rb_int_round(num, ndigits, mode);
5262 }
5263 
5264 /*
5265  * Document-method: Integer#floor
5266  * call-seq:
5267  * int.floor([ndigits]) -> integer or float
5268  *
5269  * Returns the largest number less than or equal to +int+ with
5270  * a precision of +ndigits+ decimal digits (default: 0).
5271  *
5272  * When the precision is negative, the returned value is an integer
5273  * with at least <code>ndigits.abs</code> trailing zeros.
5274  *
5275  * Returns +self+ when +ndigits+ is zero or positive.
5276  *
5277  * 1.floor #=> 1
5278  * 1.floor(2) #=> 1
5279  * 18.floor(-1) #=> 10
5280  * (-18).floor(-1) #=> -20
5281  */
5282 
5283 static VALUE
5284 int_floor(int argc, VALUE* argv, VALUE num)
5285 {
5286  int ndigits;
5287 
5288  if (!rb_check_arity(argc, 0, 1)) return num;
5289  ndigits = NUM2INT(argv[0]);
5290  if (ndigits >= 0) {
5291  return num;
5292  }
5293  return rb_int_floor(num, ndigits);
5294 }
5295 
5296 /*
5297  * Document-method: Integer#ceil
5298  * call-seq:
5299  * int.ceil([ndigits]) -> integer or float
5300  *
5301  * Returns the smallest number greater than or equal to +int+ with
5302  * a precision of +ndigits+ decimal digits (default: 0).
5303  *
5304  * When the precision is negative, the returned value is an integer
5305  * with at least <code>ndigits.abs</code> trailing zeros.
5306  *
5307  * Returns +self+ when +ndigits+ is zero or positive.
5308  *
5309  * 1.ceil #=> 1
5310  * 1.ceil(2) #=> 1
5311  * 18.ceil(-1) #=> 20
5312  * (-18).ceil(-1) #=> -10
5313  */
5314 
5315 static VALUE
5316 int_ceil(int argc, VALUE* argv, VALUE num)
5317 {
5318  int ndigits;
5319 
5320  if (!rb_check_arity(argc, 0, 1)) return num;
5321  ndigits = NUM2INT(argv[0]);
5322  if (ndigits >= 0) {
5323  return num;
5324  }
5325  return rb_int_ceil(num, ndigits);
5326 }
5327 
5328 /*
5329  * Document-method: Integer#truncate
5330  * call-seq:
5331  * int.truncate([ndigits]) -> integer or float
5332  *
5333  * Returns +int+ truncated (toward zero) to
5334  * a precision of +ndigits+ decimal digits (default: 0).
5335  *
5336  * When the precision is negative, the returned value is an integer
5337  * with at least <code>ndigits.abs</code> trailing zeros.
5338  *
5339  * Returns +self+ when +ndigits+ is zero or positive.
5340  *
5341  * 1.truncate #=> 1
5342  * 1.truncate(2) #=> 1
5343  * 18.truncate(-1) #=> 10
5344  * (-18).truncate(-1) #=> -10
5345  */
5346 
5347 static VALUE
5348 int_truncate(int argc, VALUE* argv, VALUE num)
5349 {
5350  int ndigits;
5351 
5352  if (!rb_check_arity(argc, 0, 1)) return num;
5353  ndigits = NUM2INT(argv[0]);
5354  if (ndigits >= 0) {
5355  return num;
5356  }
5357  return rb_int_truncate(num, ndigits);
5358 }
5359 
5360 #define DEFINE_INT_SQRT(rettype, prefix, argtype) \
5361 rettype \
5362 prefix##_isqrt(argtype n) \
5363 { \
5364  if (!argtype##_IN_DOUBLE_P(n)) { \
5365  unsigned int b = bit_length(n); \
5366  argtype t; \
5367  rettype x = (rettype)(n >> (b/2+1)); \
5368  x |= ((rettype)1LU << (b-1)/2); \
5369  while ((t = n/x) < (argtype)x) x = (rettype)((x + t) >> 1); \
5370  return x; \
5371  } \
5372  return (rettype)sqrt(argtype##_TO_DOUBLE(n)); \
5373 }
5374 
5375 #if SIZEOF_LONG*CHAR_BIT > DBL_MANT_DIG
5376 # define RB_ULONG_IN_DOUBLE_P(n) ((n) < (1UL << DBL_MANT_DIG))
5377 #else
5378 # define RB_ULONG_IN_DOUBLE_P(n) 1
5379 #endif
5380 #define RB_ULONG_TO_DOUBLE(n) (double)(n)
5381 #define RB_ULONG unsigned long
5382 DEFINE_INT_SQRT(unsigned long, rb_ulong, RB_ULONG)
5383 
5384 #if 2*SIZEOF_BDIGIT > SIZEOF_LONG
5385 # if 2*SIZEOF_BDIGIT*CHAR_BIT > DBL_MANT_DIG
5386 # define BDIGIT_DBL_IN_DOUBLE_P(n) ((n) < ((BDIGIT_DBL)1UL << DBL_MANT_DIG))
5387 # else
5388 # define BDIGIT_DBL_IN_DOUBLE_P(n) 1
5389 # endif
5390 # ifdef ULL_TO_DOUBLE
5391 # define BDIGIT_DBL_TO_DOUBLE(n) ULL_TO_DOUBLE(n)
5392 # else
5393 # define BDIGIT_DBL_TO_DOUBLE(n) (double)(n)
5394 # endif
5395 DEFINE_INT_SQRT(BDIGIT, rb_bdigit_dbl, BDIGIT_DBL)
5396 #endif
5397 
5398 #define domain_error(msg) \
5399  rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
5400 
5402 
5403 /*
5404  * Document-method: Integer::sqrt
5405  * call-seq:
5406  * Integer.sqrt(n) -> integer
5407  *
5408  * Returns the integer square root of the non-negative integer +n+,
5409  * i.e. the largest non-negative integer less than or equal to the
5410  * square root of +n+.
5411  *
5412  * Integer.sqrt(0) #=> 0
5413  * Integer.sqrt(1) #=> 1
5414  * Integer.sqrt(24) #=> 4
5415  * Integer.sqrt(25) #=> 5
5416  * Integer.sqrt(10**400) #=> 10**200
5417  *
5418  * Equivalent to <code>Math.sqrt(n).floor</code>, except that
5419  * the result of the latter code may differ from the true value
5420  * due to the limited precision of floating point arithmetic.
5421  *
5422  * Integer.sqrt(10**46) #=> 100000000000000000000000
5423  * Math.sqrt(10**46).floor #=> 99999999999999991611392 (!)
5424  *
5425  * If +n+ is not an Integer, it is converted to an Integer first.
5426  * If +n+ is negative, a Math::DomainError is raised.
5427  */
5428 
5429 static VALUE
5430 rb_int_s_isqrt(VALUE self, VALUE num)
5431 {
5432  unsigned long n, sq;
5433  num = rb_to_int(num);
5434  if (FIXNUM_P(num)) {
5435  if (FIXNUM_NEGATIVE_P(num)) {
5436  domain_error("isqrt");
5437  }
5438  n = FIX2ULONG(num);
5439  sq = rb_ulong_isqrt(n);
5440  return LONG2FIX(sq);
5441  }
5442  else {
5443  size_t biglen;
5444  if (RBIGNUM_NEGATIVE_P(num)) {
5445  domain_error("isqrt");
5446  }
5447  biglen = BIGNUM_LEN(num);
5448  if (biglen == 0) return INT2FIX(0);
5449 #if SIZEOF_BDIGIT <= SIZEOF_LONG
5450  /* short-circuit */
5451  if (biglen == 1) {
5452  n = BIGNUM_DIGITS(num)[0];
5453  sq = rb_ulong_isqrt(n);
5454  return ULONG2NUM(sq);
5455  }
5456 #endif
5457  return rb_big_isqrt(num);
5458  }
5459 }
5460 
5461 /*
5462  * Document-class: ZeroDivisionError
5463  *
5464  * Raised when attempting to divide an integer by 0.
5465  *
5466  * 42 / 0 #=> ZeroDivisionError: divided by 0
5467  *
5468  * Note that only division by an exact 0 will raise the exception:
5469  *
5470  * 42 / 0.0 #=> Float::INFINITY
5471  * 42 / -0.0 #=> -Float::INFINITY
5472  * 0 / 0.0 #=> NaN
5473  */
5474 
5475 /*
5476  * Document-class: FloatDomainError
5477  *
5478  * Raised when attempting to convert special float values (in particular
5479  * +Infinity+ or +NaN+) to numerical classes which don't support them.
5480  *
5481  * Float::INFINITY.to_r #=> FloatDomainError: Infinity
5482  */
5483 
5484 /*
5485  * Document-class: Numeric
5486  *
5487  * Numeric is the class from which all higher-level numeric classes should inherit.
5488  *
5489  * Numeric allows instantiation of heap-allocated objects. Other core numeric classes such as
5490  * Integer are implemented as immediates, which means that each Integer is a single immutable
5491  * object which is always passed by value.
5492  *
5493  * a = 1
5494  * 1.object_id == a.object_id #=> true
5495  *
5496  * There can only ever be one instance of the integer +1+, for example. Ruby ensures this
5497  * by preventing instantiation. If duplication is attempted, the same instance is returned.
5498  *
5499  * Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class
5500  * 1.dup #=> 1
5501  * 1.object_id == 1.dup.object_id #=> true
5502  *
5503  * For this reason, Numeric should be used when defining other numeric classes.
5504  *
5505  * Classes which inherit from Numeric must implement +coerce+, which returns a two-member
5506  * Array containing an object that has been coerced into an instance of the new class
5507  * and +self+ (see #coerce).
5508  *
5509  * Inheriting classes should also implement arithmetic operator methods (<code>+</code>,
5510  * <code>-</code>, <code>*</code> and <code>/</code>) and the <code><=></code> operator (see
5511  * Comparable). These methods may rely on +coerce+ to ensure interoperability with
5512  * instances of other numeric classes.
5513  *
5514  * class Tally < Numeric
5515  * def initialize(string)
5516  * @string = string
5517  * end
5518  *
5519  * def to_s
5520  * @string
5521  * end
5522  *
5523  * def to_i
5524  * @string.size
5525  * end
5526  *
5527  * def coerce(other)
5528  * [self.class.new('|' * other.to_i), self]
5529  * end
5530  *
5531  * def <=>(other)
5532  * to_i <=> other.to_i
5533  * end
5534  *
5535  * def +(other)
5536  * self.class.new('|' * (to_i + other.to_i))
5537  * end
5538  *
5539  * def -(other)
5540  * self.class.new('|' * (to_i - other.to_i))
5541  * end
5542  *
5543  * def *(other)
5544  * self.class.new('|' * (to_i * other.to_i))
5545  * end
5546  *
5547  * def /(other)
5548  * self.class.new('|' * (to_i / other.to_i))
5549  * end
5550  * end
5551  *
5552  * tally = Tally.new('||')
5553  * puts tally * 2 #=> "||||"
5554  * puts tally > 1 #=> true
5555  */
5556 void
5558 {
5559 #undef rb_intern
5560 #define rb_intern(str) rb_intern_const(str)
5561 
5562 #ifdef _UNICOSMP
5563  /* Turn off floating point exceptions for divide by zero, etc. */
5564  _set_Creg(0, 0);
5565 #endif
5566  id_coerce = rb_intern("coerce");
5567  id_to = rb_intern("to");
5568  id_by = rb_intern("by");
5569 
5570  rb_eZeroDivError = rb_define_class("ZeroDivisionError", rb_eStandardError);
5571  rb_eFloatDomainError = rb_define_class("FloatDomainError", rb_eRangeError);
5572  rb_cNumeric = rb_define_class("Numeric", rb_cObject);
5573 
5574  rb_define_method(rb_cNumeric, "singleton_method_added", num_sadded, 1);
5576  rb_define_method(rb_cNumeric, "coerce", num_coerce, 1);
5577  rb_define_method(rb_cNumeric, "clone", num_clone, -1);
5578  rb_define_method(rb_cNumeric, "dup", num_dup, 0);
5579 
5580  rb_define_method(rb_cNumeric, "i", num_imaginary, 0);
5581  rb_define_method(rb_cNumeric, "+@", num_uplus, 0);
5582  rb_define_method(rb_cNumeric, "-@", num_uminus, 0);
5583  rb_define_method(rb_cNumeric, "<=>", num_cmp, 1);
5584  rb_define_method(rb_cNumeric, "eql?", num_eql, 1);
5585  rb_define_method(rb_cNumeric, "fdiv", num_fdiv, 1);
5586  rb_define_method(rb_cNumeric, "div", num_div, 1);
5587  rb_define_method(rb_cNumeric, "divmod", num_divmod, 1);
5588  rb_define_method(rb_cNumeric, "%", num_modulo, 1);
5589  rb_define_method(rb_cNumeric, "modulo", num_modulo, 1);
5590  rb_define_method(rb_cNumeric, "remainder", num_remainder, 1);
5591  rb_define_method(rb_cNumeric, "abs", num_abs, 0);
5592  rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
5593  rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
5594 
5595  rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
5596  rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
5597  rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
5598  rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
5599  rb_define_method(rb_cNumeric, "finite?", num_finite_p, 0);
5600  rb_define_method(rb_cNumeric, "infinite?", num_infinite_p, 0);
5601 
5602  rb_define_method(rb_cNumeric, "floor", num_floor, -1);
5603  rb_define_method(rb_cNumeric, "ceil", num_ceil, -1);
5604  rb_define_method(rb_cNumeric, "round", num_round, -1);
5605  rb_define_method(rb_cNumeric, "truncate", num_truncate, -1);
5606  rb_define_method(rb_cNumeric, "step", num_step, -1);
5607  rb_define_method(rb_cNumeric, "positive?", num_positive_p, 0);
5608  rb_define_method(rb_cNumeric, "negative?", num_negative_p, 0);
5609 
5610  rb_cInteger = rb_define_class("Integer", rb_cNumeric);
5613  rb_define_singleton_method(rb_cInteger, "sqrt", rb_int_s_isqrt, 1);
5614 
5615  rb_define_method(rb_cInteger, "to_s", int_to_s, -1);
5616  rb_define_alias(rb_cInteger, "inspect", "to_s");
5617  rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
5619  rb_define_method(rb_cInteger, "even?", int_even_p, 0);
5620  rb_define_method(rb_cInteger, "allbits?", int_allbits_p, 1);
5621  rb_define_method(rb_cInteger, "anybits?", int_anybits_p, 1);
5622  rb_define_method(rb_cInteger, "nobits?", int_nobits_p, 1);
5623  rb_define_method(rb_cInteger, "upto", int_upto, 1);
5624  rb_define_method(rb_cInteger, "downto", int_downto, 1);
5625  rb_define_method(rb_cInteger, "times", int_dotimes, 0);
5626  rb_define_method(rb_cInteger, "succ", int_succ, 0);
5627  rb_define_method(rb_cInteger, "next", int_succ, 0);
5628  rb_define_method(rb_cInteger, "pred", int_pred, 0);
5629  rb_define_method(rb_cInteger, "chr", int_chr, -1);
5630  rb_define_method(rb_cInteger, "ord", int_ord, 0);
5631  rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
5632  rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
5633  rb_define_method(rb_cInteger, "to_f", int_to_f, 0);
5634  rb_define_method(rb_cInteger, "floor", int_floor, -1);
5635  rb_define_method(rb_cInteger, "ceil", int_ceil, -1);
5636  rb_define_method(rb_cInteger, "truncate", int_truncate, -1);
5637  rb_define_method(rb_cInteger, "round", int_round, -1);
5639 
5648  rb_define_method(rb_cInteger, "remainder", int_remainder, 1);
5652 
5653  rb_define_method(rb_cInteger, "pow", rb_int_powm, -1); /* in bignum.c */
5654 
5656  rb_define_method(rb_cInteger, "magnitude", rb_int_abs, 0);
5657 
5662  rb_define_method(rb_cInteger, "<", int_lt, 1);
5663  rb_define_method(rb_cInteger, "<=", int_le, 1);
5664 
5665  rb_define_method(rb_cInteger, "~", int_comp, 0);
5667  rb_define_method(rb_cInteger, "|", int_or, 1);
5668  rb_define_method(rb_cInteger, "^", int_xor, 1);
5669  rb_define_method(rb_cInteger, "[]", int_aref, -1);
5670 
5672  rb_define_method(rb_cInteger, ">>", rb_int_rshift, 1);
5673 
5674  rb_define_method(rb_cInteger, "size", int_size, 0);
5675  rb_define_method(rb_cInteger, "bit_length", rb_int_bit_length, 0);
5676  rb_define_method(rb_cInteger, "digits", rb_int_digits, -1);
5677 
5678 #ifndef RUBY_INTEGER_UNIFICATION
5680 #endif
5681  /* An obsolete class, use Integer */
5683  rb_deprecate_constant(rb_cObject, "Fixnum");
5684 
5686 
5689 
5690  /*
5691  * Deprecated, do not use.
5692  *
5693  * Represents the rounding mode for floating point addition at the start time.
5694  *
5695  * Usually defaults to 1, rounding to the nearest number.
5696  *
5697  * Other modes include:
5698  *
5699  * -1:: Indeterminable
5700  * 0:: Rounding towards zero
5701  * 1:: Rounding to the nearest number
5702  * 2:: Rounding towards positive infinity
5703  * 3:: Rounding towards negative infinity
5704  */
5706  rb_deprecate_constant(rb_cFloat, "ROUNDS");
5707  /*
5708  * The base of the floating point, or number of unique digits used to
5709  * represent the number.
5710  *
5711  * Usually defaults to 2 on most systems, which would represent a base-10 decimal.
5712  */
5714  /*
5715  * The number of base digits for the +double+ data type.
5716  *
5717  * Usually defaults to 53.
5718  */
5720  /*
5721  * The minimum number of significant decimal digits in a double-precision
5722  * floating point.
5723  *
5724  * Usually defaults to 15.
5725  */
5727  /*
5728  * The smallest possible exponent value in a double-precision floating
5729  * point.
5730  *
5731  * Usually defaults to -1021.
5732  */
5734  /*
5735  * The largest possible exponent value in a double-precision floating
5736  * point.
5737  *
5738  * Usually defaults to 1024.
5739  */
5741  /*
5742  * The smallest negative exponent in a double-precision floating point
5743  * where 10 raised to this power minus 1.
5744  *
5745  * Usually defaults to -307.
5746  */
5748  /*
5749  * The largest positive exponent in a double-precision floating point where
5750  * 10 raised to this power minus 1.
5751  *
5752  * Usually defaults to 308.
5753  */
5755  /*
5756  * The smallest positive normalized number in a double-precision floating point.
5757  *
5758  * Usually defaults to 2.2250738585072014e-308.
5759  *
5760  * If the platform supports denormalized numbers,
5761  * there are numbers between zero and Float::MIN.
5762  * 0.0.next_float returns the smallest positive floating point number
5763  * including denormalized numbers.
5764  */
5766  /*
5767  * The largest possible integer in a double-precision floating point number.
5768  *
5769  * Usually defaults to 1.7976931348623157e+308.
5770  */
5772  /*
5773  * The difference between 1 and the smallest double-precision floating
5774  * point number greater than 1.
5775  *
5776  * Usually defaults to 2.2204460492503131e-16.
5777  */
5779  /*
5780  * An expression representing positive infinity.
5781  */
5782  rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(HUGE_VAL));
5783  /*
5784  * An expression representing a value which is "not a number".
5785  */
5786  rb_define_const(rb_cFloat, "NAN", DBL2NUM(nan("")));
5787 
5788  rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
5789  rb_define_alias(rb_cFloat, "inspect", "to_s");
5790  rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
5793  rb_define_method(rb_cFloat, "-", flo_minus, 1);
5796  rb_define_method(rb_cFloat, "quo", flo_quo, 1);
5797  rb_define_method(rb_cFloat, "fdiv", flo_quo, 1);
5798  rb_define_method(rb_cFloat, "%", flo_mod, 1);
5799  rb_define_method(rb_cFloat, "modulo", flo_mod, 1);
5800  rb_define_method(rb_cFloat, "divmod", flo_divmod, 1);
5802  rb_define_method(rb_cFloat, "==", flo_eq, 1);
5803  rb_define_method(rb_cFloat, "===", flo_eq, 1);
5804  rb_define_method(rb_cFloat, "<=>", flo_cmp, 1);
5806  rb_define_method(rb_cFloat, ">=", flo_ge, 1);
5807  rb_define_method(rb_cFloat, "<", flo_lt, 1);
5808  rb_define_method(rb_cFloat, "<=", flo_le, 1);
5809  rb_define_method(rb_cFloat, "eql?", flo_eql, 1);
5810  rb_define_method(rb_cFloat, "hash", flo_hash, 0);
5811  rb_define_method(rb_cFloat, "to_f", flo_to_f, 0);
5813  rb_define_method(rb_cFloat, "magnitude", rb_float_abs, 0);
5814  rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0);
5815 
5816  rb_define_method(rb_cFloat, "to_i", flo_to_i, 0);
5817  rb_define_method(rb_cFloat, "to_int", flo_to_i, 0);
5818  rb_define_method(rb_cFloat, "floor", flo_floor, -1);
5819  rb_define_method(rb_cFloat, "ceil", flo_ceil, -1);
5820  rb_define_method(rb_cFloat, "round", flo_round, -1);
5821  rb_define_method(rb_cFloat, "truncate", flo_truncate, -1);
5822 
5823  rb_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0);
5826  rb_define_method(rb_cFloat, "next_float", flo_next_float, 0);
5827  rb_define_method(rb_cFloat, "prev_float", flo_prev_float, 0);
5828  rb_define_method(rb_cFloat, "positive?", flo_positive_p, 0);
5829  rb_define_method(rb_cFloat, "negative?", flo_negative_p, 0);
5830 }
5831 
5832 #undef rb_float_value
5833 double
5835 {
5836  return rb_float_value_inline(v);
5837 }
5838 
5839 #undef rb_float_new
5840 VALUE
5841 rb_float_new(double d)
5842 {
5843  return rb_float_new_inline(d);
5844 }
rb_big_remainder
VALUE rb_big_remainder(VALUE x, VALUE y)
Definition: bignum.c:6119
rb_float_div
VALUE rb_float_div(VALUE x, VALUE y)
Definition: numeric.c:1126
rb_dbl_complex_new_polar_pi
VALUE rb_dbl_complex_new_polar_pi(double abs, double ang)
Definition: complex.c:667
rb_int_divmod
VALUE rb_int_divmod(VALUE x, VALUE y)
Definition: numeric.c:3963
rb_fix2str
VALUE rb_fix2str(VALUE x, int base)
Definition: numeric.c:3508
rb_get_kwargs
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1886
i
uint32_t i
Definition: rb_mjit_min_header-2.7.1.h:5425
rb_big_eql
VALUE rb_big_eql(VALUE x, VALUE y)
Definition: bignum.c:5544
ID
unsigned long ID
Definition: ruby.h:103
rb_int_floor
VALUE rb_int_floor(VALUE num, int ndigits)
Definition: numeric.c:2137
rb_check_funcall
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:505
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:649
T_FALSE
#define T_FALSE
Definition: ruby.h:537
BIGNUM_DIGITS
#define BIGNUM_DIGITS(b)
Definition: internal.h:780
rb_id2name
const char * rb_id2name(ID)
Definition: symbol.c:801
ruby_dtoa
char * ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
rb_int_ge
VALUE rb_int_ge(VALUE x, VALUE y)
Definition: numeric.c:4292
TRUE
#define TRUE
Definition: nkf.h:175
rb_big_modulo
VALUE rb_big_modulo(VALUE x, VALUE y)
Definition: bignum.c:6103
long
#define long
Definition: rb_mjit_min_header-2.7.1.h:2848
rb_fix2ushort
unsigned short rb_fix2ushort(VALUE val)
Definition: numeric.c:3069
T_FLOAT
#define T_FLOAT
Definition: ruby.h:527
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:869
rb_num_coerce_bin
VALUE rb_num_coerce_bin(VALUE x, VALUE y, ID func)
Definition: numeric.c:446
rb_assoc_new
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:896
DBL_EPSILON
#define DBL_EPSILON
Definition: numeric.c:61
round
double round(double x)
Definition: numeric.c:80
double
double
Definition: rb_mjit_min_header-2.7.1.h:5884
rb_enc_name
#define rb_enc_name(enc)
Definition: encoding.h:177
assert
#define assert(x)
Definition: dlmalloc.c:1176
rb_num_negative_p
int rb_num_negative_p(VALUE num)
Definition: numeric.c:313
DBL_DIG
#define DBL_DIG
Definition: numeric.c:55
rb_arith_seq_new
VALUE rb_arith_seq_new(VALUE obj, VALUE meth, int argc, VALUE const *argv, rb_enumerator_size_func *size_fn, VALUE beg, VALUE end, VALUE step, int excl)
Definition: enumerator.c:3308
FIX2INT
#define FIX2INT(x)
Definition: ruby.h:717
id_to_i
#define id_to_i
Definition: numeric.c:177
rb_complex_pow
VALUE rb_complex_pow(VALUE self, VALUE other)
Definition: complex.c:985
rb_big_fdiv_double
double rb_big_fdiv_double(VALUE x, VALUE y)
Definition: bignum.c:6209
exp
double exp(double)
RFloat
Definition: internal.h:798
rb_float_plus
VALUE rb_float_plus(VALUE x, VALUE y)
Definition: numeric.c:1024
rb_enc_mbcput
#define rb_enc_mbcput(c, buf, enc)
Definition: encoding.h:217
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:898
rb_float_abs
VALUE rb_float_abs(VALUE flt)
Definition: numeric.c:1698
rb_immutable_obj_clone
VALUE rb_immutable_obj_clone(int, VALUE *, VALUE)
Definition: object.c:346
rb_num2short
short rb_num2short(VALUE val)
Definition: numeric.c:3041
rb_int_fdiv_double
double rb_int_fdiv_double(VALUE x, VALUE y)
Definition: numeric.c:3728
RSHIFT
#define RSHIFT(x, y)
Definition: rb_mjit_min_header-2.7.1.h:408
rb_cNumeric
VALUE rb_cNumeric
Definition: numeric.c:181
rb_big_le
VALUE rb_big_le(VALUE x, VALUE y)
Definition: bignum.c:5507
rb_num_coerce_bit
VALUE rb_num_coerce_bit(VALUE x, VALUE y, ID func)
Definition: numeric.c:4426
rb_big_ge
VALUE rb_big_ge(VALUE x, VALUE y)
Definition: bignum.c:5495
rb_funcall
#define rb_funcall(recv, mid, argc,...)
Definition: rb_mjit_min_header-2.7.1.h:6546
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:263
n
const char size_t n
Definition: rb_mjit_min_header-2.7.1.h:5417
strchr
char * strchr(char *, char)
rb_big2ll
long long rb_big2ll(VALUE)
USHRT_MAX
#define USHRT_MAX
Definition: rb_mjit_min_header-2.7.1.h:4016
rb_dbl_cmp
VALUE rb_dbl_cmp(double a, double b)
Definition: numeric.c:1431
NUMERR_TOOLARGE
#define NUMERR_TOOLARGE
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:1009
rb_dbl2big
VALUE rb_dbl2big(double d)
Definition: bignum.c:5249
rb_infinity
const union bytesequence4_or_float rb_infinity
Definition: numeric.c:66
rb_big_lshift
VALUE rb_big_lshift(VALUE x, VALUE y)
Definition: bignum.c:6621
idUMinus
@ idUMinus
Definition: id.h:82
NUM2LONG
#define NUM2LONG(x)
Definition: ruby.h:679
rb_int2big
VALUE rb_int2big(intptr_t n)
Definition: bignum.c:3180
bytesequence4_or_float
Definition: missing.h:140
rb_equal
VALUE rb_equal(VALUE, VALUE)
Same as Object#===, case equality.
Definition: object.c:124
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_eArgError
VALUE rb_eArgError
Definition: error.c:923
encoding.h
modf
double modf(double, double *)
rb_big2ulong
unsigned long rb_big2ulong(VALUE x)
Definition: bignum.c:5125
y0
double y0(double)
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:560
rb_intern_const
#define rb_intern_const(str)
Definition: ruby.h:1879
rb_num2ulong
unsigned long rb_num2ulong(VALUE val)
Definition: numeric.c:2918
TYPE
#define TYPE(x)
Definition: ruby.h:554
FIXNUM_ZERO_P
#define FIXNUM_ZERO_P(num)
Definition: internal.h:1778
rb_float_value
double rb_float_value(VALUE v)
Definition: numeric.c:5834
rb_enc_precise_mbclen
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
Definition: encoding.c:1032
RUBY_NUM_ROUND_HALF_EVEN
@ RUBY_NUM_ROUND_HALF_EVEN
Definition: internal.h:1789
FLT_RADIX
#define FLT_RADIX
Definition: numeric.c:31
rb_int_plus
VALUE rb_int_plus(VALUE x, VALUE y)
Definition: numeric.c:3610
RUBY_NUM_ROUND_HALF_DOWN
@ RUBY_NUM_ROUND_HALF_DOWN
Definition: internal.h:1790
rb_int_idiv
VALUE rb_int_idiv(VALUE x, VALUE y)
Definition: numeric.c:3843
LONG_MAX_PLUS_ONE
#define LONG_MAX_PLUS_ONE
Definition: numeric.c:2841
rb_cFloat
VALUE rb_cFloat
Definition: numeric.c:182
Init_Numeric
void Init_Numeric(void)
Definition: numeric.c:5557
id.h
SIGNED_VALUE
#define SIGNED_VALUE
Definition: ruby.h:104
isinf
#define isinf(__x)
Definition: rb_mjit_min_header-2.7.1.h:3641
rb_num2ushort
unsigned short rb_num2ushort(VALUE val)
Definition: numeric.c:3059
id_div
#define id_div
Definition: numeric.c:175
DEFINE_INT_SQRT
#define DEFINE_INT_SQRT(rettype, prefix, argtype)
Definition: numeric.c:5360
assert.h
rb_inspect
VALUE rb_inspect(VALUE)
Convenient wrapper of Object::inspect.
Definition: object.c:551
ruby_float_step_size
double ruby_float_step_size(double beg, double end, double unit, int excl)
Definition: numeric.c:2495
rb_float_pow
VALUE rb_float_pow(VALUE x, VALUE y)
Definition: numeric.c:1298
floor
double floor(double)
pow
double pow(double, double)
rb_usascii_str_new_cstr
#define rb_usascii_str_new_cstr(str)
Definition: rb_mjit_min_header-2.7.1.h:6082
rb_big_or
VALUE rb_big_or(VALUE x, VALUE y)
Definition: bignum.c:6479
arg
VALUE arg
Definition: rb_mjit_min_header-2.7.1.h:5562
rb_int_truncate
VALUE rb_int_truncate(VALUE num, int ndigits)
Definition: numeric.c:2184
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:2314
rb_Float
VALUE rb_Float(VALUE)
Equivalent to Kernel#Float in Ruby.
Definition: object.c:3493
rb_big2long
long rb_big2long(VALUE x)
Definition: bignum.c:5140
Qundef
#define Qundef
Definition: ruby.h:470
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1755
T_RATIONAL
#define T_RATIONAL
Definition: ruby.h:541
CHAR_BIT
#define CHAR_BIT
Definition: ruby.h:227
rb_complex_mul
VALUE rb_complex_mul(VALUE self, VALUE other)
Definition: complex.c:872
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1551
frexp
double frexp(double, int *)
rb_ulong_isqrt
unsigned long rb_ulong_isqrt(unsigned long)
idLE
@ idLE
Definition: id.h:93
ptr
struct RIMemo * ptr
Definition: debug.c:74
rb_str_new
#define rb_str_new(str, len)
Definition: rb_mjit_min_header-2.7.1.h:6077
rb_flo_div_flo
MJIT_FUNC_EXPORTED VALUE rb_flo_div_flo(VALUE x, VALUE y)
Definition: numeric.c:1110
Qfalse
#define Qfalse
Definition: ruby.h:467
rb_int_equal
VALUE rb_int_equal(VALUE x, VALUE y)
Definition: numeric.c:4163
RETURN_SIZED_ENUMERATOR
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
Definition: intern.h:271
DBL2NUM
#define DBL2NUM(dbl)
Definition: ruby.h:967
rb_name_error
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:1513
rb_int_fdiv
VALUE rb_int_fdiv(VALUE x, VALUE y)
Definition: numeric.c:3761
rb_id2str
#define rb_id2str(id)
Definition: vm_backtrace.c:30
rb_rational_reciprocal
VALUE rb_rational_reciprocal(VALUE x)
Definition: rational.c:1875
rb_cInteger
VALUE rb_cInteger
Definition: numeric.c:183
keys
const rb_iseq_t const char const VALUE keys
Definition: rb_mjit_min_header-2.7.1.h:13428
SPECIAL_CONST_P
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1313
DBL_MANT_DIG
#define DBL_MANT_DIG
Definition: numeric.c:58
NULL
#define NULL
Definition: _sdbm.c:101
T_COMPLEX
#define T_COMPLEX
Definition: ruby.h:542
MUL_OVERFLOW_FIXNUM_P
#define MUL_OVERFLOW_FIXNUM_P(a, b)
Definition: internal.h:273
char
#define char
Definition: rb_mjit_min_header-2.7.1.h:2844
rb_cmpint
#define rb_cmpint(cmp, a, b)
idFdiv
@ idFdiv
Definition: rb_mjit_min_header-2.7.1.h:8663
rb_nan
const union bytesequence4_or_float rb_nan
Definition: numeric.c:73
UINT_MAX
#define UINT_MAX
Definition: rb_mjit_min_header-2.7.1.h:4022
FL_WB_PROTECTED
#define FL_WB_PROTECTED
Definition: ruby.h:1279
rb_big_gt
VALUE rb_big_gt(VALUE x, VALUE y)
Definition: bignum.c:5489
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:166
FIX2LONG
#define FIX2LONG(x)
Definition: ruby.h:394
ID2SYM
#define ID2SYM(x)
Definition: ruby.h:414
strlen
size_t strlen(const char *)
OBJ_FREEZE
#define OBJ_FREEZE(x)
Definition: ruby.h:1377
FLOAT_ZERO_P
#define FLOAT_ZERO_P(x)
Definition: internal.h:1782
rb_big_xor
VALUE rb_big_xor(VALUE x, VALUE y)
Definition: bignum.c:6573
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1800
L
#define L(x)
Definition: asm.h:125
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1575
rb_int_gt
VALUE rb_int_gt(VALUE x, VALUE y)
Definition: numeric.c:4252
rb_check_arity
#define rb_check_arity
Definition: intern.h:347
rb_integer_float_cmp
VALUE rb_integer_float_cmp(VALUE x, VALUE y)
Definition: bignum.c:5325
rb_int_lshift
VALUE rb_int_lshift(VALUE x, VALUE y)
Definition: numeric.c:4583
ceil
double ceil(double)
RARRAY_LENINT
#define RARRAY_LENINT(ary)
Definition: ruby.h:1071
rb_str_resize
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2709
rb_int_uminus
VALUE rb_int_uminus(VALUE num)
Definition: numeric.c:3479
rb_bigzero_p
int rb_bigzero_p(VALUE x)
Definition: bignum.c:2919
rb_float_cmp
MJIT_FUNC_EXPORTED int rb_float_cmp(VALUE x, VALUE y)
Definition: numeric.c:1487
rb_big_uminus
VALUE rb_big_uminus(VALUE x)
Definition: bignum.c:5554
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
DBL_MAX_10_EXP
#define DBL_MAX_10_EXP
Definition: numeric.c:52
FLOAT_OUT_OF_RANGE
#define FLOAT_OUT_OF_RANGE(val, type)
Definition: numeric.c:2834
rb_usascii_str_new
#define rb_usascii_str_new(str, len)
Definition: rb_mjit_min_header-2.7.1.h:6079
rb_fix_plus
VALUE rb_fix_plus(VALUE x, VALUE y)
Definition: numeric.c:3604
DBL_MIN
#define DBL_MIN
Definition: numeric.c:37
rb_eRangeError
VALUE rb_eRangeError
Definition: error.c:926
RUBY_NUM_ROUND_HALF_UP
@ RUBY_NUM_ROUND_HALF_UP
Definition: internal.h:1788
num_dup
#define num_dup
Definition: numeric.c:524
LONG2NUM
#define LONG2NUM(x)
Definition: ruby.h:1644
rb_obj_class
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
BIGNUM_POSITIVE_P
#define BIGNUM_POSITIVE_P(b)
Definition: internal.h:765
rb_int_mul
VALUE rb_int_mul(VALUE x, VALUE y)
Definition: numeric.c:3699
rb_big_odd_p
VALUE rb_big_odd_p(VALUE num)
Definition: bignum.c:6831
rb_fix2short
short rb_fix2short(VALUE val)
Definition: numeric.c:3050
SIZEOF_VALUE
#define SIZEOF_VALUE
Definition: ruby.h:105
num_clone
#define num_clone
Definition: numeric.c:508
rb_big_isqrt
VALUE rb_big_isqrt(VALUE)
Definition: bignum.c:6911
rb_int_cmp
VALUE rb_int_cmp(VALUE x, VALUE y)
Definition: numeric.c:4212
ULONG2NUM
#define ULONG2NUM(x)
Definition: ruby.h:1645
rb_big_and
VALUE rb_big_and(VALUE x, VALUE y)
Definition: bignum.c:6360
FIXNUM_FLAG
#define FIXNUM_FLAG
Definition: ruby.h:472
HUGE_VAL
#define HUGE_VAL
Definition: missing.h:161
rb_ascii8bit_encoding
rb_encoding * rb_ascii8bit_encoding(void)
Definition: encoding.c:1316
id_divmod
#define id_divmod
Definition: numeric.c:176
rb_num_coerce_relop
VALUE rb_num_coerce_relop(VALUE x, VALUE y, ID func)
Definition: numeric.c:461
rb_num2fix
VALUE rb_num2fix(VALUE val)
Definition: numeric.c:3083
LIKELY
#define LIKELY(x)
Definition: ffi_common.h:125
rb_encoding
const typedef OnigEncodingType rb_encoding
Definition: encoding.h:115
rb_float_ceil
VALUE rb_float_ceil(VALUE num, int ndigits)
Definition: numeric.c:2017
POSFIXABLE
#define POSFIXABLE(f)
Definition: ruby.h:397
id_cmp
#define id_cmp
Definition: numeric.c:179
rb_int_modulo
VALUE rb_int_modulo(VALUE x, VALUE y)
Definition: numeric.c:3886
rb_big_cmp
VALUE rb_big_cmp(VALUE x, VALUE y)
Definition: bignum.c:5419
rb_frame_this_func
ID rb_frame_this_func(void)
The original name of the current method.
Definition: eval.c:1183
T_FIXNUM
#define T_FIXNUM
Definition: ruby.h:535
rb_ary_new_from_args
#define rb_ary_new_from_args(n,...)
Definition: rb_mjit_min_header-2.7.1.h:7161
rb_yield_1
VALUE rb_yield_1(VALUE val)
Definition: vm_eval.c:1231
h
size_t st_index_t h
Definition: rb_mjit_min_header-2.7.1.h:5423
rb_eFloatDomainError
VALUE rb_eFloatDomainError
Definition: numeric.c:189
domain_error
#define domain_error(msg)
Definition: numeric.c:5398
mask
enum @11::@13::@14 mask
rb_to_id
ID rb_to_id(VALUE)
Definition: string.c:11146
rb_big_divmod
VALUE rb_big_divmod(VALUE x, VALUE y)
Definition: bignum.c:6135
rb_num2long
long rb_num2long(VALUE val)
Definition: numeric.c:2849
rb_fix2int
long rb_fix2int(VALUE val)
Definition: numeric.c:3003
rb_memcicmp
int rb_memcicmp(const void *, const void *, long)
Definition: re.c:80
ISALNUM
#define ISALNUM(c)
Definition: ruby.h:2310
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:1195
rb_int_abs
VALUE rb_int_abs(VALUE num)
Definition: numeric.c:4855
NUMERR_TYPE
#define NUMERR_TYPE
rb_complex_new
VALUE rb_complex_new(VALUE x, VALUE y)
Definition: complex.c:1527
rb_float_mul
VALUE rb_float_mul(VALUE x, VALUE y)
Definition: numeric.c:1072
rb_eZeroDivError
VALUE rb_eZeroDivError
Definition: numeric.c:188
idGE
@ idGE
Definition: id.h:95
idPow
@ idPow
Definition: id.h:83
rb_integer_float_eq
COMPILER_WARNING_POP VALUE rb_integer_float_eq(VALUE x, VALUE y)
Definition: bignum.c:5386
nan
RUBY_EXTERN double nan(const char *)
Definition: nan.c:7
isnan
#define isnan(x)
Definition: win32.h:369
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:922
rb_int_minus
VALUE rb_int_minus(VALUE x, VALUE y)
Definition: numeric.c:3649
rb_big_idiv
VALUE rb_big_idiv(VALUE x, VALUE y)
Definition: bignum.c:6097
RB_ULONG
#define RB_ULONG
Definition: numeric.c:5381
rb_int_powm
VALUE rb_int_powm(int const argc, VALUE *const argv, VALUE const num)
Definition: bignum.c:7112
mod
#define mod(x, y)
Definition: date_strftime.c:28
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition: ruby.h:1101
rb_int_positive_pow
VALUE rb_int_positive_pow(long x, unsigned long y)
Definition: numeric.c:4033
strncasecmp
int strncasecmp(const char *, const char *, size_t) __attribute__((__pure__))
rb_intern
#define rb_intern(str)
rb_num2int
long rb_num2int(VALUE val)
Definition: numeric.c:2997
rb_int_ceil
VALUE rb_int_ceil(VALUE num, int ndigits)
Definition: numeric.c:2160
rb_big_minus
VALUE rb_big_minus(VALUE x, VALUE y)
Definition: bignum.c:5853
id_eq
#define id_eq
Definition: numeric.c:178
FALSE
#define FALSE
Definition: nkf.h:174
rb_num_coerce_cmp
VALUE rb_num_coerce_cmp(VALUE x, VALUE y, ID func)
Definition: numeric.c:453
FIXNUM_P
#define FIXNUM_P(f)
Definition: ruby.h:396
int_pred
#define int_pred
Definition: numeric.c:3359
rb_to_int
VALUE rb_to_int(VALUE)
Converts val into Integer.
Definition: object.c:3021
flo_eql
#define flo_eql
Definition: numeric.c:1668
RGENGC_WB_PROTECTED_FLOAT
#define RGENGC_WB_PROTECTED_FLOAT
Definition: ruby.h:823
rb_int_positive_p
int rb_int_positive_p(VALUE num)
Definition: numeric.c:301
NUM2ULL
#define NUM2ULL(x)
ruby_num_rounding_mode
ruby_num_rounding_mode
Definition: internal.h:1787
rb_error_arity
MJIT_STATIC void rb_error_arity(int argc, int min, int max)
Definition: vm_insnhelper.c:387
rb_default_internal_encoding
rb_encoding * rb_default_internal_encoding(void)
Definition: encoding.c:1512
ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
#define ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
Definition: onigmo.h:691
rb_num_get_rounding_option
enum ruby_num_rounding_mode rb_num_get_rounding_option(VALUE opts)
Definition: numeric.c:200
rb_eMathDomainError
RUBY_EXTERN VALUE rb_eMathDomainError
Definition: ruby.h:2088
SIZED_ENUMERATOR
#define SIZED_ENUMERATOR(obj, argc, argv, size_fn)
Definition: intern.h:265
rb_int_pow
VALUE rb_int_pow(VALUE x, VALUE y)
Definition: numeric.c:4106
NUMERR_NEGATIVE
#define NUMERR_NEGATIVE
RUBY_NUM_ROUND_DEFAULT
@ RUBY_NUM_ROUND_DEFAULT
Definition: internal.h:1791
RARRAY_CONST_PTR
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:1072
rb_float_gt
VALUE rb_float_gt(VALUE x, VALUE y)
Definition: numeric.c:1503
rb_eNotImpError
VALUE rb_eNotImpError
Definition: error.c:932
CLASS_OF
#define CLASS_OF(v)
Definition: ruby.h:484
rb_num_zerodiv
void rb_num_zerodiv(void)
Definition: numeric.c:194
rb_big_rshift
VALUE rb_big_rshift(VALUE x, VALUE y)
Definition: bignum.c:6651
rb_big_eq
VALUE rb_big_eq(VALUE x, VALUE y)
Definition: bignum.c:5524
fabs
double fabs(double)
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:1070
rb_flo_is_infinite_p
VALUE rb_flo_is_infinite_p(VALUE num)
Definition: numeric.c:1750
FIXNUM_POSITIVE_P
#define FIXNUM_POSITIVE_P(num)
Definition: internal.h:1776
neg
#define neg(x)
Definition: time.c:141
rb_scan_args
#define rb_scan_args(argc, argvp, fmt,...)
Definition: rb_mjit_min_header-2.7.1.h:6333
int_succ
#define int_succ
Definition: numeric.c:3333
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:2010
rb_int_and
VALUE rb_int_and(VALUE x, VALUE y)
Definition: numeric.c:4467
isfinite
#define isfinite(x)
Definition: missing.h:192
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4322
INT_MIN
#define INT_MIN
Definition: rb_mjit_min_header-2.7.1.h:4018
T_BIGNUM
#define T_BIGNUM
Definition: ruby.h:533
rb_rational_pow
VALUE rb_rational_pow(VALUE self, VALUE other)
Definition: rational.c:1002
rb_int_div
VALUE rb_int_div(VALUE x, VALUE y)
Definition: numeric.c:3816
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:634
rb_big_even_p
VALUE rb_big_even_p(VALUE num)
Definition: bignum.c:6840
internal.h
rb_to_encoding
rb_encoding * rb_to_encoding(VALUE enc)
Definition: encoding.c:245
DBL_MAX
#define DBL_MAX
Definition: numeric.c:40
T_ARRAY
#define T_ARRAY
Definition: ruby.h:530
argv
char ** argv
Definition: ruby.c:223
f
#define f
ruby_float_mod
MJIT_FUNC_EXPORTED double ruby_float_mod(double x, double y)
Definition: numeric.c:1207
ONIGERR_INVALID_CODE_POINT_VALUE
#define ONIGERR_INVALID_CODE_POINT_VALUE
Definition: onigmo.h:689
LONG_LONG
#define LONG_LONG
Definition: rb_mjit_min_header-2.7.1.h:3907
div
void div_t div(int __numer, int __denom)
ruby_num_interval_step_size
VALUE ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
Definition: numeric.c:2555
LONG_MIN_MINUS_ONE_IS_LESS_THAN
#define LONG_MIN_MINUS_ONE_IS_LESS_THAN(n)
Definition: numeric.c:2843
rb_flo_is_finite_p
VALUE rb_flo_is_finite_p(VALUE num)
Definition: numeric.c:1770
BIGNUM_NEGATIVE_P
#define BIGNUM_NEGATIVE_P(b)
Definition: internal.h:766
FIX2ULONG
#define FIX2ULONG(x)
Definition: ruby.h:395
rb_cmperr
void rb_cmperr(VALUE x, VALUE y)
Definition: compar.c:25
BDIGIT_DBL
#define BDIGIT_DBL
Definition: bigdecimal.h:49
T_NIL
#define T_NIL
Definition: ruby.h:522
BDIGIT
#define BDIGIT
Definition: bigdecimal.h:48
rb_cFixnum
#define rb_cFixnum
Definition: internal.h:1311
DBL_MAX_EXP
#define DBL_MAX_EXP
Definition: numeric.c:46
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
rb_big_size_m
VALUE rb_big_size_m(VALUE big)
Definition: bignum.c:6784
rb_rational_raw
VALUE rb_rational_raw(VALUE, VALUE)
Definition: rational.c:1939
BIGNUM_SIGN
#define BIGNUM_SIGN(b)
Definition: internal.h:761
ULONG_MAX_PLUS_ONE
#define ULONG_MAX_PLUS_ONE
Definition: numeric.c:2842
rb_float_uminus
VALUE rb_float_uminus(VALUE flt)
Definition: numeric.c:1011
rb_mComparable
VALUE rb_mComparable
Definition: compar.c:16
SIZEOF_LONG
#define SIZEOF_LONG
Definition: rb_mjit_min_header-2.7.1.h:85
memset
void * memset(void *, int, size_t)
DBL_MIN_10_EXP
#define DBL_MIN_10_EXP
Definition: numeric.c:49
rb_big_mul
VALUE rb_big_mul(VALUE x, VALUE y)
Definition: bignum.c:5933
idSize
@ idSize
Definition: rb_mjit_min_header-2.7.1.h:8627
int
__inline__ int
Definition: rb_mjit_min_header-2.7.1.h:2807
flo_eq
#define flo_eq
Definition: numeric.c:1406
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
memcpy
void * memcpy(void *__restrict, const void *__restrict, size_t)
snprintf
int snprintf(char *__restrict, size_t, const char *__restrict,...) __attribute__((__format__(__printf__
bit_length
#define bit_length(x)
Definition: internal.h:680
rb_float_equal
MJIT_FUNC_EXPORTED VALUE rb_float_equal(VALUE x, VALUE y)
Definition: numeric.c:1383
argc
int argc
Definition: ruby.c:222
rb_num_to_uint
int rb_num_to_uint(VALUE val, unsigned int *ret)
Definition: numeric.c:244
rb_exec_recursive
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:5075
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:289
ROUND_CALL
#define ROUND_CALL(mode, name, args)
Definition: internal.h:1798
rb_big_plus
VALUE rb_big_plus(VALUE x, VALUE y)
Definition: bignum.c:5824
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1725
rb_big2str
VALUE rb_big2str(VALUE x, int base)
Definition: bignum.c:5091
rb_define_const
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2880
ruby_digitmap
const char ruby_digitmap[]
Definition: bignum.c:38
err
int err
Definition: win32.c:135
DBL_MIN_EXP
#define DBL_MIN_EXP
Definition: numeric.c:43
memmove
#define memmove(dst, src, len)
Definition: rb_mjit_min_header-2.7.1.h:2816
BUILTIN_TYPE
#define BUILTIN_TYPE(x)
Definition: ruby.h:551
rb_big_norm
VALUE rb_big_norm(VALUE x)
Definition: bignum.c:3152
RFLOAT_VALUE
#define RFLOAT_VALUE(v)
Definition: ruby.h:966
rb_int_negative_p
int rb_int_negative_p(VALUE num)
Definition: numeric.c:307
xfree
#define xfree
Definition: defines.h:216
v
int VALUE v
Definition: rb_mjit_min_header-2.7.1.h:12257
FIXNUM_NEGATIVE_P
#define FIXNUM_NEGATIVE_P(num)
Definition: internal.h:1777
rb_deprecate_constant
void rb_deprecate_constant(VALUE mod, const char *name)
Definition: variable.c:2947
MJIT_FUNC_EXPORTED
#define MJIT_FUNC_EXPORTED
Definition: defines.h:396
NORETURN
NORETURN(static void num_funcall_op_1_recursion(VALUE x, ID func, VALUE y))
rb_range_values
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Definition: range.c:1243
rb_big_bit_length
VALUE rb_big_bit_length(VALUE big)
Definition: bignum.c:6790
Qtrue
#define Qtrue
Definition: ruby.h:468
rb_str_catf
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1237
rb_big_div
VALUE rb_big_div(VALUE x, VALUE y)
Definition: bignum.c:6091
len
uint8_t len
Definition: escape.c:17
rb_big_pow
VALUE rb_big_pow(VALUE x, VALUE y)
Definition: bignum.c:6244
SYMBOL_P
#define SYMBOL_P(x)
Definition: ruby.h:413
rb_usascii_str_new2
#define rb_usascii_str_new2
Definition: intern.h:909
NUM2LL
#define NUM2LL(x)
PRIdVALUE
#define PRIdVALUE
Definition: ruby.h:161
rb_big_aref
VALUE rb_big_aref(VALUE x, VALUE y)
Definition: bignum.c:6681
rb_enc_uint_chr
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
Definition: numeric.c:3375
SHRT_MIN
#define SHRT_MIN
Definition: rb_mjit_min_header-2.7.1.h:4012
rb_exec_recursive_paired
VALUE rb_exec_recursive_paired(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE, VALUE)
Definition: thread.c:5086
rb_float_eql
MJIT_FUNC_EXPORTED VALUE rb_float_eql(VALUE x, VALUE y)
Definition: numeric.c:1654
LONG2FIX
#define LONG2FIX(i)
Definition: ruby.h:265
rb_complex_plus
VALUE rb_complex_plus(VALUE self, VALUE other)
Definition: complex.c:778
rb_enc_codelen
int rb_enc_codelen(int c, rb_encoding *enc)
Definition: encoding.c:1089
T_STRING
#define T_STRING
Definition: ruby.h:528
rb_num_pow
VALUE rb_num_pow(VALUE x, VALUE y)
Definition: numeric.c:4118
rb_sym2str
VALUE rb_sym2str(VALUE)
Definition: symbol.c:784
rb_big_lt
VALUE rb_big_lt(VALUE x, VALUE y)
Definition: bignum.c:5501
fmod
double fmod(double, double)
ruby_float_step
int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl, int allow_endless)
Definition: numeric.c:2524
rb_yield
VALUE rb_yield(VALUE)
Definition: vm_eval.c:1237
FIXABLE
#define FIXABLE(f)
Definition: ruby.h:399
rb_int_succ
VALUE rb_int_succ(VALUE num)
Definition: numeric.c:3321
RB_INTEGER_TYPE_P
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:15
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:723
rb_big_abs
VALUE rb_big_abs(VALUE x)
Definition: bignum.c:6762
NEWOBJ_OF
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:785
rb_big2dbl
double rb_big2dbl(VALUE x)
Definition: bignum.c:5310
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:715
Qnil
#define Qnil
Definition: ruby.h:469
rb_big_size
size_t rb_big_size(VALUE big)
Definition: bignum.c:6778
FLT_ROUNDS
#define FLT_ROUNDS
Definition: numeric.c:34
NUM2DBL
#define NUM2DBL(x)
Definition: ruby.h:774
rb_remove_method_id
void rb_remove_method_id(VALUE, ID)
Definition: vm_method.c:1026
rb_absint_size
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
Definition: bignum.c:3247
util.h
BIGNUM_LEN
#define BIGNUM_LEN(b)
Definition: internal.h:774
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:919
rb_float_new_in_heap
VALUE rb_float_new_in_heap(double d)
Definition: numeric.c:895
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:722
rb_float_new
VALUE rb_float_new(double d)
Definition: numeric.c:5841
UNREACHABLE_RETURN
#define UNREACHABLE_RETURN(val)
Definition: ruby.h:59
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:1005
nextafter
RUBY_EXTERN double nextafter(double x, double y)
Definition: nextafter.c:9
rb_gcd
VALUE rb_gcd(VALUE x, VALUE y)
Definition: rational.c:1894
INT_MAX
#define INT_MAX
Definition: rb_mjit_min_header-2.7.1.h:4020
rb_any_to_s
VALUE rb_any_to_s(VALUE)
Default implementation of #to_s.
Definition: object.c:527
rb_big_comp
VALUE rb_big_comp(VALUE x)
Definition: bignum.c:5564
rb_enc_str_new
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Definition: string.c:796
rb_str_cat
VALUE rb_str_cat(VALUE, const char *, long)
Definition: string.c:2812
T_TRUE
#define T_TRUE
Definition: ruby.h:536
rb_obj_is_kind_of
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
Definition: object.c:692
rb_dbl_long_hash
long rb_dbl_long_hash(double d)
Definition: hash.c:160
RTEST
#define RTEST(v)
Definition: ruby.h:481
rb_fix_aref
MJIT_FUNC_EXPORTED VALUE rb_fix_aref(VALUE fix, VALUE idx)
Definition: numeric.c:4642
rb_int_odd_p
VALUE rb_int_odd_p(VALUE num)
Definition: numeric.c:3222
method_basic_p
#define method_basic_p(klass)
Definition: numeric.c:274
RBIGNUM_NEGATIVE_P
#define RBIGNUM_NEGATIVE_P(b)
Definition: ruby.h:1263
RB_FLOAT_TYPE_P
#define RB_FLOAT_TYPE_P(obj)
Definition: ruby.h:556
rb_int2str
VALUE rb_int2str(VALUE x, int base)
Definition: numeric.c:3562
FIT_SQRT_LONG
#define FIT_SQRT_LONG(n)
Definition: numeric.c:3663
RSTRING_END
#define RSTRING_END(str)
Definition: ruby.h:1013
rb_big2ull
unsigned long long rb_big2ull(VALUE)
name
const char * name
Definition: nkf.c:208
signbit
#define signbit(__x)
Definition: rb_mjit_min_header-2.7.1.h:3644
rb_funcallv
#define rb_funcallv(recv, mid, argc, argv)
Definition: rb_mjit_min_header-2.7.1.h:7826