简体   繁体   English

Rust f64/f32 是否正确舍入?

[英]Does Rust f64/f32 round correctly?

After a recent discussion about mathematical rounding with my colleagues, we analyzed the rounding of different languages.在最近与我的同事讨论了数学舍入之后,我们分析了不同语言的舍入。

For example:例如:

  • MATLAB: "Round half away from zero" MATLAB:“距离零的一半”
  • Python: "Round half to even" Python:“四舍五入到偶数”

I would not say the one is correct or the other isn't but whats bothers me is the combination of the writing in the Rust Book and what the documentation say about the round function.我不会说一个是正确的或另一个不是,但困扰我的是 Rust 书中的文字和文档中关于round function 的内容的组合。

The book 1 :1

Floating-point numbers are represented according to the IEEE-754 standard.浮点数根据 IEEE-754 标准表示。 The f32 type is a single-precision float, and f64 has double precision. f32 类型为单精度浮点数,f64 为双精度。

The documentation 2 :文档2

Returns the nearest integer to self.将最近的 integer 返回给自己。 Round half-way cases away from 0.0.从 0.0 舍入中途案例。

My concern is that the standard rounding for IEEE-754 is "Round half to even".我担心的是 IEEE-754 的标准舍入是“四舍五入”。

Most collegues who I ask tend to use and learned mostly/only "Round half away from zero" and they where actually confused as I came up with different rounding strategies.我问的大多数同事倾向于使用和学习主要/仅“从零取整一半”,当我想出不同的取整策略时,他们实际上感到困惑。 Does the developer of rust decided because of that possible confusion against the IEEE standard? rust 的开发人员是否因为可能与 IEEE 标准混淆而决定?

Nobody has contradicted IEEE-754, which defines five different valid rounding methods .没有人反对 IEEE-754,它定义了五种不同的有效舍入方法

The two methods relevant to this question are referred to as nearest roundings .与此问题相关的两种方法称为最接近舍入

  • Round to nearest, ties to even – rounds to the nearest value;四舍五入到最接近的值,与偶数相关——四舍五入到最接近的值; if the number falls midway, it is rounded to the nearest value with an even least significant digit.如果该数字在中途下降,则将其四舍五入到具有偶数最低有效数字的最接近的值。
  • Round to nearest, ties away from zero (or ties to away) – rounds to the nearest value;舍入到最近,从零开始(或从零开始)——四舍五入到最接近的值; if the number falls midway, it is rounded to the nearest value above (for positive numbers) or below (for negative numbers).如果数字在中途下降,则将其四舍五入到最接近的值高于(对于正数)或低于(对于负数)。

Python takes the first approach and Rust takes the second. Python 采用第一种方法,Rust 采用第二种方法。 Neither is contradicting the IEEE-754 standard, which defines and allows for both.两者都不与 IEEE-754 标准相矛盾,该标准定义并允许两者。

The other three are things we would probably more colloquially refer to as truncation , ie always rounding down, or always rounding up, or always rounding toward zero.其他三个是我们可能更通俗地称为截断的东西,即总是向下舍入,或总是向上舍入,或总是向零舍入。

The documentation you cite is for an explicit function, round .您引用的文档是针对明确的 function, round

IEEE-754 specifies the default rounding method for floating-point operations should be round-to-nearest, ties-to-even (with some embellishment for an unusual case). IEEE-754 指定浮点运算的默认舍入方法应该是四舍五入到最接近、平到偶数(对不寻常的情况进行一些修饰)。 The rounding method specifies how to adjust (conceptually) the mathematical result of the function or operation to a number representable in the floating-point format.舍入方法指定如何(在概念上)将 function 的数学结果或运算调整为以浮点格式表示的数字。 It does not apply to what functions functions calculate.它不适用于函数计算的函数。

Functions like round , floor , and trunc exist to calculate a specific integer from the argument.存在诸如roundfloortrunc之类的函数来根据参数计算特定的 integer。 The mathematical calculation they perform is to determine that integer.他们执行的数学计算是确定 integer。 A rounding rule only applies in determining what floating-point result to return when the ideal mathematical result is not representable in the floating-point type.当理想的数学结果不能以浮点类型表示时,舍入规则仅适用于确定要返回的浮点结果。

Eg, sin(x) is defined to return a result computed as if:例如, sin(x)被定义为返回计算结果,如下所示:

  • The sine of x were determined exactly, with “infinite” precision. x的正弦以“无限”精度精确确定。
  • That sine were then rounded to a number representable in the floating-point format according to the rounding method.然后根据舍入方法将该正弦四舍五入为可以以浮点格式表示的数字。

Similarly, round(x) can be thought of to be defined to return a result computed as if:类似地,可以认为round(x)被定义为返回一个计算结果,如下所示:

  • The nearest integer of x , rounding a half-way case away from zero, were determined exactly, with “infinite” precision. x的最接近的 integer 以“无限”精度精确确定,从零开始舍入一半大小写。
  • That nearest integer were then rounded to a number representable in the floating-point format according to the rounding method.然后根据舍入方法将最接近的 integer 舍入为可以以浮点格式表示的数字。

However, because of the nature of the routine, that second step is never necessary: The nearest integer is always representable, so rounding never changes it.但是,由于例程的性质,永远不需要第二步:最近的 integer 始终是可表示的,因此舍入永远不会改变它。 (Except, you could have abnormal floating-point formats with limited exponent range so that rounding up did yield an unrepresentable integer. For example, in a format with four-bit significands but an exponent range that limited numbers to less than 4, rounding 3.75 to the nearest integer would yield 4, but that is not representable, so +∞ would have to be returned. I cannot say I have ever seen this case explicitly addressed in a specification of the round function.) (除了,您可能具有指数范围有限的异常浮点格式,因此向上舍入确实会产生无法表示的 integer。例如,在具有四位有效数但指数范围限制数字小于 4 的格式中,四舍五入 3.75到最近的 integer 将产生 4,但这是不可表示的,因此必须返回 +∞。我不能说我曾见过这种情况在round的规范中明确解决。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM