算法挑战:生成浮点数的连分数
(编辑:为了回应脾气暴躁的评论,不,这不是家庭作业。我正在研究音调检测,获取一系列潜在的谐波峰值,并尝试构建基频的候选者。所以,这实际上是一个非常实际的问题。)
考虑(例如)pi 的最佳分数近似值,按分母递增排序:3/1、22/7、355/113,...
挑战:创建一个tidy C 算法,将为给定浮点数生成第 n 个商近似值 a/b,同时返回差异。
calcBestFrac(float frac, int n, int * a, int * b, float * err) {...}
我认为最好的技术是连分数
去掉 pi 的小数部分,然后你得到 3
现在,余数是 0.14159... = 1/7.06251..
所以下一个最佳有理数是 3 + 1/7 = 22/7
从 7.06251 中去掉 7,得到 0.06251.. 大约为 1/15.99659..
称其为 16,那么下一个最佳近似值是
3 + 1/(7 + 1/16) = 355/113
然而,将其转换为干净的 C 代码绝非易事。如果我得到一些整洁的东西,我会发布。与此同时,有人可能会喜欢它作为一个脑筋急转弯。
(EDIT: In response to grumpy comments, No it isn't homework. I am working on pitch detection, taking an array of potential harmonic peaks, and attempting to construct candidates for fundamental frequency. So, it is actually a very practical question.)
Consider the best fractional approximations for (eg) pi, ordered by increasing denominator: 3/1, 22/7, 355/113, ...
Challenge: create a tidy C algorithm that will generate the n'th quotient approximation a/b for a given float, returning also the discrepancy.
calcBestFrac(float frac, int n, int * a, int * b, float * err) {...}
The best technique I believe is continued fractions
Take away the fractional part of pi, and you get 3
Now, the remainder is 0.14159... = 1/7.06251..
So the next best rational is 3 + 1/7 = 22/7
Take away the 7 from 7.06251 and you get 0.06251.. Roughly 1/15.99659..
Call it 16, then the next best approximation is
3 + 1/(7 + 1/16) = 355/113
However, this is far from trivial to convert into clean C code. I will post if I get something tidy. In the meanwhile, someone may enjoy it as a brainteaser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[因为您要求将此作为答案而不是评论。]
对于任何实数,其连分数的收敛 p[k]/q[k] 始终是最佳有理近似,但它们并不都是最好的有理近似。要获得所有这些,您还必须采用半收敛/中位数 - 形式为
(p[k]+n*p[k+1])/(q[k]+n *q[k+1])
对于某个整数 n≥1。取 n=a[k+2] 得到 p[k+2]/q[k+2],并且要取的整数 n 是来自下限(a[k+2]/2)或上限(a[ k+2]/2),到a[k+2]。 维基百科也提到了这一点。近似 π
π 的连分数是 [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2…](OEIS 中的序列 A001203),收敛序列为3/1、22/7、333/106、355/113、103993/33102…(A002485/A002486),最佳近似值的顺序为 3/1, 13/4, 16/5, 19/6, 22/ 7、179/57…(A063674/A063673 )。
所以该算法表示 π = [3; 的最佳近似值7, 15, 1, 292, 1, 1,…] 是
程序
这是一个 C 程序,它给定一个正实数,生成它的连分数、它的收敛项以及最佳有理近似序列。函数
find_cf
求连分数(将项放入 a[] 中,将收敛项放入 p[] 和 q[] 中——请原谅全局变量),函数all_best
打印所有最佳有理近似值。示例
对于 π,这是该程序的输出,大约 0.003 秒(即,它确实比循环遍历所有可能的分母更好!),换行以提高可读性:
所有这些术语都是正确的,但如果您增加 MAX,您就会开始得到由于精度而产生的误差。仅用 13 个收敛就能得到如此多的项,这给我留下了深刻的印象。 (正如你所看到的,有一个小错误,它有时不会打印第一个“n/1”近似值,或者打印不正确 - 我将其留给你修复!)
你可以尝试使用 √2,其连分数为 [1; 2, 2, 2, 2…]:
或者黄金比例 φ = (1+√5)/2 ,其连分数为 [1; 1, 1, 1, …]:(
看到斐波那契数了吗?这里的收敛都是近似值。)
或者有理数,如 4/3 = [1; 3]:
或 14/11 = [1; 3,1,2]:
享受吧!
[Since you asked for this as an answer rather than a comment.]
For any real number, the convergents p[k]/q[k] of its continued fraction are always best rational approximations, but they aren't all the best rational approximations. To get all of them, you also have to take the semi-convergents/mediants — fractions of the form
(p[k]+n*p[k+1])/(q[k]+n*q[k+1])
for some integer n≥1. Taking n=a[k+2] gives p[k+2]/q[k+2], and the integers n to take are those from either floor(a[k+2]/2) or ceiling(a[k+2]/2), to a[k+2]. This is also mentioned on Wikipedia.Approximating π
The continued fraction for π is [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2…] (sequence A001203 in OEIS), the sequence of convergents is 3/1, 22/7, 333/106, 355/113, 103993/33102… (A002485/A002486), and the sequence of best approximations is 3/1, 13/4, 16/5, 19/6, 22/7, 179/57… (A063674/A063673).
So the algorithm says that the best approximations of π = [3; 7, 15, 1, 292, 1, 1,…] are
Program
Here's a C program that given a positive real number, generates its continued fraction, its convergents, and the sequence of best rational approximations. The function
find_cf
finds the continued fraction (putting the terms in a[] and the convergents in p[] and q[] — excuse the global variables), and the functionall_best
prints all the best rational approximations.Examples
For π, here's the output of this program, in about 0.003 seconds (i.e., it's truly better than looping through all possible denominators!), line-wrapped for readability:
All these terms are correct, though if you increase MAX you start getting errors because of precision. I'm myself impressed with how many terms you get with only 13 convergents. (As you can see, there's a small bug where it sometimes doesn't print the very first "n/1" approximation, or prints it incorrectly — I leave it for you to fix!)
You can try with √2, whose continued fraction is [1; 2, 2, 2, 2…]:
Or the golden ratio φ = (1+√5)/2 whose continued fraction is [1; 1, 1, 1, …]:
(See the Fibonacci numbers? Here the convergents are all the approximants.)
Or with rational numbers like 4/3 = [1; 3]:
or 14/11 = [1; 3, 1, 2]:
Enjoy!
C 程序很好,除了您不能信任对余数的检查这一事实之外,从计算 x*pq 也可以看出:
The C program is fine, apart from the fact that you cannot trust the check on the remainder, as can be seen from computing x*p-q as well: