struct Bigger::Int

Defined in:

bigger/bigger_int.cr

Constant Summary

BASE = 2_u64 ** BASE_NUM_BITS
BASE_NUM_BITS = (sizeof(BaseType)) * 8
BASE_ONE = 1_u32
BASE_ZERO = BaseType.zero
PRINT_DIGITS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".each_char.to_a

Constructors

Instance Method Summary

Macro Summary

Instance methods inherited from struct Int

<=>(other : Bigger::Int) : Int32 <=>

Constructor Detail

def self.new(inp : String, base : Int32 = 10) #

[View source]
def self.new(inp : Int::Primitive) #

[View source]
def self.new(inp : ::Int) #

[View source]
def self.new(inp : Float::Primitive) #

[View source]
def self.new(inp : Number) #

[View source]
def self.new(internal_digits : Array(UInt32), sign : Bool = true) #

[View source]
def self.new #

[View source]

Instance Method Detail

def %(other : Bigger::Int) : Bigger::Int #

[View source]
def %(other : Int::Primitive) : Bigger::Int #

[View source]
def %(other : Int | Number) : Bigger::Int #

[View source]
def &(other : Bigger::Int) : Bigger::Int #

[View source]
def &(other : Int::Primitive) : Bigger::Int #

[View source]
def &(other : Int | Number) : Bigger::Int #

[View source]
def &*(other : Bigger::Int) : Bigger::Int #

[View source]
def &*(other : Int::Primitive) : Bigger::Int #

[View source]
def &*(other : Int | Number) : Bigger::Int #

[View source]
def &+(other : Bigger::Int) : Bigger::Int #

[View source]
def &+(other : Int::Primitive) : Bigger::Int #

[View source]
def &+(other : Int | Number) : Bigger::Int #

[View source]
def &-(other : Bigger::Int) : Bigger::Int #

[View source]
def &-(other : Int::Primitive) : Bigger::Int #

[View source]
def &-(other : Int | Number) : Bigger::Int #

[View source]
def *(other : Bigger::Int) : Bigger::Int #

[View source]
def *(other : Int::Primitive) : Bigger::Int #

[View source]
def *(other : Int | Number) : Bigger::Int #

[View source]
def +(other : Bigger::Int) : Bigger::Int #

[View source]
def +(other : Int::Primitive) : Bigger::Int #

[View source]
def +(other : Int | Number) : Bigger::Int #

[View source]
def -(other : Bigger::Int) : Bigger::Int #

[View source]
def -(other : Int::Primitive) : Bigger::Int #

[View source]
def -(other : Int | Number) : Bigger::Int #

[View source]
def - : Bigger::Int #

[View source]
def /(other : Bigger::Int) : Bigger::Int #

[View source]
def /(other : Int::Primitive) : Bigger::Int #

[View source]
def /(other : Int | Number) : Bigger::Int #

[View source]
def //(other : Bigger::Int) : Bigger::Int #

[View source]
def //(other : Int::Primitive) : Bigger::Int #
Description copied from struct Int

Divides self by other using floored division.

In floored division, given two integers x and y:

  • q = x / y is rounded toward negative infinity
  • r = x % y has the sign of the second argument
  • x == q*y + r

For example:

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

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


[View source]
def //(other : Int | Number) : Bigger::Int #

[View source]
def <<(other : Int) : Bigger::Int #
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

[View source]
def <<(other : Int32) : Bigger::Int #

[View source]
def <=>(other : Int::Primitive) : Int32 #

TODO Current bitshifts only accept Int32. Should this be increased? wrap_in_big_int(">>") wrap_in_big_int("<<")


[View source]
def <=>(other : Bigger::Int) : Int32 #
Description copied from module Comparable(Bigger::Int)

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]
def <=>(other : Float) : Int32 #

[View source]
def <=>(other : Int | Number) : Int32 #

TODO Current bitshifts only accept Int32. Should this be increased? wrap_in_big_int(">>") wrap_in_big_int("<<")


[View source]
def >>(other : Int) : Bigger::Int #
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

[View source]
def >>(other : Int32) : Bigger::Int #

[View source]
def ^(other : Bigger::Int) : Bigger::Int #

[View source]
def ^(other : Int::Primitive) : Bigger::Int #

[View source]
def ^(other : Int | Number) : Bigger::Int #

[View source]
def |(other : Bigger::Int) : Bigger::Int #

[View source]
def |(other : Int::Primitive) : Bigger::Int #

[View source]
def |(other : Int | Number) : Bigger::Int #

[View source]
def ~ : Bigger::Int #

[View source]
def abs : Bigger::Int #
Description copied from struct Number

Returns the absolute value of this number.

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

[View source]
def clone : Bigger::Int #

[View source]
def digits(base = 10, *, absolute = false) : Array(Int32) #

[View source]
def divisible_by?(other : Bigger::Int) : Bool #

[View source]
def divisible_by?(other : Int::Primitive) : Bool #

[View source]
def divisible_by?(other : Int | Number) : Bool #

[View source]
def factorial : Bigger::Int #

[View source]
def gcd(other : Bigger::Int) : Bigger::Int #

[View source]
def gcd(other : Int::Primitive) : Bigger::Int #

[View source]
def gcd(other : Int | Number) : Bigger::Int #

[View source]
def inspect(io : IO) : Nil #
Description copied from class Object

Prints to io an unambiguous and information-rich string representation of this object, typically intended for developers.

It is similar to #to_s(IO), but often provides more information. Ideally, it should contain sufficient information to be able to recreate an object with the same value (given an identical environment).

For types that don't provide a custom implementation of this method, default implementation delegates to #to_s(IO). This said, it is advisable to have an appropriate #inspect implementation on every type. Default implementations are provided by Struct#inspect and Reference#inspect.

::p and ::p! use this method to print an object in STDOUT.


[View source]
def internal_digits : Array(BaseType) #

Least significant bit in index 0


[View source]
def negative? : Bool #
Description copied from struct Number

Returns true if self is less than zero.

-1.negative? # => true
0.negative?  # => false
1.negative?  # => false

[View source]
def popcount #
Description copied from struct Int

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

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

[View source]
def positive? : Bool #
Description copied from struct Number

Returns true if self is greater than zero.

-1.positive? # => false
0.positive?  # => false
1.positive?  # => true

[View source]
def remainder(other : Bigger::Int) : Bigger::Int #

[View source]
def remainder(other : Int::Primitive) : Bigger::Int #

[View source]
def remainder(other : Int | Number) : Bigger::Int #

[View source]
def sign : Int32 #
Description copied from struct Number

Returns the sign of this number as an Int32.

  • -1 if this number is negative
  • 0 if this number is zero
  • 1 if this number is positive
123.sign # => 1
0.sign   # => 0
-42.sign # => -1

[View source]
def tdiv(other : Int) : Bigger::Int #
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.


[View source]
def tdiv(other : Int::Primitive) : Bigger::Int #

[View source]
def tdiv(other : Int | Number) : Bigger::Int #

[View source]
def to_big_f : Bigger::Int #

[View source]
def to_f : Float64 #

[View source]
def to_f64 : Float64 #

[View source]
def to_i : Int32 #

[View source]
def to_i128 : Int128 #

def to_i128! : Int128 #

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_u : UInt32 #

[View source]
def to_u128 : UInt128 #

def to_u128! : UInt128 #

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 trailing_zeros_count #
Description copied from struct Int

Returns the number of trailing 0-bits.


[View source]
def unsafe_shl(other : Bigger::Int) : Bigger::Int #

[View source]
def unsafe_shr(other : Bigger::Int) : Bigger::Int #

[View source]
def unsafe_shr(other : Int::Primitive) : Bigger::Int #

[View source]
def unsafe_shr(other : Int | Number) : Bigger::Int #

[View source]
def zero? : Bool #
Description copied from struct Number

Returns true if self is equal to zero.

0.zero? # => true
5.zero? # => false

[View source]

Macro Detail

macro bitwise_operator(op) #

[View source]
macro define_to_method(num_bits, type, wrap_digits) #

[View source]
macro define_to_methods_for_bits(num_bits) #

[View source]
macro to_basetype(whatever, *, args = nil) #

[View source]
macro wrap_in_big_int(operator, *, return_type = "Bigger::Int") #

Helpful macro to "wrap" all methods to forward to the Bigger::Int version of the method


[View source]