No Division or Multiplication for a Fraction? #71
Answered by orchetect
latenitefilms asked this question in Q&A
-
| It looks like you can add and subtract a Is there any specific reason for this, or is it just not implemented? |
Beta Was this translation helpful? Give feedback.
Answered by orchetect Feb 23, 2024
Replies: 2 comments 3 replies
-
| Ah yes - just haven't had a chance to implement yet. In the meantime you can do one of the following:
|
Beta Was this translation helpful? Give feedback.
1 reply
-
| Maybe it's something as simple as this? import Foundation import TimecodeKit extension Fraction { public static func * (lhs: Self, rhs: Self) -> Self { //--------------------------------------------------------- // Multiply the numerators and denominators: //--------------------------------------------------------- return Fraction(reducing: lhs.numerator * rhs.numerator, lhs.denominator * rhs.denominator) } } extension Fraction { public static func / (lhs: Self, rhs: Self) -> Self { //--------------------------------------------------------- // Check for division by zero scenario: //--------------------------------------------------------- guard rhs.numerator != 0 else { fatalError("Division by zero.") } //--------------------------------------------------------- // Multiply lhs by the reciprocal of rhs: //--------------------------------------------------------- return Fraction(reducing: lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator) } } |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
In essence yes. I can add that as a co-authored commit to save the trouble of needing a PR.
Also needs corresponding unit tests which I can add.
Included in release 2.0.10.