Class: Bignum

Inherits:
Integer show all
Defined in:
bignum.c

Instance Method Summary collapse

Methods inherited from Integer

#ceil, #chr, #downto, #even?, #floor, induced_from, #integer?, #next, #odd?, #ord, #pred, #round, #succ, #times, #to_i, #to_int, #truncate, #upto

Methods included from Precision

included, #prec, #prec_f, #prec_i

Methods inherited from Numeric

#+@, #ceil, #floor, #initialize_copy, #integer?, #nonzero?, #round, #singleton_method_added, #step, #to_int, #truncate, #zero?

Methods included from Comparable

#<, #<=, #>, #>=, #between?

Instance Method Details

#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric

Returns big modulo other. See Numeric.divmod for more information.

Overloads:



# File 'bignum.c'

/*
 *  call-seq:
 *     big % other         => Numeric
 *     big.modulo(other)   => Numeric
 *
 *  Returns big modulo other. See Numeric.divmod for more
 *  information.
 */

static VALUE
rb_big_modulo(x, y)
    VALUE x, y;
{
    VALUE z;

    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    break;

      case T_BIGNUM:
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    bigdivmod(x, y, 0, &z);

    return bignorm(z);
}

#&(numeric) ⇒ Integer

Performs bitwise and between big and numeric.

Returns:



# File 'bignum.c'

/*
 * call-seq:
 *     big & numeric   =>  integer
 *
 * Performs bitwise +and+ between _big_ and _numeric_.
 */

VALUE
rb_big_and(xx, yy)
    VALUE xx, yy;
{
    volatile VALUE x, y, z;
    BDIGIT *ds1, *ds2, *zds;
    long i, l1, l2;
    char sign;

    x = xx;
    y = rb_to_int(yy);
    if (FIXNUM_P(y)) {
    y = rb_int2big(FIX2LONG(y));
    }
    if (!RBIGNUM(y)->sign) {
    y = rb_big_clone(y);
    get2comp(y);
    }
    if (!RBIGNUM(x)->sign) {
    x = rb_big_clone(x);
    get2comp(x);
    }
    if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
    l1 = RBIGNUM(y)->len;
    l2 = RBIGNUM(x)->len;
    ds1 = BDIGITS(y);
    ds2 = BDIGITS(x);
    sign = RBIGNUM(y)->sign;
    }
    else {
    l1 = RBIGNUM(x)->len;
    l2 = RBIGNUM(y)->len;
    ds1 = BDIGITS(x);
    ds2 = BDIGITS(y);
    sign = RBIGNUM(x)->sign;
    }
    z = bignew(l2, RBIGNUM(x)->sign || RBIGNUM(y)->sign);
    zds = BDIGITS(z);

    for (i=0; i<l1; i++) {
    zds[i] = ds1[i] & ds2[i];
    }
    for (; i<l2; i++) {
    zds[i] = sign?0:ds2[i];
    }
    if (!RBIGNUM(z)->sign) get2comp(z);
    return bignorm(z);
}

#*(other) ⇒ Numeric

Multiplies big and other, returning the result.

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big * other  => Numeric
 *
 *  Multiplies big and other, returning the result.
 */

VALUE
rb_big_mul(x, y)
    VALUE x, y;
{
    return bignorm(rb_big_mul0(x, y));
}

#**(exponent# = > numeric)) ⇒ Object

Raises big to the exponent power (which may be an integer, float, or anything that will coerce to a number). The result may be a Fixnum, Bignum, or Float

123456789 ** 2      #=> 15241578750190521
123456789 ** 1.2    #=> 5126464716.09932
123456789 ** -2     #=> 6.5610001194102e-17


# File 'bignum.c'

/*
 *  call-seq:
 *     big ** exponent   #=> numeric
 *
 *  Raises _big_ to the _exponent_ power (which may be an integer, float,
 *  or anything that will coerce to a number). The result may be
 *  a Fixnum, Bignum, or Float
 *
 *    123456789 ** 2      #=> 15241578750190521
 *    123456789 ** 1.2    #=> 5126464716.09932
 *    123456789 ** -2     #=> 6.5610001194102e-17
 */

VALUE
rb_big_pow(x, y)
    VALUE x, y;
{
    double d;
    long yy;

    if (y == INT2FIX(0)) return INT2FIX(1);
    switch (TYPE(y)) {
      case T_FLOAT:
    d = RFLOAT(y)->value;
    break;

      case T_BIGNUM:
    rb_warn("in a**b, b may be too big");
    d = rb_big2dbl(y);
    break;

      case T_FIXNUM:
    yy = FIX2LONG(y);
    if (yy > 0) {
        VALUE z = 0;
        long mask;
        const long BIGLEN_LIMIT = 1024*1024 / SIZEOF_BDIGITS;

        if ((RBIGNUM(x)->len > BIGLEN_LIMIT) ||
        (RBIGNUM(x)->len > BIGLEN_LIMIT / yy)) {
        rb_warn("in a**b, b may be too big");
        d = (double)yy;
        break;
        }
        for (mask = FIXNUM_MAX + 1; mask; mask >>= 1) {
        if (z) z = bigtrunc(bigsqr(z));
        if (yy & mask) {
            z = z ? bigtrunc(rb_big_mul0(z, x)) : x;
        }
        }
        return bignorm(z);
    }
    d = (double)yy;
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    return rb_float_new(pow(rb_big2dbl(x), d));
}

#+(other) ⇒ Numeric

Adds big and other, returning the result.

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big + other  => Numeric
 *
 *  Adds big and other, returning the result.
 */

VALUE
rb_big_plus(x, y)
    VALUE x, y;
{
    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    /* fall through */
      case T_BIGNUM:
    return bignorm(bigadd(x, y, 1));

      case T_FLOAT:
    return rb_float_new(rb_big2dbl(x) + RFLOAT(y)->value);

      default:
    return rb_num_coerce_bin(x, y);
    }
}

#-(other) ⇒ Numeric

Subtracts other from big, returning the result.

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big - other  => Numeric
 *
 *  Subtracts other from big, returning the result.
 */

VALUE
rb_big_minus(x, y)
    VALUE x, y;
{
    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    /* fall through */
      case T_BIGNUM:
    return bignorm(bigadd(x, y, 0));

      case T_FLOAT:
    return rb_float_new(rb_big2dbl(x) - RFLOAT(y)->value);

      default:
    return rb_num_coerce_bin(x, y);
    }
}

#-Object

Unary minus (returns a new Bignum whose value is 0-big)



# File 'bignum.c'

/*
 * call-seq:
 *    -big   =>  other_big
 *
 * Unary minus (returns a new Bignum whose value is 0-big)
 */

static VALUE
rb_big_uminus(x)
    VALUE x;
{
    VALUE z = rb_big_clone(x);

    RBIGNUM(z)->sign = !RBIGNUM(x)->sign;

    return bignorm(z);
}

#/(other) ⇒ Numeric #div(other) ⇒ Numeric

Divides big by other, returning the result.

Overloads:



# File 'bignum.c'

/*
 *  call-seq:
 *     big / other     => Numeric
 *     big.div(other)  => Numeric
 *
 *  Divides big by other, returning the result.
 */

static VALUE
rb_big_div(x, y)
    VALUE x, y;
{
    VALUE z;

    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    break;

      case T_BIGNUM:
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    bigdivmod(x, y, &z, 0);

    return bignorm(z);
}

#<<(numeric) ⇒ Integer

Shifts big left numeric positions (right if numeric is negative).

Returns:



# File 'bignum.c'

/*
 * call-seq:
 *     big << numeric   =>  integer
 *
 * Shifts big left _numeric_ positions (right if _numeric_ is negative).
 */

VALUE
rb_big_lshift(x, y)
    VALUE x, y;
{
    long shift;
    int neg = 0;

    for (;;) {
    if (FIXNUM_P(y)) {
        shift = FIX2LONG(y);
        if (shift < 0) {
        neg = 1;
        shift = -shift;
        }
        break;
    }
    else if (TYPE(y) == T_BIGNUM) {
        if (!RBIGNUM(y)->sign) {
        VALUE t = check_shiftdown(y, x);
        if (!NIL_P(t)) return t;
        neg = 1;
        }
        shift = big2ulong(y, "long");
        break;
    }
    y = rb_to_int(y);
    }

    x = neg ? big_rshift(x, shift) : big_lshift(x, shift);
    return bignorm(x);
}

#<=>(numeric) ⇒ -1, ...

Comparison---Returns -1, 0, or +1 depending on whether big is less than, equal to, or greater than numeric. This is the basis for the tests in Comparable.

Returns:

  • (-1, 0, +1)


# File 'bignum.c'

/*
 *  call-seq:
 *     big <=> numeric   => -1, 0, +1
 *
 *  Comparison---Returns -1, 0, or +1 depending on whether <i>big</i> is
 *  less than, equal to, or greater than <i>numeric</i>. This is the
 *  basis for the tests in <code>Comparable</code>.
 *
 */

static VALUE
rb_big_cmp(x, y)
    VALUE x, y;
{
    long xlen = RBIGNUM(x)->len;

    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    break;

      case T_BIGNUM:
    break;

      case T_FLOAT:
    return rb_dbl_cmp(rb_big2dbl(x), RFLOAT(y)->value);

      default:
    return rb_num_coerce_cmp(x, y);
    }

    if (RBIGNUM(x)->sign > RBIGNUM(y)->sign) return INT2FIX(1);
    if (RBIGNUM(x)->sign < RBIGNUM(y)->sign) return INT2FIX(-1);
    if (xlen < RBIGNUM(y)->len)
    return (RBIGNUM(x)->sign) ? INT2FIX(-1) : INT2FIX(1);
    if (xlen > RBIGNUM(y)->len)
    return (RBIGNUM(x)->sign) ? INT2FIX(1) : INT2FIX(-1);

    while(xlen-- && (BDIGITS(x)[xlen]==BDIGITS(y)[xlen]));
    if (-1 == xlen) return INT2FIX(0);
    return (BDIGITS(x)[xlen] > BDIGITS(y)[xlen]) ?
    (RBIGNUM(x)->sign ? INT2FIX(1) : INT2FIX(-1)) :
        (RBIGNUM(x)->sign ? INT2FIX(-1) : INT2FIX(1));
}

#==(obj) ⇒ Boolean

Returns true only if obj has the same value as big. Contrast this with Bignum#eql?, which requires obj to be a Bignum.

68719476736 == 68719476736.0   #=> true

Returns:

  • (Boolean)


# File 'bignum.c'

/*
 *  call-seq:
 *     big == obj  => true or false
 *
 *  Returns <code>true</code> only if <i>obj</i> has the same value
 *  as <i>big</i>. Contrast this with <code>Bignum#eql?</code>, which
 *  requires <i>obj</i> to be a <code>Bignum</code>.
 *
 *     68719476736 == 68719476736.0   #=> true
 */

static VALUE
rb_big_eq(x, y)
    VALUE x, y;
{
    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    break;
      case T_BIGNUM:
    break;
      case T_FLOAT:
        {
        volatile double a, b;

        a = RFLOAT(y)->value;
        if (isnan(a)) return Qfalse;
        b = rb_big2dbl(x);
        return (a == b)?Qtrue:Qfalse;
    }
      default:
    return rb_equal(y, x);
    }
    if (RBIGNUM(x)->sign != RBIGNUM(y)->sign) return Qfalse;
    if (RBIGNUM(x)->len != RBIGNUM(y)->len) return Qfalse;
    if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,RBIGNUM(y)->len) != 0) return Qfalse;
    return Qtrue;
}

#>>(numeric) ⇒ Integer

Shifts big right numeric positions (left if numeric is negative).

Returns:



# File 'bignum.c'

/*
 * call-seq:
 *     big >> numeric   =>  integer
 *
 * Shifts big right _numeric_ positions (left if _numeric_ is negative).
 */

VALUE
rb_big_rshift(x, y)
    VALUE x, y;
{
    long shift;
    int neg = 0;

    for (;;) {
    if (FIXNUM_P(y)) {
        shift = FIX2LONG(y);
        if (shift < 0) {
        neg = 1;
        shift = -shift;
        }
        break;
    }
    else if (TYPE(y) == T_BIGNUM) {
        if (RBIGNUM(y)->sign) {
        VALUE t = check_shiftdown(y, x);
        if (!NIL_P(t)) return t;
        }
        else {
        neg = 1;
        }
        shift = big2ulong(y, "long");
        break;
    }
    y = rb_to_int(y);
    }

    x = neg ? big_lshift(x, shift) : big_rshift(x, shift);
    return bignorm(x);
}

#[](n) ⇒ 0, 1

Bit Reference---Returns the nth bit in the (assumed) binary representation of big, where big[0] is the least significant bit.

a = 9**15
50.downto(0) do |n|
  print a[n]
end

produces:

000101110110100000111000011110010100111100010111001

Returns:

  • (0, 1)


# File 'bignum.c'

/*
 *  call-seq:
 *     big[n] -> 0, 1
 *
 *  Bit Reference---Returns the <em>n</em>th bit in the (assumed) binary
 *  representation of <i>big</i>, where <i>big</i>[0] is the least
 *  significant bit.
 *
 *     a = 9**15
 *     50.downto(0) do |n|
 *       print a[n]
 *     end
 *
 *  <em>produces:</em>
 *
 *     000101110110100000111000011110010100111100010111001
 *
 */

static VALUE
rb_big_aref(x, y)
    VALUE x, y;
{
    BDIGIT *xds;
    BDIGIT_DBL num;
    unsigned long shift;
    long i, s1, s2;

    if (TYPE(y) == T_BIGNUM) {
    if (!RBIGNUM(y)->sign)
        return INT2FIX(0);
    if (RBIGNUM(bigtrunc(y))->len > SIZEOF_LONG/SIZEOF_BDIGITS) {
      out_of_range:
        return RBIGNUM(x)->sign ? INT2FIX(0) : INT2FIX(1);
    }
    shift = big2ulong(y, "long");
    }
    else {
    i = NUM2LONG(y);
    if (i < 0) return INT2FIX(0);
    shift = (VALUE)i;
    }
    s1 = shift/BITSPERDIG;
    s2 = shift%BITSPERDIG;

    if (s1 >= RBIGNUM(x)->len) goto out_of_range;
    if (!RBIGNUM(x)->sign) {
    xds = BDIGITS(x);
    i = 0; num = 1;
    while (num += ~xds[i], ++i <= s1) {
        num = BIGDN(num);
    }
    }
    else {
    num = BDIGITS(x)[s1];
    }
    if (num & ((BDIGIT_DBL)1<<s2))
    return INT2FIX(1);
    return INT2FIX(0);
}

#^(numeric) ⇒ Integer

Performs bitwise exclusive or between big and numeric.

Returns:



# File 'bignum.c'

/*
 * call-seq:
 *     big ^ numeric   =>  integer
 *
 * Performs bitwise +exclusive or+ between _big_ and _numeric_.
 */

VALUE
rb_big_xor(xx, yy)
    VALUE xx, yy;
{
    volatile VALUE x, y;
    VALUE z;
    BDIGIT *ds1, *ds2, *zds;
    long i, l1, l2;
    char sign;

    x = xx;
    y = rb_to_int(yy);
    if (FIXNUM_P(y)) {
    y = rb_int2big(FIX2LONG(y));
    }
    if (!RBIGNUM(y)->sign) {
    y = rb_big_clone(y);
    get2comp(y);
    }
    if (!RBIGNUM(x)->sign) {
    x = rb_big_clone(x);
    get2comp(x);
    }
    if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
    l1 = RBIGNUM(y)->len;
    l2 = RBIGNUM(x)->len;
    ds1 = BDIGITS(y);
    ds2 = BDIGITS(x);
    sign = RBIGNUM(y)->sign;
    }
    else {
    l1 = RBIGNUM(x)->len;
    l2 = RBIGNUM(y)->len;
    ds1 = BDIGITS(x);
    ds2 = BDIGITS(y);
    sign = RBIGNUM(x)->sign;
    }
    RBIGNUM(x)->sign = RBIGNUM(x)->sign?1:0;
    RBIGNUM(y)->sign = RBIGNUM(y)->sign?1:0;
    z = bignew(l2, !(RBIGNUM(x)->sign ^ RBIGNUM(y)->sign));
    zds = BDIGITS(z);

    for (i=0; i<l1; i++) {
    zds[i] = ds1[i] ^ ds2[i];
    }
    for (; i<l2; i++) {
    zds[i] = sign?ds2[i]:~ds2[i];
    }
    if (!RBIGNUM(z)->sign) get2comp(z);

    return bignorm(z);
}

#absBignum

Returns the absolute value of big.

-1234567890987654321.abs   #=> 1234567890987654321

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big.abs -> aBignum
 *
 *  Returns the absolute value of <i>big</i>.
 *
 *     -1234567890987654321.abs   #=> 1234567890987654321
 */

static VALUE
rb_big_abs(x)
    VALUE x;
{
    if (!RBIGNUM(x)->sign) {
    x = rb_big_clone(x);
    RBIGNUM(x)->sign = 1;
    }
    return x;
}

#coerceObject

MISSING: documentation



# File 'bignum.c'

/*
 * MISSING: documentation
 */

static VALUE
rb_big_coerce(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
    return rb_assoc_new(rb_int2big(FIX2LONG(y)), x);
    }
    else if (TYPE(y) == T_BIGNUM) {
       return rb_assoc_new(y, x);
    }
    else {
    rb_raise(rb_eTypeError, "can't coerce %s to Bignum",
         rb_obj_classname(y));
    }
    /* not reached */
    return Qnil;
}

#/(other) ⇒ Numeric #div(other) ⇒ Numeric

Divides big by other, returning the result.

Overloads:



# File 'bignum.c'

/*
 *  call-seq:
 *     big / other     => Numeric
 *     big.div(other)  => Numeric
 *
 *  Divides big by other, returning the result.
 */

static VALUE
rb_big_div(x, y)
    VALUE x, y;
{
    VALUE z;

    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    break;

      case T_BIGNUM:
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    bigdivmod(x, y, &z, 0);

    return bignorm(z);
}

#divmod(numeric) ⇒ Array

See Numeric#divmod.

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big.divmod(numeric)   => array
 *
 *  See <code>Numeric#divmod</code>.
 *
 */
VALUE
rb_big_divmod(x, y)
    VALUE x, y;
{
    VALUE div, mod;

    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    break;

      case T_BIGNUM:
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    bigdivmod(x, y, &div, &mod);

    return rb_assoc_new(bignorm(div), bignorm(mod));
}

#eql?(obj) ⇒ Boolean

Returns true only if obj is a Bignum with the same value as big. Contrast this with Bignum#==, which performs type conversions.

68719476736.eql?(68719476736.0)   #=> false

Returns:

  • (Boolean)


# File 'bignum.c'

/*
 *  call-seq:
 *     big.eql?(obj)   => true or false
 *
 *  Returns <code>true</code> only if <i>obj</i> is a
 *  <code>Bignum</code> with the same value as <i>big</i>. Contrast this
 *  with <code>Bignum#==</code>, which performs type conversions.
 *
 *     68719476736.eql?(68719476736.0)   #=> false
 */

static VALUE
rb_big_eql(x, y)
    VALUE x, y;
{
    if (TYPE(y) != T_BIGNUM) return Qfalse;
    if (RBIGNUM(x)->sign != RBIGNUM(y)->sign) return Qfalse;
    if (RBIGNUM(x)->len != RBIGNUM(y)->len) return Qfalse;
    if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,RBIGNUM(y)->len) != 0) return Qfalse;
    return Qtrue;
}

#quo(numeric) ⇒ Float #fdiv(numeric) ⇒ Float

Returns the floating point result of dividing big by numeric.

-1234567890987654321.quo(13731)      #=> -89910996357705.5
-1234567890987654321.quo(13731.24)   #=> -89909424858035.7

Overloads:



# File 'bignum.c'

/*
 *  call-seq:
 *     big.quo(numeric) -> float
 *     big.fdiv(numeric) -> float
 *
 *  Returns the floating point result of dividing <i>big</i> by
 *  <i>numeric</i>.
 *
 *     -1234567890987654321.quo(13731)      #=> -89910996357705.5
 *     -1234567890987654321.quo(13731.24)   #=> -89909424858035.7
 *
 */

static VALUE
rb_big_quo(x, y)
    VALUE x, y;
{
    double dx = big2dbl(x);
    double dy;

    if (isinf(dx)) {
#define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG)
    VALUE z;
    int ex, ey;

    ex = (RBIGNUM(bigtrunc(x))->len - 1) * BITSPERDIG;
    ex += bdigbitsize(BDIGITS(x)[RBIGNUM(x)->len - 1]);
    ex -= 2 * DBL_BIGDIG * BITSPERDIG;
    if (ex) x = big_shift(x, ex);

    switch (TYPE(y)) {
      case T_FIXNUM:
        y = rb_int2big(FIX2LONG(y));
      case T_BIGNUM: {
        ey = (RBIGNUM(bigtrunc(y))->len - 1) * BITSPERDIG;
        ey += bdigbitsize(BDIGITS(y)[RBIGNUM(y)->len - 1]);
        ey -= DBL_BIGDIG * BITSPERDIG;
        if (ey) y = big_shift(y, ey);
      bignum:
        bigdivrem(x, y, &z, 0);
        return rb_float_new(ldexp(big2dbl(z), ex - ey));
      }
      case T_FLOAT:
        y = dbl2big(ldexp(frexp(RFLOAT(y)->value, &ey), DBL_MANT_DIG));
        ey -= DBL_MANT_DIG;
        goto bignum;
    }
    }
    switch (TYPE(y)) {
      case T_FIXNUM:
    dy = (double)FIX2LONG(y);
    break;

      case T_BIGNUM:
    dy = rb_big2dbl(y);
    break;

      case T_FLOAT:
    dy = RFLOAT(y)->value;
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    return rb_float_new(dx / dy);
}

#hashFixnum

Compute a hash based on the value of big.

Returns:



# File 'bignum.c'

/*
 * call-seq:
 *   big.hash   => fixnum
 *
 * Compute a hash based on the value of _big_.
 */

static VALUE
rb_big_hash(x)
    VALUE x;
{
    long i, len, key;
    BDIGIT *digits;

    key = 0; digits = BDIGITS(x); len = RBIGNUM(x)->len;
    for (i=0; i<len; i++) {
    key ^= *digits++;
    }
    return LONG2FIX(key);
}

#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric

Returns big modulo other. See Numeric.divmod for more information.

Overloads:



# File 'bignum.c'

/*
 *  call-seq:
 *     big % other         => Numeric
 *     big.modulo(other)   => Numeric
 *
 *  Returns big modulo other. See Numeric.divmod for more
 *  information.
 */

static VALUE
rb_big_modulo(x, y)
    VALUE x, y;
{
    VALUE z;

    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    break;

      case T_BIGNUM:
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    bigdivmod(x, y, 0, &z);

    return bignorm(z);
}

#quo(numeric) ⇒ Float #fdiv(numeric) ⇒ Float

Returns the floating point result of dividing big by numeric.

-1234567890987654321.quo(13731)      #=> -89910996357705.5
-1234567890987654321.quo(13731.24)   #=> -89909424858035.7

Overloads:



# File 'bignum.c'

/*
 *  call-seq:
 *     big.quo(numeric) -> float
 *     big.fdiv(numeric) -> float
 *
 *  Returns the floating point result of dividing <i>big</i> by
 *  <i>numeric</i>.
 *
 *     -1234567890987654321.quo(13731)      #=> -89910996357705.5
 *     -1234567890987654321.quo(13731.24)   #=> -89909424858035.7
 *
 */

static VALUE
rb_big_quo(x, y)
    VALUE x, y;
{
    double dx = big2dbl(x);
    double dy;

    if (isinf(dx)) {
#define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG)
    VALUE z;
    int ex, ey;

    ex = (RBIGNUM(bigtrunc(x))->len - 1) * BITSPERDIG;
    ex += bdigbitsize(BDIGITS(x)[RBIGNUM(x)->len - 1]);
    ex -= 2 * DBL_BIGDIG * BITSPERDIG;
    if (ex) x = big_shift(x, ex);

    switch (TYPE(y)) {
      case T_FIXNUM:
        y = rb_int2big(FIX2LONG(y));
      case T_BIGNUM: {
        ey = (RBIGNUM(bigtrunc(y))->len - 1) * BITSPERDIG;
        ey += bdigbitsize(BDIGITS(y)[RBIGNUM(y)->len - 1]);
        ey -= DBL_BIGDIG * BITSPERDIG;
        if (ey) y = big_shift(y, ey);
      bignum:
        bigdivrem(x, y, &z, 0);
        return rb_float_new(ldexp(big2dbl(z), ex - ey));
      }
      case T_FLOAT:
        y = dbl2big(ldexp(frexp(RFLOAT(y)->value, &ey), DBL_MANT_DIG));
        ey -= DBL_MANT_DIG;
        goto bignum;
    }
    }
    switch (TYPE(y)) {
      case T_FIXNUM:
    dy = (double)FIX2LONG(y);
    break;

      case T_BIGNUM:
    dy = rb_big2dbl(y);
    break;

      case T_FLOAT:
    dy = RFLOAT(y)->value;
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    return rb_float_new(dx / dy);
}

#remainder(numeric) ⇒ Numeric

Returns the remainder after dividing big by numeric.

-1234567890987654321.remainder(13731)      #=> -6966
-1234567890987654321.remainder(13731.24)   #=> -9906.22531493148

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big.remainder(numeric)    => number
 *
 *  Returns the remainder after dividing <i>big</i> by <i>numeric</i>.
 *
 *     -1234567890987654321.remainder(13731)      #=> -6966
 *     -1234567890987654321.remainder(13731.24)   #=> -9906.22531493148
 */
static VALUE
rb_big_remainder(x, y)
    VALUE x, y;
{
    VALUE z;

    switch (TYPE(y)) {
      case T_FIXNUM:
    y = rb_int2big(FIX2LONG(y));
    break;

      case T_BIGNUM:
    break;

      default:
    return rb_num_coerce_bin(x, y);
    }
    bigdivrem(x, y, 0, &z);

    return bignorm(z);
}

#sizeInteger

Returns the number of bytes in the machine representation of big.

(256**10 - 1).size   #=> 12
(256**20 - 1).size   #=> 20
(256**40 - 1).size   #=> 40

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big.size -> integer
 *
 *  Returns the number of bytes in the machine representation of
 *  <i>big</i>.
 *
 *     (256**10 - 1).size   #=> 12
 *     (256**20 - 1).size   #=> 20
 *     (256**40 - 1).size   #=> 40
 */

static VALUE
rb_big_size(big)
    VALUE big;
{
    return LONG2FIX(RBIGNUM(big)->len*SIZEOF_BDIGITS);
}

#to_fFloat

Converts big to a Float. If big doesn't fit in a Float, the result is infinity.

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big.to_f -> float
 *
 *  Converts <i>big</i> to a <code>Float</code>. If <i>big</i> doesn't
 *  fit in a <code>Float</code>, the result is infinity.
 *
 */

static VALUE
rb_big_to_f(x)
    VALUE x;
{
    return rb_float_new(rb_big2dbl(x));
}

#to_s(base = 10) ⇒ String

Returns a string containing the representation of big radix base (2 through 36).

12345654321.to_s         #=> "12345654321"
12345654321.to_s(2)      #=> "1011011111110110111011110000110001"
12345654321.to_s(8)      #=> "133766736061"
12345654321.to_s(16)     #=> "2dfdbbc31"
78546939656932.to_s(36)  #=> "rubyrules"

Returns:



# File 'bignum.c'

/*
 *  call-seq:
 *     big.to_s(base=10)   =>  string
 *
 *  Returns a string containing the representation of <i>big</i> radix
 *  <i>base</i> (2 through 36).
 *
 *     12345654321.to_s         #=> "12345654321"
 *     12345654321.to_s(2)      #=> "1011011111110110111011110000110001"
 *     12345654321.to_s(8)      #=> "133766736061"
 *     12345654321.to_s(16)     #=> "2dfdbbc31"
 *     78546939656932.to_s(36)  #=> "rubyrules"
 */

static VALUE
rb_big_to_s(argc, argv, x)
    int argc;
    VALUE *argv;
    VALUE x;
{
    VALUE b;
    int base;

    rb_scan_args(argc, argv, "01", &b);
    if (argc == 0) base = 10;
    else base = NUM2INT(b);
    return rb_big2str(x, base);
}

#|(numeric) ⇒ Integer

Performs bitwise or between big and numeric.

Returns:



# File 'bignum.c'

/*
 * call-seq:
 *     big | numeric   =>  integer
 *
 * Performs bitwise +or+ between _big_ and _numeric_.
 */

VALUE
rb_big_or(xx, yy)
    VALUE xx, yy;
{
    volatile VALUE x, y, z;
    BDIGIT *ds1, *ds2, *zds;
    long i, l1, l2;
    char sign;

    x = xx;
    y = rb_to_int(yy);
    if (FIXNUM_P(y)) {
    y = rb_int2big(FIX2LONG(y));
    }
    if (!RBIGNUM(y)->sign) {
    y = rb_big_clone(y);
    get2comp(y);
    }
    if (!RBIGNUM(x)->sign) {
    x = rb_big_clone(x);
    get2comp(x);
    }
    if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
    l1 = RBIGNUM(y)->len;
    l2 = RBIGNUM(x)->len;
    ds1 = BDIGITS(y);
    ds2 = BDIGITS(x);
    sign = RBIGNUM(y)->sign;
    }
    else {
    l1 = RBIGNUM(x)->len;
    l2 = RBIGNUM(y)->len;
    ds1 = BDIGITS(x);
    ds2 = BDIGITS(y);
    sign = RBIGNUM(x)->sign;
    }
    z = bignew(l2, RBIGNUM(x)->sign && RBIGNUM(y)->sign);
    zds = BDIGITS(z);

    for (i=0; i<l1; i++) {
    zds[i] = ds1[i] | ds2[i];
    }
    for (; i<l2; i++) {
    zds[i] = sign?ds2[i]:(BIGRAD-1);
    }
    if (!RBIGNUM(z)->sign) get2comp(z);

    return bignorm(z);
}

#~Integer

Inverts the bits in big. As Bignums are conceptually infinite length, the result acts as if it had an infinite number of one bits to the left. In hex representations, this is displayed as two periods to the left of the digits.

sprintf("%X", ~0x1122334455)    #=> "..FEEDDCCBBAA"

Returns:



# File 'bignum.c'

/*
 * call-seq:
 *     ~big  =>  integer
 *
 * Inverts the bits in big. As Bignums are conceptually infinite
 * length, the result acts as if it had an infinite number of one
 * bits to the left. In hex representations, this is displayed
 * as two periods to the left of the digits.
 *
 *   sprintf("%X", ~0x1122334455)    #=> "..FEEDDCCBBAA"
 */

static VALUE
rb_big_neg(x)
    VALUE x;
{
    VALUE z = rb_big_clone(x);
    long i;
    BDIGIT *ds;

    if (!RBIGNUM(x)->sign) get2comp(z);
    ds = BDIGITS(z);
    i = RBIGNUM(x)->len;
    if (!i) return INT2FIX(~0);
    while (i--) ds[i] = ~ds[i];
    RBIGNUM(z)->sign = !RBIGNUM(z)->sign;
    if (RBIGNUM(x)->sign) get2comp(z);

    return bignorm(z);
}