struct Bigger::Int
- Bigger::Int
- Int
- Number
- Value
- Object
Defined in:
bigger/bigger_int.crConstant 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
- .new(inp : String, base : Int32 = 10)
- .new(inp : Int::Primitive)
- .new(inp : ::Int)
- .new(inp : Float::Primitive)
- .new(inp : Number)
- .new(internal_digits : Array(UInt32), sign : Bool = true)
- .new
Instance Method Summary
- #%(other : Bigger::Int) : Bigger::Int
- #%(other : Int::Primitive) : Bigger::Int
- #%(other : Int | Number) : Bigger::Int
- #&(other : Bigger::Int) : Bigger::Int
- #&(other : Int::Primitive) : Bigger::Int
- #&(other : Int | Number) : Bigger::Int
- #&*(other : Bigger::Int) : Bigger::Int
- #&*(other : Int::Primitive) : Bigger::Int
- #&*(other : Int | Number) : Bigger::Int
- #&+(other : Bigger::Int) : Bigger::Int
- #&+(other : Int::Primitive) : Bigger::Int
- #&+(other : Int | Number) : Bigger::Int
- #&-(other : Bigger::Int) : Bigger::Int
- #&-(other : Int::Primitive) : Bigger::Int
- #&-(other : Int | Number) : Bigger::Int
- #*(other : Bigger::Int) : Bigger::Int
- #*(other : Int::Primitive) : Bigger::Int
- #*(other : Int | Number) : Bigger::Int
- #+(other : Bigger::Int) : Bigger::Int
- #+(other : Int::Primitive) : Bigger::Int
- #+(other : Int | Number) : Bigger::Int
- #-(other : Bigger::Int) : Bigger::Int
- #-(other : Int::Primitive) : Bigger::Int
- #-(other : Int | Number) : Bigger::Int
- #- : Bigger::Int
- #/(other : Bigger::Int) : Bigger::Int
- #/(other : Int::Primitive) : Bigger::Int
- #/(other : Int | Number) : Bigger::Int
- #//(other : Bigger::Int) : Bigger::Int
-
#//(other : Int::Primitive) : Bigger::Int
Divides
self
by other using floored division. - #//(other : Int | Number) : Bigger::Int
-
#<<(other : Int) : Bigger::Int
Returns the result of shifting this number's bits count positions to the left.
- #<<(other : Int32) : Bigger::Int
-
#<=>(other : Int::Primitive) : Int32
TODO Current bitshifts only accept Int32.
-
#<=>(other : Bigger::Int) : Int32
The comparison operator.
- #<=>(other : Float) : Int32
-
#<=>(other : Int | Number) : Int32
TODO Current bitshifts only accept Int32.
-
#>>(other : Int) : Bigger::Int
Returns the result of shifting this number's bits count positions to the right.
- #>>(other : Int32) : Bigger::Int
- #^(other : Bigger::Int) : Bigger::Int
- #^(other : Int::Primitive) : Bigger::Int
- #^(other : Int | Number) : Bigger::Int
- #|(other : Bigger::Int) : Bigger::Int
- #|(other : Int::Primitive) : Bigger::Int
- #|(other : Int | Number) : Bigger::Int
- #~ : Bigger::Int
-
#abs : Bigger::Int
Returns the absolute value of this number.
- #clone : Bigger::Int
- #digits(base = 10, *, absolute = false) : Array(Int32)
- #divisible_by?(other : Bigger::Int) : Bool
- #divisible_by?(other : Int::Primitive) : Bool
- #divisible_by?(other : Int | Number) : Bool
- #factorial : Bigger::Int
- #gcd(other : Bigger::Int) : Bigger::Int
- #gcd(other : Int::Primitive) : Bigger::Int
- #gcd(other : Int | Number) : Bigger::Int
-
#inspect(io : IO) : Nil
Prints to io an unambiguous and information-rich string representation of this object, typically intended for developers.
-
#internal_digits : Array(BaseType)
Least significant bit in index 0
-
#negative? : Bool
Returns
true
ifself
is less than zero. -
#popcount
Counts
1
-bits in the binary representation of this integer. -
#positive? : Bool
Returns
true
ifself
is greater than zero. - #remainder(other : Bigger::Int) : Bigger::Int
- #remainder(other : Int::Primitive) : Bigger::Int
- #remainder(other : Int | Number) : Bigger::Int
-
#sign : Int32
Returns the sign of this number as an
Int32
. -
#tdiv(other : Int) : Bigger::Int
Divides
self
by other using truncated division. - #tdiv(other : Int::Primitive) : Bigger::Int
- #tdiv(other : Int | Number) : Bigger::Int
- #to_big_f : Bigger::Int
- #to_f : Float64
- #to_f64 : Float64
- #to_i : Int32
- #to_i128 : Int128
- #to_i128! : Int128
- #to_i16 : Int16
- #to_i16! : Int16
- #to_i32 : Int32
- #to_i32! : Int32
- #to_i64 : Int64
- #to_i64! : Int64
- #to_i8 : Int8
- #to_i8! : Int8
- #to_u : UInt32
- #to_u128 : UInt128
- #to_u128! : UInt128
- #to_u16 : UInt16
- #to_u16! : UInt16
- #to_u32 : UInt32
- #to_u32! : UInt32
- #to_u64 : UInt64
- #to_u64! : UInt64
- #to_u8 : UInt8
- #to_u8! : UInt8
-
#trailing_zeros_count
Returns the number of trailing
0
-bits. - #unsafe_shl(other : Bigger::Int) : Bigger::Int
- #unsafe_shr(other : Bigger::Int) : Bigger::Int
- #unsafe_shr(other : Int::Primitive) : Bigger::Int
- #unsafe_shr(other : Int | Number) : Bigger::Int
-
#zero? : Bool
Returns
true
ifself
is equal to zero.
Macro Summary
- bitwise_operator(op)
- define_to_method(num_bits, type, wrap_digits)
- define_to_methods_for_bits(num_bits)
- to_basetype(whatever, *, args = nil)
-
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
Instance methods inherited from struct Int
<=>(other : Bigger::Int) : Int32
<=>
Constructor Detail
Instance Method Detail
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.
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
TODO Current bitshifts only accept Int32. Should this be increased? wrap_in_big_int(">>") wrap_in_big_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]
TODO Current bitshifts only accept Int32. Should this be increased? wrap_in_big_int(">>") wrap_in_big_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
Returns the absolute value of this number.
123.abs # => 123
-123.abs # => 123
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
.
Returns true
if self
is less than zero.
-1.negative? # => true
0.negative? # => false
1.negative? # => false
Counts 1
-bits in the binary representation of this integer.
5.popcount # => 2
-15.popcount # => 29
Returns true
if self
is greater than zero.
-1.positive? # => false
0.positive? # => false
1.positive? # => true
Returns the sign of this number as an Int32
.
-1
if this number is negative0
if this number is zero1
if this number is positive
123.sign # => 1
0.sign # => 0
-42.sign # => -1
Divides self
by other using truncated division.
In truncated division, given two integers x and y:
q = x.tdiv(y)
is rounded toward zeror = x.remainder(y)
has the sign of the first argumentx == 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.
Returns the number of trailing 0
-bits.
Returns true
if self
is equal to zero.
0.zero? # => true
5.zero? # => false
Macro Detail
Helpful macro to "wrap" all methods to forward to the Bigger::Int version of the method