X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=python%2Frational.py;h=5a9ee629b3bcdc56328f9ecc9b9a4951d4fabe56;hb=69f0ec479a6ade46d0a227755bb02562112c6743;hp=e3c93d9c17c55745d352f132325ce63cea793de8;hpb=5983075187669cba3eea0268b9eeda69f7ed2e94;p=lilypond.git diff --git a/python/rational.py b/python/rational.py index e3c93d9c17..5a9ee629b3 100644 --- a/python/rational.py +++ b/python/rational.py @@ -2,7 +2,6 @@ from __future__ import division -import decimal as _decimal import math as _math def _gcf(a, b): @@ -39,13 +38,18 @@ class Rational(object): raise TypeError('denominator must have integer type') if not denominator: raise ZeroDivisionError('rational construction') - # Store the fraction in reduced form as _n/_d - factor = _gcf(numerator, denominator) - self._n = numerator // factor - self._d = denominator // factor + self._d = denominator + self._n = numerator + self.normalize_self() + # Cancel the fraction to reduced form + def normalize_self(self): + factor = _gcf(self._n, self._d) + self._n = self._n // factor + self._d = self._d // factor if self._d < 0: self._n = -self._n self._d = -self._d + def numerator(self): return self._n