MCPcopy Create free account
hub / github.com/geekcomputers/Python / simplifyFraction

Function simplifyFraction

primelib/primelib.py:566–590  ·  view source on GitHub ↗

input: two integer 'numerator' and 'denominator' assumes: 'denominator' != 0 returns: a tuple with simplify numerator and denominator.

(numerator, denominator)

Source from the content-addressed store, hash-verified

564
565
566def simplifyFraction(numerator, denominator):
567 """
568 input: two integer 'numerator' and 'denominator'
569 assumes: 'denominator' != 0
570 returns: a tuple with simplify numerator and denominator.
571 """
572
573 # precondition
574 assert (
575 isinstance(numerator, int)
576 and isinstance(denominator, int)
577 and (denominator != 0)
578 ), "The arguments must been from type int and 'denominator' != 0"
579
580 # build the greatest common divisor of numerator and denominator.
581 gcdOfFraction = gcd(abs(numerator), abs(denominator))
582
583 # precondition
584 assert (
585 isinstance(gcdOfFraction, int)
586 and (numerator % gcdOfFraction == 0)
587 and (denominator % gcdOfFraction == 0)
588 ), "Error in function gcd(...,...)"
589
590 return (numerator // gcdOfFraction, denominator // gcdOfFraction)
591
592
593# -----------------------------------------------------------------

Callers

nothing calls this directly

Calls 1

gcdFunction · 0.85

Tested by

no test coverage detected