Select Page
Latest 1.5.1 https://github.com/igormatyushkin014/SwiftyAlgebra MIT ios 9.0 Igor Matyushkin

## At a Glance

Provides set of tools for managing numbers in Swift.

## How To Get Started

• Copy content of `Source` folder to your project.

or

• Use `SwiftyAlgebra` cocoapod

## Requirements

• iOS 9 and later
• Xcode 9 and later
• Swift 4

## Usage

### Introduction to Numbers

Instead of using many separate primitive numeric types, `SwiftyAlgebra` uses a universal type named `Number` which is working with all primitive types like `Double` values under the hood. The `Number` class is highly compatible with primitive types and currently supports:

• `Int`
• `Float`
• `Double`

Initializing new number is pretty simple:

``````let integerNumber = Number(value: 10)

let someFloatValue: Float = 4.3
let floatNumber = Number(value: someFloatValue)

let doubleNumber = Number(value: 9.124)``````

Also, you can get primitive value back easily:

``````let number = Number(value: 9.124)
number.int // 9
number.float // 9.124
number.double // 9.124``````

### Integer numbers

Check if number is integer:

``````let double1 = Number(value: 9.124)
double1.isInteger // false

let double2 = Number(value: 9.0)
double2.isInteger // true``````

Check if number is natural:

``````let int = Number(value: -20)
int1.isNatural // false

let double = Number(value: 20.0)
double.isNatural // true``````

Check if number is prime:

``````let int1 = Number(value: 10)
int1.isPrime // false

let int2 = Number(value: 3)
int2.isPrime // true``````

Natural divisors:

``````let int = Number(value: 16)
let divisors = int.naturalDivisors // [1, 2, 4, 8, 16]``````

Prime divisors:

``````let int = Number(value: 150)
let divisors = int.primeDivisors // [2, 3, 5]``````

Prime factorization:

``````let int = Number(value: 150)
let divisors = int.primeFactorization // [2, 3, 5, 5]``````

### Operators

One `Number` can be easily added to another:

``````let ten = Number(value: 10)
let two = Number(value: 2)
let sum = ten + two
sum.int // 12``````

In purpose of better compatibility with primitive numeric types, `SwiftyAlgebra` provides set of operators that might be helpful when you need to use `Number` and primitive variable in the same expression:

``````let number = Number(value: 10) + 2
number.int // 12``````

Full list of supported operators:

Left Type Operator Right Type Example
Number + Number ``` Number(value: 10) + Number(value: 2) ```
Number + Int ``` Number(value: 10) + 2 ```
Number + Float ``` Number(value: 10) + 2.0 ```
Number + Double ``` Number(value: 10) + 2.0 ```
Int + Number ``` 10 + Number(value: 2) ```
Float + Number ``` 10.0 + Number(value: 2) ```
Double + Number ``` 10.0 + Number(value: 2) ```
Number += Number ``` Number(value: 10.0) += Number(value: 2) ```
Number += Int ``` Number(value: 10.0) += 2 ```
Number += Float ``` Number(value: 10.0) += 2.0 ```
Number += Double ``` Number(value: 10.0) += 2.0 ```
Int += Number ``` var value: Int = 10; value += Number(value: 2) ```
Float += Number ``` var value: Float = 10.0; value += Number(value: 2.0) ```
Float += Number ``` var value: Double = 10.0; value += Number(value: 2.0) ```
Float ++ None ``` let number = Number(value: 10); number++ ```
Subtraction
Number Number ``` Number(value: 10) - Number(value: 2) ```
Number Int ``` Number(value: 10) - 2 ```
Number Float ``` Number(value: 10) - 2.0 ```
Number Double ``` Number(value: 10) - 2.0 ```
Int Number ``` 10 - Number(value: 2) ```
Float Number ``` 10.0 - Number(value: 2) ```
Double Number ``` 10.0 - Number(value: 2) ```
Number -= Number ``` Number(value: 10.0) -= Number(value: 2) ```
Number -= Int ``` Number(value: 10.0) -= 2 ```
Number -= Float ``` Number(value: 10.0) -= 2.0 ```
Number -= Double ``` Number(value: 10.0) -= 2.0 ```
Int -= Number ``` var value: Int = 10; value -= Number(value: 2) ```
Float -= Number ``` var value: Float = 10.0; value -= Number(value: 2.0) ```
Float -= Number ``` var value: Double = 10.0; value -= Number(value: 2.0) ```
Float None ``` let number = Number(value: 10); number-- ```
Multiplication
Number * Number ``` Number(value: 10) * Number(value: 2) ```
Number * Int ``` Number(value: 10) * 2 ```
Number * Float ``` Number(value: 10) * 2.0 ```
Number * Double ``` Number(value: 10) * 2.0 ```
Int * Number ``` 10 * Number(value: 2) ```
Float * Number ``` 10.0 * Number(value: 2) ```
Double * Number ``` 10.0 * Number(value: 2) ```
Number *= Number ``` Number(value: 10.0) *= Number(value: 2) ```
Number *= Int ``` Number(value: 10.0) *= 2 ```
Number *= Float ``` Number(value: 10.0) *= 2.0 ```
Number *= Double ``` Number(value: 10.0) *= 2.0 ```
Int *= Number ``` var value: Int = 10; value *= Number(value: 2) ```
Float *= Number ``` var value: Float = 10.0; value *= Number(value: 2.0) ```
Float *= Number ``` var value: Double = 10.0; value *= Number(value: 2.0) ```
Division
Number / Number ``` Number(value: 10) / Number(value: 2) ```
Number / Int ``` Number(value: 10) / 2 ```
Number / Float ``` Number(value: 10) / 2.0 ```
Number / Double ``` Number(value: 10) / 2.0 ```
Int / Number ``` 10 / Number(value: 2) ```
Float / Number ``` 10.0 / Number(value: 2) ```
Double / Number ``` 10.0 / Number(value: 2) ```
Number /= Number ``` Number(value: 10.0) /= Number(value: 2) ```
Number /= Int ``` Number(value: 10.0) /= 2 ```
Number /= Float ``` Number(value: 10.0) /= 2.0 ```
Number /= Double ``` Number(value: 10.0) /= 2.0 ```
Int /= Number ``` var value: Int = 10; value /= Number(value: 2) ```
Float /= Number ``` var value: Float = 10.0; value /= Number(value: 2.0) ```
Float /= Number ``` var value: Double = 10.0; value /= Number(value: 2.0) ```

`SwiftyAlgebra` is available under the MIT license. See the LICENSE file for more info.

### Latest podspec

```{
"name": "SwiftyAlgebra",
"version": "1.5.1",
"summary": "Algebra implemented in Swift",
"description": "Provides set of tools for managing numbers in Swift.",
"homepage": "https://github.com/igormatyushkin014/SwiftyAlgebra",
"screenshots": "https://github.com/igormatyushkin014/SwiftyAlgebra/raw/master/Images/logo_2048_600.png",
"type": "MIT",
},
"authors": {
"Igor Matyushkin": "[email protected]"
},
"platforms": {
"ios": "9.0"
},
"source": {
"git": "https://github.com/igormatyushkin014/SwiftyAlgebra.git",
"tag": "1.5.1"
},
"source_files": [
"Source",
"Source/**/*"
],
"swift_version": "4.0"
}```