Class: Bignum
Overview
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.
Instance Method Summary collapse
-
#% ⇒ Object
Returns big modulo other.
-
#&(numeric) ⇒ Integer
Performs bitwise
and
between big and numeric. -
#*(other) ⇒ Numeric
Multiplies big and other, returning the result.
-
#**(exponent# = > numeric)) ⇒ Object
Raises big to the exponent power (which may be an integer, float, or anything that will coerce to a number).
-
#+(other) ⇒ Numeric
Adds big and other, returning the result.
-
#-(other) ⇒ Numeric
Subtracts other from big, returning the result.
-
#- ⇒ Object
Unary minus (returns a new Bignum whose value is 0-big).
-
#/ ⇒ Object
Divides big by other, returning the result.
-
#<<(numeric) ⇒ Integer
Shifts big left numeric positions (right if numeric is negative).
-
#<=>(numeric) ⇒ -1, ...
Comparison—Returns -1, 0, or +1 depending on whether big is less than, equal to, or greater than numeric.
-
#==(obj) ⇒ Boolean
Returns
true
only if obj has the same value as big. -
#>>(numeric) ⇒ Integer
Shifts big right numeric positions (left if numeric is negative).
-
#[](n) ⇒ 0, 1
Bit Reference—Returns the nth bit in the (assumed) binary representation of big, where big[0] is the least significant bit.
-
#^(numeric) ⇒ Integer
Performs bitwise exclusive or between big and numeric.
-
#abs ⇒ Bignum
Returns the absolute value of big.
-
#coerce ⇒ Object
MISSING: documentation.
-
#div ⇒ Object
Divides big by other, returning the result.
-
#divmod(numeric) ⇒ Array
See
Numeric#divmod
. -
#eql?(obj) ⇒ Boolean
Returns
true
only if obj is aBignum
with the same value as big. -
#hash ⇒ Fixnum
Compute a hash based on the value of big.
-
#modulo ⇒ Object
Returns big modulo other.
-
#quo(numeric) ⇒ Float
Returns the floating point result of dividing big by numeric.
-
#remainder(numeric) ⇒ Numeric
Returns the remainder after dividing big by numeric.
-
#size ⇒ Integer
Returns the number of bytes in the machine representation of big.
-
#to_f ⇒ Float
Converts big to a
Float
. -
#to_s(base = 10) ⇒ String
Returns a string containing the representation of big radix base (2 through 36).
-
#|(numeric) ⇒ Integer
Performs bitwise
or
between big and numeric. -
#~ ⇒ Integer
Inverts the bits in big.
Methods inherited from Integer
#ceil, #chr, #downto, #floor, induced_from, #integer?, #next, #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
Instance Method Details
#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric
Returns big modulo other. See Numeric.divmod for more information.
1477 1478 1479 |
# File 'bignum.c', line 1477 static VALUE rb_big_modulo(x, y) VALUE x, y; |
#&(numeric) ⇒ Integer
Performs bitwise and
between big and numeric.
1667 1668 1669 |
# File 'bignum.c', line 1667 VALUE rb_big_and(xx, yy) VALUE xx, yy; |
#*(other) ⇒ Numeric
Multiplies big and other, returning the result.
1280 1281 1282 |
# File 'bignum.c', line 1280 VALUE rb_big_mul(x, y) VALUE 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
1610 1611 1612 |
# File 'bignum.c', line 1610 VALUE rb_big_pow(x, y) VALUE x, y; |
#+(other) ⇒ Numeric
Adds big and other, returning the result.
1181 1182 1183 |
# File 'bignum.c', line 1181 VALUE rb_big_plus(x, y) VALUE x, y; |
#-(other) ⇒ Numeric
Subtracts other from big, returning the result.
1207 1208 1209 |
# File 'bignum.c', line 1207 VALUE rb_big_minus(x, y) VALUE x, y; |
#- ⇒ Object
Unary minus (returns a new Bignum whose value is 0-big)
1041 1042 1043 |
# File 'bignum.c', line 1041 static VALUE rb_big_uminus(x) VALUE x; |
#/(other) ⇒ Numeric #div(other) ⇒ Numeric
Divides big by other, returning the result.
1443 1444 1445 |
# File 'bignum.c', line 1443 static VALUE rb_big_div(x, y) VALUE x, y; |
#<<(numeric) ⇒ Integer
Shifts big left numeric positions (right if numeric is negative).
1842 1843 1844 |
# File 'bignum.c', line 1842 VALUE rb_big_lshift(x, y) VALUE x, y; |
#<=>(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
.
938 939 940 |
# File 'bignum.c', line 938 static VALUE rb_big_cmp(x, y) VALUE x, y; |
#==(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
984 985 986 |
# File 'bignum.c', line 984 static VALUE rb_big_eq(x, y) VALUE x, y; |
#>>(numeric) ⇒ Integer
Shifts big right numeric positions (left if numeric is negative).
1878 1879 1880 |
# File 'bignum.c', line 1878 static VALUE rb_big_rshift(x, y) VALUE x, y; |
#[](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
1944 1945 1946 |
# File 'bignum.c', line 1944 static VALUE rb_big_aref(x, y) VALUE x, y; |
#^(numeric) ⇒ Integer
Performs bitwise exclusive or between big and numeric.
1780 1781 1782 |
# File 'bignum.c', line 1780 VALUE rb_big_xor(xx, yy) VALUE xx, yy; |
#abs ⇒ Bignum
Returns the absolute value of big.
-1234567890987654321.abs #=> 1234567890987654321
2028 2029 2030 |
# File 'bignum.c', line 2028 static VALUE rb_big_abs(x) VALUE x; |
#coerce ⇒ Object
MISSING: documentation
2001 2002 2003 |
# File 'bignum.c', line 2001 static VALUE rb_big_coerce(x, y) VALUE x, y; |
#/(other) ⇒ Numeric #div(other) ⇒ Numeric
Divides big by other, returning the result.
1443 1444 1445 |
# File 'bignum.c', line 1443 static VALUE rb_big_div(x, y) VALUE x, y; |
#divmod(numeric) ⇒ Array
See Numeric#divmod
.
1537 1538 1539 |
# File 'bignum.c', line 1537 VALUE rb_big_divmod(x, y) VALUE x, y; |
#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
1023 1024 1025 |
# File 'bignum.c', line 1023 static VALUE rb_big_eql(x, y) VALUE x, y; |
#hash ⇒ Fixnum
Compute a hash based on the value of big.
1983 1984 1985 |
# File 'bignum.c', line 1983 static VALUE rb_big_hash(x) VALUE x; |
#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric
Returns big modulo other. See Numeric.divmod for more information.
1477 1478 1479 |
# File 'bignum.c', line 1477 static VALUE rb_big_modulo(x, y) VALUE x, y; |
#quo(numeric) ⇒ Float
Returns the floating point result of dividing big by numeric.
-1234567890987654321.quo(13731) #=> -89910996357705.5
-1234567890987654321.quo(13731.24) #=> -89909424858035.7
1571 1572 1573 |
# File 'bignum.c', line 1571 static VALUE rb_big_quo(x, y) VALUE x, y; |
#remainder(numeric) ⇒ Numeric
Returns the remainder after dividing big by numeric.
-1234567890987654321.remainder(13731) #=> -6966
-1234567890987654321.remainder(13731.24) #=> -9906.22531493148
1508 1509 1510 |
# File 'bignum.c', line 1508 static VALUE rb_big_remainder(x, y) VALUE x, y; |
#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
2072 2073 2074 |
# File 'bignum.c', line 2072 static VALUE rb_big_size(big) VALUE big; |
#to_f ⇒ Float
Converts big to a Float
. If big doesn’t fit in a Float
, the result is infinity.
921 922 923 |
# File 'bignum.c', line 921 static VALUE rb_big_to_f(x) VALUE 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"
731 732 733 |
# File 'bignum.c', line 731 static VALUE rb_big_to_s(argc, argv, x) int argc; |
#|(numeric) ⇒ Integer
Performs bitwise or
between big and numeric.
1723 1724 1725 |
# File 'bignum.c', line 1723 VALUE rb_big_or(xx, yy) VALUE xx, yy; |
#~ ⇒ 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"
1064 1065 1066 |
# File 'bignum.c', line 1064 static VALUE rb_big_neg(x) VALUE x; |