如何准确将浮子转换为分数?
我正在搜索如何将浮子转换为最简单的分数,该分数将转换回它并找到这个答案。
问题是,给出的Python实现依赖于python标准库中的as_integer_ratio
设施,该设施在Rust中不存在。我在评论中问了有关f64 :: frexp
的问题,但我不确定我是否理解它的工作原理,因为它的文档非常神秘(至少对我来说):
将数字分解为归一分的分数和基本2指数,令人满意:
self = x * 2^exp
0.5< = abs(x)< 1.0
,最重要的是,这是一个不稳定的功能。
我应该怎么办?
I was searching how to convert a float to the simplest fraction that would convert back to it and found this answer.
Problem is, the Python implementation given relies on the as_integer_ratio
facility in the python standard library, which isn't present in Rust. I asked about this in a comment and found out about f64::frexp
but I'm not sure I understand how it works, as its documentation is quite cryptic (to me at least):
Breaks the number into a normalized fraction and a base-2 exponent, satisfying:
self = x * 2^exp
0.5 <= abs(x) < 1.0
And on top of that, it's an unstable feature.
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于浮动到分数转换的复杂之处在于,所有浮子已经具有两个两个分母的理性,但这可能并不是那么有用。您正在寻找的是“ nofollow noreferrer”>“最佳理性近似”某些最大分母内的目标浮点值。
该算法(链接中描述)在其背后有一些巧妙的持续分数数学,但并不难输入代码。这是一个实现它的小型C库:
示例使用:
希望这是直接用于端口或包装以供其他语言使用。
The complicated thing about float-to-fraction conversion is that all floats are already rationals with a power-of-two denominator, but that probably isn't so useful. What you are looking for is "best rational approximation", to find the closest rational to a target float value within some max denominator.
The algorithm (described in the link) has some clever continued fractions math behind it, but not too difficult to put into code. Here is a small C library that implements it:
Example use:
This is hopefully straightforward to port or wrap for use in other languages.
除了算法,我想最简单的方法是使用已经有效的东西,例如
分数
板条板:源自
Algorithm apart, I guess the easiest way would be to use something working already, like the
fraction
crate:From the example