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

发布于 2024-09-26 03:58:15 字数 407 浏览 18 评论 0原文

我编写了这段代码来重载矩阵类上的一元运算符:

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 中使用调试器运行程序时,它向我显示,当对双精度等于零进行操作时,它会插入结果矩阵 -0.00000 。 这是一些奇怪的 VS 显示功能,还是我应该小心处理?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

调妓 2024-10-03 03:58:15

有符号零是带有关联的零
符号。在普通算术中,−0 = +0
= 0。然而,在计算中,某些数字表示允许
存在两个零,通常表示为
通过 −0(负零)和 +0(正零)
零)。这发生在一些签名中
整数的数字表示,
并且在大多数浮点数中
交涉。数字 0 是
通常编码为+0,但是它可以
用+0或-0表示。

IEEE 754 浮动标准
点算术(目前使用
大多数计算机和编程
支持浮点的语言
数字)需要+0和-0。这
零可以被视为一种变体
扩展实数轴,例如
1/−0 = −∞ 且 1/+0 = +∞,除法
零仅对于 ±0/±0 未定义。

负号零表示
数学分析的概念
从下面逼近 0 作为
单边极限,可以表示为
x → 0−、x → 0− 或 x → ↑0。这
符号“−0”可以非正式地使用
表示一个小的负数
已四舍五入为零。概念
负零也有一些
理论应用
统计力学及其他
学科。

据称,其中包含
IEEE 754 中的带符号零使其意义重大
更容易达到数值精度
在一些关键问题中,1
特别是在进行复杂计算时
初等函数。[2]另一方面
手,有符号零游程的概念
与一般假设相反
在大多数数学领域(以及
在大多数数学课程中)
负零与
零。允许的表示
负零可以是一个来源
程序中的错误,如软件
开发人员没有意识到(或者可能
忘了)那个,而两个零
表示在以下情况下表现相同
数值比较,它们是
不同的位模式和产量
某些操作的结果不同。

有关详细信息,请参阅签名零 wiki 页面。

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.

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.

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.

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.

For more information see Signed Zero wiki page.

猫卆 2024-10-03 03:58:15

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

using double (IEEE754), there is defined positive and negative zero.

能否归途做我良人 2024-10-03 03:58:15

好吧,对于双打来说,“0.0”和“-0.0”实际上有不同的值,我认为这很有意义......

您期望什么不同的结果?

Well for doubles actually have different values for '0.0' and '-0.0' I think it makes perfect sense....

What different result did you expect?

窝囊感情。 2024-10-03 03:58:15

正如 ereOn 所说,你有一个负零:

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

As ereOn said, you've got a negative zero:

#include <stdio.h>
int main()
{
    printf("%f\n", -0.0);
}
小伙你站住 2024-10-03 03:58:15

-0和0是同一个东西,没什么好担心的。出于数学原因,浮点数可以同时具有正数和负数 0。但 -0 的解释方式与 C/C++ 算术中的 0 相同。

-0 and 0 are the same thing, and it is nothing to worry about. Floating point numbers have the capability to have both a positive and negative 0, for math reasons. But -0 is interpreted the same way as 0 in C/C++ arithmetic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文