0和0的差值是多少?

时间:2022-04-17 13:53:54

In C++, for example fmod(-2,2) returns -0. The expression -0 == 0 is true, but the bits are different. What is the purpose of having something like -0 which should be 0 but is represented differently? Is -0 used exactly the same way as 0 in any computations?

在c++中,例如fmod(-2,2)返回-0。表达式-0 = 0是正确的,但是位元是不同的。得到-0的目的是什么? -0应该是0,但是用不同的方式表示?在任何计算中-0和0的用法都是一样的吗?

4 个解决方案

#1


13  

No, +0 and -0 are not used in the same way in every computation. For example:

不,+0和-0在每次计算中都没有相同的用法。例如:

3·(+0) = +0
+0/-3 = -0

I suggest you to read What Every Computer Scientist Should Know About Floating-point arithmetic by David Goldberg, that sheds a light on why +0 and -0 are needed in floating point arithmetic and in which way they differ.

我建议你阅读David Goldberg的每一个计算机科学家都应该知道的浮点算法,这就解释了为什么浮点算法需要+0和-0,以及它们的不同之处。

Examples on how +0 ad -0 differ (and why that can be useful when dealing with complex values) can be found in Kahan, W. 1987. Branch Cuts for Complex Elementary Functions, in "The State of the Art in Numerical Analysis" (I wasn't able to find a pdf of this article, you may find one at your local university library).

在Kahan, W. 1987中可以找到关于+0 ad -0的不同之处(以及在处理复杂值时为什么它是有用的)的示例。在《数值分析的艺术现状》(我找不到这篇文章的pdf格式,你可以在当地的大学图书馆找到它)中,分支为复杂的基本函数进行裁剪。

#2


10  

The Signed Zero Wikipedia page will answer most of those questions:

签署了零*的页面将回答大多数这些问题:

Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = 0. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero). This occurs in some signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, however it can be represented by either +0 or −0.

有符号0和一个相关的符号。在普通算术,−0 = 0。然而,在计算一些数字表示允许存在的两个零,经常用−0(负0)和+ 0(积极零)。这发生在整数的一些有符号数表示中,在大多数浮点数表示中。0是通常编码+ 0,但是它可以用+ 0或者−0。

The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0. The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0.

IEEE 754标准浮点运算(目前使用的大多数计算机和编程语言支持浮点数)需要+ 0和−0。可以视为零扩展实数线的一种变体,0 = 1 /−−∞1 / + 0 = +∞,除零只是定义为±0 /±0。

(...)

(…)

It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems, in particular when computing with complex elementary functions.

据称,在IEEE 754中包含符号零,使得在某些关键问题中,特别是在计算复杂的初等函数时,更容易实现数值精度。

#3


3  

IEEE Standard 754 allows both +0 and -0. Same mantissa, different sign. They should be the same in computations.

IEEE标准754允许+0和-0。同样的尾数,不同的信号。它们在计算中应该是相同的。

#4


-3  

i believe the negative sign is caused by fmod (mis?)implementation, where the sign bit is handled explicitly and attached back to the result at the end of processing.

我认为负符号是由fmod (mis?)实现引起的,其中符号位被显式地处理并在处理结束时附加回结果。

#1


13  

No, +0 and -0 are not used in the same way in every computation. For example:

不,+0和-0在每次计算中都没有相同的用法。例如:

3·(+0) = +0
+0/-3 = -0

I suggest you to read What Every Computer Scientist Should Know About Floating-point arithmetic by David Goldberg, that sheds a light on why +0 and -0 are needed in floating point arithmetic and in which way they differ.

我建议你阅读David Goldberg的每一个计算机科学家都应该知道的浮点算法,这就解释了为什么浮点算法需要+0和-0,以及它们的不同之处。

Examples on how +0 ad -0 differ (and why that can be useful when dealing with complex values) can be found in Kahan, W. 1987. Branch Cuts for Complex Elementary Functions, in "The State of the Art in Numerical Analysis" (I wasn't able to find a pdf of this article, you may find one at your local university library).

在Kahan, W. 1987中可以找到关于+0 ad -0的不同之处(以及在处理复杂值时为什么它是有用的)的示例。在《数值分析的艺术现状》(我找不到这篇文章的pdf格式,你可以在当地的大学图书馆找到它)中,分支为复杂的基本函数进行裁剪。

#2


10  

The Signed Zero Wikipedia page will answer most of those questions:

签署了零*的页面将回答大多数这些问题:

Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = 0. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero). This occurs in some signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, however it can be represented by either +0 or −0.

有符号0和一个相关的符号。在普通算术,−0 = 0。然而,在计算一些数字表示允许存在的两个零,经常用−0(负0)和+ 0(积极零)。这发生在整数的一些有符号数表示中,在大多数浮点数表示中。0是通常编码+ 0,但是它可以用+ 0或者−0。

The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0. The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0.

IEEE 754标准浮点运算(目前使用的大多数计算机和编程语言支持浮点数)需要+ 0和−0。可以视为零扩展实数线的一种变体,0 = 1 /−−∞1 / + 0 = +∞,除零只是定义为±0 /±0。

(...)

(…)

It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems, in particular when computing with complex elementary functions.

据称,在IEEE 754中包含符号零,使得在某些关键问题中,特别是在计算复杂的初等函数时,更容易实现数值精度。

#3


3  

IEEE Standard 754 allows both +0 and -0. Same mantissa, different sign. They should be the same in computations.

IEEE标准754允许+0和-0。同样的尾数,不同的信号。它们在计算中应该是相同的。

#4


-3  

i believe the negative sign is caused by fmod (mis?)implementation, where the sign bit is handled explicitly and attached back to the result at the end of processing.

我认为负符号是由fmod (mis?)实现引起的,其中符号位被显式地处理并在处理结束时附加回结果。