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
-
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
Parametertext
: A string optionally starting with-
or+
followed by characters corresponding to numerals in the given radix. (0-9, a-z, A-Z)Parameter
Parameterradix
: The base of the number system to use, or 10 if unspecified.Returns
The integer represented bytext
, or nil iftext
contains a character that does not represent a numeral inradix
.Declaration
Swift
public init?(_ text: String, radix: Int = 10)
Return Value
The integer represented by
text
, or nil iftext
contains a character that does not represent a numeral inradix
.
-
Return the decimal representation of this integer.
Declaration
Swift
public var description: String
-
Initialize a new big integer from an integer literal.
Declaration
Swift
public init(integerLiteral value: IntMax)
-
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 the playground quick look representation of this integer.
Declaration
Swift
public var customPlaygroundQuickLook: PlaygroundQuickLook
-
Return true iff
a
is equal tob
.Declaration
Swift
public static func ==(a: BigInt, b: BigInt) -> Bool
-
Return true iff
a
is less thanb
.Declaration
Swift
public static func <(a: BigInt, b: BigInt) -> Bool
-
Append this
BigInt
to the specified hasher.Declaration
Swift
public func appendHashes(to hasher: inout SipHasher)
-
Negate
a
and return the result.Declaration
Swift
public static prefix func -(a: BigInt) -> BigInt
-
Subtract
b
froma
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
tob
and return the result.Declaration
Swift
public static func +(a: BigInt, b: BigInt) -> BigInt
-
Multiply
a
withb
and return the result.Declaration
Swift
public static func *(a: BigInt, b: BigInt) -> BigInt
-
Divide
a
byb
and return the quotient. Traps ifb
is zero.Declaration
Swift
public static func /(a: BigInt, b: BigInt) -> BigInt
-
Divide
a
byb
and return the remainder.Declaration
Swift
public static func %(a: BigInt, b: BigInt) -> BigInt
-
Adds
lhs
andrhs
together. An overflow is never reported.Declaration
Swift
public static func addWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
-
Subtracts
rhs
fromlhs
. An overflow is never reported.Declaration
Swift
public static func subtractWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
-
Multiplies
lhs
withrhs
. An overflow is never reported.Declaration
Swift
public static func multiplyWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
-
Divides
lhs
withrhs
, returning the quotient, or trapping ifrhs
is zero. An overflow is never reported.Declaration
Swift
public static func divideWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
-
Divides
lhs
withrhs
, returning the remainder, or trapping ifrhs
is zero. An overflow is never reported.Declaration
Swift
public static func remainderWithOverflow(_ lhs: BigInt, _ rhs: BigInt) -> (BigInt, overflow: Bool)
-
Add
b
toa
in place.Declaration
Swift
public static func +=(a: inout BigInt, b: BigInt)
-
Subtract
b
froma
in place.Declaration
Swift
public static func -=(a: inout BigInt, b: BigInt)
-
Multiply
a
withb
in place.Declaration
Swift
public static func *=(a: inout BigInt, b: BigInt)
-
Divide
a
byb
storing the quotient ina
.Declaration
Swift
public static func /=(a: inout BigInt, b: BigInt)
-
Divide
a
byb
storing the remainder ina
.Declaration
Swift
public static func %=(a: inout BigInt, b: BigInt)