objecthub/swift-numberkit
This is a lightweight framework implementing advanced numeric data types for the Swift programming
Overview
This is a lightweight framework implementing advanced numeric data types for the Swift programming language on macOS, iOS, and Linux. Currently, the framework provides four new numeric types, each represented as a struct or enumeration:
1. BigInt: arbitrary-size signed integers 2. Integer: arbitrary-size signed integers whose implementation depends on the size of the represented value. 3. Rational: signed rational numbers 4. Complex: complex floating-point numbers
Note: In the early days of Swift, with every major release, Apple introduced breaking changes in the foundational APIs of the numeric types in Swift, consistently in backward incompatible ways. In order to be more isolated from such changes, with Swift 3, I decided to introduce a distinct integer type used in NumberKit based on a new protocol IntegerNumber. All standard numeric integer types implement this protocol. This is now consistent with the usage of protocol FloatingPointNumber for floating point numbers, where there was, so far, never a real, generic enough foundation (and still isn't).
BigInt
BigInt values are immutable, signed, arbitrary-size integers that can be used as a drop-in replacement for the existing binary integer types of Swift 5. Struct BigInt defines all the standard arithmetic integer operations and implements the corresponding numeric protocols of Swift.
Integer
Integer values are immutable, signed, arbitrary-size integers that can be used as a drop-in replacement for the existing binary integer types of Swift 5. As opposed to BigInt, the representation of values is chosen to optimize for memory size and performance of arithmetic operations. Enum Integer defines all the standard arithmetic integer operations and implements the corresponding numeric protocols of Swift.
Rational
Struct Rational<T> defines immutable, rational numbers based on an existing signed integer type T, like Int32, Int64, or BigInt. A rational number is a signed number that can be expressed as the quotient of two integers a and b: a / b.
Complex
Struct Complex<T> defines complex numbers based on an existing floating point type T, like Float or Double. A complex number consists of two components, a real part re and an imaginary part im and is typically written as: re + im * i where i is the imaginary unit.
Requirements
The following technologies are needed to build the components of the Swift NumberKit framework:
- Xcode 26
- Swift 5
- Swift Package Manager
- macOS, iOS, tvOS, watchOS, and Linux
Copyright
Author: Matthias Zenger (<matthias@objecthub.net>) Copyright © 2016-2026 Matthias Zenger. All rights reserved.
Package Metadata
Repository: objecthub/swift-numberkit
Default branch: master
README: README.md