Created
February 18, 2015 16:05
-
-
Save unau/63ad748a82e9e7d71fe5 to your computer and use it in GitHub Desktop.
与えられた小数に近い整数比を生成するジェネレータ ref: http://qiita.com/unau/items/46a98043557729db45be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import rasi | |
>>> for i in rasi.rasi(0.315): print "%d/%d = %g : %g" % i | |
... | |
1/1 = 1 : 0.685 | |
1/2 = 0.5 : 0.185 | |
1/3 = 0.333333 : 0.0183333 | |
3/10 = 0.3 : 0.015 | |
4/13 = 0.307692 : 0.00730769 | |
5/16 = 0.3125 : 0.0025 | |
6/19 = 0.315789 : 0.000789474 | |
11/35 = 0.314286 : 0.000714286 | |
17/54 = 0.314815 : 0.000185185 | |
23/73 = 0.315068 : 6.84932e-05 | |
40/127 = 0.314961 : 3.93701e-05 | |
63/200 = 0.315 : 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> for i in rasi.rasi(math.pi): print "%d/%d = %g : %g" % i | |
... | |
1/1 = 1 : 2.14159 | |
2/1 = 2 : 1.14159 | |
3/1 = 3 : 0.141593 | |
13/4 = 3.25 : 0.108407 | |
16/5 = 3.2 : 0.0584073 | |
19/6 = 3.16667 : 0.025074 | |
22/7 = 3.14286 : 0.00126449 | |
179/57 = 3.14035 : 0.00124178 | |
201/64 = 3.14062 : 0.000967654 | |
223/71 = 3.14085 : 0.000747583 | |
245/78 = 3.14103 : 0.000567013 | |
267/85 = 3.14118 : 0.000416183 | |
289/92 = 3.1413 : 0.000288306 | |
311/99 = 3.14141 : 0.000178512 | |
333/106 = 3.14151 : 8.32196e-05 | |
355/113 = 3.14159 : 2.66764e-07 | |
52163/16604 = 3.14159 : 2.66213e-07 | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rasi(target): | |
a, b = 1, 1 | |
min_error = -1 | |
while True: | |
ratio = float(b) / a | |
diff = target - ratio | |
error = abs(diff) | |
if min_error < 0 or min_error > error: | |
min_error = error | |
yield (b, a, ratio, error) | |
if error == 0: | |
return | |
if diff > 0: | |
b += 1 | |
else: | |
a += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment