Setting a maximum demoninator for fractions
|
10-04-2023, 04:59 PM
Post: #12
|
|||
|
|||
RE: Setting a maximum demoninator for fractions
(10-04-2023 02:33 PM)Albert Chan Wrote: DOT([p,q], [d1,-n1]) = (d1*q) * (p/q - n1/d1) Perhaps we could use MOD again, to improve accuracy greatly! Let s = DOT([p,q], [d1,-n1]) = ±1, trivial to get (see previous post) DOT([p,q,2*x], [d1,n1,-d1*q]) = p*d1 + q*n1 - 2*x*d1*q = s + 2*q*(n1 - d1*x) We decompose n1 using MOD: n1 = n2 + d2*x: n1/d1 is a convergent of x → sign(x) = sign(n1) → trunc/floor mod, n2 = (n1 MOD x) is exact x ≈ n1/d1 d1*x ≈ n1 = n2 + d2*x n2 ≈ (d1-d2)*x Since |n2| < |x|, (d1-d2) = 0 or 1, no need to use dot product. Either case, (n2 - (d1-d2)*x) is exact (Sterbenz lemma) We pick smaller denominator convergent, if product of 2 DOT's ≥ 0: s * (s + 2*q*(n1 - d1*x)) ≥ 0 1 + 2*s*q*(n1 - d1*x) ≥ 0 s*q*(n1 - d1*x) = s*q*(n2 - (d1-d2)*x) ≥ -1/2 CF, version 3: Code: EXPORT CF(x, dmax) // closest fraction HOME> CF(PI,3000000000) → [9253131363,2945363191] Test is now much more accurate: -0.5890726382 ≥ -0.5 is false --> pick semi-convergent Confirm in Speedcrunch calculator (v 0.12) x = 0.14159265359 n1 = 10225711 d1 = 72219220 p = 417041790 q = 2945363191 s = p*d1 - q*n1 → -1 s*q*(n1 - d1*x) → -0.5890726382 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: