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