原始浮点值如何为-0.0?这意味着什么?

How can a primitive float value be -0.0? What does that mean?(原始浮点值如何为-0.0?这意味着什么?)
本文介绍了原始浮点值如何为-0.0?这意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

为什么原始浮点值可以是-0.0?这是什么意思?
我可以取消该功能吗?

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

当我有:

  float fl;  

然后 fl == -0.0 返回 truefl == 0 也是如此.但是当我打印它时,它会打印 -0.0.

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

推荐答案

因为 Java 使用 IEEE 浮点标准-Point Arithmetic (IEEE 754),它定义了 -0.0 以及何时应该使用它.

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

可表示的最小数在次正规有效数中没有 1 位,由符号确定,称为正或 负零.它实际上表示在零和相同符号的最小可表示非零数之间的范围内的数字四舍五入到零,这就是它有符号的原因,以及它的倒数 +Inf 或 -Inf 也有符号.

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.

您可以通过添加 0.0

例如

Double.toString(value + 0.0);

参见:Java浮点数的复杂性

涉及负零的操作
...
(-0.0) + 0.0 -> 0.0

Operations Involving Negative Zero
...
(-0.0) + 0.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 it cannot be represented normally.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)