Structs

The following structs are available globally.

  • An arbitary precision unsigned integer type, also known as a big integer.

    Operations on big integers never overflow, but they might take a long time to execute. The amount of memory (and address space) available is the only constraint to the magnitude of these numbers.

    This particular big integer type uses base-2^64 digits to represent integers; you can think of it as a wrapper around Array<UInt64>. In fact, BigUInt implements a mutable collection of its UInt64 digits, with the digit at index 0 being the least significant.

    To make memory management simple, BigUInt allows you to subscript it with out-of-bounds indexes: the subscript getter zero-extends the digit sequence, while the subscript setter automatically extends the underlying storage when necessary:

    var number = BigUInt(1)
    number[42]                // Not an error, returns 0
    number[23] = 1            // Not an error, number is now 2^1472 + 1.
    

    Note that it is rarely a good idea to use big integers as collections; in the vast majority of cases it is much easier to work with the provided high-level methods and operators rather than with raw big digits.

    See more

    Declaration

    Swift

    public struct BigUInt
  • State for iterating through the digits of a big integer.

    See more

    Declaration

    Swift

    public struct DigitIterator<Digit>: IteratorProtocol
  • An arbitary precision signed integer type, also known as a big integer.

    Operations on big integers never overflow, but they might take a long time to execute. The amount of memory (and address space) available is the only constraint to the magnitude of these numbers.

    This particular big integer type uses base-2^64 digits to represent integers.

    BigInt is essentially a tiny wrapper that extends BigUInt with a sign bit and provides signed integer operations. Both the underlying absolute value and the negative/positive flag are available as read-write properties.

    Not all algorithms of BigUInt are available for BigInt values; for example, there is no square root or primality test for signed integers. When you need to call one of these, just extract the absolute value:

    BigInt(255).abs.isPrime()   // Returns false
    
    See more

    Declaration

    Swift

    public struct BigInt