Is there ever a case where these two methods would return different values given the same inputs?
int compare1(float a, float b)
{
return Double.compare(a, b);
}
int compare2(float a, float b)
{
return Float.compare(a, b);
}
By the same token, is it true (or false) that any number storable in a java’s Float can be stored in a java’s Double without losing any precision?
Thanks
Yes; casting doubles to floats can yield different results.If the difference between
aandbis too small to show up in a float,compare2()will return0whereascompare1()would not.You just edited the question to reverse what you were asking. The new answer is:
I’m almost certain that they will always be the same.