A small library implementing support for rational numbers in Swift, as the Rational
type.
WARNING: This is unmaintained proof-of-concept software; don't use it for anything that actually matters. Try something like Xiaodi Wu's NumericAnnex library instead.
- Construct rationals from integers, doubles, integer literals, double literals, or a numerator and denominator
- Rationals are always in irreducible form
- Value semantics, just like the standard numeric types you know and love
+
,-
,*
,/
,==
,<
, and all other comparison operators- Conforms to
Printable
- print them out or stringify them - Conforms to
Hashable
- use as dictionary keys -
prefix operator to negateinverse
property to get the inverse- Conforms to
AbsoluteValuable
; get the absolute value usingabs
- Perform overflow-checked arithmetic using
addWithOverflow
,subtractWithOverflow
,multiplyWithOverflow
, anddivideWithOverflow
- Clone the repository
- Copy the file
rational.swift
into your project, or add the framework - Enjoy using rational numbers
- Unit tests
%
andremainderWithOverflow
support- Get demo project to stop giving me cryptic warnings (note that these warnings won't affect your usage of the code)
Pull requests, bug reports, and any other feedback are all welcome.
SwiftRational © 2015 Austin Zheng, released as open-source software subject to the MIT license.