Best Practice: Never compare floating-point numbers with ==.
Always compare floating-point numbers as being "close-enough" by testing that the absolute value of their difference is less than some very small number.
>), less-than (<) and equal-to (==) operators. Note that testing for equality in Java is accomplished using the double-equal operator (==). The single-equal operator (=) is reserved for assignment.>=), less-than or equal-to (<=) and not-equal-to (!=). These have the familiar interpretations.| Operator | Type | Description |
|---|---|---|
< |
binary, infix | less-than |
<= |
binary, infix | less-than or equal-to |
> |
binary, infix | greater-than |
>= |
binary, infix | greater-than or equal-to |
== |
binary, infix | equal-to |
!= |
binary, infix | not equal-to |
boolean value (true or false). If you think about that for a moment, you should see the logic in that statement. In general, relational operators involving > or < take numerical operands.jshell> int a = 1, b = 2, c = 3; a ==> 1 | created variable a : int b ==> 2 | created variable b : int c ==> 3 | created variable c : int jshell> a < b; $4 ==> true | created scratch variable $4 : boolean jshell> a > b; $5 ==> false | created scratch variable $5 : boolean jshell> a + b < c; $6 ==> false | created scratch variable $6 : boolean jshell> a + b == c; $7 ==> true | created scratch variable $7 : boolean jshell> a + b <= c; $8 ==> true | created scratch variable $8 : boolean jshell> c > a; $9 ==> true | created scratch variable $9 : boolean jshell> c >= a; $10 ==> true | created scratch variable $10 : boolean jshell> c != a + b; $11 ==> false | created scratch variable $11 : boolean jshell> a < b < c; | Error: | bad operand types for binary operator '<' | first type: boolean | second type: int | a < b < c; | ^-------^ jshell>
a < b < c is common mathematics notation, but it is invalid Java. When evaluating this expression, the left-most operator is evaluated first to the boolean value of true. That is, a < b becomes true. Then the right-most operator is evaluated as true < c. The < operator cannot take a boolean value type as an operand, therefore the expression results in an error.jshell> 0.1 + 0.2 == 0.3; $12 ==> false | created scratch variable $12 : boolean jshell>
false? Is Java broken?0.1 + 0.2.jshell> 0.1 + 0.2; $13 ==> 0.30000000000000004 | created scratch variable $13 : double jshell>
0.1, 0.2, and 0.3 cannot be represented exactly in the underlying representation of a double-precision number. Recall from Section 1.2 that computer memory stores numeric values in binary (base 2 numbers), not base 10. While integer values may be represented exactly in binary, certain fractional floating-point numbers are not.0.3, out to 55 decimal places, is0.299999999999999988897769753748434595763683319091796875.0.1 + 0.2 evaluates to the value0.30000000000000004440892098500626161694526672363281250.1 + 0.2 == 0.3 evaluates to false, and teaches us an important lesson about comparing floating-point numbers.==.0.1 + 0.2 is "close enough" to 0.3.jshell> Math.abs((0.1 + 0.2) - 0.3) < 0.000000000000001; $1 ==> true | created scratch variable $1 : boolean jshell>
jshell> (64 + 51 + 42) / 60 + " hours" $2 ==> "2 hours" | created scratch variable $2 : String jshell> (64 + 51 + 42) % 60 + " minutes" $3 ==> "23 minutes" | created scratch variable $3 : String
jshell> 3*2.55 + 3.10 < 10 $7 ==> false | created scratch variable $7 : boolean jshell> 2*2.55 + 3.10 < 10 $8 ==> true | created scratch variable $8 : boolean
double xc = 0.5, yc = 0.5, r = 0.3, x = 0.6, y = 0.7;
jshell> double xc = 0.5, yc = 0.5, r = 0.3, x = 0.6, y = 0.7; xc ==> 0.5 | created variable xc : double yc ==> 0.5 | created variable yc : double r ==> 0.3 | created variable r : double x ==> 0.6 | created variable x : double y ==> 0.7 | created variable y : double jshell> Math.sqrt( Math.pow(x-xc,2) + Math.pow(y-yc,2)) < r $14 ==> true | created scratch variable $14 : boolean