impl/python/calculate_rational.py
843 bytes · the Python implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from .math_rational_add_rational import add_rational ← addRational, another function of this group · built into the same file, even by a slim install
from .math_rational_divide_rational import divide_rational ← divideRational, another function of this group · built into the same file, even by a slim install
from .math_rational_multiply_rational import multiply_rational ← multiplyRational, another function of this group · built into the same file, even by a slim install
from .math_rational_subtract_rational import subtract_rational ← subtractRational, another function of this group · built into the same file, even by a slim install
from .math_rational_types import Rational, RationalOp
def calculate_rational(a: Rational, op: RationalOp, b: Rational) -> Rational:
"""Add, subtract, multiply or divide two fractions exactly.
The result is always reduced with a positive denominator, so equal values
have equal fields.
"""
if op == "add":
return add_rational(a, b)
if op == "subtract":
return subtract_rational(a, b)
if op == "multiply":
return multiply_rational(a, b)
if op == "divide":
return divide_rational(a, b)
raise ValueError('unknown operation "%s"' % (op,))