小数负数四舍五入后保持正负号 [英] Small negative number maintains sign after rounding

查看:281
本文介绍了小数负数四舍五入后保持正负号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB似乎记住了原始符号,即使四舍五入的结果为零.由于将较小的负值舍入为零,因此该信号不再有意义.

MATLAB seems to remember the original sign even if the rounded result is zero. The signal does not make sense anymore since the small negative value was rounded to zero.

代码示例:

a = - 0.001;
ar = round(a,2);
fprintf('a: %.2f. a rounded: %.2f. Zero: %.2f\n', a,ar,0);

结果:

a:-0.00.四舍五入:-0.00.零:0.00

a: -0.00. a rounded: -0.00. Zero: 0.00

预期结果:

a:-0.00.四舍五入:0.00.零:0.00

a: -0.00. a rounded: 0.00. Zero: 0.00

推荐答案

这根本不是特定于Matlab的.实际上,所有使用IEEE754进行浮点表示的程序都可能具有这种特殊性.

This is not specific to Matlab at all. In fact, all the programs which use IEEE754 for their floating point representation may have this particularity.

在IEEE-754格式中,有一个符号位.在四舍五入操作期间,此位可能保持不变.因此,即使最后结果是纯0,符号位也会保留.对于这种浮点数格式,这是完全正常的行为:

In the IEEE-754 format, there is a sign bit. This bit is probably left untouched during the rounding operation. So even if the result is a pure 0 in the end, the sign bit remain. This is perfectly normal behaviour for this format of floating point number:

主要文章:零签名

Main article: Signed zero

在IEEE 754标准中,零符号表示存在 既是正零"(+0)又是负零"(-0).多数情况 在运行时环境中,通常将正零打印为"0",并且将 负零为"-0".这两个值在数值上表现相同 比较,但某些操作会返回+0和+的不同结果 −0.例如,1/(− 0)返回负无穷大,而1/+ 0返回 正无穷大(因此保持恒等式1/(1/±∞)=±∞). 在x = 0处有不连续性的其他常见函数可能会处理 对于任何负数y,+ 0和-0不同地包括log(x),signum(x)和y + xi的主平方根.与任何 近似方案,涉及负零"的运算可以 偶尔引起混乱.例如,在IEEE 754中,x = y不 总是暗示1/x = 1/y,因为0 = −0但1/0≠1/−0.

In the IEEE 754 standard, zero is signed, meaning that there exist both a "positive zero" (+0) and a "negative zero" (−0). In most run-time environments, positive zero is usually printed as "0" and the negative zero as "-0". The two values behave as equal in numerical comparisons, but some operations return different results for +0 and −0. For instance, 1/(−0) returns negative infinity, while 1/+0 returns positive infinity (so that the identity 1/(1/±∞) = ±∞ is maintained). Other common functions with a discontinuity at x=0 which might treat +0 and −0 differently include log(x), signum(x), and the principal square root of y + xi for any negative number y. As with any approximation scheme, operations involving "negative zero" can occasionally cause confusion. For example, in IEEE 754, x = y does not always imply 1/x = 1/y, as 0 = −0 but 1/0 ≠ 1/−0.

来源:维基百科Floating_point Signed_zero

现在,由于要求您以浮点格式(%.2f)显示Matlab,因此Matlab在零之前显示一个符号.

Now Matlab display a sign in front of the zero because you asked to display it in a floating point format (%.2f), so Matlab respect the norm and display the sign.

如果让Matlab选择最佳的显示方式,则Matlab会很聪明,并删除零:

If you let Matlab choose the best way to display, Matlab will be smart and drop the zero:

>> disp(ar)
     0

此外,Matlab知道该值是0,如果您查询该值的符号,它将返回正确的值:

Also, Matlab knows that the value is 0 and will return the right value if you query the sign of the value:

>> sign(ar)
ans =
     0

如果

Matlab被认为是负数,它会返回-1,如果它被认为是正数,它会返回+1.因此,尽管在显示时遵守了IEEE-754规范,Matlab并没有感到困惑,并且知道此值没有有意义的符号.

Matlab would return -1 if it was considered negative and +1 if it was considered positive. So despite the respect of the IEEE-754 norm at display time, Matlab is not confused and knows that this value has no meaningful sign.

总而言之,不必担心.它不会带来任何程序性问题或计算问题.

In conclusion, do not worry about it. It is not going to bring any programmatic problem or calculation issue.

如果唯一的麻烦是显示,则另一种解决方法可能只是将绝对值乘以Matlab检测到的sign(因为它检测到正确的符号). 您可以创建一个自定义的舍入函数,如果确实很重要,可以解决该问题:

If your only grief is the display, another workaround could be simply to multiply the absolute value by the sign detected by Matlab (since it detects the right sign). You can make a custom rounding function which could take care of that if really important:

mround = @(x,n) abs(round(x,n))*sign(round(x,n)) ;
arm = mround(a,2) ;
fprintf('a: %.2f. a rounded: %.2f. Zero: %.2f\n', a,arm,0);

a: -0.00. a rounded: 0.00. Zero: 0.00

在这里授予了四舍五入的运算两次,因为我在一个内联函数中使用了该运算,但是如果将它放在具有多行的函数中,则只对该四舍五入进行一次运算,然后使用相同的方法校正该符号.

Granted here the rounding operation is evaluated twice because I used that in an inline function, but if you put that in a function with multiple lines, you evaluate the rounding only once then correct the sign with the same method.

这篇关于小数负数四舍五入后保持正负号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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