原始浮点值如何为-0.0?那是什么意思? [英] How can a primitive float value be -0.0? What does that mean?

查看:159
本文介绍了原始浮点值如何为-0.0?那是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个原始的浮点值如何能够为-0.0?这是什么意思?

我可以取消这个功能吗?



当我有:

  float fl; 

然后 fl == -0.0 code> true ,所以 fl == 0 。但是,当我打印它时,它打印 -0.0

解决方案

Java使用浮点运算IEEE标准(IEEE 754),它定义了 -0.0 以及何时应该使用。
$ b


最小数字表示没有1位低于正常的有效数,被称为正数或负数零符号。它实际上是表示在0和同一符号的最小可表示的非零数字之间的范围内舍入到零的数字,这就是为什么它有一个符号,为什么它的倒数+ Inf或 - Inf还有一个标志。

你可以通过添加 0.0

  Double.toString(value + 0.0) ; 

请参阅:


涉及负零的操作 >

...

(-0.0)+ 0.0 - > 0.0

-
$ b


- 0.0是在浮点运算产生一个如此接近0的负浮点数而不能产生正常表示。


How come a primitive float value can be -0.0? What does that mean?
Can I cancel that feature?

When I have:

  float fl;  

Then fl == -0.0 returns true and so does fl == 0. But when I print it, it prints -0.0.

解决方案

Because Java uses the IEEE Standard for Floating-Point Arithmetic (IEEE 754) which defines -0.0 and when it should be used.

The smallest number representable has no 1 bit in the subnormal significand and is called the positive or negative zero as determined by the sign. It actually represents a rounding to zero of numbers in the range between zero and the smallest representable non-zero number of the same sign, which is why it has a sign, and why its reciprocal +Inf or -Inf also has a sign.

You can get around your specific problem by adding 0.0

e.g.

Double.toString(value + 0.0);

See: Java Floating-Point Number Intricacies

Operations Involving Negative Zero
...
(-0.0) + 0.0 -> 0.0

-

"-0.0" is produced when a floating-point operation results in a negative floating-point number so close to 0 that cannot be represented normally.

这篇关于原始浮点值如何为-0.0?那是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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