Skip to content

Commit 4eb4c94

Browse files
Fraction: Added * and / math operators between two instances (#71)
Co-Authored-By: Chris Hocking <[email protected]>
1 parent bec5f3d commit 4eb4c94

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Sources/TimecodeKit/Utilities/Fraction.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@ extension Fraction {
206206
let num = (lhs.numerator * lcm.lhsMultiplier) - (rhs.numerator * lcm.rhsMultiplier)
207207
return Fraction(reducing: num, lcm.denominator)
208208
}
209+
210+
public static func * (lhs: Self, rhs: Self) -> Self {
211+
Fraction(reducing: lhs.numerator * rhs.numerator, lhs.denominator * rhs.denominator)
212+
}
213+
214+
public static func / (lhs: Self, rhs: Self) -> Self {
215+
// Check for division by zero scenario
216+
guard rhs.numerator != 0 else {
217+
fatalError("Division by zero.")
218+
}
219+
220+
// Multiply lhs by the reciprocal of rhs
221+
return Fraction(reducing: lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator)
222+
}
209223
}
210224

211225
// MARK: - Double

0 commit comments

Comments
 (0)