Is it valid to compare a double with an int in java?
Solution 1
Yes, it's valid - it will promote the int
to a double
before performing the comparison.
See JLS section 15.20.1 (Numerical Comparison Operators) which links to JLS section 5.6.2 (Binary Numeric Promotion).
From the latter:
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
If either operand is of type double, the other is converted to double.
...
Solution 2
When performing operations (including comparisons) with two different numerical types, Java will perform an implicit widening conversion. This means that when you compare a double
with an int
, the int
is converted to a double
so that Java can then compare the values as two double
s. So the short answer is yes, comparing an int and a double is valid, with a caveat.
The problem is that that you should not compare two floating-piont values for equality using ==
, <=
, or >=
operators because of possible errors in precision. Also, you need to be careful about the special values which a double can take: NaN
, POSITIVE_INFINITY
, and NEGATIVE_INFINITY
. I strongly suggest you do some research and learn about these problems when comparing double
s.
Solution 3
This should be fine. In floating point operation/comparisons, if one argument is floating/double then other one being int is also promoted to the same.
Solution 4
yes it is absolutely valid compare int datatype and double datatype..
int i =10;
double j= 10.0;
if (i==j)
{
System.out.println("IT IS TRUE");
}
Solution 5
Yes it valid, and your code should work as expected without any glitch, but this is not the best practice, static code analyzers like SonarQube shows this as a "Major" "Bug",
Major Bug description from sonarQube
so, the right way to do this can be,
Double.compare(val1,val2)==0
if any parameter are not floating point variable, they will be promoted to floating point.
Tasos
Updated on July 12, 2020Comments
-
Tasos almost 4 years
Utilities.getDistance(uni, enemyuni) <= uni.getAttackRange()
Utilities.getDistance returns double and getAttackRange returns int. The above code is part of an if statement and it needs to be true. So is the comparison valid?
Thanks
-
Tasos over 11 yearsFunny thing is this is my professors code. But yea I will investigate the subject now
-
Code-Apprentice over 11 yearsYou must be careful (and usually even avoid) comparing floating-point values with the
==
operator. -
Code-Apprentice over 11 years@Crone For a school assignment, you probably can ignore these edge cases, but you should still know about them. Choosing to ignore them is preferrably than complete ignorance.
-
Mickäel A. over 11 yearsAnd what do you want to compare these 2 values with ?
-
Marko Topolnik over 11 yearsAll
int
s are exactly representable asdouble
. -
Marko Topolnik over 11 yearsI don't see anything wrong with comparing a
double
with<=
or>=
against anint
. Only exact equality might be questionable. -
Code-Apprentice over 11 years@MarkoTopolnik <= or >= may give undesirable results in some cases, just like == does.
-
Marko Topolnik over 11 years"Some cases" is not as unpredictable as you picture it and when calculating Euclidian distance, it would be perfectly alright.
-
Code-Apprentice over 11 years@MarkoTopolnik Granted, it is not entirely unpredictable, just as it is not with
==
. There is plenty of information available about comparing floating point values for equality, so if the OP needs/wants more detail, they can research it further. -
Tasos over 11 yearsYea I can understand this is a case of way too much detail for a university code. I also completely agree with the fact that tmi can be really harmful when you start to learn a language. Thanks a lot for the input guys
-
Daniel about 9 yearsAll Integers up to 2^52 are exactly represented as double. So there is no problem to compare int with double, even for equality, if you know it stores an integer, which could be not true if you operate over that variable.