问题描述
我正在尝试执行与,但使用R而不是Python.
I am trying to do something similar to what is discussed in this post, but in R rather than Python.
但是:
require(MASS)
fractions(0.723618,max.denominator = 1000000)
#[1] 89/123
这似乎表明,分数89/123比361809/500000更好地描述了浮点数0.723618,这对我来说似乎不正确.
This seems to indicate that floating point number 0.723618 is better described by fraction 89/123 than by 361809/500000, which does not seem correct to me.
更令人困惑的是:
fractions(0.7236,max.denominator = 100000000000)
#[1] 89/123
当然最好将0.7236写为1809/5000,不是吗?
Surely it would be better to write 0.7236 as 1809/5000, wouldn't it?
您知道为什么会这样吗?您认为这是正常"吗?
对于上下文:我问是因为这在尝试为浮点数的向量找到公分母< = 1000000时会引起问题,可能要使用浮点数将其转换为具有指定整数的向量最小有效数字位数.
这些奇怪的分母的出现使得分母向量的LCM非常大.
For context: I'm asking because this causes issues when trying to find a common denominator <= 1000000 for a vector of floating point numbers, which one may want to use to convert them to a vector of integers with a specified minimal number of significant digits.
The appearance of these weird denominators makes the LCM of the vector of denominators very large.
编辑:乔恩·斯普林(Jon Spring)的建议的后续行动
EDIT : follow-up from Jon Spring's suggestion
for (i in 1:18) (print(fractions(0.723618,cycles=i)))
#[1] 1
#[1] 2/3
#[1] 3/4
#[1] 5/7
#[1] 8/11
#[1] 13/18
#[1] 21/29
#[1] 34/47
#[1] 55/76
#[1] 89/123
#[1] 144/199
#[1] 40121/55445
#[1] 40265/55644
#[1] 80386/111089
#[1] 281423/388911
#[1] 361809/5e+05
#[1] 361809/5e+05
#[1] 361809/5e+05
但是:
fractions(0.3333,cycles=1)
#[1] 1/3
fractions(0.3333,cycles=10)
#[1] 1/3
fractions(0.3333,cycles=100)
#[1] 1/3
fractions(0.3333,cycles=100,max.denominator = 1000)
#[1] 1/3
fractions(0.3333,cycles=100,max.denominator = 10000)
#[1] 3333/10000
所以看来,确实两个参数max.denominator
和cycles
决定了分母的大小,但是乍一看,这种关系看起来并不十分直接.
So it seems that indeed, the two parameters max.denominator
and cycles
somehow determine how large the denominator can become, but at first sight the relationship does not look very straightforward.
推荐答案
这是仅考虑最大分母并通过将cycles
分配给大数而忽略它的一种方法:
This is one way of considering only max.denominator and disregarding cycles
by assigning it to a big number:
library(purrr)
max.denominator = 1000000
decimal_num <- 0.723618
max_cycles <- 1000
do_not_know_cycles <- map(1:max_cycles,
function(x) {
try <- fractions(decimal_num,cycles = x,
max.denominator = max.denominator)
nom_denom <- strsplit(attr(try,"fracs"),"/")
int_denom <- nom_denom[[1]][2]
list_return <- list(try,int_denom)
})
binded_ <- do_not_know_cycles %>%
do.call(rbind,.)
cc <- max.denominator - as.numeric(binded_[,2])
indx <- which.min(cc[cc>=0])
out <- do_not_know_cycles[[indx]][1]
out
如前所述,您只需将max_cycles
放到足够大的位置并指定max.denominator
即可为给定的十进制带来最大分母.
As said, you need only to put max_cycles
big enough and specify max.denominator
which will bring the max-denominator fraction for a given decimal.
以下是0.723618
的最大分母输出:1000000,100000,100
Here are the outputs of 0.723618
for max.denominators: 1000000,100000,100
> out
[[1]]
[1] 361809/5e+05
> out
[[1]]
[1] 40265/55644
> out
[[1]]
[1] 55/76
这篇关于使用R中的MASS :: fractions将浮点数转换为分数时的奇怪结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!