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