Skip to content

struct BigInt
inherits Int

A BigInt can represent arbitrarily large integers.

It is implemented under the hood with GMP.

Included modules

Comparable Comparable Comparable Comparable

Class methods

.new(str : String, base = 10)

Creates a BigInt with the value denoted by str in the given base.

Raises ArgumentError if the string doesn't denote a valid integer.

require "big"

BigInt.new("123456789123456789123456789123456789") # => 123456789123456789123456789123456789
BigInt.new("123_456_789_123_456_789_123_456_789")  # => 123456789123456789123456789
BigInt.new("1234567890ABCDEF", base: 16)           # => 1311768467294899695
View source

.new(num : Int::Signed)

Creates a BigInt from the given num.

View source

.new(num : Int::Unsigned)

Creates a BigInt from the given num.

View source

.new(num : Float::Primitive)

Creates a BigInt from the given num.

View source

.new(num : BigFloat)

Creates a BigInt from the given num.

View source

.new(num : BigDecimal)

Creates a BigInt from the given num.

View source

.new(num : BigRational)

Creates a BigInt from the given num.

View source

.new(num : BigInt)

Returns num. Useful for generic code that does T.new(...) with T being a Number.

View source

.new

Creates a BigInt with the value zero.

require "big"

BigInt.new # => 0
View source

Methods

#%(other : Int) : BigInt

Returns self modulo other.

This uses floored division.

See Int#/ for more details.

View source

#&(other : Int) : BigInt

View source

#&*(other) : BigInt

View source

#&+(other) : BigInt

View source

#&-(other) : BigInt

View source

#*(other : Int) : BigInt

View source

#*(other : LibGMP::IntPrimitiveUnsigned) : BigInt

View source

#*(other : LibGMP::IntPrimitiveSigned) : BigInt

View source

#*(other : BigInt) : BigInt

View source

#**(other : Int) : BigInt

Returns the value of raising self to the power of exponent.

Raises ArgumentError if exponent is negative: if this is needed, either use a float base or a float exponent.

Raises OverflowError in case of overflow.

2 ** 3  # => 8
2 ** 0  # => 1
2 ** -1 # ArgumentError
View source

#+(other : Int) : BigInt

View source

#+(other : BigInt) : BigInt

View source

#-(other : Int) : BigInt

View source

#-(other : BigInt) : BigInt

View source

#/(other : UInt128) : BigFloat

View source

#/(other : Int128) : BigFloat

View source

#/(other : Float32) : BigFloat

View source

#/(other : UInt64) : BigFloat

View source

#/(other : Int64) : BigFloat

View source

#/(other : UInt32) : BigFloat

View source

#/(other : Int32) : BigFloat

View source

#/(other : UInt16) : BigFloat

View source

#/(other : Int16) : BigFloat

View source

#/(other : Int8) : BigFloat

View source

#/(other : BigInt) : BigFloat

View source

#/(other : UInt8) : BigFloat

View source

#/(other : Float64) : BigFloat

View source

#//(other : Int) : BigInt

View source

#<<(other : Int) : BigInt

Returns the result of shifting this number's bits count positions to the left.

  • If count is greater than the number of bits of this integer, returns 0
  • If count is negative, a right shift is performed
8000 << 1  # => 16000
8000 << 2  # => 32000
8000 << 32 # => 0
8000 << -1 # => 4000
View source

#<=>(other : BigInt)

View source

#<=>(other : Float)

The comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greater than other, or nil if the two objects are not comparable.

Subclasses define this method to provide class-specific ordering.

The comparison operator is usually used to sort values:

# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]

# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
View source

#<=>(other : Int::Unsigned)

View source

#<=>(other : Int::Signed)

View source

#>>(other : Int) : BigInt

Returns the result of shifting this number's bits count positions to the right. Also known as arithmetic right shift.

  • If count is greater than the number of bits of this integer, returns 0
  • If count is negative, a left shift is performed
8000 >> 1  # => 4000
8000 >> 2  # => 2000
8000 >> 32 # => 0
8000 >> -1 # => 16000

-8000 >> 1 # => -4000
View source

#^(other : Int) : BigInt

View source

#abs : BigInt

Returns the absolute value of this number.

123.abs  # => 123
-123.abs # => 123
View source

#bit_length : Int32

Returns the number of bits of this int value.

“The number of bits” means that the bit position of the highest bit which is different to the sign bit. (The bit position of the bit 2**n is n+1.) If there is no such bit (zero or minus one), zero is returned.

I.e. This method returns ceil(log2(self < 0 ? -self : self + 1)).

0.bit_length # => 0
1.bit_length # => 1
2.bit_length # => 2
3.bit_length # => 2
4.bit_length # => 3
5.bit_length # => 3

# The above is the same as
0b0.bit_length   # => 0
0b1.bit_length   # => 1
0b10.bit_length  # => 2
0b11.bit_length  # => 2
0b100.bit_length # => 3
0b101.bit_length # => 3
View source

#clone

View source

#divmod(number : Int::Unsigned)

View source

#divmod(number : Int::Signed)

View source

#divmod(number : LibGMP::ULong)

View source

#divmod(number : BigInt)

View source

#factorial : BigInt

View source

#gcd(other : Int) : Int

Returns the greatest common divisor of self and other.

View source

#gcd(other : BigInt) : BigInt

Returns the greatest common divisor of self and other.

View source

#hash(hasher)

Todo

check hash equality for numbers >= 2**63

View source

#lcm(other : Int) : BigInt

Returns the least common multiple of self and other.

View source

#lcm(other : BigInt) : BigInt

Returns the least common multiple of self and other.

View source

#popcount

Counts 1-bits in the binary representation of this integer.

5.popcount   # => 2
-15.popcount # => 29
View source

#remainder(other : Int) : BigInt

Returns self remainder other.

This uses truncated division.

See Int#tdiv for more details.

View source

#tdiv(other : Int) : BigInt

Divides self by other using truncated division.

In truncated division, given two integers x and y: * q = x.tdiv(y) is rounded toward zero * r = x.remainder(y) has the sign of the first argument * x == q*y + r

For example:

 x     y     x / y     x % y
 5     3       1         2
-5     3      -1        -2
 5    -3      -1         2
-5    -3       1        -2

Raises if other is 0, or if other is -1 and self is signed and is the minimum value for that integer type.

View source

#to_big_d

Converts self to BigDecimal.

require "big"
12123415151254124124.to_big_d

View source

#to_big_f

View source

#to_big_i

Returns a BigInt representing this integer.

require "big"

123.to_big_i

View source

#to_big_r

Returns a BigRational representing this integer.

require "big"

123.to_big_r

View source

#to_f

View source

#to_f!

View source

#to_f32

View source

#to_f32!

View source

#to_f64

View source

#to_f64!

View source

#to_i

View source

#to_i!

View source

#to_i16

View source

#to_i16!

View source

#to_i32

View source

#to_i32!

View source

#to_i64

View source

#to_i64!

View source

#to_i8

View source

#to_i8!

View source

#to_s : String

Returns a string representation of self.

require "big"

BigInt.new("123456789101101987654321").to_s # => 123456789101101987654321
View source

#to_s(base : Int) : String

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

require "big"

BigInt.new("123456789101101987654321").to_s(8)  # => "32111154373025463465765261"
BigInt.new("123456789101101987654321").to_s(16) # => "1a249b1f61599cd7eab1"
BigInt.new("123456789101101987654321").to_s(36) # => "k3qmt029k48nmpd"
View source

#to_s(io : IO) : Nil

Returns a string representation of self.

require "big"

BigInt.new("123456789101101987654321").to_s # => 123456789101101987654321
View source

#to_u

View source

#to_u!

View source

#to_u16

View source

#to_u16!

View source

#to_u32

View source

#to_u32!

View source

#to_u64

View source

#to_u64!

View source

#to_u8

View source

#to_u8!

View source

#to_unsafe

View source

#trailing_zeros_count

Returns the number of trailing 0-bits.

View source

#unsafe_floored_div(other : BigInt) : BigInt

View source

#unsafe_floored_div(other : Int) : BigInt

View source

#unsafe_floored_divmod(number : LibGMP::ULong)

View source

#unsafe_floored_divmod(number : BigInt)

View source

#unsafe_floored_mod(other : Int) : BigInt

View source

#unsafe_floored_mod(other : BigInt) : BigInt

View source

#unsafe_truncated_div(other : Int) : BigInt

View source

#unsafe_truncated_div(other : BigInt) : BigInt

View source

#unsafe_truncated_divmod(number : BigInt)

View source

#unsafe_truncated_divmod(number : LibGMP::ULong)

View source

#unsafe_truncated_mod(other : Int) : BigInt

View source

#unsafe_truncated_mod(other : BigInt) : BigInt

View source

#unsafe_truncated_mod(other : LibGMP::IntPrimitive) : BigInt

View source

#|(other : Int) : BigInt

View source