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