简体   繁体   English

零值上的一元运算符 - () - c ++

[英]Unary Operator-() on zero values - c++

I wrote this code to overload the unary operator- on a matrix class: 我写了这个代码来重载矩阵类上的一元运算符:

const RegMatrix RegMatrix::operator-()const{
    RegMatrix result(numRow,numCol);
    int i,j;
    for(i=0;i<numRow;++i)
        for(j=0;j<numCol;++j){
            result.setElement(i,j,(-_matrix[i][j]));
        }

        return result;
}

When i ran my program with debugger in visual studio, it showed me that when the operation is done on a double equals zero, it inserts the result matrix the number -0.00000. 当我在visual studio中使用调试器运行我的程序时,它向我显示当操作在double等于零时完成时,它会将结果矩阵插入数字-0.00000。 Is it some weird VS-display feature, or is it something i should handle carefully? 它是一些奇怪的VS显示功能,还是我应该小心处理的东西?

Signed zero is zero with an associated sign. 带符号的零为零,带有相关符号。 In ordinary arithmetic, −0 = +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). 在普通算术中,-0 = + 0 = 0.然而,在计算中,一些数字表示允许存在两个零,通常用-0(负零)和+0(正零)表示。 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表示。

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. 用于浮点运算的IEEE 754标准(目前大多数计算机和支持浮点数的编程语言使用)都需要+0和-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. 零可以被认为是扩展实数线的变体,使得1 / -0 =-∞和1 / + 0 = +∞,除以0仅对于±0 /±0未定义。

Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. 负符号零回应从下面接近0的数学分析概念作为单侧限制,其可以由x→0-,x→0-或x→↑0表示。 The notation "−0" may be used informally to denote a small negative number that has been rounded to zero. 符号“-0”可以非正式地用于表示已舍入为零的小负数。 The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines. 负零的概念在统计力学和其他学科中也有一些理论应用。

It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems, 1 in particular when computing with complex elementary functions.[2] 据称,在IEEE 754中包含有符号零点可以更容易地在一些关键问题中实现数值精度, 1尤其是在使用复杂的基本函数进行计算时。[2] On the other hand, the concept of signed zero runs contrary to the general assumption made in most mathematical fields (and in most mathematics courses) that negative zero is the same thing as zero. 另一方面,有符号零运算的概念与大多数数学领域(以及大多数数学课程)中的一般假设相反,即负零与零相同。 Representations that allow negative zero can be a source of errors in programs, as software developers do not realize (or may forget) that, while the two zero representations behave as equal under numeric comparisons, they are different bit patterns and yield different results in some operations. 一些允许负零的表示可能是程序中的错误来源,因为软件开发人员没有意识到(或可能忘记)虽然两个零表示在数字比较下表现相同,但它们是不同的位模式并且在一些中产生不同的结果操作。

For more information see Signed Zero wiki page. 有关更多信息,请参阅签名零维基页面。

使用double( IEEE754 ),定义了正负零。

Well for doubles actually have different values for '0.0' and '-0.0' I think it makes perfect sense.... 好吧,对于双打实际上有'0.0'和'-0.0'的不同值我认为这是完全合理的....

What different result did you expect? 你期望得到什么不同的结果?

As ereOn said, you've got a negative zero: 正如ereOn所说,你有一个负零:

#include <stdio.h>
int main()
{
    printf("%f\n", -0.0);
}

-0 and 0 are the same thing, and it is nothing to worry about. -0和0是一回事,没什么好担心的。 Floating point numbers have the capability to have both a positive and negative 0, for math reasons. 出于数学原因,浮点数能够具有正0和负0。 But -0 is interpreted the same way as 0 in C/C++ arithmetic. 但-0的解释方式与C / C ++算术中的0相同。

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

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