Skip to content

Commit

Permalink
Improved Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanday committed Oct 29, 2018
1 parent 328b3da commit 409298e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 12 deletions.
140 changes: 128 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<b)
// false
```
### Getting the numerator and denominator of a Rational numbers

```swift
let a = Rational(12,18);
print(a.numerator)
// 2
print(a.denominator)
// 3
```
### Creating a Rational from a floating point number

```swift
let a = Rational( 0.251, maxDenominator: 16 );
print(a)
// 1/4
let b = Rational( 0.251, maxDenominator: 256 )
print(a)
// 63/251
```

### Converting a Rational to other Types

```swift
let a = Rational( 3,2 );
let d = Double(a);
print(d)
// 1.5
let i = Int(a);
print(i)
// 1
let s = String(a);
print(s)
// "3/2"
```

## Deployment

Add additional notes about how to deploy this on a live system

## Built With

* [Dropwizard](http://www.dropwizard.io/1.0.2/docs/) - The web framework used
* [Maven](https://maven.apache.org/) - Dependency Management
* [ROME](https://rometools.github.io/rome/) - Used to generate RSS Feeds

## Contributing

Please read [CONTRIBUTING.md](https://gist.github.com/PurpleBooth/b24679402957c63ec426) for details on our code of conduct, and the process for submitting pull requests to us.

## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags).

## Authors

* **Nathaniel Day** - [PurpleBooth](https://github.com/PurpleBooth)

## License

This project is licensed under the MIT License
2 changes: 2 additions & 0 deletions RationalNumeric.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
D8039D5221870AEB0065C995 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
D85B4B8320FCAD8100A7224A /* RationalNumeric */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = RationalNumeric; sourceTree = BUILT_PRODUCTS_DIR; };
D85B4B8620FCAD8100A7224A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
D85B4B8E20FCAE2B00A7224A /* RationalNumeric.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RationalNumeric.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -63,6 +64,7 @@
children = (
D85B4B8520FCAD8100A7224A /* RationalNumeric */,
D85B4B8D20FCAE2B00A7224A /* Types */,
D8039D5221870AEB0065C995 /* README.md */,
D85B4B9720FCAE9F00A7224A /* RationalNumericTests */,
D85B4B8420FCAD8100A7224A /* Products */,
);
Expand Down
2 changes: 2 additions & 0 deletions RationalNumeric/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ print("Double(Rational(\(a.numerator,a.denominator))) = \(d)")
let i = Int(a)
print("Double(Rational(\(a.numerator,a.denominator))) = \(i)")

let f = Rational( 0.251, maxDenominator: 16 );
print("Rational( 0.251, maxDenominator: 16 )=\(f)");

0 comments on commit 409298e

Please sign in to comment.