Ruby 中浮点的一致舍入

发布于 2024-08-15 17:43:56 字数 275 浏览 3 评论 0原文

据我了解,由于浮点的不精确表示,以下代码“感觉”不一致。

"%.1f" % 1.14 # => 1.1
"%.1f" % 1.15 # => 1.1
"%.1f" % 1.16 # => 1.2
"%.0f" % 1.4 # => 1
"%.0f" % 1.5 # => 2
"%.0f" % 1.6 # => 2

然而,有没有一种简单的方法可以使浮点数四舍五入一致?一种方法可能是显式地进行字符串操作。有更简单的方法或现有的库吗?

I understand due to the inexact representation of floating points, the following code 'feels' inconsistent.

"%.1f" % 1.14 # => 1.1
"%.1f" % 1.15 # => 1.1
"%.1f" % 1.16 # => 1.2
"%.0f" % 1.4 # => 1
"%.0f" % 1.5 # => 2
"%.0f" % 1.6 # => 2

However, is there an easy way of doing consistent floating points rounding by 5? One way might be to do string manipulation explicitly. Is there an easier way or existent library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

高冷爸爸 2024-08-22 17:43:56

如果您想要小数精度,请使用 BigDecimal 而不是浮点数。

编辑:在将数字传递给%之前,您必须手动将数字舍入到所需的长度,否则它会在舍入之前转换为普通浮点数。

"%.1f" % BigDecimal('1.15').round(1) => "1.2"
"%.0f" % BigDecimal('1.5').round(0) => "2"

If you want decimal precision, use BigDecimal instead of floats.

Edit: You will have to manually round the number to the desired length before passing it to %, otherwise it gets converted to a normal float before being rounded.

"%.1f" % BigDecimal('1.15').round(1) => "1.2"
"%.0f" % BigDecimal('1.5').round(0) => "2"
你的笑 2024-08-22 17:43:56

只需添加一个微小的扰动,即可确保浮点值略低于 0.5 的部分变得刚好结束。

例如,

x = 1.15
"%.1f" % (1.000001*x)  # include correction for imprecise floating-point.

这足以处理格式问题,同时不太可能导致相关错误。

另外:这是我之前的问题的明显后续这里,这很好,但包含在完整性。

Just add a tiny pertubation, to ensure things that are just under 0.5 in floating-point become just over.

For example,

x = 1.15
"%.1f" % (1.000001*x)  # include correction for imprecise floating-point.

this will be enough to deal with the formatting problems, while very unlikely to cause a relevant error.

also: an obvious follow-on to my earlier question here, which is fine, but included for completeness.

谁人与我共长歌 2024-08-22 17:43:56

此示例中的函数 roundthis() 展示了如何以可控、一致的方式对数字进行舍入。注意小软糖值。尝试在不做任何改动的情况下运行这个示例,看看会发生什么。

def roundthis(x, m)
    return (x/m+0.50001).floor*m
end

for x in [1.14, 1.15, 1.16]
    print "#{x}   #{roundthis(x, 0.1)}  \n"
end

for x in [1.4, 1.5, 1.6]
    print "#{x}   #{roundthis(x, 1.0)}  \n"
end

将其放入名为 roundtest.rb 的文件中并执行打印。

bash> ruby roundtest.rb
1.14   1.1  
1.15   1.2  
1.16   1.2  
1.4   1.0  
1.5   2.0  
1.6   2.0  

请注意舍入到最接近的 2、15、0.005 或其他值的容易程度。

The function roundthis() in this example shows how to round numbers in a controllable, consistent way. Note the small fudge value. Try running this example without the fudge to see what happens.

def roundthis(x, m)
    return (x/m+0.50001).floor*m
end

for x in [1.14, 1.15, 1.16]
    print "#{x}   #{roundthis(x, 0.1)}  \n"
end

for x in [1.4, 1.5, 1.6]
    print "#{x}   #{roundthis(x, 1.0)}  \n"
end

This, put into a file named roundtest.rb and executed prints

bash> ruby roundtest.rb
1.14   1.1  
1.15   1.2  
1.16   1.2  
1.4   1.0  
1.5   2.0  
1.6   2.0  

Note the ease of rounding to the nearest 2, 15, 0.005, or whatever.

网白 2024-08-22 17:43:56

乘以 100,然后四舍五入,然后除以 100:

(1.15 * 100).round / 100.0 # => 1.15

这不太优雅,但它避免了使用字符串。

Multiply by 100, then round, then divide by 100:

(1.15 * 100).round / 100.0 # => 1.15

It's not exactly elegant, but it avoids using strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文