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