[discussion on the syntax to create rationals] Are you sure that you really want a special syntax for this rather than just a simply constructor like rat(2,3) ? Just think of how difficult it would be to explain how the following would work (assuming that you use @ as the magical operator): 1 @ 2.3 1.5 @ 1.3 1j+3.4 @ 2 Note that intution isn't going to help here because you are missing a precision indicator. mxNumber has a constructor called FareyRational() which converts floats to rationals: FareyRational(value, maxden) Returns a Rational-object reflecting the given value and using maxden as maximum denominator Here's the algorithm: /* Farey Function This is a GNU MP implementation of the following function which Scott David Daniels posted to the Python Cookbook; http://www.activestate.com/ASPN/Python/Cookbook/Recipe/52317 : def farey(v, lim): '''Named after James Farey, an English surveyor. No error checking on args -- lim = max denominator, results are (numerator, denominator), (1,0) is infinity ''' if v < 0: n,d = farey(-v, lim) return -n,d z = lim-lim # get 0 of right type for denominator lower, upper = (z,z+1), (z+1,z) while 1: mediant = (lower[0] + upper[0]), (lower[1]+upper[1]) if v * mediant[1] > mediant[0]: if lim < mediant[1]: return upper lower = mediant elif v * mediant[1] == mediant[0]: if lim >= mediant[1]: return mediant if lower[1] < upper[1]: return lower return upper else: if lim < mediant[1]: return lower upper = mediant A nice proof of the algorithm can be found at "Cut the Knot": http://www.cut-the-knot.com/blue/Farey.html */ -- Marc-Andre Lemburg CEO eGenix.com Software GmbH _______________________________________________________________________ eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,... Python Consulting: http://www.egenix.com/ Python Software: http://www.egenix.com/files/python/
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4