-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNumberExpressible.swift
42 lines (33 loc) · 1.06 KB
/
NumberExpressible.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// NumberExpressible.swift
// EquationKit
//
// Created by Alexander Cyon on 2018-08-22.
// Copyright © 2018 Sajjon. All rights reserved.
//
import Foundation
public protocol NumberExpressible: Numeric, Negatable, AbsoluteConvertible, Hashable, Comparable {
static func + (lhs: Self, rhs: Self) -> Self
static func * (lhs: Self, rhs: Self) -> Self
static func - (lhs: Self, rhs: Self) -> Self
static func / (lhs: Self, rhs: Self) -> Self
var asInteger: Int { get }
func raised(to exponent: Self) -> Self
func mod(_ modulus: Self, mode: ModulusMode) -> Self
static var zero: Self { get }
static var one: Self { get }
var isNegative: Bool { get }
var isPositive: Bool { get }
var shortFormat: String { get }
}
public extension NumberExpressible {
var isZero: Bool {
return self == .zero
}
}
public extension NumberExpressible {
func modIfNeeded(_ modulus: Modulus<Self>?) -> Self {
guard let modulus = modulus else { return self }
return mod(modulus.number, mode: modulus.mode)
}
}