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