BigInt

public struct BigInt

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
  • abs

    The absolute value of this integer.

    Declaration

    Swift

    public var abs: BigUInt
  • The type representing a digit in BigInt’s underlying number system.

    Declaration

    Swift

    public typealias Digit = BigUInt.Digit
  • True iff the value of this integer is negative.

    Declaration

    Swift

    public var negative: Bool
  • Initializes a new big integer with the provided absolute number and sign flag.

    Declaration

    Swift

    public init(abs: BigUInt, negative: Bool = false)
  • Initializes a new big integer with the same value as the specified integer.

    Declaration

    Swift

    public init<I: UnsignedInteger>(_ integer: I)
  • Initializes a new big integer with the same value as the specified integer.

    Declaration

    Swift

    public init<I: SignedInteger>(_ integer: I)
  • Initializes a new signed big integer with the same value as the specified unsigned big integer.

    Declaration

    Swift

    public init(_ integer: BigUInt)
  • Initialize a big integer from an ASCII representation in a given radix. Numerals above 9 are represented by letters from the English alphabet.

    Requires

    radix > 1 && radix < 36

    Parameter

    Parameter text: A string optionally starting with - or + followed by characters corresponding to numerals in the given radix. (0-9, a-z, A-Z)

    Parameter

    Parameter radix: The base of the number system to use, or 10 if unspecified.

    Returns

    The integer represented by text, or nil if text contains a character that does not represent a numeral in radix.

    Declaration

    Swift

    public init?(_ text: String, radix: Int = 10)

    Return Value

    The integer represented by text, or nil if text contains a character that does not represent a numeral in radix.

  • Return the decimal representation of this integer.

    Declaration

    Swift

    public var description: String
  • Initialize a new big integer from a Unicode scalar. The scalar must represent a decimal digit.

    Declaration

    Swift

    public init(unicodeScalarLiteral value: UnicodeScalar)
  • Initialize a new big integer from an extended grapheme cluster. The cluster must consist of a decimal digit.

    Declaration

    Swift

    public init(extendedGraphemeClusterLiteral value: String)
  • Initialize a new big integer from a decimal number represented by a string literal of arbitrary length. The string must contain only decimal digits.

    Declaration

    Swift

    public init(stringLiteral value: StringLiteralType)
  • Return true iff a is equal to b.

    Declaration

    Swift

    public static func ==(a: BigInt, b: BigInt) -> Bool
  • Return true iff a is less than b.

    Declaration

    Swift

    public static func <(a: BigInt, b: BigInt) -> Bool
  • Negate a and return the result.

    Declaration

    Swift

    public static prefix func -(a: BigInt) -> BigInt
  • Subtract b from a and return the result.

    Declaration

    Swift

    public static func -(a: BigInt, b: BigInt) -> BigInt
  • Returns the absolute value of x.

    Declaration

    Swift

    public static func abs(_ x: BigInt) -> BigInt
  • Explicitly convert to IntMax, trapping on overflow.

    Declaration

    Swift

    public func toIntMax() -> IntMax
  • Add a to b and return the result.

    Declaration

    Swift

    public static func +(a: BigInt, b: BigInt) -> BigInt
  • Multiply a with b and return the result.

    Declaration

    Swift

    public static func *(a: BigInt, b: BigInt) -> BigInt
  • Divide a by b and return the quotient. Traps if b is zero.

    Declaration

    Swift

    public static func /(a: BigInt, b: BigInt) -> BigInt
  • Divide a by b and return the remainder.

    Declaration

    Swift

    public static func %(a: BigInt, b: BigInt) -> BigInt
  • Adds lhs and rhs together. An overflow is never reported.

    Declaration

    Swift

    public static func addWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
  • Subtracts rhs from lhs. An overflow is never reported.

    Declaration

    Swift

    public static func subtractWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
  • Multiplies lhs with rhs. An overflow is never reported.

    Declaration

    Swift

    public static func multiplyWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
  • Divides lhs with rhs, returning the quotient, or trapping if rhs is zero. An overflow is never reported.

    Declaration

    Swift

    public static func divideWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
  • Divides lhs with rhs, returning the remainder, or trapping if rhs is zero. An overflow is never reported.

    Declaration

    Swift

    public static func remainderWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
  • Add b to a in place.

    Declaration

    Swift

    public static func +=(a: inout BigInt, b: BigInt)
  • Subtract b from a in place.

    Declaration

    Swift

    public static func -=(a: inout BigInt, b: BigInt)
  • Multiply a with b in place.

    Declaration

    Swift

    public static func *=(a: inout BigInt, b: BigInt)
  • Divide a by b storing the quotient in a.

    Declaration

    Swift

    public static func /=(a: inout BigInt, b: BigInt)
  • Divide a by b storing the remainder in a.

    Declaration

    Swift

    public static func %=(a: inout BigInt, b: BigInt)