Class

Bignum

Inheritance
< Integer

Bignum objects hold integers outside the range of Fixnum. Bignum objects are created automatically when integer calculations would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.

For the purposes of the bitwise operations and [], a Bignum is treated as if it were an infinite-length bitstring with 2‘s complement representation.

While Fixnum values are immediate, Bignum objects are not—assignment and parameter passing work with references to objects, not the objects themselves.

Methods

Instance

Visibility Signature
public % (p1)
public & (p1)
public * (p1)
public ** (other)
public ** (p1)
public + (p1)
public - (p1)
public -@ ()
public / (p1)
public / (p1)
public << (p1)
public <=> (p1)
public == (p1)
public >> (p1)
public [] (p1)
public ^ (p1)
public abs ()
public coerce (p1)
public div (p1)
public divmod (p1)
public eql? (p1)
public fdiv (p1)
public hash ()
public modulo (p1)
public power! (p1)
public quo (other)
public quo (p1)
public rdiv (p1)
public remainder (p1)
public rpower (other)
public size ()
public to_f ()
public to_s (...)
public | (p1)
public ~ ()

Instance Method Detail

big % other => Numeric
big.modulo(other) => Numeric

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

big & numeric => integer

Performs bitwise and between big and numeric.

big * other => Numeric

Multiplies big and other, returning the result.

**(other)

Alias for rpower

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

big + other => Numeric

Adds big and other, returning the result.

big - other => Numeric

Subtracts other from big, returning the result.

-big => other_big

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

big / other => Numeric
big.div(other) => Numeric

Divides big by other, returning the result.

/(p1)

Alias for quo

big << numeric => integer

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

big <=> numeric => -1, 0, +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.

big == obj => true or false

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

big >> numeric => integer

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

big[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

big ^ numeric => integer

Performs bitwise +exclusive or+ between big and numeric.

big.abs → aBignum

Returns the absolute value of big.

   -1234567890987654321.abs   #=> 1234567890987654321

coerce(p1)

MISSING: documentation

big / other => Numeric
big.div(other) => Numeric

Divides big by other, returning the result.

big.divmod(numeric) => array

big.eql?(obj) => true or false

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

big.quo(numeric) → float
big.fdiv(numeric) → float

Returns the floating point result of dividing big by numeric.

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

big.hash => fixnum

Compute a hash based on the value of big.

big % other => Numeric
big.modulo(other) => Numeric

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

power!(p1)

Alias for #**

quo(other)

If Rational is defined, returns a Rational number instead of a Float.

big.quo(numeric) → float
big.fdiv(numeric) → float

Returns the floating point result of dividing big by numeric.

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

rdiv(p1)

Alias for quo

big.remainder(numeric) => number

Returns the remainder after dividing big by numeric.

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

rpower(other)

Returns a Rational number if the result is in fact rational (i.e. other < 0).

big.size → integer

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

big.to_f → float

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

big.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"

big | numeric => integer

Performs bitwise or between big and numeric.

~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"