# struct BigInt

## Overview

A `BigInt` can represent arbitrarily large integers.

It is implemented under the hood with GMP.

NOTE To use `BigInt`, you must explicitly import it with `require "big"`

big.cr
big/big_int.cr
big/number.cr

## Constructor Detail

def self.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``````

def self.new(num : Int::Signed) #

Creates a `BigInt` from the given num.

def self.new(num : Int::Unsigned) #

Creates a `BigInt` from the given num.

def self.new(num : Float::Primitive) #

Creates a `BigInt` from the given num.

def self.new(num : BigFloat) #

Creates a `BigInt` from the given num.

def self.new(num : BigDecimal) #

Creates a `BigInt` from the given num.

def self.new(num : BigRational) #

Creates a `BigInt` from the given num.

def self.new(num : BigInt) #

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

def self.new #

Creates a `BigInt` with the value zero.

``````require "big"

BigInt.new # => 0``````

## Instance Method Detail

def %(other : Int) : BigInt #
Description copied from struct Int

Returns `self` modulo other.

This uses floored division.

See `Int#/` for more details.

def &(other : Int) : BigInt #

def &*(other) : BigInt #

def &+(other) : BigInt #

def &-(other) : BigInt #

def *(other : BigInt) : BigInt #

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

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

def *(other : Int) : BigInt #

def **(other : Int) : BigInt #
Description copied from struct Int

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``````

def +(other : BigInt) : BigInt #

def +(other : Int) : BigInt #

def -(other : BigInt) : BigInt #

def -(other : Int) : BigInt #

def - : BigInt #

def /(other : BigInt) : BigFloat #

def /(other : BigDecimal) : BigDecimal #

def /(other : BigRational) : BigRational #

def /(other : Int8) : BigFloat #

def /(other : UInt8) : BigFloat #

def /(other : Int16) : BigFloat #

def /(other : UInt16) : BigFloat #

def /(other : Int32) : BigFloat #

def /(other : UInt32) : BigFloat #

def /(other : Int64) : BigFloat #

def /(other : UInt64) : BigFloat #

def /(other : Int128) : BigFloat #

def /(other : UInt128) : BigFloat #

def /(other : Float32) : BigFloat #

def /(other : Float64) : BigFloat #

def //(other : Int::Unsigned) : BigInt #

def //(other : Int) : BigInt #

def <<(other : Int) : BigInt #
Description copied from struct Int

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``````

def <=>(other : BigInt) #

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

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

def <=>(other : Float) #
Description copied from module Comparable(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]``````

def >>(other : Int) : BigInt #
Description copied from struct Int

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``````

def ^(other : Int) : BigInt #

def |(other : Int) : BigInt #

def ~ : BigInt #

def abs : BigInt #
Description copied from struct Number

Returns the absolute value of this number.

``````123.abs  # => 123
-123.abs # => 123``````

def bit_length : Int32 #
Description copied from struct Int

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``````

def clone #

def digits(base = 10) : Array(Int32) #
Description copied from struct Int

Returns the digits of a number in a given base. The digits are returned as an array with the least significant digit as the first array element.

``````12345.digits      # => [5, 4, 3, 2, 1]
12345.digits(7)   # => [4, 6, 6, 0, 5]
12345.digits(100) # => [45, 23, 1]

-12345.digits(7) # => ArgumentError``````

def divisible_by?(number : BigInt) : Bool #

def divisible_by?(number : LibGMP::ULong) : Bool #

def divisible_by?(number : Int) : Bool #

def divmod(number : BigInt) : Tuple(BigInt, BigInt) #

def divmod(number : LibGMP::ULong) #

def divmod(number : Int::Signed) : Tuple(BigInt, BigInt) #

def divmod(number : Int::Unsigned) #

def factorial : BigInt #

def gcd(other : BigInt) : BigInt #

Returns the greatest common divisor of `self` and other.

def gcd(other : Int) : Int #

Returns the greatest common divisor of `self` and other.

def hash(hasher) #

TODO check hash equality for numbers >= 2**63

def lcm(other : BigInt) : BigInt #

Returns the least common multiple of `self` and other.

def lcm(other : Int) : BigInt #

Returns the least common multiple of `self` and other.

def popcount : Int #
Description copied from struct Int

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

``````5.popcount   # => 2
-15.popcount # => 29``````

def remainder(other : Int) : BigInt #
Description copied from struct Int

Returns `self` remainder other.

This uses truncated division.

See `Int#tdiv` for more details.

def tdiv(other : Int) : BigInt #
Description copied from struct Int

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.

def to_big_d : BigDecimal #
Description copied from struct Int

Converts `self` to `BigDecimal`.

``````require "big"
123456789012345678.to_big_d``````

def to_big_f : BigFloat #

def to_big_i : BigInt #
Description copied from struct Int

Returns a `BigInt` representing this integer.

``````require "big"

123.to_big_i``````

def to_big_r : BigRational #
Description copied from struct Int

Returns a `BigRational` representing this integer.

``````require "big"

123.to_big_r``````

def to_f : Float64 #

def to_f! #

def to_f32 : Float32 #

def to_f32! #

def to_f64 : Float64 #

def to_f64! #

def to_i : Int32 #

def to_i! #

def to_i16 : Int16 #

def to_i16! : Int16 #

def to_i32 : Int32 #

def to_i32! : Int32 #

def to_i64 : Int64 #

def to_i64! : Int64 #

def to_i8 : Int8 #

def to_i8! : Int8 #

def to_s(io : IO, base : Int = 10, *, precision : Int = 1, upcase : Bool = false) : Nil #
Description copied from struct Int

Appends a string representation of this integer to the given io.

base specifies the radix of the written string, and must be either 62 or a number between 2 and 36. By default, digits above 9 are represented by ASCII lowercase letters (`a` for 10, `b` for 11, etc.), but uppercase letters may be used if upcase is `true`, unless base 62 is used. In that case, lowercase letters are used for 10 to 35, and uppercase ones for 36 to 61, and upcase must be `false`.

precision specifies the minimum number of digits in the written string. If there are fewer digits than this number, the string is left-padded by zeros. If `self` and precision are both zero, returns an empty string.

def to_s(base : Int = 10, *, precision : Int = 1, upcase : Bool = false) : String #
Description copied from struct Int

Returns a string representation of this integer.

base specifies the radix of the returned string, and must be either 62 or a number between 2 and 36. By default, digits above 9 are represented by ASCII lowercase letters (`a` for 10, `b` for 11, etc.), but uppercase letters may be used if upcase is `true`, unless base 62 is used. In that case, lowercase letters are used for 10 to 35, and uppercase ones for 36 to 61, and upcase must be `false`.

precision specifies the minimum number of digits in the returned string. If there are fewer digits than this number, the string is left-padded by zeros. If `self` and precision are both zero, returns an empty string.

``````1234.to_s                   # => "1234"
1234.to_s(2)                # => "10011010010"
1234.to_s(16)               # => "4d2"
1234.to_s(16, upcase: true) # => "4D2"
1234.to_s(36)               # => "ya"
1234.to_s(62)               # => "jU"
1234.to_s(precision: 2)     # => "1234"
1234.to_s(precision: 6)     # => "001234"``````

def to_u : UInt32 #

def to_u! #

def to_u16 : UInt16 #

def to_u16! : UInt16 #

def to_u32 : UInt32 #

def to_u32! : UInt32 #

def to_u64 : UInt64 #

def to_u64! : UInt64 #

def to_u8 : UInt8 #

def to_u8! : UInt8 #

def to_unsafe #

def trailing_zeros_count : Int #
Description copied from struct Int

Returns the number of trailing `0`-bits.

def unsafe_floored_div(other : BigInt) : BigInt #

def unsafe_floored_div(other : Int) : BigInt #

def unsafe_floored_divmod(number : BigInt) : Tuple(BigInt, BigInt) #

def unsafe_floored_divmod(number : LibGMP::ULong) : Tuple(BigInt, BigInt) #

def unsafe_floored_mod(other : BigInt) : BigInt #

def unsafe_floored_mod(other : Int) : BigInt #

def unsafe_truncated_div(other : BigInt) : BigInt #

def unsafe_truncated_div(other : Int) : BigInt #

def unsafe_truncated_divmod(number : BigInt) #

def unsafe_truncated_divmod(number : LibGMP::ULong) #

def unsafe_truncated_mod(other : BigInt) : BigInt #

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

def unsafe_truncated_mod(other : Int) : BigInt #