Pelago
Core typesPrimitive types

int256

The 256-bit signed integer: range, literals, checked arithmetic, methods, conversions and JSON.

int256 is a 256-bit two's complement integer, stored as 4 64-bit words, least significant first. Its values run from -57896044618658097711785492504343953926634992332820282019728792003956564819968 to 57896044618658097711785492504343953926634992332820282019728792003956564819967. It follows exactly the rules of int with a wider range, so there is nothing new to learn.

Writing a value

A decimal, hexadecimal, binary or octal literal that fits the type, such as const a: int256 = 57896044618658097711785492504343953926634992332820282019728792003956564819967. A literal outside the range is a compile-time error.

Arithmetic

+ - * / %, ++, --, the bitwise operators, <<, >>, the wrapping forms +% -% *%, the comparisons and every op= form work on two values of int256.

  • Overflow stops the program with stopped: arithmetic overflow; so does a division by zero (stopped: division by zero) and a shift by 256 or more (stopped: shifted by the width or more).
  • The wrapping forms wrap modulo 2^256.
  • Two different integer types are never mixed silently.
fn main() {
    const lo: int256 = -57896044618658097711785492504343953926634992332820282019728792003956564819968
    const hi: int256 = 57896044618658097711785492504343953926634992332820282019728792003956564819967
    print("#lo")
    print("#hi")
    print("#(hi +% 1) #(hi / 3) #(hi % 1000) #(hi >> 100) #(hi & 255)")
    var f: int256 = 1
    var i: int256 = 1
    while i <= 25 {
        f = f * i
        i += 1
    }
    print("25! = #f")
}
-57896044618658097711785492504343953926634992332820282019728792003956564819968
57896044618658097711785492504343953926634992332820282019728792003956564819967
-57896044618658097711785492504343953926634992332820282019728792003956564819968 19298681539552699237261830834781317975544997444273427339909597334652188273322 967 45671926166590716193865151022383844364247891967 255
25! = 15511210043330985984000000

Overflow is a stop:

fn main() {
    const top: int256 = 57896044618658097711785492504343953926634992332820282019728792003956564819967
    print(top + 1)
}
stopped: arithmetic overflow

Methods

abs(), min(o), max(o) and pow(e). Like every other operation, pow stops the program on overflow. Everything else is an operator.

Converting

to converts when nothing can be lost. From int256, to is accepted for: float. to float rounds to within one step in the last place. Into int256, a narrower type of the same signedness, or a narrower unsigned type into a signed one, is accepted. Converting down, and float to a wide integer, are not written yet.

Equality, hashing and JSON

A int256 is eq, order, hash and show, so it works as a map key, a set member, a struct field and in for ranges and switch patterns. With @derive(toJson) it is written as a JSON string of digits, because most JSON tools read numbers as 64-bit floats; reading accepts the string, and a plain JSON number when it fits int. A value outside the range is an error, never a wrap. See typed JSON.

Not finished

Speed: correctness came first, so division is a shift-and-subtract loop and printing a large value is slow. Narrowing to, and float to a wide integer, are not written yet.

int, Wide integers overview

On this page