We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent df8b641 commit 9095233Copy full SHA for 9095233
src/quicktions.pyx
@@ -569,12 +569,15 @@ cdef class Fraction:
569
n, d = d, n-a*d
570
571
k = (max_denominator-q0)//q1
572
- bound1 = Fraction(p0+k*p1, q0+k*q1)
573
- bound2 = Fraction(p1, q1)
574
- if abs(bound2 - self) <= abs(bound1-self):
575
- return bound2
+
+ # Determine which of the candidates (p0+k*p1)/(q0+k*q1) and p1/q1 is
+ # closer to self. The distance between them is 1/(q1*(q0+k*q1)), while
+ # the distance from p1/q1 to self is d/(q1*self._denominator). So we
576
+ # need to compare 2*(q0+k*q1) with self._denominator/d.
577
+ if 2*d*(q0+k*q1) <= self._denominator:
578
+ return Fraction(p1, q1, _normalize=False)
579
else:
- return bound1
580
+ return Fraction(p0+k*p1, q0+k*q1, _normalize=False)
581
582
@property
583
def numerator(self):
0 commit comments