class Int

include Comparable[Int]

A 64-bit integer: arithmetic that leaves the range raises OverflowError. Integer is the integer of any size.

An integer literal is an Int (an Integer where one is expected, or when it is too large for an Int). Arithmetic with a Float gives a Float, with an Integer an Integer. As in Ruby, / rounds toward negative infinity and % takes the divisor's sign, so that a == (a / b) * b + a % b.

9223372036854775807 + 1        # raises OverflowError
9223372036854775807.to_integer + 1
# => 9223372036854775808
-7 / 2                         # => -4
-7 % 2                         # => 1
7 / 2.0                        # => 3.5

<prelude>:278

Class methods

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

Int.sqrt(n) is the integer square root (Ruby's Integer.sqrt): the largest Int whose square is at most n. ArgumentError for a negative n.

Int.sqrt(24)    # => 4
Int.sqrt(25)    # => 5
Int.sqrt(-1)    # raises ArgumentError

<prelude>:597

Methods

def +(other: Int) -> Int

The sum; OverflowError when it leaves the Int range.

<prelude>:283

def +(other: Float) -> Float

The sum, as a Float.

<prelude>:285

def +(other: Integer) -> Integer

The sum, as an Integer.

<prelude>:287

def -(other: Int) -> Int

The difference; OverflowError when it leaves the Int range.

<prelude>:289

def -(other: Float) -> Float

The difference, as a Float.

<prelude>:291

def -(other: Integer) -> Integer

The difference, as an Integer.

<prelude>:293

def *(other: Int) -> Int

The product; OverflowError when it leaves the Int range.

<prelude>:295

def *(other: Float) -> Float

The product, as a Float.

<prelude>:297

def *(other: Integer) -> Integer

The product, as an Integer.

<prelude>:299

def /(other: Int) -> Int

The quotient, rounded toward negative infinity (Ruby's floored division, not C's truncating one).

Raises

ZeroDivisionError when other is 0, and OverflowError for the smallest Int divided by -1.

7 / 2     # => 3
-7 / 2    # => -4
7 / -2    # => -4
7 / 0     # raises ZeroDivisionError

<prelude>:314

def /(other: Float) -> Float

The quotient, as a Float: dividing by 0.0 gives an infinity or NaN and raises nothing (7 / 0.0 is Float::INFINITY).

<prelude>:317

def /(other: Integer) -> Integer

The quotient, as an Integer, rounded toward negative infinity; ZeroDivisionError when other is 0.

<prelude>:320

def %(other: Int) -> Int

The modulo: the remainder of /, with the sign of other (Ruby's, not C's). ZeroDivisionError when other is 0.

7 % 3     # => 1
-7 % 3    # => 2
7 % -3    # => -2
7 % 0     # raises ZeroDivisionError

<prelude>:330

def %(other: Float) -> Float

The modulo, as a Float, with the sign of other; NaN when other is 0.0 (-7 % 2.5 is 0.5).

<prelude>:333

def %(other: Integer) -> Integer

The modulo, as an Integer, with the sign of other; ZeroDivisionError when other is 0.

<prelude>:336

def **(other: Int) -> Int

self raised to the power other. 0 ** 0 is 1.

Raises

OverflowError when the power leaves the Int range (use to_integer first for a large one), ArgumentError for a negative other (write 2.0 ** -1 for a fraction).

2 ** 10                 # => 1024
2 ** 63                 # raises OverflowError
2.to_integer ** 64      # => 18446744073709551616
2 ** -1                 # raises ArgumentError

<prelude>:351

def **(other: Float) -> Float

self raised to the power other, as a Float; NaN for a negative self and a fractional other (where Ruby gives a complex number).

<prelude>:354

def -@ -> Int

The negation; OverflowError for the smallest Int, whose negation does not fit.

<prelude>:357

def +@ -> Int

self.

<prelude>:359

def ~ -> Int

The bitwise complement, -self - 1.

<prelude>:361

def &(other: Int) -> Int

The bitwise AND, in two's complement.

<prelude>:363

def |(other: Int) -> Int

The bitwise OR, in two's complement.

<prelude>:365

def ^(other: Int) -> Int

The bitwise exclusive OR, in two's complement.

<prelude>:367

def <<(other: Int) -> Int

self shifted left by other bits (right by -other when other is negative); OverflowError when the result leaves the Int range.

1 << 10       # => 1024
1 << 63       # raises OverflowError
8 << -2       # => 2

<prelude>:376

def >>(other: Int) -> Int

self shifted right by other bits, an arithmetic shift: the sign is kept, so a negative number rounds toward negative infinity (-7 >> 1 is -4) and a shift of 64 or more gives 0 or -1. A negative other shifts left, as << by -other does, with its OverflowError when the result leaves the Int range.

-7 >> 1       # => -4
5 >> -1       # => 10
1 >> -63      # raises OverflowError

<prelude>:388

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

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

<prelude>:390

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#<=>: 1 <=> Float::NAN is -1 (where Ruby gives nil).

<prelude>:394

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

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

<prelude>:396

def ==(other: Object) -> Bool

Whether other is a number of the same value: 1 == 1.0 and 1 == 1.to_integer are true, 1 == "1" is false.

<prelude>:401

def !=(other: Object) -> Bool

Whether other is not a number of the same value (!(self == other)).

<prelude>:403

def <(other: Int) -> Bool

Whether self is less than other.

<prelude>:405

def <=(other: Int) -> Bool

Whether self is less than or equal to other.

<prelude>:407

def >(other: Int) -> Bool

Whether self is greater than other.

<prelude>:409

def >=(other: Int) -> Bool

Whether self is greater than or equal to other.

<prelude>:411

def <(other: Float) -> Bool

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

<prelude>:414

def <=(other: Float) -> Bool

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

<prelude>:417

def >(other: Float) -> Bool

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

<prelude>:420

def >=(other: Float) -> Bool

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

<prelude>:423

def <(other: Integer) -> Bool

Whether self is less than other.

<prelude>:425

def <=(other: Integer) -> Bool

Whether self is less than or equal to other.

<prelude>:427

def >(other: Integer) -> Bool

Whether self is greater than other.

<prelude>:429

def >=(other: Integer) -> Bool

Whether self is greater than or equal to other.

<prelude>:431

def abs -> Int

The absolute value; OverflowError for the smallest Int, whose absolute value does not fit.

<prelude>:434

def negative? -> Bool

Whether self is less than 0.

<prelude>:436

def positive? -> Bool

Whether self is greater than 0.

<prelude>:438

def zero? -> Bool

Whether self is 0.

<prelude>:440

def even? -> Bool

Whether self is divisible by 2 (negative numbers included).

<prelude>:442

def odd? -> Bool

Whether self is not divisible by 2.

<prelude>:444

def succ -> Int

self + 1; OverflowError for the largest Int.

<prelude>:446

def pred -> Int

self - 1; OverflowError for the smallest Int.

<prelude>:448

def to_i -> Int

self.

<prelude>:450

def to_integer -> Integer

self as an Integer, whose arithmetic does not overflow.

9223372036854775807.to_integer * 2   # => 18446744073709551614

<prelude>:456

def to_f -> Float

The nearest Float. Beyond 2**53 not every Int has one of its own: 9007199254740993.to_f is 9007199254740992.0.

<prelude>:459

def to_s -> String

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

<prelude>:461

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.

255.to_s(16)      # => "ff"
255.to_s(2)       # => "11111111"
(-255).to_s(16)   # => "-ff"
255.to_s(37)      # raises ArgumentError

<prelude>:472

def chr -> String

The character of code point self, as a one-character String. Unlike Ruby, a code point from 128 to 255 is a Unicode character too (233.chr is "é"), not a byte.

Raises

RangeError for a negative number, a surrogate (0xD800 to 0xDFFF), or a number above 0x10FFFF.

65.chr        # => "A"
0x3042.chr    # => "あ"
-1.chr        # raises RangeError

<prelude>:487

def floor -> Int

self: an Int is already whole.

<prelude>:489

def ceil -> Int

self: an Int is already whole.

<prelude>:491

def round -> Int

self: an Int is already whole.

<prelude>:493

def truncate -> Int

self: an Int is already whole.

<prelude>:495

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

[self / other, self % other]: the quotient rounded toward negative infinity and the modulo with the sign of other. ZeroDivisionError when other is 0.

7.divmod(2)       # => [3, 1]
(-7).divmod(2)    # => [-4, 1]
7.divmod(-2)      # => [-4, -1]

<prelude>:505

def pow(exp: Int) -> Int

self ** exp, as **: OverflowError when it does not fit, ArgumentError for a negative exp.

<prelude>:508

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

self ** exp modulo mod, computed without the whole power: the result has the sign of mod, as with %.

Raises

RangeError for a negative exp (as in Ruby), ZeroDivisionError when mod is 0.

2.pow(100, 1_000_000_007)   # => 976371285
3.pow(4, -5)                # => -4
2.pow(-1, 5)                # raises RangeError

<prelude>:522

def gcd(other: Int) -> Int

The greatest common divisor, never negative; 0.gcd(0) is 0. OverflowError when it does not fit, which is only when it is 2**63: the smallest Int's with 0 or with itself.

<prelude>:526

def lcm(other: Int) -> Int

The least common multiple, never negative; 0 when either is 0. OverflowError when it leaves the Int range (use to_integer first for a large one).

12.gcd(-18)                     # => 6
4.lcm(-6)                       # => 12
9223372036854775807.lcm(2)      # raises OverflowError

<prelude>:536

def bit_length -> Int

The number of bits self takes in two's complement, besides the sign bit: 8 for 255 and for -256, 0 for 0 and for -1.

<prelude>:539

def digits -> Array[Int]

The decimal digits, the least significant first; ArgumentError for a negative number.

1234.digits   # => [4, 3, 2, 1]
0.digits      # => [0]

<prelude>:547

def digits(base: Int) -> Array[Int]

The digits in base (2 or more), the least significant first; ArgumentError for a negative number or a base below 2.

<prelude>:550

def times(&: (Int) -> Void) -> Void

Calls the block with each of 0 up to self - 1; with none when self is not positive.

<prelude>:553

def times -> Enumerator[Int]

An Enumerator of 0 up to self - 1.

3.times.to_a                  # => [0, 1, 2]
4.times.map { |i| i * i }     # => [0, 1, 4, 9]

<prelude>:560

def upto(limit: Int, &: (Int) -> Void) -> Void

Calls the block with each of self up to limit, both included; with none when limit is below self.

<prelude>:563

def upto(limit: Int) -> Enumerator[Int]

An Enumerator of self up to limit, both included.

1.upto(4).to_a    # => [1, 2, 3, 4]
3.upto(1).to_a    # => []

<prelude>:570

def downto(limit: Int, &: (Int) -> Void) -> Void

Calls the block with each of self down to limit, both included; with none when limit is above self.

<prelude>:573

def downto(limit: Int) -> Enumerator[Int]

An Enumerator of self down to limit, both included.

<prelude>:575

def step(limit: Int, step: Int, &: (Int) -> Void) -> Void

Calls the block with self, self + step, self + 2 * step and so on while not past limit (above it for a positive step, below it for a negative one); ArgumentError when step is 0.

<prelude>:579

def step(limit: Int, step: Int) -> Enumerator[Int]

An Enumerator of self, self + step and so on while not past limit; ArgumentError when step is 0.

1.step(10, 3).to_a    # => [1, 4, 7, 10]
10.step(1, -4).to_a   # => [10, 6, 2]

<prelude>:587

def fdiv(other: Int) -> Float

The quotient as a Float: 7.fdiv(2) is 3.5, and dividing by 0 gives an infinity or NaN instead of raising.

<prelude>:600

def fdiv(other: Float) -> Float

The quotient as a Float: 7.fdiv(2) is 3.5, and dividing by 0 gives an infinity or NaN instead of raising.

<prelude>:602

def ceildiv(other: Int) -> Int

The quotient rounded toward positive infinity; ZeroDivisionError when other is 0.

7.ceildiv(2)      # => 4
(-7).ceildiv(2)   # => -3

<prelude>:610

def floor(digits: Int) -> Int

Rounding to a negative number of digits: 1234.round(-2) is 1200.

self rounded to a multiple of 10 ** -digits: floor toward negative infinity, ceil toward positive infinity, truncate toward 0, and round to the nearest, a half away from 0. A digits of 0 or more gives self. OverflowError when the result leaves the Int range.

1234.floor(-2)        # => 1200
(-1234).floor(-2)     # => -1300
1234.ceil(-2)         # => 1300
(-1234).truncate(-2)  # => -1200
1250.round(-2)        # => 1300
(-1250).round(-2)     # => -1300
1234.round(2)         # => 1234

<prelude>:628

def ceil(digits: Int) -> Int

Rounding to a negative number of digits: 1234.round(-2) is 1200.

self rounded to a multiple of 10 ** -digits: floor toward negative infinity, ceil toward positive infinity, truncate toward 0, and round to the nearest, a half away from 0. A digits of 0 or more gives self. OverflowError when the result leaves the Int range.

1234.floor(-2)        # => 1200
(-1234).floor(-2)     # => -1300
1234.ceil(-2)         # => 1300
(-1234).truncate(-2)  # => -1200
1250.round(-2)        # => 1300
(-1250).round(-2)     # => -1300
1234.round(2)         # => 1234

<prelude>:630

def round(digits: Int) -> Int

Rounding to a negative number of digits: 1234.round(-2) is 1200.

self rounded to a multiple of 10 ** -digits: floor toward negative infinity, ceil toward positive infinity, truncate toward 0, and round to the nearest, a half away from 0. A digits of 0 or more gives self. OverflowError when the result leaves the Int range.

1234.floor(-2)        # => 1200
(-1234).floor(-2)     # => -1300
1234.ceil(-2)         # => 1300
(-1234).truncate(-2)  # => -1200
1250.round(-2)        # => 1300
(-1250).round(-2)     # => -1300
1234.round(2)         # => 1234

<prelude>:632

def truncate(digits: Int) -> Int

Rounding to a negative number of digits: 1234.round(-2) is 1200.

self rounded to a multiple of 10 ** -digits: floor toward negative infinity, ceil toward positive infinity, truncate toward 0, and round to the nearest, a half away from 0. A digits of 0 or more gives self. OverflowError when the result leaves the Int range.

1234.floor(-2)        # => 1200
(-1234).floor(-2)     # => -1300
1234.ceil(-2)         # => 1300
(-1234).truncate(-2)  # => -1200
1250.round(-2)        # => 1300
(-1250).round(-2)     # => -1300
1234.round(2)         # => 1234

<prelude>:634

def [](bit: Int) -> Int

The bit at position bit (0 is the lowest), 0 or 1, in two's complement: a negative number has 1s above its highest bit (-1[100] is 1). A negative bit gives 0.

6[1]    # => 1
6[0]    # => 0

<prelude>:643

def bit_set?(bit: Int) -> Bool

Whether the bit at position bit is 1 (self[bit] == 1).

<prelude>:645

def allbits?(mask: Int) -> Bool

Whether every bit set in mask is set in self (true for a mask of 0).

<prelude>:648

def anybits?(mask: Int) -> Bool

Whether some bit set in mask is set in self.

<prelude>:650

def nobits?(mask: Int) -> Bool

Whether no bit set in mask is set in self.

<prelude>:652

def integer? -> Bool

True: an Int is an integer (Float#integer? is false).

<prelude>:654

def ord -> Int

self, the code point of chr (as in Ruby).

<prelude>:656

def ms -> Duration

A Duration of self milliseconds.

1500.ms                # => 1500ms
2.seconds + 500.ms     # => 2500ms
3.minutes.to_f         # => 180.0

<prelude>:664

def milliseconds -> Duration

A Duration of self milliseconds.

1500.ms                # => 1500ms
2.seconds + 500.ms     # => 2500ms
3.minutes.to_f         # => 180.0

<prelude>:666

def second -> Duration

A Duration of self seconds.

<prelude>:668

def seconds -> Duration

A Duration of self seconds.

<prelude>:670

def minute -> Duration

A Duration of self minutes.

<prelude>:672

def minutes -> Duration

A Duration of self minutes.

<prelude>:674

def hour -> Duration

A Duration of self hours.

<prelude>:676

def hours -> Duration

A Duration of self hours.

<prelude>:678

def day -> Duration

A Duration of self days of 24 hours.

<prelude>:680

def days -> Duration

A Duration of self days of 24 hours.

<prelude>:682