class Integer

include Comparable[Integer]

An integer of any size: arithmetic with an Int gives an Integer (an Int becomes one only by to_integer, or as a literal where an Integer is expected, or too large for an Int).

An Integer stays an Integer whatever its value; to_i gives the Int back. Division and modulo round as Int's do.

big = 2.to_integer ** 100
big                     # => 1267650600228229401496703205376
big.bit_length          # => 101
(big + 1) % 7           # => 3
big.to_i                # raises OverflowError
5.to_integer.to_i       # => 5

<prelude>:700

Class methods

def self.sqrt(n: Integer) -> Integer

The integer square root: the largest Integer whose square is at most n; ArgumentError for a negative n.

Integer.sqrt(10.to_integer ** 40)   # => 100000000000000000000

<prelude>:836

def self.parse(text: String) -> Integer

The integer text writes in decimal: an optional sign, then digits and _s, with whitespace around it ignored.

Raises

ArgumentError for any other text, an empty one or one with a prefix such as 0x included.

Integer.parse("123456789012345678901234567890")
# => 123456789012345678901234567890
Integer.parse(" -42 ")   # => -42
Integer.parse("1_000")   # => 1000
Integer.parse("0x1f")    # raises ArgumentError

<prelude>:852

def self.parse(text: String, base: Int) -> Integer

The integer text writes in base (2 to 36, letters in either case), as .parse reads a decimal one; ArgumentError for an invalid text or base.

Integer.parse("ff", 16)    # => 255
Integer.parse("-101", 2)   # => -5

<prelude>:861

Methods

def +(other: Int) -> Integer

The sum.

<prelude>:705

def +(other: Integer) -> Integer

The sum.

<prelude>:707

def +(other: Float) -> Float

The sum, as a Float.

<prelude>:709

def -(other: Int) -> Integer

The difference.

<prelude>:711

def -(other: Integer) -> Integer

The difference.

<prelude>:713

def -(other: Float) -> Float

The difference, as a Float.

<prelude>:715

def *(other: Int) -> Integer

The product.

<prelude>:717

def *(other: Integer) -> Integer

The product.

<prelude>:719

def *(other: Float) -> Float

The product, as a Float.

<prelude>:721

def /(other: Int) -> Integer

The quotient, rounded toward negative infinity, as Int#/; ZeroDivisionError when other is 0.

<prelude>:724

def /(other: Integer) -> Integer

The quotient, rounded toward negative infinity, as Int#/; ZeroDivisionError when other is 0.

<prelude>:726

def /(other: Float) -> Float

The quotient, as a Float: dividing by 0.0 gives an infinity or NaN.

<prelude>:728

def %(other: Int) -> Integer

The modulo, with the sign of other, as Int#%; ZeroDivisionError when other is 0.

<prelude>:731

def %(other: Integer) -> Integer

The modulo, with the sign of other, as Int#%; ZeroDivisionError when other is 0.

<prelude>:733

def %(other: Float) -> Float

The modulo, as a Float, with the sign of other; NaN when other is 0.0.

<prelude>:736

def **(other: Int) -> Integer

self raised to the power other; ArgumentError for a negative other.

<prelude>:739

def **(other: Float) -> Float

self raised to the power other, as a Float.

<prelude>:741

def -@ -> Integer

The negation.

<prelude>:743

def +@ -> Integer

self.

<prelude>:745

def <=>(other: Int) -> Int

-1, 0 or 1 as self is less than, equal to or greater than other.

<prelude>:747

def <=>(other: Integer) -> Int

-1, 0 or 1 as self is less than, equal to or greater than other.

<prelude>:749

def <=>(other: Float) -> Int

-1, 0 or 1 as self is less than, equal to or greater than other. NaN comes after every number, as in Float#<=>: the result is -1 when other is NaN.

<prelude>:753

def ==(other: Object) -> Bool

Whether other is an Int or an Integer of the same value. Unlike Int#==, a Float is never equal: 5.to_integer == 5.0 is false.

<prelude>:756

def !=(other: Object) -> Bool

Whether other is not equal to self (!(self == other)).

<prelude>:758

def <(other: Int) -> Bool

Whether self is less than other.

<prelude>:760

def <=(other: Int) -> Bool

Whether self is less than or equal to other.

<prelude>:762

def >(other: Int) -> Bool

Whether self is greater than other.

<prelude>:764

def >=(other: Int) -> Bool

Whether self is greater than or equal to other.

<prelude>:766

def <(other: Integer) -> Bool

Whether self is less than other.

<prelude>:768

def <=(other: Integer) -> Bool

Whether self is less than or equal to other.

<prelude>:770

def >(other: Integer) -> Bool

Whether self is greater than other.

<prelude>:772

def >=(other: Integer) -> Bool

Whether self is greater than or equal to other.

<prelude>:774

def <(other: Float) -> Bool

Whether self is less than other; false when other is NaN (although <=> orders NaN last).

<prelude>:777

def <=(other: Float) -> Bool

Whether self is less than or equal to other; false when other is NaN.

<prelude>:780

def >(other: Float) -> Bool

Whether self is greater than other; false when other is NaN (although <=> orders NaN last).

<prelude>:783

def >=(other: Float) -> Bool

Whether self is greater than or equal to other; false when other is NaN.

<prelude>:786

def abs -> Integer

The absolute value.

<prelude>:788

def negative? -> Bool

Whether self is less than 0.

<prelude>:790

def positive? -> Bool

Whether self is greater than 0.

<prelude>:792

def zero? -> Bool

Whether self is 0.

<prelude>:794

def even? -> Bool

Whether self is divisible by 2.

<prelude>:796

def odd? -> Bool

Whether self is not divisible by 2.

<prelude>:798

def succ -> Integer

self + 1.

<prelude>:800

def pred -> Integer

self - 1.

<prelude>:802

def to_i -> Int

The Int, or OverflowError when it does not fit.

<prelude>:804

def to_integer -> Integer

self.

<prelude>:806

def to_f -> Float

The nearest Float; Float::INFINITY (or its negation) beyond Float::MAX.

<prelude>:809

def to_s -> String

The decimal digits, after a - for a negative number.

<prelude>:811

def to_s(base: Int) -> String

The digits in base, from 2 to 36, with lowercase letters for the digits above 9, after a - for a negative number; ArgumentError for another base.

<prelude>:815

def divmod(other: Integer) -> [Integer, Integer]

[self / other, self % other], rounded as Int#divmod; ZeroDivisionError when other is 0.

<prelude>:818

def pow(exp: Integer, mod: Integer) -> Integer

self ** exp modulo mod, computed without the whole power: the result has the sign of mod. RangeError for a negative exp, ZeroDivisionError when mod is 0.

<prelude>:822

def gcd(other: Integer) -> Integer

The greatest common divisor, never negative.

<prelude>:824

def lcm(other: Integer) -> Integer

The least common multiple, never negative; 0 when either is 0.

<prelude>:826

def bit_length -> Int

The number of bits self takes in two's complement, besides the sign bit, as Int#bit_length.

<prelude>:829

def integer? -> Bool

True: an Integer is an integer.

<prelude>:863