|
| 1 | + |
| 2 | +/// Represents a reduced fractional number. |
| 3 | +/// An invariant exists such that it is not possible to create a ``Fraction`` |
| 4 | +/// that is not represented in its most reduced form. |
| 5 | +public struct Fraction: Hashable, Equatable, Sendable { |
| 6 | + public let numerator: Int |
| 7 | + public let denominator: Int |
| 8 | + |
| 9 | + /// Combines the provided `numerator` and `denominator` into a reduced ``Fraction``. |
| 10 | + /// - Warning: Attempts to create a ``Fraction`` with a zero denominator will fatally error. |
| 11 | + public init(numerator: Int, denominator: Int) { |
| 12 | + let gcd = Self.gcd(numerator, denominator) |
| 13 | + self.numerator = numerator / gcd |
| 14 | + self.denominator = denominator / gcd |
| 15 | + } |
| 16 | + |
| 17 | + public var positive: Bool { |
| 18 | + switch (numerator, denominator) { |
| 19 | + // 0/0 is not positive in this logic |
| 20 | + case let (n, d) where n >= 0 && d > 0: true |
| 21 | + |
| 22 | + // Seems like this case can't happen because |
| 23 | + // all Fractions are reduced. |
| 24 | + case let (n, d) where n < 0 && d < 0: true |
| 25 | + |
| 26 | + default: false |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +private extension Fraction { |
| 32 | + static func gcd(_ a: Int, _ b: Int) -> Int { |
| 33 | + // See: https://en.wikipedia.org/wiki/Euclidean_algorithm |
| 34 | + var latestRemainder = max(a, b) |
| 35 | + var previousRemainder = min(a, b) |
| 36 | + |
| 37 | + while latestRemainder != 0 { |
| 38 | + let tmp = latestRemainder |
| 39 | + latestRemainder = previousRemainder % latestRemainder |
| 40 | + previousRemainder = tmp |
| 41 | + } |
| 42 | + return previousRemainder |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +extension Fraction { |
| 48 | + public static func * (lhs: Self, rhs: Self) -> Self { |
| 49 | + Self(numerator: lhs.numerator * rhs.numerator, denominator: lhs.denominator * rhs.denominator) |
| 50 | + } |
| 51 | + |
| 52 | + public static func / (lhs: Self, rhs: Self) -> Self { |
| 53 | + Self(numerator: lhs.numerator * rhs.denominator, denominator: lhs.denominator * rhs.numerator) |
| 54 | + } |
| 55 | + |
| 56 | + public static func + (lhs: Self, rhs: Self) -> Self { |
| 57 | + Self(numerator: (lhs.numerator * rhs.denominator) + (rhs.numerator * lhs.denominator), denominator: lhs.denominator * rhs.denominator) |
| 58 | + } |
| 59 | + |
| 60 | + public static func - (lhs: Self, rhs: Self) -> Self { |
| 61 | + Self(numerator: (lhs.numerator * rhs.denominator) - (rhs.numerator * lhs.denominator), denominator: lhs.denominator * rhs.denominator) |
| 62 | + } |
| 63 | +} |
| 64 | +extension Fraction { |
| 65 | + public static func * (lhs: Self, rhs: Int) -> Self { |
| 66 | + lhs * Self(integerLiteral: rhs) |
| 67 | + } |
| 68 | + |
| 69 | + public static func / (lhs: Self, rhs: Int) -> Self { |
| 70 | + lhs / Self(integerLiteral: rhs) |
| 71 | + } |
| 72 | + |
| 73 | + public static func * (lhs: Int, rhs: Self) -> Self { |
| 74 | + Self(integerLiteral: lhs) * rhs |
| 75 | + } |
| 76 | + |
| 77 | + public static func / (lhs: Int, rhs: Self) -> Self { |
| 78 | + Self(integerLiteral: lhs) / rhs |
| 79 | + } |
| 80 | + |
| 81 | + public static func + (lhs: Self, rhs: Int) -> Self { |
| 82 | + lhs + Self(integerLiteral: rhs) |
| 83 | + } |
| 84 | + |
| 85 | + public static func - (lhs: Self, rhs: Int) -> Self { |
| 86 | + lhs - Self(integerLiteral: rhs) |
| 87 | + } |
| 88 | + |
| 89 | + public static func + (lhs: Int, rhs: Self) -> Self { |
| 90 | + Self(integerLiteral: lhs) + rhs |
| 91 | + } |
| 92 | + |
| 93 | + public static func - (lhs: Int, rhs: Self) -> Self { |
| 94 | + Self(integerLiteral: lhs) - rhs |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +extension Fraction: ExpressibleByIntegerLiteral { |
| 99 | + public typealias IntegerLiteralType = Int |
| 100 | + |
| 101 | + public init(integerLiteral value: Int) { |
| 102 | + self.init(numerator: value, denominator: 1) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +extension Fraction: SignedNumeric { |
| 107 | + |
| 108 | + public init?<T>(exactly source: T) where T : BinaryInteger { |
| 109 | + self.init(integerLiteral: Int(source)) |
| 110 | + } |
| 111 | + |
| 112 | + public static func *= (lhs: inout Fraction, rhs: Fraction) { |
| 113 | + lhs = lhs * rhs |
| 114 | + } |
| 115 | + |
| 116 | + public var magnitude: Fraction { |
| 117 | + Self(numerator: abs(numerator), denominator: abs(denominator)) |
| 118 | + } |
| 119 | + |
| 120 | + public typealias Magnitude = Self |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +extension Fraction { |
| 125 | + public var asDouble: Double { |
| 126 | + Double(numerator) / Double(denominator) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +extension Fraction: Comparable { |
| 131 | + public static func < (lhs: Fraction, rhs: Fraction) -> Bool { |
| 132 | + lhs.numerator * rhs.denominator < rhs.numerator * lhs.denominator |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +extension Fraction: LosslessStringConvertible { |
| 137 | + /// The format for string conversion is: `(<integer>|<integer>)` or `<integer>` |
| 138 | + public init?(_ description: String) { |
| 139 | + if |
| 140 | + description.first == "(", |
| 141 | + description.last == ")" |
| 142 | + { |
| 143 | + let parts = description.dropFirst().dropLast().split(separator: "|").compactMap({ Int(String($0)) }) |
| 144 | + guard |
| 145 | + parts.count == 2, |
| 146 | + let numerator = parts.first, |
| 147 | + let denominator = parts.last |
| 148 | + else { |
| 149 | + return nil |
| 150 | + } |
| 151 | + self.init(numerator: numerator, denominator: denominator) |
| 152 | + } else if let number = Int(description) { |
| 153 | + self.init(integerLiteral: number) |
| 154 | + } else { |
| 155 | + return nil |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public var description: String { |
| 160 | + if denominator == 1 { |
| 161 | + "\(!positive && numerator != 0 ? "-" : "")\(abs(numerator))" |
| 162 | + } else { |
| 163 | + "(\(positive ? "" : "-")\(abs(numerator))|\(abs(denominator)))" |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +extension SignedInteger { |
| 169 | + func over<T: SignedInteger>(_ denominator: T) -> Fraction { |
| 170 | + Fraction(numerator: Int(self), denominator: Int(denominator)) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +extension Int { |
| 175 | + public static func |(_ lhs: Self, _ rhs: Self) -> Fraction { |
| 176 | + Fraction(numerator: lhs, denominator: rhs) |
| 177 | + } |
| 178 | +} |
0 commit comments