简体   繁体   English

浮点数字

[英]Floating Point Numbers

This seems like a real simple question but I just to want clear my doubt. 这似乎是一个非常简单的问题,但我只是想清除我的疑问。 I am looking at code which some other developer wrote. 我正在看其他开发人员编写的代码。 There are some calculations involving floating-point numbers. 有些计算涉及浮点数。

Example: Float fNotAvlbl = new Float(-99); 示例: Float fNotAvlbl = new Float(-99); Why is he creating a new object? 他为什么要创建一个新对象? What would happen if we do Float fNotAvlbl = -99; 如果我们执行Float fNotAvlbl = -99;会发生什么? (-99 is used as flag here to indicate Not Applicable) Later down the code, we define: (-99用作标志,指示不适用)在代码的后面,我们定义:

fltValue1 = 0.00f;
fltValue2 = 0.00f;

and populate these two values with a method call which returns float. 并使用返回float的方法调用填充这两个值。 After that we again convert these two values into Float Objects with: 之后,我们再次将这两个值转换为Float对象:

fltVal1 = new Float(fltValue1); 
fltVal2 = new Float(fltValue2); 

and than do a comparison if(fltVal1.compareTo(fNotAvailable) == 0) do something. 然后比较if(fltVal1.compareTo(fNotAvailable) == 0) do something.
Is it all because compareTo expects Wrapper Class Objects? 这是因为compareTo期望包装器类对象吗?

I apologize if this is a real basic question. 抱歉,这是一个真正的基本问题。

  • You don't need the wrappers at all 您根本不需要包装器
  • Even if you needed them, using the constructor is not preferred - use Float.valueOf(..) instead. 即使您需要它们,也不推荐使用构造函数-而是使用Float.valueOf(..)
  • Writing Float fNotAvlbl = -99; 编写Float fNotAvlbl = -99; relies on autoboxing, which has only been added in Java 5, so older code could not use it. 依赖自动装箱,自动装箱仅在Java 5中添加,因此较旧的代码无法使用它。
  • Using -99 as a Float value to mean "Not Applicable" is really, really bad. 使用-99作为浮点值表示“不适用”确实非常糟糕。 Either use null or Float.Nan 使用nullFloat.Nan
  • fltVal1.compareTo(fNotAvailable) == 0 means exactly the same as fltValue1==fltValue2 fltVal1.compareTo(fNotAvailable) == 0表示与fltValue1==fltValue2
  • Comparing float values for strict equality should not be done because it will often fail to work as expected. 不应对浮点值进行严格相等的比较,因为它经常无法按预期工作。 Read The Float-Point Guide to understand why. 阅读浮点指南以了解原因。

On the subject of what compareTo() does compared with == 关于compareTo()==进行比较的主题

float a = Float.NaN;
float b = Float.NaN;
System.out.println(a + " == " + b + " is " + (a == b));
System.out.println(a + ".compareTo(" + b + ") is " + ((Float) a).compareTo(b));

float c = -0.0f;
float d = 0.0f;
System.out.println(c + " == " + d + " is " + (c == d));
System.out.println(c + ".compareTo(" + d + ") is " + ((Float) c).compareTo(d));

prints 版画

NaN == NaN is false
NaN.compareTo(NaN) is 0
-0.0 == 0.0 is true
-0.0.compareTo(0.0) is -1

compareTo compares the binary representation (after normalising all NaN values to be the same) As the binary representation for -0.0f and 0.0f are different compareTo does not return 0. There is no special handling in the code other that to use floatToIntBits() and compare that instea dof using == compareTo比较二进制表示形式(将所有NaN值标准化为相同之后),因为-0.0f和0.0f的二进制表示形式不同,compareTo不会返回0。除了使用floatToIntBits() ,代码中没有其他特殊处理。并使用==比较该instea自由度

The comparison may be using the Float object rather than the built-in float type because of the inherent issues with floating point comparison . 由于浮点比较的内在问题,比较可能使用Float对象而不是内置的float类型。 Because of the way that floating point numbers are stored on a computer system occasionally the equality comparison between two floating point numbers throws up false negatives. 由于浮点数存储在计算机系统上的方式有时会导致两个浮点数之间的相等性比较产生错误的否定性。 The compareTo from Float may take this into account. FloatcompareTo可能会考虑到这一点。 The original author possibly at least thought it did. 最初的作者可能至少认为这样做了。

You can also write your own floating point comparison algorithm that checks for a difference within a reasonable standard deviation for your system. 您还可以编写自己的浮点比较算法,以检查系统的合理标准偏差内的差异。 You can also use the method used by equals in Float, which looks at the integer value of the floating point bits for each number. 您也可以使用Float中的equals使用的方法,该方法查看每个数字的浮点位的整数值。

Note that using Float in and of itself doesn't fix this problem, as the problem is a round-off error in storage. 请注意,本身使用Float并不能解决此问题,因为该问题是存储中的舍入错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM