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