为什么浮点数有0的符号?

时间:2021-04-24 11:34:35

Why do doubles have -0 as well as +0? What is the background and significance?

为什么双打有-0和+0?背景和意义是什么?

4 个解决方案

#1


86  

-0 is (generally) treated as 0 *******. It can result when a negative floating-point number is so close to zero that it can be considered 0 (to be clear, I'm referring to arithmetic underflow, and the results of the following computations are interpreted as being exactly ±0, not just really small numbers). e.g.

-0(一般)被视为0 ******* *。它可以当一个负面结果浮点数是如此接近于零,它可以被认为是0(很明显,我指的是算术下溢,和下面的计算的结果被解释为±0,不只是很小的数字)。如。

System.out.println(-1 / Float.POSITIVE_INFINITY);
-0.0

If we consider the same case with a positive number, we will receive our good old 0:

如果我们考虑同样的情况与一个正数,我们将收到我们的好旧的0:

System.out.println(1 / Float.POSITIVE_INFINITY);
0.0

******* Here's a case where using -0.0 results in something different than when using 0.0:

******* **这里有一个例子,使用-0.0会产生与使用0.0时不同的结果:

System.out.println(1 / 0.0);
System.out.println(1 / -0.0);
Infinity
-Infinity

This makes sense if we consider the function 1 / x. As x approaches 0 from the +-side, we should get positive infinity, but as it approaches from the --side, we should get negative infinity. The graph of the function should make this clear:

如果我们考虑函数1 / x,这是有道理的,当x从+端接近0时,我们应该得到正无穷,但是当它从-端接近时,我们应该得到负无穷。函数的图形应该表明:

为什么浮点数有0的符号?

(source)

(源)

In math-terms:

在数学术语:

为什么浮点数有0的符号?

为什么浮点数有0的符号?

This illustrates one significant difference between 0 and -0 in the computational sense.

这说明了在计算意义上0和-0之间的一个显著差异。


Here are some relevant resources, some of which have been brought up already. I've included them for the sake of completeness:

这里有一些相关的资源,其中一些已经被提及。为了完整起见,我将它们包括在内:

#2


29  

From Wikipedia

从*

Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = +0 = 0. In computing, existes the concept of existence of two zeros in some numbers representations, usually denoted by −0 and '+0', representing negative zero and +0 positive zero, respectively (source).

有符号0和一个相关的符号。在普通算术,−0 = + 0 = 0。在计算、跌水存在两个零的概念在一些数字表示,通常用−0和+ 0,分别代表负0和+ 0积极零(源)。

This occurs in the sign and magnitude and ones' complement signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, but can be represented by either +0 or −0.

这发生在整数的符号、幅度和1的补符号数字表示法,以及大多数浮点数表示法中。0是通常编码+ 0,但可以用+ 0或−0。

According to the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual (numerical) comparison operators, like the == operators of C and Java. (source).

根据IEEE 754标准,负零和正零应该与通常的(数值)比较运算符进行比较,如C和Java的==运算符。(来源)。

When you have a floating-point operation that produces a results that is a negative floating-point number close to zero, but that can not be represented (in computing) it is produce a "-0.0". For example -5.0 / Float.POSITIVE_INFINITY -> -0.0.

当你有一个浮点运算产生一个接近于零的负浮点数的结果时,但它不能被(在计算中)表示,它会产生一个“-0.0”。例如-5.0 / Float。POSITIVE_INFINITY - > -0.0。

This distinguish, in -0.0, and +0.0, gives you more information than simply giving you an final result 0. Of course this concept it "only" exists in a finite representation system like the one use in computers. In mathematical you can represent any number, even if it is very very near to zero.

这个-0.0和+0.0的区别,给出的信息比给出最终结果0要多。当然,这个概念“只”存在于有限表示系统中,就像计算机中使用的那样。在数学中,你可以表示任何数,即使它非常接近于零。

−0 and +0 are result from operations that cause underflows, similar −00 or +00 are result from operations that cause overflow. For the operations that cause mathematically indetermination the result ins NaN (e.g. 0/0).

−0 + 0从操作,结果导致下溢,类似−00或+ 00源于操作导致溢出。对于引起数学上不确定结果的运算(例如,0/0)。

What's the difference between -0.0 and 0.0?

-0和0。0的差是多少?

In reality both are represent 0. Furthermore, (-0.0 == 0.0) returns true. Nevertheless:

实际上它们都代表0。此外,(-0.0 = 0.0)返回true。然而:

1) 1/-0.0 produces -Infinity while 1/0.0 produces Infinity.

1) 1/-0.0产生-∞,1/0产生∞。

2) 3 * (+0) = +0 and +0/-3 = -0. The sign rules is apply, when performing multiplications or division over a signed zero.

2) 3 *(+0) = +0 +0/-3 = -0。当执行乘法或除带符号的零时,则应用符号规则。

Mandatory reading "What Every Computer Scientist Should Know About Floating-Point Arithmetic" (suggest in the comments).

强制阅读“每个计算机科学家都应该知道的浮点算术”(建议在评论中)。

#3


25  

See the section on "Signed Zero" in What Every Computer Scientist Should Know About Floating-Point Arithmetic

请参阅每一个计算机科学家都应该知道的关于浮点运算的“符号零”一节

Zeros in Java float and double do not just represent true zero. They are also used as the result for any calculation whose exact result has too small a magnitude to be represented. There is a big difference, in many contexts, between underflow of a negative number and underflow of a positive number. For example, if x is a very small magnitude positive number, 1/x should be positive infinity and 1/(-x) should be negative infinity. Signed zero preserves the sign of underflow results.

Java float和double中的0并不仅仅表示真正的0。它们也被用来作为任何计算的结果,其确切结果太小,无法表示。在很多情况下,负数的底流和正数的底流有很大的不同。例如,如果x是一个很小的正数,1/x应该是正无穷,1/(-x)应该是负无穷。有符号零保留了底流结果的符号。

#4


8  

The canonical reference for the usefulness of signed zeros in floating-point is Kahan's paper "Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit" (and some of his talks on the subject).

在浮点数中有符号的零的有用性的典型参考是Kahan的论文“对于复杂的基本函数的分支切割,或者是关于没有任何符号的Ado”(以及他关于这个主题的一些讨论)。

The short version is that in reasonably common engineering applications, the sign information that is preserved by having signed zero is necessary to get correct solutions from numerical methods. The sign of zero has little meaning for most real operations, but when complex-valued functions are considered, or conformal mappings are used, the sign of zero may suddenly become quite critical.

简而言之,在相当常见的工程应用中,符号信息通过符号零被保留,从数值方法中得到正确的解是必要的。对于大多数实际操作来说,0的符号没有什么意义,但是当考虑复值函数或使用正形映射时,0的符号可能会突然变得非常重要。

It's also worth noting that the original (1985) IEEE-754 committee considered, and rejected, supporting a projective mode for floating-point operations, under which there would only be a single unsigned infinity (+/-0 would be semantically identical in such a mode, so even if there were still two encodings, there would only be a single zero as well).

同样值得注意的是,原来的ieee - 754(1985)委员会认为,和拒绝,支持浮点操作的投影模式下,只会有一个无符号∞(+ / 0将在这样一个语义相同的模式,因此,即使仍有两个编码,只会有一个零)。

#1


86  

-0 is (generally) treated as 0 *******. It can result when a negative floating-point number is so close to zero that it can be considered 0 (to be clear, I'm referring to arithmetic underflow, and the results of the following computations are interpreted as being exactly ±0, not just really small numbers). e.g.

-0(一般)被视为0 ******* *。它可以当一个负面结果浮点数是如此接近于零,它可以被认为是0(很明显,我指的是算术下溢,和下面的计算的结果被解释为±0,不只是很小的数字)。如。

System.out.println(-1 / Float.POSITIVE_INFINITY);
-0.0

If we consider the same case with a positive number, we will receive our good old 0:

如果我们考虑同样的情况与一个正数,我们将收到我们的好旧的0:

System.out.println(1 / Float.POSITIVE_INFINITY);
0.0

******* Here's a case where using -0.0 results in something different than when using 0.0:

******* **这里有一个例子,使用-0.0会产生与使用0.0时不同的结果:

System.out.println(1 / 0.0);
System.out.println(1 / -0.0);
Infinity
-Infinity

This makes sense if we consider the function 1 / x. As x approaches 0 from the +-side, we should get positive infinity, but as it approaches from the --side, we should get negative infinity. The graph of the function should make this clear:

如果我们考虑函数1 / x,这是有道理的,当x从+端接近0时,我们应该得到正无穷,但是当它从-端接近时,我们应该得到负无穷。函数的图形应该表明:

为什么浮点数有0的符号?

(source)

(源)

In math-terms:

在数学术语:

为什么浮点数有0的符号?

为什么浮点数有0的符号?

This illustrates one significant difference between 0 and -0 in the computational sense.

这说明了在计算意义上0和-0之间的一个显著差异。


Here are some relevant resources, some of which have been brought up already. I've included them for the sake of completeness:

这里有一些相关的资源,其中一些已经被提及。为了完整起见,我将它们包括在内:

#2


29  

From Wikipedia

从*

Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = +0 = 0. In computing, existes the concept of existence of two zeros in some numbers representations, usually denoted by −0 and '+0', representing negative zero and +0 positive zero, respectively (source).

有符号0和一个相关的符号。在普通算术,−0 = + 0 = 0。在计算、跌水存在两个零的概念在一些数字表示,通常用−0和+ 0,分别代表负0和+ 0积极零(源)。

This occurs in the sign and magnitude and ones' complement signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, but can be represented by either +0 or −0.

这发生在整数的符号、幅度和1的补符号数字表示法,以及大多数浮点数表示法中。0是通常编码+ 0,但可以用+ 0或−0。

According to the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual (numerical) comparison operators, like the == operators of C and Java. (source).

根据IEEE 754标准,负零和正零应该与通常的(数值)比较运算符进行比较,如C和Java的==运算符。(来源)。

When you have a floating-point operation that produces a results that is a negative floating-point number close to zero, but that can not be represented (in computing) it is produce a "-0.0". For example -5.0 / Float.POSITIVE_INFINITY -> -0.0.

当你有一个浮点运算产生一个接近于零的负浮点数的结果时,但它不能被(在计算中)表示,它会产生一个“-0.0”。例如-5.0 / Float。POSITIVE_INFINITY - > -0.0。

This distinguish, in -0.0, and +0.0, gives you more information than simply giving you an final result 0. Of course this concept it "only" exists in a finite representation system like the one use in computers. In mathematical you can represent any number, even if it is very very near to zero.

这个-0.0和+0.0的区别,给出的信息比给出最终结果0要多。当然,这个概念“只”存在于有限表示系统中,就像计算机中使用的那样。在数学中,你可以表示任何数,即使它非常接近于零。

−0 and +0 are result from operations that cause underflows, similar −00 or +00 are result from operations that cause overflow. For the operations that cause mathematically indetermination the result ins NaN (e.g. 0/0).

−0 + 0从操作,结果导致下溢,类似−00或+ 00源于操作导致溢出。对于引起数学上不确定结果的运算(例如,0/0)。

What's the difference between -0.0 and 0.0?

-0和0。0的差是多少?

In reality both are represent 0. Furthermore, (-0.0 == 0.0) returns true. Nevertheless:

实际上它们都代表0。此外,(-0.0 = 0.0)返回true。然而:

1) 1/-0.0 produces -Infinity while 1/0.0 produces Infinity.

1) 1/-0.0产生-∞,1/0产生∞。

2) 3 * (+0) = +0 and +0/-3 = -0. The sign rules is apply, when performing multiplications or division over a signed zero.

2) 3 *(+0) = +0 +0/-3 = -0。当执行乘法或除带符号的零时,则应用符号规则。

Mandatory reading "What Every Computer Scientist Should Know About Floating-Point Arithmetic" (suggest in the comments).

强制阅读“每个计算机科学家都应该知道的浮点算术”(建议在评论中)。

#3


25  

See the section on "Signed Zero" in What Every Computer Scientist Should Know About Floating-Point Arithmetic

请参阅每一个计算机科学家都应该知道的关于浮点运算的“符号零”一节

Zeros in Java float and double do not just represent true zero. They are also used as the result for any calculation whose exact result has too small a magnitude to be represented. There is a big difference, in many contexts, between underflow of a negative number and underflow of a positive number. For example, if x is a very small magnitude positive number, 1/x should be positive infinity and 1/(-x) should be negative infinity. Signed zero preserves the sign of underflow results.

Java float和double中的0并不仅仅表示真正的0。它们也被用来作为任何计算的结果,其确切结果太小,无法表示。在很多情况下,负数的底流和正数的底流有很大的不同。例如,如果x是一个很小的正数,1/x应该是正无穷,1/(-x)应该是负无穷。有符号零保留了底流结果的符号。

#4


8  

The canonical reference for the usefulness of signed zeros in floating-point is Kahan's paper "Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit" (and some of his talks on the subject).

在浮点数中有符号的零的有用性的典型参考是Kahan的论文“对于复杂的基本函数的分支切割,或者是关于没有任何符号的Ado”(以及他关于这个主题的一些讨论)。

The short version is that in reasonably common engineering applications, the sign information that is preserved by having signed zero is necessary to get correct solutions from numerical methods. The sign of zero has little meaning for most real operations, but when complex-valued functions are considered, or conformal mappings are used, the sign of zero may suddenly become quite critical.

简而言之,在相当常见的工程应用中,符号信息通过符号零被保留,从数值方法中得到正确的解是必要的。对于大多数实际操作来说,0的符号没有什么意义,但是当考虑复值函数或使用正形映射时,0的符号可能会突然变得非常重要。

It's also worth noting that the original (1985) IEEE-754 committee considered, and rejected, supporting a projective mode for floating-point operations, under which there would only be a single unsigned infinity (+/-0 would be semantically identical in such a mode, so even if there were still two encodings, there would only be a single zero as well).

同样值得注意的是,原来的ieee - 754(1985)委员会认为,和拒绝,支持浮点操作的投影模式下,只会有一个无符号∞(+ / 0将在这样一个语义相同的模式,因此,即使仍有两个编码,只会有一个零)。

相关文章