Ruby  2.0.0p451(2014-02-24revision45167)
numeric.c
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   numeric.c -
00004 
00005   $Author: nagachika $
00006   created at: Fri Aug 13 18:33:09 JST 1993
00007 
00008   Copyright (C) 1993-2007 Yukihiro Matsumoto
00009 
00010 **********************************************************************/
00011 
00012 #include "ruby/ruby.h"
00013 #include "ruby/encoding.h"
00014 #include "ruby/util.h"
00015 #include "internal.h"
00016 #include "id.h"
00017 #include <ctype.h>
00018 #include <math.h>
00019 #include <stdio.h>
00020 
00021 #if defined(__FreeBSD__) && __FreeBSD__ < 4
00022 #include <floatingpoint.h>
00023 #endif
00024 
00025 #ifdef HAVE_FLOAT_H
00026 #include <float.h>
00027 #endif
00028 
00029 #ifdef HAVE_IEEEFP_H
00030 #include <ieeefp.h>
00031 #endif
00032 
00033 /* use IEEE 64bit values if not defined */
00034 #ifndef FLT_RADIX
00035 #define FLT_RADIX 2
00036 #endif
00037 #ifndef FLT_ROUNDS
00038 #define FLT_ROUNDS 1
00039 #endif
00040 #ifndef DBL_MIN
00041 #define DBL_MIN 2.2250738585072014e-308
00042 #endif
00043 #ifndef DBL_MAX
00044 #define DBL_MAX 1.7976931348623157e+308
00045 #endif
00046 #ifndef DBL_MIN_EXP
00047 #define DBL_MIN_EXP (-1021)
00048 #endif
00049 #ifndef DBL_MAX_EXP
00050 #define DBL_MAX_EXP 1024
00051 #endif
00052 #ifndef DBL_MIN_10_EXP
00053 #define DBL_MIN_10_EXP (-307)
00054 #endif
00055 #ifndef DBL_MAX_10_EXP
00056 #define DBL_MAX_10_EXP 308
00057 #endif
00058 #ifndef DBL_DIG
00059 #define DBL_DIG 15
00060 #endif
00061 #ifndef DBL_MANT_DIG
00062 #define DBL_MANT_DIG 53
00063 #endif
00064 #ifndef DBL_EPSILON
00065 #define DBL_EPSILON 2.2204460492503131e-16
00066 #endif
00067 
00068 #ifdef HAVE_INFINITY
00069 #elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
00070 const union bytesequence4_or_float rb_infinity = {{0x00, 0x00, 0x80, 0x7f}};
00071 #else
00072 const union bytesequence4_or_float rb_infinity = {{0x7f, 0x80, 0x00, 0x00}};
00073 #endif
00074 
00075 #ifdef HAVE_NAN
00076 #elif !defined(WORDS_BIGENDIAN) /* BYTE_ORDER == LITTLE_ENDIAN */
00077 const union bytesequence4_or_float rb_nan = {{0x00, 0x00, 0xc0, 0x7f}};
00078 #else
00079 const union bytesequence4_or_float rb_nan = {{0x7f, 0xc0, 0x00, 0x00}};
00080 #endif
00081 
00082 #ifndef HAVE_ROUND
00083 double
00084 round(double x)
00085 {
00086     double f;
00087 
00088     if (x > 0.0) {
00089         f = floor(x);
00090         x = f + (x - f >= 0.5);
00091     }
00092     else if (x < 0.0) {
00093         f = ceil(x);
00094         x = f - (f - x >= 0.5);
00095     }
00096     return x;
00097 }
00098 #endif
00099 
00100 static VALUE fix_uminus(VALUE num);
00101 static VALUE fix_mul(VALUE x, VALUE y);
00102 static VALUE int_pow(long x, unsigned long y);
00103 
00104 static ID id_coerce, id_to_i, id_eq, id_div;
00105 
00106 VALUE rb_cNumeric;
00107 VALUE rb_cFloat;
00108 VALUE rb_cInteger;
00109 VALUE rb_cFixnum;
00110 
00111 VALUE rb_eZeroDivError;
00112 VALUE rb_eFloatDomainError;
00113 
00114 void
00115 rb_num_zerodiv(void)
00116 {
00117     rb_raise(rb_eZeroDivError, "divided by 0");
00118 }
00119 
00120 /* experimental API */
00121 int
00122 rb_num_to_uint(VALUE val, unsigned int *ret)
00123 {
00124 #define NUMERR_TYPE     1
00125 #define NUMERR_NEGATIVE 2
00126 #define NUMERR_TOOLARGE 3
00127     if (FIXNUM_P(val)) {
00128         long v = FIX2LONG(val);
00129 #if SIZEOF_INT < SIZEOF_LONG
00130         if (v > (long)UINT_MAX) return NUMERR_TOOLARGE;
00131 #endif
00132         if (v < 0) return NUMERR_NEGATIVE;
00133         *ret = (unsigned int)v;
00134         return 0;
00135     }
00136 
00137     switch (TYPE(val)) {
00138       case T_BIGNUM:
00139         if (RBIGNUM_NEGATIVE_P(val)) return NUMERR_NEGATIVE;
00140 #if SIZEOF_INT < SIZEOF_LONG
00141         /* long is 64bit */
00142         return NUMERR_TOOLARGE;
00143 #else
00144         /* long is 32bit */
00145 #define DIGSPERLONG (SIZEOF_LONG/SIZEOF_BDIGITS)
00146         if (RBIGNUM_LEN(val) > DIGSPERLONG) return NUMERR_TOOLARGE;
00147         *ret = (unsigned int)rb_big2ulong((VALUE)val);
00148         return 0;
00149 #endif
00150     }
00151     return NUMERR_TYPE;
00152 }
00153 
00154 #define method_basic_p(klass) rb_method_basic_definition_p(klass, mid)
00155 
00156 static inline int
00157 positive_int_p(VALUE num)
00158 {
00159     const ID mid = '>';
00160 
00161     if (FIXNUM_P(num)) {
00162         if (method_basic_p(rb_cFixnum))
00163             return (SIGNED_VALUE)num > 0;
00164     }
00165     else if (RB_TYPE_P(num, T_BIGNUM)) {
00166         if (method_basic_p(rb_cBignum))
00167             return RBIGNUM_POSITIVE_P(num);
00168     }
00169     return RTEST(rb_funcall(num, mid, 1, INT2FIX(0)));
00170 }
00171 
00172 static inline int
00173 negative_int_p(VALUE num)
00174 {
00175     const ID mid = '<';
00176 
00177     if (FIXNUM_P(num)) {
00178         if (method_basic_p(rb_cFixnum))
00179             return (SIGNED_VALUE)num < 0;
00180     }
00181     else if (RB_TYPE_P(num, T_BIGNUM)) {
00182         if (method_basic_p(rb_cBignum))
00183             return RBIGNUM_NEGATIVE_P(num);
00184     }
00185     return RTEST(rb_funcall(num, mid, 1, INT2FIX(0)));
00186 }
00187 
00188 int
00189 rb_num_negative_p(VALUE num)
00190 {
00191     return negative_int_p(num);
00192 }
00193 
00194 /*
00195  *  call-seq:
00196  *     num.coerce(numeric)  ->  array
00197  *
00198  *  If <i>aNumeric</i> is the same type as <i>num</i>, returns an array
00199  *  containing <i>aNumeric</i> and <i>num</i>. Otherwise, returns an
00200  *  array with both <i>aNumeric</i> and <i>num</i> represented as
00201  *  <code>Float</code> objects. This coercion mechanism is used by
00202  *  Ruby to handle mixed-type numeric operations: it is intended to
00203  *  find a compatible common type between the two operands of the operator.
00204  *
00205  *     1.coerce(2.5)   #=> [2.5, 1.0]
00206  *     1.2.coerce(3)   #=> [3.0, 1.2]
00207  *     1.coerce(2)     #=> [2, 1]
00208  */
00209 
00210 static VALUE
00211 num_coerce(VALUE x, VALUE y)
00212 {
00213     if (CLASS_OF(x) == CLASS_OF(y))
00214         return rb_assoc_new(y, x);
00215     x = rb_Float(x);
00216     y = rb_Float(y);
00217     return rb_assoc_new(y, x);
00218 }
00219 
00220 static VALUE
00221 coerce_body(VALUE *x)
00222 {
00223     return rb_funcall(x[1], id_coerce, 1, x[0]);
00224 }
00225 
00226 NORETURN(static void coerce_failed(VALUE x, VALUE y));
00227 static void
00228 coerce_failed(VALUE x, VALUE y)
00229 {
00230     rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
00231              (rb_special_const_p(y)? rb_inspect(y) : rb_obj_class(y)),
00232              rb_obj_class(x));
00233 }
00234 
00235 static VALUE
00236 coerce_rescue(VALUE *x)
00237 {
00238     coerce_failed(x[0], x[1]);
00239     return Qnil;                /* dummy */
00240 }
00241 
00242 static int
00243 do_coerce(VALUE *x, VALUE *y, int err)
00244 {
00245     VALUE ary;
00246     VALUE a[2];
00247 
00248     a[0] = *x; a[1] = *y;
00249 
00250     if (!rb_respond_to(*y, id_coerce)) {
00251         if (err) {
00252             coerce_rescue(a);
00253         }
00254         return FALSE;
00255     }
00256 
00257     ary = rb_rescue(coerce_body, (VALUE)a, err ? coerce_rescue : 0, (VALUE)a);
00258     if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) {
00259         if (err) {
00260             rb_raise(rb_eTypeError, "coerce must return [x, y]");
00261         }
00262         return FALSE;
00263     }
00264 
00265     *x = RARRAY_PTR(ary)[0];
00266     *y = RARRAY_PTR(ary)[1];
00267     return TRUE;
00268 }
00269 
00270 VALUE
00271 rb_num_coerce_bin(VALUE x, VALUE y, ID func)
00272 {
00273     do_coerce(&x, &y, TRUE);
00274     return rb_funcall(x, func, 1, y);
00275 }
00276 
00277 VALUE
00278 rb_num_coerce_cmp(VALUE x, VALUE y, ID func)
00279 {
00280     if (do_coerce(&x, &y, FALSE))
00281         return rb_funcall(x, func, 1, y);
00282     return Qnil;
00283 }
00284 
00285 VALUE
00286 rb_num_coerce_relop(VALUE x, VALUE y, ID func)
00287 {
00288     VALUE c, x0 = x, y0 = y;
00289 
00290     if (!do_coerce(&x, &y, FALSE) ||
00291         NIL_P(c = rb_funcall(x, func, 1, y))) {
00292         rb_cmperr(x0, y0);
00293         return Qnil;            /* not reached */
00294     }
00295     return c;
00296 }
00297 
00298 /*
00299  * Trap attempts to add methods to <code>Numeric</code> objects. Always
00300  * raises a <code>TypeError</code>
00301  */
00302 
00303 static VALUE
00304 num_sadded(VALUE x, VALUE name)
00305 {
00306     ID mid = rb_to_id(name);
00307     /* ruby_frame = ruby_frame->prev; */ /* pop frame for "singleton_method_added" */
00308     /* Numerics should be values; singleton_methods should not be added to them */
00309     rb_remove_method_id(rb_singleton_class(x), mid);
00310     rb_raise(rb_eTypeError,
00311              "can't define singleton method \"%"PRIsVALUE"\" for %"PRIsVALUE,
00312              rb_id2str(mid),
00313              rb_obj_class(x));
00314 
00315     UNREACHABLE;
00316 }
00317 
00318 /* :nodoc: */
00319 static VALUE
00320 num_init_copy(VALUE x, VALUE y)
00321 {
00322     /* Numerics are immutable values, which should not be copied */
00323     rb_raise(rb_eTypeError, "can't copy %"PRIsVALUE, rb_obj_class(x));
00324 
00325     UNREACHABLE;
00326 }
00327 
00328 /*
00329  *  call-seq:
00330  *     +num  ->  num
00331  *
00332  *  Unary Plus---Returns the receiver's value.
00333  */
00334 
00335 static VALUE
00336 num_uplus(VALUE num)
00337 {
00338     return num;
00339 }
00340 
00341 /*
00342  *  call-seq:
00343  *     num.i  ->  Complex(0,num)
00344  *
00345  *  Returns the corresponding imaginary number.
00346  *  Not available for complex numbers.
00347  */
00348 
00349 static VALUE
00350 num_imaginary(VALUE num)
00351 {
00352     return rb_complex_new(INT2FIX(0), num);
00353 }
00354 
00355 
00356 /*
00357  *  call-seq:
00358  *     -num  ->  numeric
00359  *
00360  *  Unary Minus---Returns the receiver's value, negated.
00361  */
00362 
00363 static VALUE
00364 num_uminus(VALUE num)
00365 {
00366     VALUE zero;
00367 
00368     zero = INT2FIX(0);
00369     do_coerce(&zero, &num, TRUE);
00370 
00371     return rb_funcall(zero, '-', 1, num);
00372 }
00373 
00374 /*
00375  *  call-seq:
00376  *     num.quo(numeric)  ->  real
00377  *
00378  *  Returns most exact division (rational for integers, float for floats).
00379  */
00380 
00381 static VALUE
00382 num_quo(VALUE x, VALUE y)
00383 {
00384     return rb_funcall(rb_rational_raw1(x), '/', 1, y);
00385 }
00386 
00387 
00388 /*
00389  *  call-seq:
00390  *     num.fdiv(numeric)  ->  float
00391  *
00392  *  Returns float division.
00393  */
00394 
00395 static VALUE
00396 num_fdiv(VALUE x, VALUE y)
00397 {
00398     return rb_funcall(rb_Float(x), '/', 1, y);
00399 }
00400 
00401 
00402 /*
00403  *  call-seq:
00404  *     num.div(numeric)  ->  integer
00405  *
00406  *  Uses <code>/</code> to perform division, then converts the result to
00407  *  an integer. <code>numeric</code> does not define the <code>/</code>
00408  *  operator; this is left to subclasses.
00409  *
00410  *  Equivalent to
00411  *  <i>num</i>.<code>divmod(</code><i>aNumeric</i><code>)[0]</code>.
00412  *
00413  *  See <code>Numeric#divmod</code>.
00414  */
00415 
00416 static VALUE
00417 num_div(VALUE x, VALUE y)
00418 {
00419     if (rb_equal(INT2FIX(0), y)) rb_num_zerodiv();
00420     return rb_funcall(rb_funcall(x, '/', 1, y), rb_intern("floor"), 0);
00421 }
00422 
00423 
00424 /*
00425  *  call-seq:
00426  *     num.modulo(numeric)  ->  real
00427  *
00428  *     x.modulo(y) means x-y*(x/y).floor
00429  *
00430  *  Equivalent to
00431  *  <i>num</i>.<code>divmod(</code><i>aNumeric</i><code>)[1]</code>.
00432  *
00433  *  See <code>Numeric#divmod</code>.
00434  */
00435 
00436 static VALUE
00437 num_modulo(VALUE x, VALUE y)
00438 {
00439     return rb_funcall(x, '-', 1,
00440                       rb_funcall(y, '*', 1,
00441                                  rb_funcall(x, rb_intern("div"), 1, y)));
00442 }
00443 
00444 /*
00445  *  call-seq:
00446  *     num.remainder(numeric)  ->  real
00447  *
00448  *     x.remainder(y) means x-y*(x/y).truncate
00449  *
00450  *  See <code>Numeric#divmod</code>.
00451  */
00452 
00453 static VALUE
00454 num_remainder(VALUE x, VALUE y)
00455 {
00456     VALUE z = rb_funcall(x, '%', 1, y);
00457 
00458     if ((!rb_equal(z, INT2FIX(0))) &&
00459         ((negative_int_p(x) &&
00460           positive_int_p(y)) ||
00461          (positive_int_p(x) &&
00462           negative_int_p(y)))) {
00463         return rb_funcall(z, '-', 1, y);
00464     }
00465     return z;
00466 }
00467 
00468 /*
00469  *  call-seq:
00470  *     num.divmod(numeric)  ->  array
00471  *
00472  *  Returns an array containing the quotient and modulus obtained by
00473  *  dividing <i>num</i> by <i>numeric</i>. If <code>q, r =
00474  *  x.divmod(y)</code>, then
00475  *
00476  *      q = floor(x/y)
00477  *      x = q*y+r
00478  *
00479  *  The quotient is rounded toward -infinity, as shown in the following table:
00480  *
00481  *     a    |  b  |  a.divmod(b)  |   a/b   | a.modulo(b) | a.remainder(b)
00482  *    ------+-----+---------------+---------+-------------+---------------
00483  *     13   |  4  |   3,    1     |   3     |    1        |     1
00484  *    ------+-----+---------------+---------+-------------+---------------
00485  *     13   | -4  |  -4,   -3     |  -4     |   -3        |     1
00486  *    ------+-----+---------------+---------+-------------+---------------
00487  *    -13   |  4  |  -4,    3     |  -4     |    3        |    -1
00488  *    ------+-----+---------------+---------+-------------+---------------
00489  *    -13   | -4  |   3,   -1     |   3     |   -1        |    -1
00490  *    ------+-----+---------------+---------+-------------+---------------
00491  *     11.5 |  4  |   2,    3.5   |   2.875 |    3.5      |     3.5
00492  *    ------+-----+---------------+---------+-------------+---------------
00493  *     11.5 | -4  |  -3,   -0.5   |  -2.875 |   -0.5      |     3.5
00494  *    ------+-----+---------------+---------+-------------+---------------
00495  *    -11.5 |  4  |  -3,    0.5   |  -2.875 |    0.5      |    -3.5
00496  *    ------+-----+---------------+---------+-------------+---------------
00497  *    -11.5 | -4  |   2,   -3.5   |   2.875 |   -3.5      |    -3.5
00498  *
00499  *
00500  *  Examples
00501  *
00502  *     11.divmod(3)         #=> [3, 2]
00503  *     11.divmod(-3)        #=> [-4, -1]
00504  *     11.divmod(3.5)       #=> [3, 0.5]
00505  *     (-11).divmod(3.5)    #=> [-4, 3.0]
00506  *     (11.5).divmod(3.5)   #=> [3, 1.0]
00507  */
00508 
00509 static VALUE
00510 num_divmod(VALUE x, VALUE y)
00511 {
00512     return rb_assoc_new(num_div(x, y), num_modulo(x, y));
00513 }
00514 
00515 /*
00516  *  call-seq:
00517  *     num.real?  ->  true or false
00518  *
00519  *  Returns <code>true</code> if <i>num</i> is a <code>Real</code>
00520  *  (i.e. non <code>Complex</code>).
00521  */
00522 
00523 static VALUE
00524 num_real_p(VALUE num)
00525 {
00526     return Qtrue;
00527 }
00528 
00529 /*
00530  *  call-seq:
00531  *     num.integer?  ->  true or false
00532  *
00533  *  Returns +true+ if +num+ is an Integer (including Fixnum and Bignum).
00534  *
00535  *      (1.0).integer? #=> false
00536  *      (1).integer?   #=> true
00537  */
00538 
00539 static VALUE
00540 num_int_p(VALUE num)
00541 {
00542     return Qfalse;
00543 }
00544 
00545 /*
00546  *  call-seq:
00547  *     num.abs        ->  numeric
00548  *     num.magnitude  ->  numeric
00549  *
00550  *  Returns the absolute value of <i>num</i>.
00551  *
00552  *     12.abs         #=> 12
00553  *     (-34.56).abs   #=> 34.56
00554  *     -34.56.abs     #=> 34.56
00555  */
00556 
00557 static VALUE
00558 num_abs(VALUE num)
00559 {
00560     if (negative_int_p(num)) {
00561         return rb_funcall(num, rb_intern("-@"), 0);
00562     }
00563     return num;
00564 }
00565 
00566 
00567 /*
00568  *  call-seq:
00569  *     num.zero?  ->  true or false
00570  *
00571  *  Returns <code>true</code> if <i>num</i> has a zero value.
00572  */
00573 
00574 static VALUE
00575 num_zero_p(VALUE num)
00576 {
00577     if (rb_equal(num, INT2FIX(0))) {
00578         return Qtrue;
00579     }
00580     return Qfalse;
00581 }
00582 
00583 
00584 /*
00585  *  call-seq:
00586  *     num.nonzero?  ->  self or nil
00587  *
00588  *  Returns +self+ if <i>num</i> is not zero, <code>nil</code>
00589  *  otherwise. This behavior is useful when chaining comparisons:
00590  *
00591  *     a = %w( z Bb bB bb BB a aA Aa AA A )
00592  *     b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
00593  *     b   #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
00594  */
00595 
00596 static VALUE
00597 num_nonzero_p(VALUE num)
00598 {
00599     if (RTEST(rb_funcall(num, rb_intern("zero?"), 0, 0))) {
00600         return Qnil;
00601     }
00602     return num;
00603 }
00604 
00605 /*
00606  *  call-seq:
00607  *     num.to_int  ->  integer
00608  *
00609  *  Invokes the child class's +to_i+ method to convert +num+ to an integer.
00610  *
00611  *      1.0.class => Float
00612  *      1.0.to_int.class => Fixnum
00613  *      1.0.to_i.class => Fixnum
00614  */
00615 
00616 static VALUE
00617 num_to_int(VALUE num)
00618 {
00619     return rb_funcall(num, id_to_i, 0, 0);
00620 }
00621 
00622 
00623 /********************************************************************
00624  *
00625  * Document-class: Float
00626  *
00627  *  <code>Float</code> objects represent inexact real numbers using
00628  *  the native architecture's double-precision floating point
00629  *  representation.
00630  *
00631  *  Floating point has a different arithmetic and is a inexact number.
00632  *  So you should know its esoteric system. see following:
00633  *
00634  *  - http://docs.sun.com/source/806-3568/ncg_goldberg.html
00635  *  - http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#wiki-floats_imprecise
00636  *  - http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
00637  */
00638 
00639 VALUE
00640 rb_float_new_in_heap(double d)
00641 {
00642     NEWOBJ_OF(flt, struct RFloat, rb_cFloat, T_FLOAT);
00643 
00644     flt->float_value = d;
00645     OBJ_FREEZE(flt);
00646     return (VALUE)flt;
00647 }
00648 
00649 /*
00650  *  call-seq:
00651  *     flt.to_s  ->  string
00652  *
00653  *  Returns a string containing a representation of self. As well as a
00654  *  fixed or exponential form of the number, the call may return
00655  *  ``<code>NaN</code>'', ``<code>Infinity</code>'', and
00656  *  ``<code>-Infinity</code>''.
00657  */
00658 
00659 static VALUE
00660 flo_to_s(VALUE flt)
00661 {
00662     char *ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve);
00663     enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
00664     enum {float_dig = DBL_DIG+1};
00665     char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
00666     double value = RFLOAT_VALUE(flt);
00667     VALUE s;
00668     char *p, *e;
00669     int sign, decpt, digs;
00670 
00671     if (isinf(value))
00672         return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
00673     else if (isnan(value))
00674         return rb_usascii_str_new2("NaN");
00675 
00676     p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
00677     s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0);
00678     if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1;
00679     memcpy(buf, p, digs);
00680     xfree(p);
00681     if (decpt > 0) {
00682         if (decpt < digs) {
00683             memmove(buf + decpt + 1, buf + decpt, digs - decpt);
00684             buf[decpt] = '.';
00685             rb_str_cat(s, buf, digs + 1);
00686         }
00687         else if (decpt <= DBL_DIG) {
00688             long len;
00689             char *ptr;
00690             rb_str_cat(s, buf, digs);
00691             rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2);
00692             ptr = RSTRING_PTR(s) + len;
00693             if (decpt > digs) {
00694                 memset(ptr, '0', decpt - digs);
00695                 ptr += decpt - digs;
00696             }
00697             memcpy(ptr, ".0", 2);
00698         }
00699         else {
00700             goto exp;
00701         }
00702     }
00703     else if (decpt > -4) {
00704         long len;
00705         char *ptr;
00706         rb_str_cat(s, "0.", 2);
00707         rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs);
00708         ptr = RSTRING_PTR(s);
00709         memset(ptr += len, '0', -decpt);
00710         memcpy(ptr -= decpt, buf, digs);
00711     }
00712     else {
00713       exp:
00714         if (digs > 1) {
00715             memmove(buf + 2, buf + 1, digs - 1);
00716         }
00717         else {
00718             buf[2] = '0';
00719             digs++;
00720         }
00721         buf[1] = '.';
00722         rb_str_cat(s, buf, digs + 1);
00723         rb_str_catf(s, "e%+03d", decpt - 1);
00724     }
00725     return s;
00726 }
00727 
00728 /*
00729  *  call-seq:
00730  *     flt.coerce(numeric)  ->  array
00731  *
00732  *  Returns an array with both <i>aNumeric</i> and <i>flt</i> represented
00733  *  as <code>Float</code> objects.
00734  *  This is achieved by converting <i>aNumeric</i> to a <code>Float</code>.
00735  *
00736  *     1.2.coerce(3)       #=> [3.0, 1.2]
00737  *     2.5.coerce(1.1)     #=> [1.1, 2.5]
00738  */
00739 
00740 static VALUE
00741 flo_coerce(VALUE x, VALUE y)
00742 {
00743     return rb_assoc_new(rb_Float(y), x);
00744 }
00745 
00746 /*
00747  * call-seq:
00748  *    -float  ->  float
00749  *
00750  * Returns float, negated.
00751  */
00752 
00753 static VALUE
00754 flo_uminus(VALUE flt)
00755 {
00756     return DBL2NUM(-RFLOAT_VALUE(flt));
00757 }
00758 
00759 /*
00760  * call-seq:
00761  *   float + other  ->  float
00762  *
00763  * Returns a new float which is the sum of <code>float</code>
00764  * and <code>other</code>.
00765  */
00766 
00767 static VALUE
00768 flo_plus(VALUE x, VALUE y)
00769 {
00770     switch (TYPE(y)) {
00771       case T_FIXNUM:
00772         return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
00773       case T_BIGNUM:
00774         return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
00775       case T_FLOAT:
00776         return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
00777       default:
00778         return rb_num_coerce_bin(x, y, '+');
00779     }
00780 }
00781 
00782 /*
00783  * call-seq:
00784  *   float - other  ->  float
00785  *
00786  * Returns a new float which is the difference of <code>float</code>
00787  * and <code>other</code>.
00788  */
00789 
00790 static VALUE
00791 flo_minus(VALUE x, VALUE y)
00792 {
00793     switch (TYPE(y)) {
00794       case T_FIXNUM:
00795         return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
00796       case T_BIGNUM:
00797         return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
00798       case T_FLOAT:
00799         return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
00800       default:
00801         return rb_num_coerce_bin(x, y, '-');
00802     }
00803 }
00804 
00805 /*
00806  * call-seq:
00807  *   float * other  ->  float
00808  *
00809  * Returns a new float which is the product of <code>float</code>
00810  * and <code>other</code>.
00811  */
00812 
00813 static VALUE
00814 flo_mul(VALUE x, VALUE y)
00815 {
00816     switch (TYPE(y)) {
00817       case T_FIXNUM:
00818         return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
00819       case T_BIGNUM:
00820         return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
00821       case T_FLOAT:
00822         return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
00823       default:
00824         return rb_num_coerce_bin(x, y, '*');
00825     }
00826 }
00827 
00828 /*
00829  * call-seq:
00830  *   float / other  ->  float
00831  *
00832  * Returns a new float which is the result of dividing
00833  * <code>float</code> by <code>other</code>.
00834  */
00835 
00836 static VALUE
00837 flo_div(VALUE x, VALUE y)
00838 {
00839     long f_y;
00840     double d;
00841 
00842     switch (TYPE(y)) {
00843       case T_FIXNUM:
00844         f_y = FIX2LONG(y);
00845         return DBL2NUM(RFLOAT_VALUE(x) / (double)f_y);
00846       case T_BIGNUM:
00847         d = rb_big2dbl(y);
00848         return DBL2NUM(RFLOAT_VALUE(x) / d);
00849       case T_FLOAT:
00850         return DBL2NUM(RFLOAT_VALUE(x) / RFLOAT_VALUE(y));
00851       default:
00852         return rb_num_coerce_bin(x, y, '/');
00853     }
00854 }
00855 
00856 /*
00857  *  call-seq:
00858  *     float.quo(numeric)  ->  float
00859  *
00860  *  Returns float / numeric.
00861  */
00862 
00863 static VALUE
00864 flo_quo(VALUE x, VALUE y)
00865 {
00866     return rb_funcall(x, '/', 1, y);
00867 }
00868 
00869 static void
00870 flodivmod(double x, double y, double *divp, double *modp)
00871 {
00872     double div, mod;
00873 
00874     if (y == 0.0) rb_num_zerodiv();
00875     if ((x == 0.0) || (isinf(y) && !isinf(x)))
00876         mod = x;
00877     else {
00878 #ifdef HAVE_FMOD
00879         mod = fmod(x, y);
00880 #else
00881         double z;
00882 
00883         modf(x/y, &z);
00884         mod = x - z * y;
00885 #endif
00886     }
00887     if (isinf(x) && !isinf(y) && !isnan(y))
00888         div = x;
00889     else
00890         div = (x - mod) / y;
00891     if (y*mod < 0) {
00892         mod += y;
00893         div -= 1.0;
00894     }
00895     if (modp) *modp = mod;
00896     if (divp) *divp = div;
00897 }
00898 
00899 /*
00900  * Returns the modulo of division of x by y.
00901  * An error will be raised if y == 0.
00902  */
00903 
00904 double
00905 ruby_float_mod(double x, double y)
00906 {
00907     double mod;
00908     flodivmod(x, y, 0, &mod);
00909     return mod;
00910 }
00911 
00912 
00913 /*
00914  *  call-seq:
00915  *     float % other        ->  float
00916  *     float.modulo(other)  ->  float
00917  *
00918  *  Return the modulo after division of +float+ by +other+.
00919  *
00920  *     6543.21.modulo(137)      #=> 104.21
00921  *     6543.21.modulo(137.24)   #=> 92.9299999999996
00922  */
00923 
00924 static VALUE
00925 flo_mod(VALUE x, VALUE y)
00926 {
00927     double fy;
00928 
00929     switch (TYPE(y)) {
00930       case T_FIXNUM:
00931         fy = (double)FIX2LONG(y);
00932         break;
00933       case T_BIGNUM:
00934         fy = rb_big2dbl(y);
00935         break;
00936       case T_FLOAT:
00937         fy = RFLOAT_VALUE(y);
00938         break;
00939       default:
00940         return rb_num_coerce_bin(x, y, '%');
00941     }
00942     return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
00943 }
00944 
00945 static VALUE
00946 dbl2ival(double d)
00947 {
00948     d = round(d);
00949     if (FIXABLE(d)) {
00950         return LONG2FIX((long)d);
00951     }
00952     return rb_dbl2big(d);
00953 }
00954 
00955 /*
00956  *  call-seq:
00957  *     float.divmod(numeric)  ->  array
00958  *
00959  *  See Numeric#divmod.
00960  *
00961  *      42.0.divmod 6 #=> [7, 0.0]
00962  *      42.0.divmod 5 #=> [8, 2.0]
00963  */
00964 
00965 static VALUE
00966 flo_divmod(VALUE x, VALUE y)
00967 {
00968     double fy, div, mod;
00969     volatile VALUE a, b;
00970 
00971     switch (TYPE(y)) {
00972       case T_FIXNUM:
00973         fy = (double)FIX2LONG(y);
00974         break;
00975       case T_BIGNUM:
00976         fy = rb_big2dbl(y);
00977         break;
00978       case T_FLOAT:
00979         fy = RFLOAT_VALUE(y);
00980         break;
00981       default:
00982         return rb_num_coerce_bin(x, y, rb_intern("divmod"));
00983     }
00984     flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
00985     a = dbl2ival(div);
00986     b = DBL2NUM(mod);
00987     return rb_assoc_new(a, b);
00988 }
00989 
00990 /*
00991  * call-seq:
00992  *
00993  *  flt ** other  ->  float
00994  *
00995  * Raises <code>float</code> the <code>other</code> power.
00996  *
00997  *    2.0**3      #=> 8.0
00998  */
00999 
01000 static VALUE
01001 flo_pow(VALUE x, VALUE y)
01002 {
01003     switch (TYPE(y)) {
01004       case T_FIXNUM:
01005         return DBL2NUM(pow(RFLOAT_VALUE(x), (double)FIX2LONG(y)));
01006       case T_BIGNUM:
01007         return DBL2NUM(pow(RFLOAT_VALUE(x), rb_big2dbl(y)));
01008       case T_FLOAT:
01009         {
01010             double dx = RFLOAT_VALUE(x);
01011             double dy = RFLOAT_VALUE(y);
01012             if (dx < 0 && dy != round(dy))
01013                 return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
01014             return DBL2NUM(pow(dx, dy));
01015         }
01016       default:
01017         return rb_num_coerce_bin(x, y, rb_intern("**"));
01018     }
01019 }
01020 
01021 /*
01022  *  call-seq:
01023  *     num.eql?(numeric)  ->  true or false
01024  *
01025  *  Returns <code>true</code> if <i>num</i> and <i>numeric</i> are the
01026  *  same type and have equal values.
01027  *
01028  *     1 == 1.0          #=> true
01029  *     1.eql?(1.0)       #=> false
01030  *     (1.0).eql?(1.0)   #=> true
01031  */
01032 
01033 static VALUE
01034 num_eql(VALUE x, VALUE y)
01035 {
01036     if (TYPE(x) != TYPE(y)) return Qfalse;
01037 
01038     return rb_equal(x, y);
01039 }
01040 
01041 /*
01042  *  call-seq:
01043  *     number <=> other  ->  0 or nil
01044  *
01045  *  Returns zero if +number+ equals +other+, otherwise +nil+ is returned if the
01046  *  two values are incomparable.
01047  */
01048 
01049 static VALUE
01050 num_cmp(VALUE x, VALUE y)
01051 {
01052     if (x == y) return INT2FIX(0);
01053     return Qnil;
01054 }
01055 
01056 static VALUE
01057 num_equal(VALUE x, VALUE y)
01058 {
01059     if (x == y) return Qtrue;
01060     return rb_funcall(y, id_eq, 1, x);
01061 }
01062 
01063 /*
01064  *  call-seq:
01065  *     flt == obj  ->  true or false
01066  *
01067  *  Returns <code>true</code> only if <i>obj</i> has the same value
01068  *  as <i>flt</i>. Contrast this with <code>Float#eql?</code>, which
01069  *  requires <i>obj</i> to be a <code>Float</code>.
01070  *  The result of <code>NaN == NaN</code> is undefined, so the
01071  *  implementation-dependent value is returned.
01072  *
01073  *     1.0 == 1   #=> true
01074  *
01075  */
01076 
01077 static VALUE
01078 flo_eq(VALUE x, VALUE y)
01079 {
01080     volatile double a, b;
01081 
01082     switch (TYPE(y)) {
01083       case T_FIXNUM:
01084       case T_BIGNUM:
01085         return rb_integer_float_eq(y, x);
01086       case T_FLOAT:
01087         b = RFLOAT_VALUE(y);
01088 #if defined(_MSC_VER) && _MSC_VER < 1300
01089         if (isnan(b)) return Qfalse;
01090 #endif
01091         break;
01092       default:
01093         return num_equal(x, y);
01094     }
01095     a = RFLOAT_VALUE(x);
01096 #if defined(_MSC_VER) && _MSC_VER < 1300
01097     if (isnan(a)) return Qfalse;
01098 #endif
01099     return (a == b)?Qtrue:Qfalse;
01100 }
01101 
01102 /*
01103  * call-seq:
01104  *   flt.hash  ->  integer
01105  *
01106  * Returns a hash code for this float.
01107  */
01108 
01109 static VALUE
01110 flo_hash(VALUE num)
01111 {
01112     double d;
01113     st_index_t hash;
01114 
01115     d = RFLOAT_VALUE(num);
01116     /* normalize -0.0 to 0.0 */
01117     if (d == 0.0) d = 0.0;
01118     hash = rb_memhash(&d, sizeof(d));
01119     return LONG2FIX(hash);
01120 }
01121 
01122 VALUE
01123 rb_dbl_cmp(double a, double b)
01124 {
01125     if (isnan(a) || isnan(b)) return Qnil;
01126     if (a == b) return INT2FIX(0);
01127     if (a > b) return INT2FIX(1);
01128     if (a < b) return INT2FIX(-1);
01129     return Qnil;
01130 }
01131 
01132 /*
01133  *  call-seq:
01134  *     float <=> real  ->  -1, 0, +1 or nil
01135  *
01136  *  Returns -1, 0, +1 or nil depending on whether +float+ is less than, equal
01137  *  to, or greater than +real+. This is the basis for the tests in Comparable.
01138  *
01139  *  The result of <code>NaN <=> NaN</code> is undefined, so the
01140  *  implementation-dependent value is returned.
01141  *
01142  *  +nil+ is returned if the two values are incomparable.
01143  */
01144 
01145 static VALUE
01146 flo_cmp(VALUE x, VALUE y)
01147 {
01148     double a, b;
01149     VALUE i;
01150 
01151     a = RFLOAT_VALUE(x);
01152     if (isnan(a)) return Qnil;
01153     switch (TYPE(y)) {
01154       case T_FIXNUM:
01155       case T_BIGNUM:
01156       {
01157         VALUE rel = rb_integer_float_cmp(y, x);
01158         if (FIXNUM_P(rel))
01159             return INT2FIX(-FIX2INT(rel));
01160         return rel;
01161       }
01162 
01163       case T_FLOAT:
01164         b = RFLOAT_VALUE(y);
01165         break;
01166 
01167       default:
01168         if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) {
01169             if (RTEST(i)) {
01170                 int j = rb_cmpint(i, x, y);
01171                 j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
01172                 return INT2FIX(j);
01173             }
01174             if (a > 0.0) return INT2FIX(1);
01175             return INT2FIX(-1);
01176         }
01177         return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
01178     }
01179     return rb_dbl_cmp(a, b);
01180 }
01181 
01182 /*
01183  * call-seq:
01184  *   flt > real  ->  true or false
01185  *
01186  * <code>true</code> if <code>flt</code> is greater than <code>real</code>.
01187  * The result of <code>NaN > NaN</code> is undefined, so the
01188  * implementation-dependent value is returned.
01189  */
01190 
01191 static VALUE
01192 flo_gt(VALUE x, VALUE y)
01193 {
01194     double a, b;
01195 
01196     a = RFLOAT_VALUE(x);
01197     switch (TYPE(y)) {
01198       case T_FIXNUM:
01199       case T_BIGNUM:
01200       {
01201         VALUE rel = rb_integer_float_cmp(y, x);
01202         if (FIXNUM_P(rel))
01203             return -FIX2INT(rel) > 0 ? Qtrue : Qfalse;
01204         return Qfalse;
01205       }
01206 
01207       case T_FLOAT:
01208         b = RFLOAT_VALUE(y);
01209 #if defined(_MSC_VER) && _MSC_VER < 1300
01210         if (isnan(b)) return Qfalse;
01211 #endif
01212         break;
01213 
01214       default:
01215         return rb_num_coerce_relop(x, y, '>');
01216     }
01217 #if defined(_MSC_VER) && _MSC_VER < 1300
01218     if (isnan(a)) return Qfalse;
01219 #endif
01220     return (a > b)?Qtrue:Qfalse;
01221 }
01222 
01223 /*
01224  * call-seq:
01225  *   flt >= real  ->  true or false
01226  *
01227  * <code>true</code> if <code>flt</code> is greater than
01228  * or equal to <code>real</code>.
01229  * The result of <code>NaN >= NaN</code> is undefined, so the
01230  * implementation-dependent value is returned.
01231  */
01232 
01233 static VALUE
01234 flo_ge(VALUE x, VALUE y)
01235 {
01236     double a, b;
01237 
01238     a = RFLOAT_VALUE(x);
01239     switch (TYPE(y)) {
01240       case T_FIXNUM:
01241       case T_BIGNUM:
01242       {
01243         VALUE rel = rb_integer_float_cmp(y, x);
01244         if (FIXNUM_P(rel))
01245             return -FIX2INT(rel) >= 0 ? Qtrue : Qfalse;
01246         return Qfalse;
01247       }
01248 
01249       case T_FLOAT:
01250         b = RFLOAT_VALUE(y);
01251 #if defined(_MSC_VER) && _MSC_VER < 1300
01252         if (isnan(b)) return Qfalse;
01253 #endif
01254         break;
01255 
01256       default:
01257         return rb_num_coerce_relop(x, y, rb_intern(">="));
01258     }
01259 #if defined(_MSC_VER) && _MSC_VER < 1300
01260     if (isnan(a)) return Qfalse;
01261 #endif
01262     return (a >= b)?Qtrue:Qfalse;
01263 }
01264 
01265 /*
01266  * call-seq:
01267  *   flt < real  ->  true or false
01268  *
01269  * <code>true</code> if <code>flt</code> is less than <code>real</code>.
01270  * The result of <code>NaN < NaN</code> is undefined, so the
01271  * implementation-dependent value is returned.
01272  */
01273 
01274 static VALUE
01275 flo_lt(VALUE x, VALUE y)
01276 {
01277     double a, b;
01278 
01279     a = RFLOAT_VALUE(x);
01280     switch (TYPE(y)) {
01281       case T_FIXNUM:
01282       case T_BIGNUM:
01283       {
01284         VALUE rel = rb_integer_float_cmp(y, x);
01285         if (FIXNUM_P(rel))
01286             return -FIX2INT(rel) < 0 ? Qtrue : Qfalse;
01287         return Qfalse;
01288       }
01289 
01290       case T_FLOAT:
01291         b = RFLOAT_VALUE(y);
01292 #if defined(_MSC_VER) && _MSC_VER < 1300
01293         if (isnan(b)) return Qfalse;
01294 #endif
01295         break;
01296 
01297       default:
01298         return rb_num_coerce_relop(x, y, '<');
01299     }
01300 #if defined(_MSC_VER) && _MSC_VER < 1300
01301     if (isnan(a)) return Qfalse;
01302 #endif
01303     return (a < b)?Qtrue:Qfalse;
01304 }
01305 
01306 /*
01307  * call-seq:
01308  *   flt <= real  ->  true or false
01309  *
01310  * <code>true</code> if <code>flt</code> is less than
01311  * or equal to <code>real</code>.
01312  * The result of <code>NaN <= NaN</code> is undefined, so the
01313  * implementation-dependent value is returned.
01314  */
01315 
01316 static VALUE
01317 flo_le(VALUE x, VALUE y)
01318 {
01319     double a, b;
01320 
01321     a = RFLOAT_VALUE(x);
01322     switch (TYPE(y)) {
01323       case T_FIXNUM:
01324       case T_BIGNUM:
01325       {
01326         VALUE rel = rb_integer_float_cmp(y, x);
01327         if (FIXNUM_P(rel))
01328             return -FIX2INT(rel) <= 0 ? Qtrue : Qfalse;
01329         return Qfalse;
01330       }
01331 
01332       case T_FLOAT:
01333         b = RFLOAT_VALUE(y);
01334 #if defined(_MSC_VER) && _MSC_VER < 1300
01335         if (isnan(b)) return Qfalse;
01336 #endif
01337         break;
01338 
01339       default:
01340         return rb_num_coerce_relop(x, y, rb_intern("<="));
01341     }
01342 #if defined(_MSC_VER) && _MSC_VER < 1300
01343     if (isnan(a)) return Qfalse;
01344 #endif
01345     return (a <= b)?Qtrue:Qfalse;
01346 }
01347 
01348 /*
01349  *  call-seq:
01350  *     flt.eql?(obj)  ->  true or false
01351  *
01352  *  Returns <code>true</code> only if <i>obj</i> is a
01353  *  <code>Float</code> with the same value as <i>flt</i>. Contrast this
01354  *  with <code>Float#==</code>, which performs type conversions.
01355  *  The result of <code>NaN.eql?(NaN)</code> is undefined, so the
01356  *  implementation-dependent value is returned.
01357  *
01358  *     1.0.eql?(1)   #=> false
01359  */
01360 
01361 static VALUE
01362 flo_eql(VALUE x, VALUE y)
01363 {
01364     if (RB_TYPE_P(y, T_FLOAT)) {
01365         double a = RFLOAT_VALUE(x);
01366         double b = RFLOAT_VALUE(y);
01367 #if defined(_MSC_VER) && _MSC_VER < 1300
01368         if (isnan(a) || isnan(b)) return Qfalse;
01369 #endif
01370         if (a == b)
01371             return Qtrue;
01372     }
01373     return Qfalse;
01374 }
01375 
01376 /*
01377  * call-seq:
01378  *   flt.to_f  ->  self
01379  *
01380  * As <code>flt</code> is already a float, returns +self+.
01381  */
01382 
01383 static VALUE
01384 flo_to_f(VALUE num)
01385 {
01386     return num;
01387 }
01388 
01389 /*
01390  *  call-seq:
01391  *     flt.abs        ->  float
01392  *     flt.magnitude  ->  float
01393  *
01394  *  Returns the absolute value of <i>flt</i>.
01395  *
01396  *     (-34.56).abs   #=> 34.56
01397  *     -34.56.abs     #=> 34.56
01398  *
01399  */
01400 
01401 static VALUE
01402 flo_abs(VALUE flt)
01403 {
01404     double val = fabs(RFLOAT_VALUE(flt));
01405     return DBL2NUM(val);
01406 }
01407 
01408 /*
01409  *  call-seq:
01410  *     flt.zero?  ->  true or false
01411  *
01412  *  Returns <code>true</code> if <i>flt</i> is 0.0.
01413  *
01414  */
01415 
01416 static VALUE
01417 flo_zero_p(VALUE num)
01418 {
01419     if (RFLOAT_VALUE(num) == 0.0) {
01420         return Qtrue;
01421     }
01422     return Qfalse;
01423 }
01424 
01425 /*
01426  *  call-seq:
01427  *     flt.nan?  ->  true or false
01428  *
01429  *  Returns <code>true</code> if <i>flt</i> is an invalid IEEE floating
01430  *  point number.
01431  *
01432  *     a = -1.0      #=> -1.0
01433  *     a.nan?        #=> false
01434  *     a = 0.0/0.0   #=> NaN
01435  *     a.nan?        #=> true
01436  */
01437 
01438 static VALUE
01439 flo_is_nan_p(VALUE num)
01440 {
01441     double value = RFLOAT_VALUE(num);
01442 
01443     return isnan(value) ? Qtrue : Qfalse;
01444 }
01445 
01446 /*
01447  *  call-seq:
01448  *     flt.infinite?  ->  nil, -1, +1
01449  *
01450  *  Returns <code>nil</code>, -1, or +1 depending on whether <i>flt</i>
01451  *  is finite, -infinity, or +infinity.
01452  *
01453  *     (0.0).infinite?        #=> nil
01454  *     (-1.0/0.0).infinite?   #=> -1
01455  *     (+1.0/0.0).infinite?   #=> 1
01456  */
01457 
01458 static VALUE
01459 flo_is_infinite_p(VALUE num)
01460 {
01461     double value = RFLOAT_VALUE(num);
01462 
01463     if (isinf(value)) {
01464         return INT2FIX( value < 0 ? -1 : 1 );
01465     }
01466 
01467     return Qnil;
01468 }
01469 
01470 /*
01471  *  call-seq:
01472  *     flt.finite?  ->  true or false
01473  *
01474  *  Returns <code>true</code> if <i>flt</i> is a valid IEEE floating
01475  *  point number (it is not infinite, and <code>nan?</code> is
01476  *  <code>false</code>).
01477  *
01478  */
01479 
01480 static VALUE
01481 flo_is_finite_p(VALUE num)
01482 {
01483     double value = RFLOAT_VALUE(num);
01484 
01485 #if HAVE_FINITE
01486     if (!finite(value))
01487         return Qfalse;
01488 #else
01489     if (isinf(value) || isnan(value))
01490         return Qfalse;
01491 #endif
01492 
01493     return Qtrue;
01494 }
01495 
01496 /*
01497  *  call-seq:
01498  *     flt.floor  ->  integer
01499  *
01500  *  Returns the largest integer less than or equal to <i>flt</i>.
01501  *
01502  *     1.2.floor      #=> 1
01503  *     2.0.floor      #=> 2
01504  *     (-1.2).floor   #=> -2
01505  *     (-2.0).floor   #=> -2
01506  */
01507 
01508 static VALUE
01509 flo_floor(VALUE num)
01510 {
01511     double f = floor(RFLOAT_VALUE(num));
01512     long val;
01513 
01514     if (!FIXABLE(f)) {
01515         return rb_dbl2big(f);
01516     }
01517     val = (long)f;
01518     return LONG2FIX(val);
01519 }
01520 
01521 /*
01522  *  call-seq:
01523  *     flt.ceil  ->  integer
01524  *
01525  *  Returns the smallest <code>Integer</code> greater than or equal to
01526  *  <i>flt</i>.
01527  *
01528  *     1.2.ceil      #=> 2
01529  *     2.0.ceil      #=> 2
01530  *     (-1.2).ceil   #=> -1
01531  *     (-2.0).ceil   #=> -2
01532  */
01533 
01534 static VALUE
01535 flo_ceil(VALUE num)
01536 {
01537     double f = ceil(RFLOAT_VALUE(num));
01538     long val;
01539 
01540     if (!FIXABLE(f)) {
01541         return rb_dbl2big(f);
01542     }
01543     val = (long)f;
01544     return LONG2FIX(val);
01545 }
01546 
01547 /*
01548  * Assumes num is an Integer, ndigits <= 0
01549  */
01550 static VALUE
01551 int_round_0(VALUE num, int ndigits)
01552 {
01553     VALUE n, f, h, r;
01554     long bytes;
01555     ID op;
01556     /* If 10**N / 2 > num, then return 0 */
01557     /* We have log_256(10) > 0.415241 and log_256(1/2) = -0.125, so */
01558     bytes = FIXNUM_P(num) ? sizeof(long) : rb_funcall(num, idSize, 0);
01559     if (-0.415241 * ndigits - 0.125 > bytes ) {
01560         return INT2FIX(0);
01561     }
01562 
01563     f = int_pow(10, -ndigits);
01564     if (FIXNUM_P(num) && FIXNUM_P(f)) {
01565         SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
01566         int neg = x < 0;
01567         if (neg) x = -x;
01568         x = (x + y / 2) / y * y;
01569         if (neg) x = -x;
01570         return LONG2NUM(x);
01571     }
01572     if (RB_TYPE_P(f, T_FLOAT)) {
01573         /* then int_pow overflow */
01574         return INT2FIX(0);
01575     }
01576     h = rb_funcall(f, '/', 1, INT2FIX(2));
01577     r = rb_funcall(num, '%', 1, f);
01578     n = rb_funcall(num, '-', 1, r);
01579     op = negative_int_p(num) ? rb_intern("<=") : '<';
01580     if (!RTEST(rb_funcall(r, op, 1, h))) {
01581         n = rb_funcall(n, '+', 1, f);
01582     }
01583     return n;
01584 }
01585 
01586 static VALUE
01587 flo_truncate(VALUE num);
01588 
01589 /*
01590  *  call-seq:
01591  *     flt.round([ndigits])  ->  integer or float
01592  *
01593  *  Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits).
01594  *  Precision may be negative.  Returns a floating point number when ndigits
01595  *  is more than zero.
01596  *
01597  *     1.4.round      #=> 1
01598  *     1.5.round      #=> 2
01599  *     1.6.round      #=> 2
01600  *     (-1.5).round   #=> -2
01601  *
01602  *     1.234567.round(2)  #=> 1.23
01603  *     1.234567.round(3)  #=> 1.235
01604  *     1.234567.round(4)  #=> 1.2346
01605  *     1.234567.round(5)  #=> 1.23457
01606  *
01607  *     34567.89.round(-5) #=> 0
01608  *     34567.89.round(-4) #=> 30000
01609  *     34567.89.round(-3) #=> 35000
01610  *     34567.89.round(-2) #=> 34600
01611  *     34567.89.round(-1) #=> 34570
01612  *     34567.89.round(0)  #=> 34568
01613  *     34567.89.round(1)  #=> 34567.9
01614  *     34567.89.round(2)  #=> 34567.89
01615  *     34567.89.round(3)  #=> 34567.89
01616  *
01617  */
01618 
01619 static VALUE
01620 flo_round(int argc, VALUE *argv, VALUE num)
01621 {
01622     VALUE nd;
01623     double number, f;
01624     int ndigits = 0;
01625     int binexp;
01626     enum {float_dig = DBL_DIG+2};
01627 
01628     if (argc > 0 && rb_scan_args(argc, argv, "01", &nd) == 1) {
01629         ndigits = NUM2INT(nd);
01630     }
01631     if (ndigits < 0) {
01632         return int_round_0(flo_truncate(num), ndigits);
01633     }
01634     number  = RFLOAT_VALUE(num);
01635     if (ndigits == 0) {
01636         return dbl2ival(number);
01637     }
01638     frexp(number, &binexp);
01639 
01640 /* Let `exp` be such that `number` is written as:"0.#{digits}e#{exp}",
01641    i.e. such that  10 ** (exp - 1) <= |number| < 10 ** exp
01642    Recall that up to float_dig digits can be needed to represent a double,
01643    so if ndigits + exp >= float_dig, the intermediate value (number * 10 ** ndigits)
01644    will be an integer and thus the result is the original number.
01645    If ndigits + exp <= 0, the result is 0 or "1e#{exp}", so
01646    if ndigits + exp < 0, the result is 0.
01647    We have:
01648         2 ** (binexp-1) <= |number| < 2 ** binexp
01649         10 ** ((binexp-1)/log_2(10)) <= |number| < 10 ** (binexp/log_2(10))
01650         If binexp >= 0, and since log_2(10) = 3.322259:
01651            10 ** (binexp/4 - 1) < |number| < 10 ** (binexp/3)
01652            floor(binexp/4) <= exp <= ceil(binexp/3)
01653         If binexp <= 0, swap the /4 and the /3
01654         So if ndigits + floor(binexp/(4 or 3)) >= float_dig, the result is number
01655         If ndigits + ceil(binexp/(3 or 4)) < 0 the result is 0
01656 */
01657     if (isinf(number) || isnan(number) ||
01658         (ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1))) {
01659         return num;
01660     }
01661     if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
01662         return DBL2NUM(0);
01663     }
01664     f = pow(10, ndigits);
01665     return DBL2NUM(round(number * f) / f);
01666 }
01667 
01668 /*
01669  *  call-seq:
01670  *     flt.to_i      ->  integer
01671  *     flt.to_int    ->  integer
01672  *     flt.truncate  ->  integer
01673  *
01674  *  Returns <i>flt</i> truncated to an <code>Integer</code>.
01675  */
01676 
01677 static VALUE
01678 flo_truncate(VALUE num)
01679 {
01680     double f = RFLOAT_VALUE(num);
01681     long val;
01682 
01683     if (f > 0.0) f = floor(f);
01684     if (f < 0.0) f = ceil(f);
01685 
01686     if (!FIXABLE(f)) {
01687         return rb_dbl2big(f);
01688     }
01689     val = (long)f;
01690     return LONG2FIX(val);
01691 }
01692 
01693 /*
01694  *  call-seq:
01695  *     num.floor  ->  integer
01696  *
01697  *  Returns the largest integer less than or equal to <i>num</i>.
01698  *  <code>Numeric</code> implements this by converting <i>anInteger</i>
01699  *  to a <code>Float</code> and invoking <code>Float#floor</code>.
01700  *
01701  *     1.floor      #=> 1
01702  *     (-1).floor   #=> -1
01703  */
01704 
01705 static VALUE
01706 num_floor(VALUE num)
01707 {
01708     return flo_floor(rb_Float(num));
01709 }
01710 
01711 
01712 /*
01713  *  call-seq:
01714  *     num.ceil  ->  integer
01715  *
01716  *  Returns the smallest <code>Integer</code> greater than or equal to
01717  *  <i>num</i>. Class <code>Numeric</code> achieves this by converting
01718  *  itself to a <code>Float</code> then invoking
01719  *  <code>Float#ceil</code>.
01720  *
01721  *     1.ceil        #=> 1
01722  *     1.2.ceil      #=> 2
01723  *     (-1.2).ceil   #=> -1
01724  *     (-1.0).ceil   #=> -1
01725  */
01726 
01727 static VALUE
01728 num_ceil(VALUE num)
01729 {
01730     return flo_ceil(rb_Float(num));
01731 }
01732 
01733 /*
01734  *  call-seq:
01735  *     num.round([ndigits])  ->  integer or float
01736  *
01737  *  Rounds <i>num</i> to a given precision in decimal digits (default 0 digits).
01738  *  Precision may be negative.  Returns a floating point number when <i>ndigits</i>
01739  *  is more than zero.  <code>Numeric</code> implements this by converting itself
01740  *  to a <code>Float</code> and invoking <code>Float#round</code>.
01741  */
01742 
01743 static VALUE
01744 num_round(int argc, VALUE* argv, VALUE num)
01745 {
01746     return flo_round(argc, argv, rb_Float(num));
01747 }
01748 
01749 /*
01750  *  call-seq:
01751  *     num.truncate  ->  integer
01752  *
01753  *  Returns <i>num</i> truncated to an integer. <code>Numeric</code>
01754  *  implements this by converting its value to a float and invoking
01755  *  <code>Float#truncate</code>.
01756  */
01757 
01758 static VALUE
01759 num_truncate(VALUE num)
01760 {
01761     return flo_truncate(rb_Float(num));
01762 }
01763 
01764 static double
01765 ruby_float_step_size(double beg, double end, double unit, int excl)
01766 {
01767     const double epsilon = DBL_EPSILON;
01768     double n = (end - beg)/unit;
01769     double err = (fabs(beg) + fabs(end) + fabs(end-beg)) / fabs(unit) * epsilon;
01770 
01771     if (isinf(unit)) {
01772         return unit > 0 ? beg <= end : beg >= end;
01773     }
01774     if (err>0.5) err=0.5;
01775     if (excl) {
01776         if (n<=0) return 0;
01777         if (n<1)
01778             n = 0;
01779         else
01780             n = floor(n - err);
01781     }
01782     else {
01783         if (n<0) return 0;
01784         n = floor(n + err);
01785     }
01786     return n+1;
01787 }
01788 
01789 int
01790 ruby_float_step(VALUE from, VALUE to, VALUE step, int excl)
01791 {
01792     if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
01793         double beg = NUM2DBL(from);
01794         double end = NUM2DBL(to);
01795         double unit = NUM2DBL(step);
01796         double n = ruby_float_step_size(beg, end, unit, excl);
01797         long i;
01798 
01799         if (isinf(unit)) {
01800             /* if unit is infinity, i*unit+beg is NaN */
01801             if (n) rb_yield(DBL2NUM(beg));
01802         }
01803         else {
01804             for (i=0; i<n; i++) {
01805                 double d = i*unit+beg;
01806                 if (unit >= 0 ? end < d : d < end) d = end;
01807                 rb_yield(DBL2NUM(d));
01808             }
01809         }
01810         return TRUE;
01811     }
01812     return FALSE;
01813 }
01814 
01815 VALUE
01816 num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
01817 {
01818     if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
01819         long delta, diff, result;
01820 
01821         diff = FIX2LONG(step);
01822         delta = FIX2LONG(to) - FIX2LONG(from);
01823         if (excl) {
01824             delta += (diff > 0 ? -1 : +1);
01825         }
01826         result = delta / diff;
01827         return LONG2FIX(result >= 0 ? result + 1 : 0);
01828     }
01829     else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
01830         double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
01831 
01832         if (isinf(n)) return DBL2NUM(n);
01833         return LONG2FIX(n);
01834     }
01835     else {
01836         VALUE result;
01837         ID cmp = RTEST(rb_funcall(step, '>', 1, INT2FIX(0))) ? '>' : '<';
01838         if (RTEST(rb_funcall(from, cmp, 1, to))) return INT2FIX(0);
01839         result = rb_funcall(rb_funcall(to, '-', 1, from), id_div, 1, step);
01840         if (!excl || RTEST(rb_funcall(rb_funcall(from, '+', 1, rb_funcall(result, '*', 1, step)), cmp, 1, to))) {
01841             result = rb_funcall(result, '+', 1, INT2FIX(1));
01842         }
01843         return result;
01844     }
01845 }
01846 
01847 static VALUE
01848 num_step_size(VALUE from, VALUE args)
01849 {
01850     VALUE to = RARRAY_PTR(args)[0];
01851     VALUE step = (RARRAY_LEN(args) > 1) ? RARRAY_PTR(args)[1] : INT2FIX(1);
01852     return num_interval_step_size(from, to, step, FALSE);
01853 }
01854 /*
01855  *  call-seq:
01856  *     num.step(limit[, step]) {|i| block }  ->  self
01857  *     num.step(limit[, step])               ->  an_enumerator
01858  *
01859  *  Invokes <em>block</em> with the sequence of numbers starting at
01860  *  <i>num</i>, incremented by <i>step</i> (default 1) on each
01861  *  call. The loop finishes when the value to be passed to the block
01862  *  is greater than <i>limit</i> (if <i>step</i> is positive) or less
01863  *  than <i>limit</i> (if <i>step</i> is negative). If all the
01864  *  arguments are integers, the loop operates using an integer
01865  *  counter. If any of the arguments are floating point numbers, all
01866  *  are converted to floats, and the loop is executed <i>floor(n +
01867  *  n*epsilon)+ 1</i> times, where <i>n = (limit -
01868  *  num)/step</i>. Otherwise, the loop starts at <i>num</i>, uses
01869  *  either the <code><</code> or <code>></code> operator to compare
01870  *  the counter against <i>limit</i>, and increments itself using the
01871  *  <code>+</code> operator.
01872  *
01873  *  If no block is given, an enumerator is returned instead.
01874  *
01875  *     1.step(10, 2) { |i| print i, " " }
01876  *     Math::E.step(Math::PI, 0.2) { |f| print f, " " }
01877  *
01878  *  <em>produces:</em>
01879  *
01880  *     1 3 5 7 9
01881  *     2.71828182845905 2.91828182845905 3.11828182845905
01882  */
01883 
01884 static VALUE
01885 num_step(int argc, VALUE *argv, VALUE from)
01886 {
01887     VALUE to, step;
01888 
01889     RETURN_SIZED_ENUMERATOR(from, argc, argv, num_step_size);
01890     if (argc == 1) {
01891         to = argv[0];
01892         step = INT2FIX(1);
01893     }
01894     else {
01895         rb_check_arity(argc, 1, 2);
01896         to = argv[0];
01897         step = argv[1];
01898         if (rb_equal(step, INT2FIX(0))) {
01899             rb_raise(rb_eArgError, "step can't be 0");
01900         }
01901     }
01902 
01903     if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
01904         long i, end, diff;
01905 
01906         i = FIX2LONG(from);
01907         end = FIX2LONG(to);
01908         diff = FIX2LONG(step);
01909 
01910         if (diff > 0) {
01911             while (i <= end) {
01912                 rb_yield(LONG2FIX(i));
01913                 i += diff;
01914             }
01915         }
01916         else {
01917             while (i >= end) {
01918                 rb_yield(LONG2FIX(i));
01919                 i += diff;
01920             }
01921         }
01922     }
01923     else if (!ruby_float_step(from, to, step, FALSE)) {
01924         VALUE i = from;
01925         ID cmp;
01926 
01927         if (positive_int_p(step)) {
01928             cmp = '>';
01929         }
01930         else {
01931             cmp = '<';
01932         }
01933         for (;;) {
01934             if (RTEST(rb_funcall(i, cmp, 1, to))) break;
01935             rb_yield(i);
01936             i = rb_funcall(i, '+', 1, step);
01937         }
01938     }
01939     return from;
01940 }
01941 
01942 #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1)
01943 #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1))
01944 #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1))
01945 
01946 SIGNED_VALUE
01947 rb_num2long(VALUE val)
01948 {
01949   again:
01950     if (NIL_P(val)) {
01951         rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
01952     }
01953 
01954     if (FIXNUM_P(val)) return FIX2LONG(val);
01955 
01956     switch (TYPE(val)) {
01957       case T_FLOAT:
01958         if (RFLOAT_VALUE(val) < LONG_MAX_PLUS_ONE
01959             && RFLOAT_VALUE(val) > LONG_MIN_MINUS_ONE) {
01960             return (SIGNED_VALUE)(RFLOAT_VALUE(val));
01961         }
01962         else {
01963             char buf[24];
01964             char *s;
01965 
01966             snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
01967             if ((s = strchr(buf, ' ')) != 0) *s = '\0';
01968             rb_raise(rb_eRangeError, "float %s out of range of integer", buf);
01969         }
01970 
01971       case T_BIGNUM:
01972         return rb_big2long(val);
01973 
01974       default:
01975         val = rb_to_int(val);
01976         goto again;
01977     }
01978 }
01979 
01980 VALUE
01981 rb_num2ulong(VALUE val)
01982 {
01983   again:
01984     if (NIL_P(val)) {
01985        rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
01986     }
01987 
01988     if (FIXNUM_P(val)) return FIX2LONG(val); /* this is FIX2LONG, inteneded */
01989 
01990     switch (TYPE(val)) {
01991       case T_FLOAT:
01992        if (RFLOAT_VALUE(val) < ULONG_MAX_PLUS_ONE
01993            && RFLOAT_VALUE(val) > LONG_MIN_MINUS_ONE) {
01994            return (VALUE)RFLOAT_VALUE(val);
01995        }
01996        else {
01997            char buf[24];
01998            char *s;
01999 
02000            snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
02001            if ((s = strchr(buf, ' ')) != 0) *s = '\0';
02002            rb_raise(rb_eRangeError, "float %s out of range of integer", buf);
02003        }
02004 
02005       case T_BIGNUM:
02006         return rb_big2ulong(val);
02007 
02008       default:
02009        val = rb_to_int(val);
02010        goto again;
02011     }
02012 }
02013 
02014 #if SIZEOF_INT < SIZEOF_VALUE
02015 void
02016 rb_out_of_int(SIGNED_VALUE num)
02017 {
02018     rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `int'",
02019              num, num < 0 ? "small" : "big");
02020 }
02021 
02022 static void
02023 check_int(SIGNED_VALUE num)
02024 {
02025     if ((SIGNED_VALUE)(int)num != num) {
02026         rb_out_of_int(num);
02027     }
02028 }
02029 
02030 static void
02031 check_uint(VALUE num, int sign)
02032 {
02033     static const VALUE mask = ~(VALUE)UINT_MAX;
02034 
02035     if (sign) {
02036         /* minus */
02037         if ((num & mask) != mask || (num & ~mask) <= INT_MAX)
02038 #define VALUE_MSBMASK   ((VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1))
02039             rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too small to convert to `unsigned int'", num|VALUE_MSBMASK);
02040     }
02041     else {
02042         /* plus */
02043         if ((num & mask) != 0)
02044             rb_raise(rb_eRangeError, "integer %"PRIuVALUE " too big to convert to `unsigned int'", num);
02045     }
02046 }
02047 
02048 long
02049 rb_num2int(VALUE val)
02050 {
02051     long num = rb_num2long(val);
02052 
02053     check_int(num);
02054     return num;
02055 }
02056 
02057 long
02058 rb_fix2int(VALUE val)
02059 {
02060     long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
02061 
02062     check_int(num);
02063     return num;
02064 }
02065 
02066 unsigned long
02067 rb_num2uint(VALUE val)
02068 {
02069     VALUE num = rb_num2ulong(val);
02070 
02071     check_uint(num, negative_int_p(val));
02072     return (unsigned long)num;
02073 }
02074 
02075 unsigned long
02076 rb_fix2uint(VALUE val)
02077 {
02078     unsigned long num;
02079 
02080     if (!FIXNUM_P(val)) {
02081         return rb_num2uint(val);
02082     }
02083     num = FIX2ULONG(val);
02084 
02085     check_uint(num, negative_int_p(val));
02086     return num;
02087 }
02088 #else
02089 long
02090 rb_num2int(VALUE val)
02091 {
02092     return rb_num2long(val);
02093 }
02094 
02095 long
02096 rb_fix2int(VALUE val)
02097 {
02098     return FIX2INT(val);
02099 }
02100 #endif
02101 
02102 void
02103 rb_out_of_short(SIGNED_VALUE num)
02104 {
02105     rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `short'",
02106              num, num < 0 ? "small" : "big");
02107 }
02108 
02109 static void
02110 check_short(SIGNED_VALUE num)
02111 {
02112     if ((SIGNED_VALUE)(short)num != num) {
02113         rb_out_of_short(num);
02114     }
02115 }
02116 
02117 static void
02118 check_ushort(VALUE num, int sign)
02119 {
02120     static const VALUE mask = ~(VALUE)USHRT_MAX;
02121 
02122     if (sign) {
02123         /* minus */
02124         if ((num & mask) != mask || (num & ~mask) <= SHRT_MAX)
02125 #define VALUE_MSBMASK   ((VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1))
02126             rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too small to convert to `unsigned short'", num|VALUE_MSBMASK);
02127     }
02128     else {
02129         /* plus */
02130         if ((num & mask) != 0)
02131             rb_raise(rb_eRangeError, "integer %"PRIuVALUE " too big to convert to `unsigned short'", num);
02132     }
02133 }
02134 
02135 short
02136 rb_num2short(VALUE val)
02137 {
02138     long num = rb_num2long(val);
02139 
02140     check_short(num);
02141     return num;
02142 }
02143 
02144 short
02145 rb_fix2short(VALUE val)
02146 {
02147     long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
02148 
02149     check_short(num);
02150     return num;
02151 }
02152 
02153 unsigned short
02154 rb_num2ushort(VALUE val)
02155 {
02156     VALUE num = rb_num2ulong(val);
02157 
02158     check_ushort(num, negative_int_p(val));
02159     return (unsigned long)num;
02160 }
02161 
02162 unsigned short
02163 rb_fix2ushort(VALUE val)
02164 {
02165     unsigned long num;
02166 
02167     if (!FIXNUM_P(val)) {
02168         return rb_num2ushort(val);
02169     }
02170     num = FIX2ULONG(val);
02171 
02172     check_ushort(num, negative_int_p(val));
02173     return num;
02174 }
02175 
02176 VALUE
02177 rb_num2fix(VALUE val)
02178 {
02179     SIGNED_VALUE v;
02180 
02181     if (FIXNUM_P(val)) return val;
02182 
02183     v = rb_num2long(val);
02184     if (!FIXABLE(v))
02185         rb_raise(rb_eRangeError, "integer %"PRIdVALUE " out of range of fixnum", v);
02186     return LONG2FIX(v);
02187 }
02188 
02189 #if HAVE_LONG_LONG
02190 
02191 #define LLONG_MIN_MINUS_ONE ((double)LLONG_MIN-1)
02192 #define LLONG_MAX_PLUS_ONE (2*(double)(LLONG_MAX/2+1))
02193 #define ULLONG_MAX_PLUS_ONE (2*(double)(ULLONG_MAX/2+1))
02194 #ifndef ULLONG_MAX
02195 #define ULLONG_MAX ((unsigned LONG_LONG)LLONG_MAX*2+1)
02196 #endif
02197 
02198 LONG_LONG
02199 rb_num2ll(VALUE val)
02200 {
02201     if (NIL_P(val)) {
02202         rb_raise(rb_eTypeError, "no implicit conversion from nil");
02203     }
02204 
02205     if (FIXNUM_P(val)) return (LONG_LONG)FIX2LONG(val);
02206 
02207     switch (TYPE(val)) {
02208       case T_FLOAT:
02209         if (RFLOAT_VALUE(val) < LLONG_MAX_PLUS_ONE
02210             && RFLOAT_VALUE(val) > LLONG_MIN_MINUS_ONE) {
02211             return (LONG_LONG)(RFLOAT_VALUE(val));
02212         }
02213         else {
02214             char buf[24];
02215             char *s;
02216 
02217             snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
02218             if ((s = strchr(buf, ' ')) != 0) *s = '\0';
02219             rb_raise(rb_eRangeError, "float %s out of range of long long", buf);
02220         }
02221 
02222       case T_BIGNUM:
02223         return rb_big2ll(val);
02224 
02225       case T_STRING:
02226         rb_raise(rb_eTypeError, "no implicit conversion from string");
02227         break;
02228 
02229       case T_TRUE:
02230       case T_FALSE:
02231         rb_raise(rb_eTypeError, "no implicit conversion from boolean");
02232         break;
02233 
02234       default:
02235         break;
02236     }
02237 
02238     val = rb_to_int(val);
02239     return NUM2LL(val);
02240 }
02241 
02242 unsigned LONG_LONG
02243 rb_num2ull(VALUE val)
02244 {
02245     switch (TYPE(val)) {
02246       case T_NIL:
02247         rb_raise(rb_eTypeError, "no implicit conversion from nil");
02248 
02249       case T_FIXNUM:
02250         return (LONG_LONG)FIX2LONG(val); /* this is FIX2LONG, inteneded */
02251 
02252       case T_FLOAT:
02253         if (RFLOAT_VALUE(val) < ULLONG_MAX_PLUS_ONE
02254             && RFLOAT_VALUE(val) > 0) {
02255             return (unsigned LONG_LONG)(RFLOAT_VALUE(val));
02256         }
02257         else {
02258             char buf[24];
02259             char *s;
02260 
02261             snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
02262             if ((s = strchr(buf, ' ')) != 0) *s = '\0';
02263             rb_raise(rb_eRangeError, "float %s out of range of unsgined long long", buf);
02264         }
02265 
02266       case T_BIGNUM:
02267         return rb_big2ull(val);
02268 
02269       case T_STRING:
02270         rb_raise(rb_eTypeError, "no implicit conversion from string");
02271         break;
02272 
02273       case T_TRUE:
02274       case T_FALSE:
02275         rb_raise(rb_eTypeError, "no implicit conversion from boolean");
02276         break;
02277 
02278       default:
02279         break;
02280     }
02281 
02282     val = rb_to_int(val);
02283     return NUM2ULL(val);
02284 }
02285 
02286 #endif  /* HAVE_LONG_LONG */
02287 
02288 /*
02289  * Document-class: Integer
02290  *
02291  *  <code>Integer</code> is the basis for the two concrete classes that
02292  *  hold whole numbers, <code>Bignum</code> and <code>Fixnum</code>.
02293  *
02294  */
02295 
02296 /*
02297  *  call-seq:
02298  *     int.to_i      ->  integer
02299  *     int.to_int    ->  integer
02300  *     int.floor     ->  integer
02301  *     int.ceil      ->  integer
02302  *     int.truncate  ->  integer
02303  *
02304  *  As <i>int</i> is already an <code>Integer</code>, all these
02305  *  methods simply return the receiver.
02306  */
02307 
02308 static VALUE
02309 int_to_i(VALUE num)
02310 {
02311     return num;
02312 }
02313 
02314 /*
02315  *  call-seq:
02316  *     int.integer?  ->  true
02317  *
02318  *  Always returns <code>true</code>.
02319  */
02320 
02321 static VALUE
02322 int_int_p(VALUE num)
02323 {
02324     return Qtrue;
02325 }
02326 
02327 /*
02328  *  call-seq:
02329  *     int.odd?  ->  true or false
02330  *
02331  *  Returns <code>true</code> if <i>int</i> is an odd number.
02332  */
02333 
02334 static VALUE
02335 int_odd_p(VALUE num)
02336 {
02337     if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) {
02338         return Qtrue;
02339     }
02340     return Qfalse;
02341 }
02342 
02343 /*
02344  *  call-seq:
02345  *     int.even?  ->  true or false
02346  *
02347  *  Returns <code>true</code> if <i>int</i> is an even number.
02348  */
02349 
02350 static VALUE
02351 int_even_p(VALUE num)
02352 {
02353     if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
02354         return Qtrue;
02355     }
02356     return Qfalse;
02357 }
02358 
02359 /*
02360  *  call-seq:
02361  *     fixnum.next  ->  integer
02362  *     fixnum.succ  ->  integer
02363  *
02364  *  Returns the <code>Integer</code> equal to <i>int</i> + 1.
02365  *
02366  *     1.next      #=> 2
02367  *     (-1).next   #=> 0
02368  */
02369 
02370 static VALUE
02371 fix_succ(VALUE num)
02372 {
02373     long i = FIX2LONG(num) + 1;
02374     return LONG2NUM(i);
02375 }
02376 
02377 /*
02378  *  call-seq:
02379  *     int.next  ->  integer
02380  *     int.succ  ->  integer
02381  *
02382  *  Returns the <code>Integer</code> equal to <i>int</i> + 1.
02383  *
02384  *     1.next      #=> 2
02385  *     (-1).next   #=> 0
02386  */
02387 
02388 VALUE
02389 rb_int_succ(VALUE num)
02390 {
02391     if (FIXNUM_P(num)) {
02392         long i = FIX2LONG(num) + 1;
02393         return LONG2NUM(i);
02394     }
02395     return rb_funcall(num, '+', 1, INT2FIX(1));
02396 }
02397 
02398 #define int_succ rb_int_succ
02399 
02400 /*
02401  *  call-seq:
02402  *     int.pred  ->  integer
02403  *
02404  *  Returns the <code>Integer</code> equal to <i>int</i> - 1.
02405  *
02406  *     1.pred      #=> 0
02407  *     (-1).pred   #=> -2
02408  */
02409 
02410 VALUE
02411 rb_int_pred(VALUE num)
02412 {
02413     if (FIXNUM_P(num)) {
02414         long i = FIX2LONG(num) - 1;
02415         return LONG2NUM(i);
02416     }
02417     return rb_funcall(num, '-', 1, INT2FIX(1));
02418 }
02419 
02420 #define int_pred rb_int_pred
02421 
02422 VALUE
02423 rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
02424 {
02425     int n;
02426     VALUE str;
02427     switch (n = rb_enc_codelen(code, enc)) {
02428       case ONIGERR_INVALID_CODE_POINT_VALUE:
02429         rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
02430         break;
02431       case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
02432       case 0:
02433         rb_raise(rb_eRangeError, "%u out of char range", code);
02434         break;
02435     }
02436     str = rb_enc_str_new(0, n, enc);
02437     rb_enc_mbcput(code, RSTRING_PTR(str), enc);
02438     if (rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_END(str), enc) != n) {
02439         rb_raise(rb_eRangeError, "invalid codepoint 0x%X in %s", code, rb_enc_name(enc));
02440     }
02441     return str;
02442 }
02443 
02444 /*
02445  *  call-seq:
02446  *     int.chr([encoding])  ->  string
02447  *
02448  *  Returns a string containing the character represented by the
02449  *  receiver's value according to +encoding+.
02450  *
02451  *     65.chr    #=> "A"
02452  *     230.chr   #=> "\346"
02453  *     255.chr(Encoding::UTF_8)   #=> "\303\277"
02454  */
02455 
02456 static VALUE
02457 int_chr(int argc, VALUE *argv, VALUE num)
02458 {
02459     char c;
02460     unsigned int i;
02461     rb_encoding *enc;
02462 
02463     if (rb_num_to_uint(num, &i) == 0) {
02464     }
02465     else if (FIXNUM_P(num)) {
02466         rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(num));
02467     }
02468     else {
02469         rb_raise(rb_eRangeError, "bignum out of char range");
02470     }
02471 
02472     switch (argc) {
02473       case 0:
02474         if (0xff < i) {
02475             enc = rb_default_internal_encoding();
02476             if (!enc) {
02477                 rb_raise(rb_eRangeError, "%d out of char range", i);
02478             }
02479             goto decode;
02480         }
02481         c = (char)i;
02482         if (i < 0x80) {
02483             return rb_usascii_str_new(&c, 1);
02484         }
02485         else {
02486             return rb_str_new(&c, 1);
02487         }
02488       case 1:
02489         break;
02490       default:
02491         rb_check_arity(argc, 0, 1);
02492         break;
02493     }
02494     enc = rb_to_encoding(argv[0]);
02495     if (!enc) enc = rb_ascii8bit_encoding();
02496   decode:
02497     return rb_enc_uint_chr(i, enc);
02498 }
02499 
02500 /*
02501  *  call-seq:
02502  *     int.ord  ->  self
02503  *
02504  *  Returns the int itself.
02505  *
02506  *     ?a.ord    #=> 97
02507  *
02508  *  This method is intended for compatibility to
02509  *  character constant in Ruby 1.9.
02510  *  For example, ?a.ord returns 97 both in 1.8 and 1.9.
02511  */
02512 
02513 static VALUE
02514 int_ord(VALUE num)
02515 {
02516     return num;
02517 }
02518 
02519 /********************************************************************
02520  *
02521  * Document-class: Fixnum
02522  *
02523  *  A <code>Fixnum</code> holds <code>Integer</code> values that can be
02524  *  represented in a native machine word (minus 1 bit). If any operation
02525  *  on a <code>Fixnum</code> exceeds this range, the value is
02526  *  automatically converted to a <code>Bignum</code>.
02527  *
02528  *  <code>Fixnum</code> objects have immediate value. This means that
02529  *  when they are assigned or passed as parameters, the actual object is
02530  *  passed, rather than a reference to that object. Assignment does not
02531  *  alias <code>Fixnum</code> objects. There is effectively only one
02532  *  <code>Fixnum</code> object instance for any given integer value, so,
02533  *  for example, you cannot add a singleton method to a
02534  *  <code>Fixnum</code>.
02535  */
02536 
02537 
02538 /*
02539  * call-seq:
02540  *   -fix  ->  integer
02541  *
02542  * Negates <code>fix</code> (which might return a Bignum).
02543  */
02544 
02545 static VALUE
02546 fix_uminus(VALUE num)
02547 {
02548     return LONG2NUM(-FIX2LONG(num));
02549 }
02550 
02551 VALUE
02552 rb_fix2str(VALUE x, int base)
02553 {
02554     extern const char ruby_digitmap[];
02555     char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf;
02556     long val = FIX2LONG(x);
02557     int neg = 0;
02558 
02559     if (base < 2 || 36 < base) {
02560         rb_raise(rb_eArgError, "invalid radix %d", base);
02561     }
02562     if (val == 0) {
02563         return rb_usascii_str_new2("0");
02564     }
02565     if (val < 0) {
02566         val = -val;
02567         neg = 1;
02568     }
02569     *--b = '\0';
02570     do {
02571         *--b = ruby_digitmap[(int)(val % base)];
02572     } while (val /= base);
02573     if (neg) {
02574         *--b = '-';
02575     }
02576 
02577     return rb_usascii_str_new2(b);
02578 }
02579 
02580 /*
02581  *  call-seq:
02582  *     fix.to_s(base=10)  ->  string
02583  *
02584  *  Returns a string containing the representation of <i>fix</i> radix
02585  *  <i>base</i> (between 2 and 36).
02586  *
02587  *     12345.to_s       #=> "12345"
02588  *     12345.to_s(2)    #=> "11000000111001"
02589  *     12345.to_s(8)    #=> "30071"
02590  *     12345.to_s(10)   #=> "12345"
02591  *     12345.to_s(16)   #=> "3039"
02592  *     12345.to_s(36)   #=> "9ix"
02593  *
02594  */
02595 static VALUE
02596 fix_to_s(int argc, VALUE *argv, VALUE x)
02597 {
02598     int base;
02599 
02600     if (argc == 0) base = 10;
02601     else {
02602         VALUE b;
02603 
02604         rb_scan_args(argc, argv, "01", &b);
02605         base = NUM2INT(b);
02606     }
02607 
02608     return rb_fix2str(x, base);
02609 }
02610 
02611 /*
02612  * call-seq:
02613  *   fix + numeric  ->  numeric_result
02614  *
02615  * Performs addition: the class of the resulting object depends on
02616  * the class of <code>numeric</code> and on the magnitude of the
02617  * result.
02618  */
02619 
02620 static VALUE
02621 fix_plus(VALUE x, VALUE y)
02622 {
02623     if (FIXNUM_P(y)) {
02624         long a, b, c;
02625         VALUE r;
02626 
02627         a = FIX2LONG(x);
02628         b = FIX2LONG(y);
02629         c = a + b;
02630         r = LONG2NUM(c);
02631 
02632         return r;
02633     }
02634     switch (TYPE(y)) {
02635       case T_BIGNUM:
02636         return rb_big_plus(y, x);
02637       case T_FLOAT:
02638         return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));
02639       default:
02640         return rb_num_coerce_bin(x, y, '+');
02641     }
02642 }
02643 
02644 /*
02645  * call-seq:
02646  *   fix - numeric  ->  numeric_result
02647  *
02648  * Performs subtraction: the class of the resulting object depends on
02649  * the class of <code>numeric</code> and on the magnitude of the
02650  * result.
02651  */
02652 
02653 static VALUE
02654 fix_minus(VALUE x, VALUE y)
02655 {
02656     if (FIXNUM_P(y)) {
02657         long a, b, c;
02658         VALUE r;
02659 
02660         a = FIX2LONG(x);
02661         b = FIX2LONG(y);
02662         c = a - b;
02663         r = LONG2NUM(c);
02664 
02665         return r;
02666     }
02667     switch (TYPE(y)) {
02668       case T_BIGNUM:
02669         x = rb_int2big(FIX2LONG(x));
02670         return rb_big_minus(x, y);
02671       case T_FLOAT:
02672         return DBL2NUM((double)FIX2LONG(x) - RFLOAT_VALUE(y));
02673       default:
02674         return rb_num_coerce_bin(x, y, '-');
02675     }
02676 }
02677 
02678 #define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
02679 /*tests if N*N would overflow*/
02680 #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
02681 
02682 /*
02683  * call-seq:
02684  *   fix * numeric  ->  numeric_result
02685  *
02686  * Performs multiplication: the class of the resulting object depends on
02687  * the class of <code>numeric</code> and on the magnitude of the
02688  * result.
02689  */
02690 
02691 static VALUE
02692 fix_mul(VALUE x, VALUE y)
02693 {
02694     if (FIXNUM_P(y)) {
02695 #ifdef __HP_cc
02696 /* avoids an optimization bug of HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005] */
02697         volatile
02698 #endif
02699         long a, b;
02700 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
02701         LONG_LONG d;
02702 #else
02703         VALUE r;
02704 #endif
02705 
02706         a = FIX2LONG(x);
02707         b = FIX2LONG(y);
02708 
02709 #if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
02710         d = (LONG_LONG)a * b;
02711         if (FIXABLE(d)) return LONG2FIX(d);
02712         return rb_ll2inum(d);
02713 #else
02714         if (FIT_SQRT_LONG(a) && FIT_SQRT_LONG(b))
02715             return LONG2FIX(a*b);
02716         if (a == 0) return x;
02717         if (MUL_OVERFLOW_FIXNUM_P(a, b))
02718             r = rb_big_mul(rb_int2big(a), rb_int2big(b));
02719         else
02720             r = LONG2FIX(a * b);
02721         return r;
02722 #endif
02723     }
02724     switch (TYPE(y)) {
02725       case T_BIGNUM:
02726         return rb_big_mul(y, x);
02727       case T_FLOAT:
02728         return DBL2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y));
02729       default:
02730         return rb_num_coerce_bin(x, y, '*');
02731     }
02732 }
02733 
02734 static void
02735 fixdivmod(long x, long y, long *divp, long *modp)
02736 {
02737     long div, mod;
02738 
02739     if (y == 0) rb_num_zerodiv();
02740     if (y < 0) {
02741         if (x < 0)
02742             div = -x / -y;
02743         else
02744             div = - (x / -y);
02745     }
02746     else {
02747         if (x < 0)
02748             div = - (-x / y);
02749         else
02750             div = x / y;
02751     }
02752     mod = x - div*y;
02753     if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
02754         mod += y;
02755         div -= 1;
02756     }
02757     if (divp) *divp = div;
02758     if (modp) *modp = mod;
02759 }
02760 
02761 /*
02762  *  call-seq:
02763  *     fix.fdiv(numeric)  ->  float
02764  *
02765  *  Returns the floating point result of dividing <i>fix</i> by
02766  *  <i>numeric</i>.
02767  *
02768  *     654321.fdiv(13731)      #=> 47.6528293642124
02769  *     654321.fdiv(13731.24)   #=> 47.6519964693647
02770  *
02771  */
02772 
02773 static VALUE
02774 fix_fdiv(VALUE x, VALUE y)
02775 {
02776     if (FIXNUM_P(y)) {
02777         return DBL2NUM((double)FIX2LONG(x) / (double)FIX2LONG(y));
02778     }
02779     switch (TYPE(y)) {
02780       case T_BIGNUM:
02781         return rb_big_fdiv(rb_int2big(FIX2LONG(x)), y);
02782       case T_FLOAT:
02783         return DBL2NUM((double)FIX2LONG(x) / RFLOAT_VALUE(y));
02784       default:
02785         return rb_num_coerce_bin(x, y, rb_intern("fdiv"));
02786     }
02787 }
02788 
02789 static VALUE
02790 fix_divide(VALUE x, VALUE y, ID op)
02791 {
02792     if (FIXNUM_P(y)) {
02793         long div;
02794 
02795         fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0);
02796         return LONG2NUM(div);
02797     }
02798     switch (TYPE(y)) {
02799       case T_BIGNUM:
02800         x = rb_int2big(FIX2LONG(x));
02801         return rb_big_div(x, y);
02802       case T_FLOAT:
02803         {
02804             double div;
02805 
02806             if (op == '/') {
02807                 div = (double)FIX2LONG(x) / RFLOAT_VALUE(y);
02808                 return DBL2NUM(div);
02809             }
02810             else {
02811                 if (RFLOAT_VALUE(y) == 0) rb_num_zerodiv();
02812                 div = (double)FIX2LONG(x) / RFLOAT_VALUE(y);
02813                 return rb_dbl2big(floor(div));
02814             }
02815         }
02816       case T_RATIONAL:
02817         if (op == '/' && FIX2LONG(x) == 1)
02818             return rb_rational_reciprocal(y);
02819         /* fall through */
02820       default:
02821         return rb_num_coerce_bin(x, y, op);
02822     }
02823 }
02824 
02825 /*
02826  * call-seq:
02827  *   fix / numeric  ->  numeric_result
02828  *
02829  * Performs division: the class of the resulting object depends on
02830  * the class of <code>numeric</code> and on the magnitude of the
02831  * result.
02832  */
02833 
02834 static VALUE
02835 fix_div(VALUE x, VALUE y)
02836 {
02837     return fix_divide(x, y, '/');
02838 }
02839 
02840 /*
02841  * call-seq:
02842  *   fix.div(numeric)  ->  integer
02843  *
02844  * Performs integer division: returns integer value.
02845  */
02846 
02847 static VALUE
02848 fix_idiv(VALUE x, VALUE y)
02849 {
02850     return fix_divide(x, y, rb_intern("div"));
02851 }
02852 
02853 /*
02854  *  call-seq:
02855  *    fix % other        ->  real
02856  *    fix.modulo(other)  ->  real
02857  *
02858  *  Returns <code>fix</code> modulo <code>other</code>.
02859  *  See <code>numeric.divmod</code> for more information.
02860  */
02861 
02862 static VALUE
02863 fix_mod(VALUE x, VALUE y)
02864 {
02865     if (FIXNUM_P(y)) {
02866         long mod;
02867 
02868         fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod);
02869         return LONG2NUM(mod);
02870     }
02871     switch (TYPE(y)) {
02872       case T_BIGNUM:
02873         x = rb_int2big(FIX2LONG(x));
02874         return rb_big_modulo(x, y);
02875       case T_FLOAT:
02876         return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y)));
02877       default:
02878         return rb_num_coerce_bin(x, y, '%');
02879     }
02880 }
02881 
02882 /*
02883  *  call-seq:
02884  *     fix.divmod(numeric)  ->  array
02885  *
02886  *  See <code>Numeric#divmod</code>.
02887  */
02888 static VALUE
02889 fix_divmod(VALUE x, VALUE y)
02890 {
02891     if (FIXNUM_P(y)) {
02892         long div, mod;
02893 
02894         fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod);
02895 
02896         return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod));
02897     }
02898     switch (TYPE(y)) {
02899       case T_BIGNUM:
02900         x = rb_int2big(FIX2LONG(x));
02901         return rb_big_divmod(x, y);
02902       case T_FLOAT:
02903         {
02904             double div, mod;
02905             volatile VALUE a, b;
02906 
02907             flodivmod((double)FIX2LONG(x), RFLOAT_VALUE(y), &div, &mod);
02908             a = dbl2ival(div);
02909             b = DBL2NUM(mod);
02910             return rb_assoc_new(a, b);
02911         }
02912       default:
02913         return rb_num_coerce_bin(x, y, rb_intern("divmod"));
02914     }
02915 }
02916 
02917 static VALUE
02918 int_pow(long x, unsigned long y)
02919 {
02920     int neg = x < 0;
02921     long z = 1;
02922 
02923     if (neg) x = -x;
02924     if (y & 1)
02925         z = x;
02926     else
02927         neg = 0;
02928     y &= ~1;
02929     do {
02930         while (y % 2 == 0) {
02931             if (!FIT_SQRT_LONG(x)) {
02932                 VALUE v;
02933               bignum:
02934                 v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
02935                 if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
02936                 return v;
02937             }
02938             x = x * x;
02939             y >>= 1;
02940         }
02941         {
02942             if (MUL_OVERFLOW_FIXNUM_P(x, z)) {
02943                 goto bignum;
02944             }
02945             z = x * z;
02946         }
02947     } while (--y);
02948     if (neg) z = -z;
02949     return LONG2NUM(z);
02950 }
02951 
02952 /*
02953  *  call-seq:
02954  *    fix ** numeric  ->  numeric_result
02955  *
02956  *  Raises <code>fix</code> to the <code>numeric</code> power, which may
02957  *  be negative or fractional.
02958  *
02959  *    2 ** 3      #=> 8
02960  *    2 ** -1     #=> (1/2)
02961  *    2 ** 0.5    #=> 1.4142135623731
02962  */
02963 
02964 static VALUE
02965 fix_pow(VALUE x, VALUE y)
02966 {
02967     long a = FIX2LONG(x);
02968 
02969     if (FIXNUM_P(y)) {
02970         long b = FIX2LONG(y);
02971 
02972         if (a == 1) return INT2FIX(1);
02973         if (a == -1) {
02974             if (b % 2 == 0)
02975                 return INT2FIX(1);
02976             else
02977                 return INT2FIX(-1);
02978         }
02979         if (b < 0)
02980             return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
02981 
02982         if (b == 0) return INT2FIX(1);
02983         if (b == 1) return x;
02984         if (a == 0) {
02985             if (b > 0) return INT2FIX(0);
02986             return DBL2NUM(INFINITY);
02987         }
02988         return int_pow(a, b);
02989     }
02990     switch (TYPE(y)) {
02991       case T_BIGNUM:
02992         if (a == 1) return INT2FIX(1);
02993         if (a == -1) {
02994             if (int_even_p(y)) return INT2FIX(1);
02995             else return INT2FIX(-1);
02996         }
02997         if (negative_int_p(y))
02998             return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
02999         if (a == 0) return INT2FIX(0);
03000         x = rb_int2big(FIX2LONG(x));
03001         return rb_big_pow(x, y);
03002       case T_FLOAT:
03003         if (RFLOAT_VALUE(y) == 0.0) return DBL2NUM(1.0);
03004         if (a == 0) {
03005             return DBL2NUM(RFLOAT_VALUE(y) < 0 ? INFINITY : 0.0);
03006         }
03007         if (a == 1) return DBL2NUM(1.0);
03008         {
03009             double dy = RFLOAT_VALUE(y);
03010             if (a < 0 && dy != round(dy))
03011                 return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
03012             return DBL2NUM(pow((double)a, dy));
03013         }
03014       default:
03015         return rb_num_coerce_bin(x, y, rb_intern("**"));
03016     }
03017 }
03018 
03019 /*
03020  * call-seq:
03021  *   fix == other  ->  true or false
03022  *
03023  * Return <code>true</code> if <code>fix</code> equals <code>other</code>
03024  * numerically.
03025  *
03026  *   1 == 2      #=> false
03027  *   1 == 1.0    #=> true
03028  */
03029 
03030 static VALUE
03031 fix_equal(VALUE x, VALUE y)
03032 {
03033     if (x == y) return Qtrue;
03034     if (FIXNUM_P(y)) return Qfalse;
03035     switch (TYPE(y)) {
03036       case T_BIGNUM:
03037         return rb_big_eq(y, x);
03038       case T_FLOAT:
03039         return rb_integer_float_eq(x, y);
03040       default:
03041         return num_equal(x, y);
03042     }
03043 }
03044 
03045 /*
03046  *  call-seq:
03047  *     fix <=> numeric  ->  -1, 0, +1 or nil
03048  *
03049  *  Comparison---Returns -1, 0, +1 or nil depending on whether +fix+ is less
03050  *  than, equal to, or greater than +numeric+. This is the basis for the tests
03051  *  in  Comparable.
03052  *
03053  *  +nil+ is returned if the two values are incomparable.
03054  */
03055 
03056 static VALUE
03057 fix_cmp(VALUE x, VALUE y)
03058 {
03059     if (x == y) return INT2FIX(0);
03060     if (FIXNUM_P(y)) {
03061         if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
03062         return INT2FIX(-1);
03063     }
03064     switch (TYPE(y)) {
03065       case T_BIGNUM:
03066         return rb_big_cmp(rb_int2big(FIX2LONG(x)), y);
03067       case T_FLOAT:
03068         return rb_integer_float_cmp(x, y);
03069       default:
03070         return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
03071     }
03072 }
03073 
03074 /*
03075  * call-seq:
03076  *   fix > real  ->  true or false
03077  *
03078  * Returns <code>true</code> if the value of <code>fix</code> is
03079  * greater than that of <code>real</code>.
03080  */
03081 
03082 static VALUE
03083 fix_gt(VALUE x, VALUE y)
03084 {
03085     if (FIXNUM_P(y)) {
03086         if (FIX2LONG(x) > FIX2LONG(y)) return Qtrue;
03087         return Qfalse;
03088     }
03089     switch (TYPE(y)) {
03090       case T_BIGNUM:
03091         return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) > 0 ? Qtrue : Qfalse;
03092       case T_FLOAT:
03093         return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse;
03094       default:
03095         return rb_num_coerce_relop(x, y, '>');
03096     }
03097 }
03098 
03099 /*
03100  * call-seq:
03101  *   fix >= real  ->  true or false
03102  *
03103  * Returns <code>true</code> if the value of <code>fix</code> is
03104  * greater than or equal to that of <code>real</code>.
03105  */
03106 
03107 static VALUE
03108 fix_ge(VALUE x, VALUE y)
03109 {
03110     if (FIXNUM_P(y)) {
03111         if (FIX2LONG(x) >= FIX2LONG(y)) return Qtrue;
03112         return Qfalse;
03113     }
03114     switch (TYPE(y)) {
03115       case T_BIGNUM:
03116         return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) >= 0 ? Qtrue : Qfalse;
03117       case T_FLOAT:
03118         {
03119           VALUE rel = rb_integer_float_cmp(x, y);
03120           return rel == INT2FIX(1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
03121         }
03122       default:
03123         return rb_num_coerce_relop(x, y, rb_intern(">="));
03124     }
03125 }
03126 
03127 /*
03128  * call-seq:
03129  *   fix < real  ->  true or false
03130  *
03131  * Returns <code>true</code> if the value of <code>fix</code> is
03132  * less than that of <code>real</code>.
03133  */
03134 
03135 static VALUE
03136 fix_lt(VALUE x, VALUE y)
03137 {
03138     if (FIXNUM_P(y)) {
03139         if (FIX2LONG(x) < FIX2LONG(y)) return Qtrue;
03140         return Qfalse;
03141     }
03142     switch (TYPE(y)) {
03143       case T_BIGNUM:
03144         return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) < 0 ? Qtrue : Qfalse;
03145       case T_FLOAT:
03146         return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse;
03147       default:
03148         return rb_num_coerce_relop(x, y, '<');
03149     }
03150 }
03151 
03152 /*
03153  * call-seq:
03154  *   fix <= real  ->  true or false
03155  *
03156  * Returns <code>true</code> if the value of <code>fix</code> is
03157  * less than or equal to that of <code>real</code>.
03158  */
03159 
03160 static VALUE
03161 fix_le(VALUE x, VALUE y)
03162 {
03163     if (FIXNUM_P(y)) {
03164         if (FIX2LONG(x) <= FIX2LONG(y)) return Qtrue;
03165         return Qfalse;
03166     }
03167     switch (TYPE(y)) {
03168       case T_BIGNUM:
03169         return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) <= 0 ? Qtrue : Qfalse;
03170       case T_FLOAT:
03171         {
03172           VALUE rel = rb_integer_float_cmp(x, y);
03173           return rel == INT2FIX(-1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
03174         }
03175       default:
03176         return rb_num_coerce_relop(x, y, rb_intern("<="));
03177     }
03178 }
03179 
03180 /*
03181  * call-seq:
03182  *   ~fix  ->  integer
03183  *
03184  * One's complement: returns a number where each bit is flipped.
03185  */
03186 
03187 static VALUE
03188 fix_rev(VALUE num)
03189 {
03190     return ~num | FIXNUM_FLAG;
03191 }
03192 
03193 static int
03194 bit_coerce(VALUE *x, VALUE *y, int err)
03195 {
03196     if (!FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) {
03197         do_coerce(x, y, err);
03198         if (!FIXNUM_P(*x) && !RB_TYPE_P(*x, T_BIGNUM)
03199             && !FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) {
03200             if (!err) return FALSE;
03201             coerce_failed(*x, *y);
03202         }
03203     }
03204     return TRUE;
03205 }
03206 
03207 VALUE
03208 rb_num_coerce_bit(VALUE x, VALUE y, ID func)
03209 {
03210     bit_coerce(&x, &y, TRUE);
03211     return rb_funcall(x, func, 1, y);
03212 }
03213 
03214 /*
03215  * call-seq:
03216  *   fix & integer  ->  integer_result
03217  *
03218  * Bitwise AND.
03219  */
03220 
03221 static VALUE
03222 fix_and(VALUE x, VALUE y)
03223 {
03224     if (FIXNUM_P(y)) {
03225         long val = FIX2LONG(x) & FIX2LONG(y);
03226         return LONG2NUM(val);
03227     }
03228 
03229     if (RB_TYPE_P(y, T_BIGNUM)) {
03230         return rb_big_and(y, x);
03231     }
03232 
03233     bit_coerce(&x, &y, TRUE);
03234     return rb_funcall(x, rb_intern("&"), 1, y);
03235 }
03236 
03237 /*
03238  * call-seq:
03239  *   fix | integer  ->  integer_result
03240  *
03241  * Bitwise OR.
03242  */
03243 
03244 static VALUE
03245 fix_or(VALUE x, VALUE y)
03246 {
03247     if (FIXNUM_P(y)) {
03248         long val = FIX2LONG(x) | FIX2LONG(y);
03249         return LONG2NUM(val);
03250     }
03251 
03252     if (RB_TYPE_P(y, T_BIGNUM)) {
03253         return rb_big_or(y, x);
03254     }
03255 
03256     bit_coerce(&x, &y, TRUE);
03257     return rb_funcall(x, rb_intern("|"), 1, y);
03258 }
03259 
03260 /*
03261  * call-seq:
03262  *   fix ^ integer  ->  integer_result
03263  *
03264  * Bitwise EXCLUSIVE OR.
03265  */
03266 
03267 static VALUE
03268 fix_xor(VALUE x, VALUE y)
03269 {
03270     if (FIXNUM_P(y)) {
03271         long val = FIX2LONG(x) ^ FIX2LONG(y);
03272         return LONG2NUM(val);
03273     }
03274 
03275     if (RB_TYPE_P(y, T_BIGNUM)) {
03276         return rb_big_xor(y, x);
03277     }
03278 
03279     bit_coerce(&x, &y, TRUE);
03280     return rb_funcall(x, rb_intern("^"), 1, y);
03281 }
03282 
03283 static VALUE fix_lshift(long, unsigned long);
03284 static VALUE fix_rshift(long, unsigned long);
03285 
03286 /*
03287  * call-seq:
03288  *   fix << count  ->  integer
03289  *
03290  * Shifts _fix_ left _count_ positions (right if _count_ is negative).
03291  */
03292 
03293 static VALUE
03294 rb_fix_lshift(VALUE x, VALUE y)
03295 {
03296     long val, width;
03297 
03298     val = NUM2LONG(x);
03299     if (!FIXNUM_P(y))
03300         return rb_big_lshift(rb_int2big(val), y);
03301     width = FIX2LONG(y);
03302     if (width < 0)
03303         return fix_rshift(val, (unsigned long)-width);
03304     return fix_lshift(val, width);
03305 }
03306 
03307 static VALUE
03308 fix_lshift(long val, unsigned long width)
03309 {
03310     if (width > (SIZEOF_LONG*CHAR_BIT-1)
03311         || ((unsigned long)val)>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) {
03312         return rb_big_lshift(rb_int2big(val), ULONG2NUM(width));
03313     }
03314     val = val << width;
03315     return LONG2NUM(val);
03316 }
03317 
03318 /*
03319  * call-seq:
03320  *   fix >> count  ->  integer
03321  *
03322  * Shifts _fix_ right _count_ positions (left if _count_ is negative).
03323  */
03324 
03325 static VALUE
03326 rb_fix_rshift(VALUE x, VALUE y)
03327 {
03328     long i, val;
03329 
03330     val = FIX2LONG(x);
03331     if (!FIXNUM_P(y))
03332         return rb_big_rshift(rb_int2big(val), y);
03333     i = FIX2LONG(y);
03334     if (i == 0) return x;
03335     if (i < 0)
03336         return fix_lshift(val, (unsigned long)-i);
03337     return fix_rshift(val, i);
03338 }
03339 
03340 static VALUE
03341 fix_rshift(long val, unsigned long i)
03342 {
03343     if (i >= sizeof(long)*CHAR_BIT-1) {
03344         if (val < 0) return INT2FIX(-1);
03345         return INT2FIX(0);
03346     }
03347     val = RSHIFT(val, i);
03348     return LONG2FIX(val);
03349 }
03350 
03351 /*
03352  *  call-seq:
03353  *     fix[n]  ->  0, 1
03354  *
03355  *  Bit Reference---Returns the <em>n</em>th bit in the binary
03356  *  representation of <i>fix</i>, where <i>fix</i>[0] is the least
03357  *  significant bit.
03358  *
03359  *     a = 0b11001100101010
03360  *     30.downto(0) do |n| print a[n] end
03361  *
03362  *  <em>produces:</em>
03363  *
03364  *     0000000000000000011001100101010
03365  */
03366 
03367 static VALUE
03368 fix_aref(VALUE fix, VALUE idx)
03369 {
03370     long val = FIX2LONG(fix);
03371     long i;
03372 
03373     idx = rb_to_int(idx);
03374     if (!FIXNUM_P(idx)) {
03375         idx = rb_big_norm(idx);
03376         if (!FIXNUM_P(idx)) {
03377             if (!RBIGNUM_SIGN(idx) || val >= 0)
03378                 return INT2FIX(0);
03379             return INT2FIX(1);
03380         }
03381     }
03382     i = FIX2LONG(idx);
03383 
03384     if (i < 0) return INT2FIX(0);
03385     if (SIZEOF_LONG*CHAR_BIT-1 < i) {
03386         if (val < 0) return INT2FIX(1);
03387         return INT2FIX(0);
03388     }
03389     if (val & (1L<<i))
03390         return INT2FIX(1);
03391     return INT2FIX(0);
03392 }
03393 
03394 /*
03395  *  call-seq:
03396  *     fix.to_f  ->  float
03397  *
03398  *  Converts <i>fix</i> to a <code>Float</code>.
03399  *
03400  */
03401 
03402 static VALUE
03403 fix_to_f(VALUE num)
03404 {
03405     double val;
03406 
03407     val = (double)FIX2LONG(num);
03408 
03409     return DBL2NUM(val);
03410 }
03411 
03412 /*
03413  *  call-seq:
03414  *     fix.abs        ->  integer
03415  *     fix.magnitude  ->  integer
03416  *
03417  *  Returns the absolute value of <i>fix</i>.
03418  *
03419  *     -12345.abs   #=> 12345
03420  *     12345.abs    #=> 12345
03421  *
03422  */
03423 
03424 static VALUE
03425 fix_abs(VALUE fix)
03426 {
03427     long i = FIX2LONG(fix);
03428 
03429     if (i < 0) i = -i;
03430 
03431     return LONG2NUM(i);
03432 }
03433 
03434 
03435 
03436 /*
03437  *  call-seq:
03438  *     fix.size  ->  fixnum
03439  *
03440  *  Returns the number of <em>bytes</em> in the machine representation
03441  *  of a <code>Fixnum</code>.
03442  *
03443  *     1.size            #=> 4
03444  *     -1.size           #=> 4
03445  *     2147483647.size   #=> 4
03446  */
03447 
03448 static VALUE
03449 fix_size(VALUE fix)
03450 {
03451     return INT2FIX(sizeof(long));
03452 }
03453 
03454 static VALUE
03455 int_upto_size(VALUE from, VALUE args)
03456 {
03457     return num_interval_step_size(from, RARRAY_PTR(args)[0], INT2FIX(1), FALSE);
03458 }
03459 
03460 /*
03461  *  call-seq:
03462  *     int.upto(limit) {|i| block }  ->  self
03463  *     int.upto(limit)               ->  an_enumerator
03464  *
03465  *  Iterates <em>block</em>, passing in integer values from <i>int</i>
03466  *  up to and including <i>limit</i>.
03467  *
03468  *  If no block is given, an enumerator is returned instead.
03469  *
03470  *     5.upto(10) { |i| print i, " " }
03471  *
03472  *  <em>produces:</em>
03473  *
03474  *     5 6 7 8 9 10
03475  */
03476 
03477 static VALUE
03478 int_upto(VALUE from, VALUE to)
03479 {
03480     RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size);
03481     if (FIXNUM_P(from) && FIXNUM_P(to)) {
03482         long i, end;
03483 
03484         end = FIX2LONG(to);
03485         for (i = FIX2LONG(from); i <= end; i++) {
03486             rb_yield(LONG2FIX(i));
03487         }
03488     }
03489     else {
03490         VALUE i = from, c;
03491 
03492         while (!(c = rb_funcall(i, '>', 1, to))) {
03493             rb_yield(i);
03494             i = rb_funcall(i, '+', 1, INT2FIX(1));
03495         }
03496         if (NIL_P(c)) rb_cmperr(i, to);
03497     }
03498     return from;
03499 }
03500 
03501 static VALUE
03502 int_downto_size(VALUE from, VALUE args)
03503 {
03504     return num_interval_step_size(from, RARRAY_PTR(args)[0], INT2FIX(-1), FALSE);
03505 }
03506 
03507 /*
03508  *  call-seq:
03509  *     int.downto(limit) {|i| block }  ->  self
03510  *     int.downto(limit)               ->  an_enumerator
03511  *
03512  *  Iterates <em>block</em>, passing decreasing values from <i>int</i>
03513  *  down to and including <i>limit</i>.
03514  *
03515  *  If no block is given, an enumerator is returned instead.
03516  *
03517  *     5.downto(1) { |n| print n, ".. " }
03518  *     print "  Liftoff!\n"
03519  *
03520  *  <em>produces:</em>
03521  *
03522  *     5.. 4.. 3.. 2.. 1..   Liftoff!
03523  */
03524 
03525 static VALUE
03526 int_downto(VALUE from, VALUE to)
03527 {
03528     RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size);
03529     if (FIXNUM_P(from) && FIXNUM_P(to)) {
03530         long i, end;
03531 
03532         end = FIX2LONG(to);
03533         for (i=FIX2LONG(from); i >= end; i--) {
03534             rb_yield(LONG2FIX(i));
03535         }
03536     }
03537     else {
03538         VALUE i = from, c;
03539 
03540         while (!(c = rb_funcall(i, '<', 1, to))) {
03541             rb_yield(i);
03542             i = rb_funcall(i, '-', 1, INT2FIX(1));
03543         }
03544         if (NIL_P(c)) rb_cmperr(i, to);
03545     }
03546     return from;
03547 }
03548 
03549 static VALUE
03550 int_dotimes_size(VALUE num)
03551 {
03552     if (FIXNUM_P(num)) {
03553         if (NUM2LONG(num) <= 0) return INT2FIX(0);
03554     }
03555     else {
03556         if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) return INT2FIX(0);
03557     }
03558     return num;
03559 }
03560 
03561 /*
03562  *  call-seq:
03563  *     int.times {|i| block }  ->  self
03564  *     int.times               ->  an_enumerator
03565  *
03566  *  Iterates block <i>int</i> times, passing in values from zero to
03567  *  <i>int</i> - 1.
03568  *
03569  *  If no block is given, an enumerator is returned instead.
03570  *
03571  *     5.times do |i|
03572  *       print i, " "
03573  *     end
03574  *
03575  *  <em>produces:</em>
03576  *
03577  *     0 1 2 3 4
03578  */
03579 
03580 static VALUE
03581 int_dotimes(VALUE num)
03582 {
03583     RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size);
03584 
03585     if (FIXNUM_P(num)) {
03586         long i, end;
03587 
03588         end = FIX2LONG(num);
03589         for (i=0; i<end; i++) {
03590             rb_yield(LONG2FIX(i));
03591         }
03592     }
03593     else {
03594         VALUE i = INT2FIX(0);
03595 
03596         for (;;) {
03597             if (!RTEST(rb_funcall(i, '<', 1, num))) break;
03598             rb_yield(i);
03599             i = rb_funcall(i, '+', 1, INT2FIX(1));
03600         }
03601     }
03602     return num;
03603 }
03604 
03605 /*
03606  *  call-seq:
03607  *     int.round([ndigits])  ->  integer or float
03608  *
03609  *  Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits).
03610  *  Precision may be negative.  Returns a floating point number when +ndigits+
03611  *  is positive, +self+ for zero, and round down for negative.
03612  *
03613  *     1.round        #=> 1
03614  *     1.round(2)     #=> 1.0
03615  *     15.round(-1)   #=> 20
03616  */
03617 
03618 static VALUE
03619 int_round(int argc, VALUE* argv, VALUE num)
03620 {
03621     VALUE n;
03622     int ndigits;
03623 
03624     if (argc == 0) return num;
03625     rb_scan_args(argc, argv, "1", &n);
03626     ndigits = NUM2INT(n);
03627     if (ndigits > 0) {
03628         return rb_Float(num);
03629     }
03630     if (ndigits == 0) {
03631         return num;
03632     }
03633     return int_round_0(num, ndigits);
03634 }
03635 
03636 /*
03637  *  call-seq:
03638  *     fix.zero?  ->  true or false
03639  *
03640  *  Returns <code>true</code> if <i>fix</i> is zero.
03641  *
03642  */
03643 
03644 static VALUE
03645 fix_zero_p(VALUE num)
03646 {
03647     if (FIX2LONG(num) == 0) {
03648         return Qtrue;
03649     }
03650     return Qfalse;
03651 }
03652 
03653 /*
03654  *  call-seq:
03655  *     fix.odd?  ->  true or false
03656  *
03657  *  Returns <code>true</code> if <i>fix</i> is an odd number.
03658  */
03659 
03660 static VALUE
03661 fix_odd_p(VALUE num)
03662 {
03663     if (num & 2) {
03664         return Qtrue;
03665     }
03666     return Qfalse;
03667 }
03668 
03669 /*
03670  *  call-seq:
03671  *     fix.even?  ->  true or false
03672  *
03673  *  Returns <code>true</code> if <i>fix</i> is an even number.
03674  */
03675 
03676 static VALUE
03677 fix_even_p(VALUE num)
03678 {
03679     if (num & 2) {
03680         return Qfalse;
03681     }
03682     return Qtrue;
03683 }
03684 
03685 /*
03686  *  Document-class: ZeroDivisionError
03687  *
03688  *  Raised when attempting to divide an integer by 0.
03689  *
03690  *     42 / 0
03691  *
03692  *  <em>raises the exception:</em>
03693  *
03694  *     ZeroDivisionError: divided by 0
03695  *
03696  *  Note that only division by an exact 0 will raise that exception:
03697  *
03698  *     42 /  0.0 #=> Float::INFINITY
03699  *     42 / -0.0 #=> -Float::INFINITY
03700  *     0  /  0.0 #=> NaN
03701  */
03702 
03703 /*
03704  *  Document-class: FloatDomainError
03705  *
03706  *  Raised when attempting to convert special float values
03707  *  (in particular infinite or NaN)
03708  *  to numerical classes which don't support them.
03709  *
03710  *     Float::INFINITY.to_r
03711  *
03712  *  <em>raises the exception:</em>
03713  *
03714  *     FloatDomainError: Infinity
03715  */
03716 
03717 void
03718 Init_Numeric(void)
03719 {
03720 #undef rb_intern
03721 #define rb_intern(str) rb_intern_const(str)
03722 
03723 #if defined(__FreeBSD__) && __FreeBSD__ < 4
03724     /* allow divide by zero -- Inf */
03725     fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL));
03726 #elif defined(_UNICOSMP)
03727     /* Turn off floating point exceptions for divide by zero, etc. */
03728     _set_Creg(0, 0);
03729 #elif defined(__BORLANDC__)
03730     /* Turn off floating point exceptions for overflow, etc. */
03731     _control87(MCW_EM, MCW_EM);
03732     _control87(_control87(0,0),0x1FFF);
03733 #endif
03734     id_coerce = rb_intern("coerce");
03735     id_to_i = rb_intern("to_i");
03736     id_eq = rb_intern("==");
03737     id_div = rb_intern("div");
03738 
03739     rb_eZeroDivError = rb_define_class("ZeroDivisionError", rb_eStandardError);
03740     rb_eFloatDomainError = rb_define_class("FloatDomainError", rb_eRangeError);
03741     rb_cNumeric = rb_define_class("Numeric", rb_cObject);
03742 
03743     rb_define_method(rb_cNumeric, "singleton_method_added", num_sadded, 1);
03744     rb_include_module(rb_cNumeric, rb_mComparable);
03745     rb_define_method(rb_cNumeric, "initialize_copy", num_init_copy, 1);
03746     rb_define_method(rb_cNumeric, "coerce", num_coerce, 1);
03747 
03748     rb_define_method(rb_cNumeric, "i", num_imaginary, 0);
03749     rb_define_method(rb_cNumeric, "+@", num_uplus, 0);
03750     rb_define_method(rb_cNumeric, "-@", num_uminus, 0);
03751     rb_define_method(rb_cNumeric, "<=>", num_cmp, 1);
03752     rb_define_method(rb_cNumeric, "eql?", num_eql, 1);
03753     rb_define_method(rb_cNumeric, "quo", num_quo, 1);
03754     rb_define_method(rb_cNumeric, "fdiv", num_fdiv, 1);
03755     rb_define_method(rb_cNumeric, "div", num_div, 1);
03756     rb_define_method(rb_cNumeric, "divmod", num_divmod, 1);
03757     rb_define_method(rb_cNumeric, "%", num_modulo, 1);
03758     rb_define_method(rb_cNumeric, "modulo", num_modulo, 1);
03759     rb_define_method(rb_cNumeric, "remainder", num_remainder, 1);
03760     rb_define_method(rb_cNumeric, "abs", num_abs, 0);
03761     rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
03762     rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
03763 
03764     rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
03765     rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
03766     rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
03767     rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
03768 
03769     rb_define_method(rb_cNumeric, "floor", num_floor, 0);
03770     rb_define_method(rb_cNumeric, "ceil", num_ceil, 0);
03771     rb_define_method(rb_cNumeric, "round", num_round, -1);
03772     rb_define_method(rb_cNumeric, "truncate", num_truncate, 0);
03773     rb_define_method(rb_cNumeric, "step", num_step, -1);
03774 
03775     rb_cInteger = rb_define_class("Integer", rb_cNumeric);
03776     rb_undef_alloc_func(rb_cInteger);
03777     rb_undef_method(CLASS_OF(rb_cInteger), "new");
03778 
03779     rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
03780     rb_define_method(rb_cInteger, "odd?", int_odd_p, 0);
03781     rb_define_method(rb_cInteger, "even?", int_even_p, 0);
03782     rb_define_method(rb_cInteger, "upto", int_upto, 1);
03783     rb_define_method(rb_cInteger, "downto", int_downto, 1);
03784     rb_define_method(rb_cInteger, "times", int_dotimes, 0);
03785     rb_define_method(rb_cInteger, "succ", int_succ, 0);
03786     rb_define_method(rb_cInteger, "next", int_succ, 0);
03787     rb_define_method(rb_cInteger, "pred", int_pred, 0);
03788     rb_define_method(rb_cInteger, "chr", int_chr, -1);
03789     rb_define_method(rb_cInteger, "ord", int_ord, 0);
03790     rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
03791     rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
03792     rb_define_method(rb_cInteger, "floor", int_to_i, 0);
03793     rb_define_method(rb_cInteger, "ceil", int_to_i, 0);
03794     rb_define_method(rb_cInteger, "truncate", int_to_i, 0);
03795     rb_define_method(rb_cInteger, "round", int_round, -1);
03796 
03797     rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
03798 
03799     rb_define_method(rb_cFixnum, "to_s", fix_to_s, -1);
03800     rb_define_alias(rb_cFixnum, "inspect", "to_s");
03801 
03802     rb_define_method(rb_cFixnum, "-@", fix_uminus, 0);
03803     rb_define_method(rb_cFixnum, "+", fix_plus, 1);
03804     rb_define_method(rb_cFixnum, "-", fix_minus, 1);
03805     rb_define_method(rb_cFixnum, "*", fix_mul, 1);
03806     rb_define_method(rb_cFixnum, "/", fix_div, 1);
03807     rb_define_method(rb_cFixnum, "div", fix_idiv, 1);
03808     rb_define_method(rb_cFixnum, "%", fix_mod, 1);
03809     rb_define_method(rb_cFixnum, "modulo", fix_mod, 1);
03810     rb_define_method(rb_cFixnum, "divmod", fix_divmod, 1);
03811     rb_define_method(rb_cFixnum, "fdiv", fix_fdiv, 1);
03812     rb_define_method(rb_cFixnum, "**", fix_pow, 1);
03813 
03814     rb_define_method(rb_cFixnum, "abs", fix_abs, 0);
03815     rb_define_method(rb_cFixnum, "magnitude", fix_abs, 0);
03816 
03817     rb_define_method(rb_cFixnum, "==", fix_equal, 1);
03818     rb_define_method(rb_cFixnum, "===", fix_equal, 1);
03819     rb_define_method(rb_cFixnum, "<=>", fix_cmp, 1);
03820     rb_define_method(rb_cFixnum, ">",  fix_gt, 1);
03821     rb_define_method(rb_cFixnum, ">=", fix_ge, 1);
03822     rb_define_method(rb_cFixnum, "<",  fix_lt, 1);
03823     rb_define_method(rb_cFixnum, "<=", fix_le, 1);
03824 
03825     rb_define_method(rb_cFixnum, "~", fix_rev, 0);
03826     rb_define_method(rb_cFixnum, "&", fix_and, 1);
03827     rb_define_method(rb_cFixnum, "|", fix_or,  1);
03828     rb_define_method(rb_cFixnum, "^", fix_xor, 1);
03829     rb_define_method(rb_cFixnum, "[]", fix_aref, 1);
03830 
03831     rb_define_method(rb_cFixnum, "<<", rb_fix_lshift, 1);
03832     rb_define_method(rb_cFixnum, ">>", rb_fix_rshift, 1);
03833 
03834     rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0);
03835     rb_define_method(rb_cFixnum, "size", fix_size, 0);
03836     rb_define_method(rb_cFixnum, "zero?", fix_zero_p, 0);
03837     rb_define_method(rb_cFixnum, "odd?", fix_odd_p, 0);
03838     rb_define_method(rb_cFixnum, "even?", fix_even_p, 0);
03839     rb_define_method(rb_cFixnum, "succ", fix_succ, 0);
03840 
03841     rb_cFloat  = rb_define_class("Float", rb_cNumeric);
03842 
03843     rb_undef_alloc_func(rb_cFloat);
03844     rb_undef_method(CLASS_OF(rb_cFloat), "new");
03845 
03846     /*
03847      *  Represents the rounding mode for floating point addition.
03848      *
03849      *  Usually defaults to 1, rounding to the nearest number.
03850      *
03851      *  Other modes include:
03852      *
03853      *  -1::    Indeterminable
03854      *  0::     Rounding towards zero
03855      *  1::     Rounding to the nearest number
03856      *  2::     Rounding towards positive infinity
03857      *  3::     Rounding towards negative infinity
03858      */
03859     rb_define_const(rb_cFloat, "ROUNDS", INT2FIX(FLT_ROUNDS));
03860     /*
03861      *  The base of the floating point, or number of unique digits used to
03862      *  represent the number.
03863      *
03864      *  Usually defaults to 2 on most systems, which would represent a base-10 decimal.
03865      */
03866     rb_define_const(rb_cFloat, "RADIX", INT2FIX(FLT_RADIX));
03867     /*
03868      * The number of base digits for the +double+ data type.
03869      *
03870      * Usually defaults to 53.
03871      */
03872     rb_define_const(rb_cFloat, "MANT_DIG", INT2FIX(DBL_MANT_DIG));
03873     /*
03874      *  The number of decimal digits in a double-precision floating point.
03875      *
03876      *  Usually defaults to 15.
03877      */
03878     rb_define_const(rb_cFloat, "DIG", INT2FIX(DBL_DIG));
03879     /*
03880      *  The smallest posable exponent value in a double-precision floating
03881      *  point.
03882      *
03883      *  Usually defaults to -1021.
03884      */
03885     rb_define_const(rb_cFloat, "MIN_EXP", INT2FIX(DBL_MIN_EXP));
03886     /*
03887      *  The largest possible exponent value in a double-precision floating
03888      *  point.
03889      *
03890      *  Usually defaults to 1024.
03891      */
03892     rb_define_const(rb_cFloat, "MAX_EXP", INT2FIX(DBL_MAX_EXP));
03893     /*
03894      *  The smallest negative exponent in a double-precision floating point
03895      *  where 10 raised to this power minus 1.
03896      *
03897      *  Usually defaults to -307.
03898      */
03899     rb_define_const(rb_cFloat, "MIN_10_EXP", INT2FIX(DBL_MIN_10_EXP));
03900     /*
03901      *  The largest positive exponent in a double-precision floating point where
03902      *  10 raised to this power minus 1.
03903      *
03904      *  Usually defaults to 308.
03905      */
03906     rb_define_const(rb_cFloat, "MAX_10_EXP", INT2FIX(DBL_MAX_10_EXP));
03907     /*
03908      *  The smallest positive integer in a double-precision floating point.
03909      *
03910      *  Usually defaults to 2.2250738585072014e-308.
03911      */
03912     rb_define_const(rb_cFloat, "MIN", DBL2NUM(DBL_MIN));
03913     /*
03914      *  The largest possible integer in a double-precision floating point number.
03915      *
03916      *  Usually defaults to 1.7976931348623157e+308.
03917      */
03918     rb_define_const(rb_cFloat, "MAX", DBL2NUM(DBL_MAX));
03919     /*
03920      *  The difference between 1 and the smallest double-precision floating
03921      *  point number.
03922      *
03923      *  Usually defaults to 2.2204460492503131e-16.
03924      */
03925     rb_define_const(rb_cFloat, "EPSILON", DBL2NUM(DBL_EPSILON));
03926     /*
03927      *  An expression representing positive infinity.
03928      */
03929     rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(INFINITY));
03930     /*
03931      *  An expression representing a value which is "not a number".
03932      */
03933     rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
03934 
03935     rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
03936     rb_define_alias(rb_cFloat, "inspect", "to_s");
03937     rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
03938     rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
03939     rb_define_method(rb_cFloat, "+", flo_plus, 1);
03940     rb_define_method(rb_cFloat, "-", flo_minus, 1);
03941     rb_define_method(rb_cFloat, "*", flo_mul, 1);
03942     rb_define_method(rb_cFloat, "/", flo_div, 1);
03943     rb_define_method(rb_cFloat, "quo", flo_quo, 1);
03944     rb_define_method(rb_cFloat, "fdiv", flo_quo, 1);
03945     rb_define_method(rb_cFloat, "%", flo_mod, 1);
03946     rb_define_method(rb_cFloat, "modulo", flo_mod, 1);
03947     rb_define_method(rb_cFloat, "divmod", flo_divmod, 1);
03948     rb_define_method(rb_cFloat, "**", flo_pow, 1);
03949     rb_define_method(rb_cFloat, "==", flo_eq, 1);
03950     rb_define_method(rb_cFloat, "===", flo_eq, 1);
03951     rb_define_method(rb_cFloat, "<=>", flo_cmp, 1);
03952     rb_define_method(rb_cFloat, ">",  flo_gt, 1);
03953     rb_define_method(rb_cFloat, ">=", flo_ge, 1);
03954     rb_define_method(rb_cFloat, "<",  flo_lt, 1);
03955     rb_define_method(rb_cFloat, "<=", flo_le, 1);
03956     rb_define_method(rb_cFloat, "eql?", flo_eql, 1);
03957     rb_define_method(rb_cFloat, "hash", flo_hash, 0);
03958     rb_define_method(rb_cFloat, "to_f", flo_to_f, 0);
03959     rb_define_method(rb_cFloat, "abs", flo_abs, 0);
03960     rb_define_method(rb_cFloat, "magnitude", flo_abs, 0);
03961     rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0);
03962 
03963     rb_define_method(rb_cFloat, "to_i", flo_truncate, 0);
03964     rb_define_method(rb_cFloat, "to_int", flo_truncate, 0);
03965     rb_define_method(rb_cFloat, "floor", flo_floor, 0);
03966     rb_define_method(rb_cFloat, "ceil", flo_ceil, 0);
03967     rb_define_method(rb_cFloat, "round", flo_round, -1);
03968     rb_define_method(rb_cFloat, "truncate", flo_truncate, 0);
03969 
03970     rb_define_method(rb_cFloat, "nan?",      flo_is_nan_p, 0);
03971     rb_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0);
03972     rb_define_method(rb_cFloat, "finite?",   flo_is_finite_p, 0);
03973 }
03974