diff --git a/README.md b/README.md index e8b7be7..e28c5bd 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,129 @@ # RationalNumeric -Struct, `Rational` to represent a rational number with a numerator and denominator, supports basic math operations by -implementation of the protocol `SignedNumeric`, useful it you need to track rational numbers or ratios that can be -displayed to a user without loss of precision and with some math. Can be converted to and from -`Float`, `Double` and `String`. - -Not much documentation at the moment, but most of it is self explanatory. -All fractions will be reduce to their simplest form, as a consequence the initialiser of the form `Rational(Int,Int)` -can potentially be slow finding the largest common division for the numerator and denominator. Requires Swift 4.2. - -There is a `RationalNumeric` protocol that `Rational` implements, as a project I was working on might benefit -from representing values as a product of primes for quick factorisation and it was going to implement -the `RationalNumeric` protocol, if I do create this type I will add it to this project. + +Struct, `Rational` to represent a rational number with a numerator and denominator. + +## Features + +RationalNumeric represents type that have a numerator and a denominator, supports basic math operations by implementing the +protocol `SignedNumeric` through the protocol RationalNumeric. Its usefully when you need to deall with ratio without loss of precission. + +The sole type of RationalNumeric is Rational, Rational will be reduce to their simplest form, as a consequence the initialiser +of the form `Rational(Int,Int) can potentially be slow finding the largest common division for the numerator and denominator. Requires Swift 4.2. + +## Including in your project + +To included RationalNumeric in your project, add the files within the folder Type to your project. + +## Examples + +### Creating a Rational number + +```swift +let a = Rational(1,2); +print(a) +// 1/2 + +let b = Rational(2,4); +print(b) +// 1/2 + +let d = Rational(5); +print(d) +// 5 + +let f : Rational = 7; +print(f) +// 7 +``` +### Rational math + +```swift +let a = Rational(1,2); +let b = Rational(2,5); + +let mul = a*b; +print(mul) +// 1/5 + +let add = a+b; +print(add) +// 9/10 + +let div = a/b; +print(div) +// 5/4 + +let sub = a-b; +print(sub) +// 1/10 +``` +### Comparing two Rational numbers + +```swift +let a = Rational(1,2); +let b = Rational(3,6); +print(a==b) +// true +print(a