# 从干将莫邪的故事说起--java比较操作注意要点

《搜神记》：

java中的雌雄双剑0与-0

0与-0按理说都是一样的，但在程序中不一定就相等哦。请看下面的程序：

    public static void main(String[] args) {
System.out.println(Float.compare(-0.0f, 0.0f));
System.out.println(Math.max(-0.0f, 0.0f));
System.out.println(Math.min(-0.0f, 0.0));
System.out.println(-0.0f<0.0f);
System.out.println(-0.0f<=0.0f);
}

-10.0-0.0falsetrue

15.20.1. Numerical Comparison Operators <, <=, >, and >=The type of each of the operands of a numerical comparison operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.Binary numeric promotion is performed on the operands (§5.6.2).Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).If the promoted type of the operands is int or long, then signed integer comparison is performed.If the promoted type is float or double, then floating-point comparison is performed.Comparison is carried out accurately on floating-point values, no matter what value sets their representing values were drawn from.The result of a floating-point comparison, as determined by the specification of the IEEE 754 standard, is:If either operand is NaN, then the result is false.All values other than NaN are ordered, with negative infinity less than all finite values, and positive infinity greater than all finite values.Positive zero and negative zero are considered equal.For example, -0.0<0.0 is false, but -0.0<=0.0 is true.Note, however, that the methods Math.min and Math.max treat negative zero as being strictly smaller than positive zero.Subject to these considerations for floating-point numbers, the following rules then hold for integer operands or for floating-point operands other than NaN:The value produced by the < operator is true if the value of the left-hand operand is less than the value of the right-hand operand, and otherwise is false.The value produced by the <= operator is true if the value of the left-hand operand is less than or equal to the value of the right-hand operand, and otherwise is false.The value produced by the > operator is true if the value of the left-hand operand is greater than the value of the right-hand operand, and otherwise is false.The value produced by the >= operator is true if the value of the left-hand operand is greater than or equal to the value of the right-hand operand, and otherwise is false.

Positive zero and negative zero are considered equal.(浮点数中0.0和-0.0进行关系比较是相等的)

For example, -0.0<0.0 is false, but -0.0<=0.0 is true.(例如：-0.0<0.0是false，但-0.0<=0.0 是true)

Note, however, that the methods Math.min and Math.max treat negative zero as being strictly smaller than positive zero.(主意：然而，使用Math.min和Math.max进行浮点数比较时，-0.0时小于0.0的)

java中浮点数在循环中使用要慎重

    public static void main(String[] args) {
final int START = 2000000000;
int count = 0;
for (float f = START; f < START + 50; f++)
count++;
System.out.println(count);
}

f的初始值接近于Integer.MAX_VALUE，因此它需要用31 位来精确表示，而float 类型只能提供24 位的精度。对如此巨大的一个float 数值进行增量操作将不会改变其值。因此，这个程序看起来应该无限地循环下去，因为f 永远也不可能解决其终止值。但是，如果你运行该程序，就会发现它并没有无限循环下去，事实上，它立即就终止了，并打印出0。怎么回事呢？

f 的初始值太大了，以至于在对其加上50，然后将结果转型为float 时，所产生的数值等于直接将f 转换成float 的数值。换句话说，(float)2000000000 ==2000000050，因此表达式f < START + 50 即使是在循环体第一次执行之前就是false，所以，循环体也就永远的不到机会去运行。