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