From 8fef95c5c2ce6742ee57c0af4f387d0e5003c1d5 Mon Sep 17 00:00:00 2001 From: Jan Schmidt Date: Wed, 14 May 2008 16:00:39 +0000 Subject: [PATCH] gst/__init__.py: Make gst.Fraction simplify like the C counterpart Original commit message from CVS: Patch by: Jan Schmidt * gst/__init__.py: Make gst.Fraction simplify like the C counterpart Fixes #532809 --- ChangeLog | 7 +++++++ gst/__init__.py | 28 +++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 7020bd0ae7..c564ffc65d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-05-14 Edward Hervey + + Patch by: Jan Schmidt + * gst/__init__.py: + Make gst.Fraction simplify like the C counterpart + Fixes #532809 + 2008-05-14 Edward Hervey * gst/gstcaps.override: diff --git a/gst/__init__.py b/gst/__init__.py index 54a441ae09..314379725d 100644 --- a/gst/__init__.py +++ b/gst/__init__.py @@ -77,10 +77,37 @@ class FractionRange(Value): class Fraction(Value): def __init__(self, num, denom=1): + def __gcd(a,b): + while b != 0: + tmp = a + a = b + b = tmp % b + return abs(a) + + def __simplify(): + num = self.num + denom = self.denom + + if num < 0: + num = -num + denom = -denom + + # Compute greatest common divisor + gcd = __gcd(num,denom) + if gcd != 0: + num /= gcd + denom /= gcd + + self.num = num + self.denom = denom + Value.__init__(self, 'fraction') + self.num = num self.denom = denom + __simplify() + def __repr__(self): return '' % (self.num, self.denom) @@ -118,7 +145,6 @@ class Fraction(Value): def __float__(self): return float(self.num) / float(self.denom) - import sys dlsave = sys.getdlopenflags() try: