一元运算符-()零值-C ++ [英] Unary Operator-() on zero values - c++

查看:108
本文介绍了一元运算符-()零值-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;
}

当我在Visual Studio中使用调试器运行程序时,它向我展示了当对等于零的double进行运算时,它将插入结果矩阵-0.00000. 是某些奇怪的VS显示功能,还是我应该仔细处理的事情?

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. Is it some weird VS-display feature, or is it something i should handle carefully?

推荐答案

有符号的零为零,且有关联 标志.在普通算术中,−0 = +0 =0.但是,在计算中,某些数字表示形式允许 存在两个零,通常表示为 分别为-0(负零)和+0(正) 零).这发生在一些签名 整数的数字表示, 并且在大多数浮点数中 表示形式.数字0是 通常编码为+0,但是可以 用+0或-0表示.

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). 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.

用于浮动的IEEE 754标准 点算术(目前由 大多数计算机和编程 支持浮点的语言 数字)需要+0和-0.这 零可以视为变体 扩展实数线的 1/−0 =-∞和1/+ 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.

负号为零的回显 数学分析的概念 从下面接近0 单边极限,可以表示为 x→0−,x→0−或x→↑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. The notation "−0" may be used informally to denote a small negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines.

据称包含 IEEE 754中的零符号使它变得非常有用 更容易达到数值精度 在某些严重问题中, 1 在 特别是在复杂的计算中 基本功能.[2]在另一 手,零号运行的概念 与一般假设相反 在大多数数学领域(和 在大多数数学课程中) 负零与 零.允许的陈述 负零可能是 程序错误,作为软件 开发人员没有意识到(或可能 忘了),而两个零 表示在以下情况下表现均等 数值比较,它们是 不同的位模式和产量 在某些操作中会产生不同的结果.

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] 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.

有关更多信息,请参见签名为零 Wiki页面.

For more information see Signed Zero wiki page.

这篇关于一元运算符-()零值-C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆