Grav v1.14.0

Integer

import "std/types" (included in import "std") · namespace std.types

Integer holds what whole numbers can do beyond arithmetic. Every static method whose first parameter is the number is also a method of any integer value, so n.toHex() and Integer.toHex(n) are the same direct call.

grav
import "std"

fn main() {
    let n = 255
    print(n.toHex() + " " + 255.toBinary())
    print(Integer.parse("42") + 1)
    print(1234567.grouped() + " " + 22.ordinal() + " " + 7.padded(3))
    print("${(-7).abs()} ${12.gcd(18)} ${n.bitCount()} ${1000.nextPowerOfTwo()}")
    let xs: Array<int> = [5, 3, 9]
    xs.sortBy(Integer.descending)
    print(xs)
    try { print(Integer.MAX.addExact(1)) } catch (e: ArithmeticError) { print(e.message()) }
}
Output
ff 11111111
43
1,234,567 22nd 007
7 6 8 1024
[9, 5, 3]
integer overflow: 2147483647 + 1
  • Parsing: Integer.parse(s) (an int, range-checked), parseLong, parse(s, base) (2 to 36; 0xff too), parseOr, tryParse (null when it is not a number), isValid. A cast does the same: "42" as int.
  • Comparators (compare, descending) fit sortBy and Sort; checked arithmetic (addExact, multiplyExact, ...) throws ArithmeticError instead of wrapping.
  • int and long versions exist where the result's width matters (abs, min, bitCount, rotateLeft), so n.abs() stays an int for an int.

API

class Integer

Whole numbers (see the top of this file).

Static members

  • static readonly MAX: int = 2147483647 — The largest and smallest int (32-bit).
  • static readonly MIN: int = -2147483647 - 1
  • static readonly LONG_MAX: long = 9223372036854775807 — The largest and smallest long (64-bit).
  • static readonly LONG_MIN: long = -9223372036854775807 - 1
  • static fn parse(s: string) -> int — s as an int ("42", " -7 "); ValueError when it is not one or is out of range.
  • static fn parseLong(s: string) -> long — s as a long; ValueError when it is not one.
  • static fn parse(s: string, base: int) -> long — s in base (2..36: "ff" in 16, "1010" in 2; base 16 also takes "0xff"); ValueError otherwise.
  • static fn parseOr(s: string, fallback: long) -> long — s as a long, or fallback when it is not one.
  • static fn tryParse(s: string) -> long? — s as a long, or null when it is not one.
  • static fn isValid(s: string) -> bool — Whether all of s is a whole number.
  • static fn toString(n: long) -> string — The number in decimal ("42", "-7").
  • static fn toString(n: long, base: int) -> string — The number in base (2..36), lower-case digits, with a "-" when negative.
  • static fn toHex(n: long) -> string — Hexadecimal, lower case ("ff"; "-ff" for -255).
  • static fn toBinary(n: long) -> string — Binary ("1010").
  • static fn toOctal(n: long) -> string — Octal ("17").
  • static fn grouped(n: long) -> string — With thousands separators ("1,234,567").
  • static fn padded(n: long, width: int) -> string — Padded with zeros to width digits ("007"; a "-" comes first).
  • static fn toChar(n: int) -> string — The character with code point n ("A" for 65), as a UTF-8 string.
  • static fn ordinal(n: long) -> string — As an ordinal: "1st", "2nd", "3rd", "11th", "22nd".
  • static fn abs(n: int) -> int
  • static fn abs(n: long) -> long
  • static fn sign(n: long) -> int — -1, 0 or 1.
  • static fn min(a: int, b: int) -> int
  • static fn min(a: long, b: long) -> long
  • static fn max(a: int, b: int) -> int
  • static fn max(a: long, b: long) -> long
  • static fn clamp(n: int, lo: int, hi: int) -> int — n kept within lo..hi.
  • static fn clamp(n: long, lo: long, hi: long) -> long
  • static fn between(n: long, lo: long, hi: long) -> bool — Whether lo <= n <= hi.
  • static fn isEven(n: long) -> bool
  • static fn isOdd(n: long) -> bool
  • static fn isPrime(n: long) -> bool — Whether n is prime.
  • static fn gcd(a: long, b: long) -> long — The greatest common divisor (never negative).
  • static fn lcm(a: long, b: long) -> long — The least common multiple (0 when either is 0).
  • static fn pow(n: long, e: int) -> long — n to the power e (e >= 0); ArithmeticError when it overflows a long.
  • static fn digitCount(n: long) -> int — How many decimal digits n has (the sign aside; 1 for 0).
  • static fn digitSum(n: long) -> int — The sum of its decimal digits (the sign aside).
  • static fn digits(n: long) -> Array<int> — Its decimal digits, most significant first (the sign aside).
  • static fn times(n: int, f: fn(int)) — Calls f with 0, 1, ..., n - 1.
  • static fn toFloat(n: long) -> float — The number as a float, a Decimal.
  • static fn toDecimal(n: long) -> Decimal
  • static fn compare(a: int, b: int) -> int — Comparators for sorting (xs.sortBy(Integer.compare)): -1, 0 or 1.
  • static fn compare(a: long, b: long) -> int
  • static fn descending(a: int, b: int) -> int — Largest first.
  • static fn descending(a: long, b: long) -> int
  • static fn compareTo(a: long, b: long) -> int — a.compareTo(b): as compare.
  • static fn equals(a: long, b: long) -> bool
  • static fn addExact(a: int, b: int) -> int
  • static fn addExact(a: long, b: long) -> long
  • static fn subtractExact(a: int, b: int) -> int
  • static fn subtractExact(a: long, b: long) -> long
  • static fn multiplyExact(a: int, b: int) -> int
  • static fn multiplyExact(a: long, b: long) -> long
  • static fn toIntExact(n: long) -> int — n as an int; ArithmeticError when it does not fit.
  • static fn bitCount(n: int) -> int — How many bits are 1 (of the two's-complement bits: 64 of them for a long, 32 for an int).
  • static fn bitCount(n: long) -> int
  • static fn leadingZeros(n: int) -> int — Zero bits above the highest 1 (32 or 64 for 0).
  • static fn leadingZeros(n: long) -> int
  • static fn trailingZeros(n: int) -> int — Zero bits below the lowest 1 (32 or 64 for 0).
  • static fn trailingZeros(n: long) -> int
  • static fn isPowerOfTwo(n: long) -> bool — Whether n is a power of two (1, 2, 4, ...).
  • static fn nextPowerOfTwo(n: long) -> long — The smallest power of two >= n (1 for n <= 1); ArithmeticError past a long.
  • static fn rotateLeft(n: int, k: int) -> int — The bits turned k places to the left (those leaving on the left come back on the right).
  • static fn rotateLeft(n: long, k: int) -> long
  • static fn rotateRight(n: int, k: int) -> int
  • static fn rotateRight(n: long, k: int) -> long
  • static fn testBit(n: long, i: int) -> bool — Whether bit i (0 = the lowest) is 1.

Edit this page on GitHub