how to compare double

• 06-17-2014, 11:47 AM
009
how to compare double
hi how can i compare two double values

is this correct

Code:

public boolean isEqualToD(double s_src, double s_compareTo)
{

boolean flag = false;
try
{

if(s_src == s_compareTo)
{
flag = true;
}
} catch (NullPointerException npe)
{
logger.log(Level.FATAL, npe.getMessage(), npe);
return false;
}

return flag;
}

• 06-17-2014, 12:32 PM
makpandian
Re: how to compare double
Yes You are right.

In general, if both variables are primitive, better use ==, and if both variables are objects, then better use equals() method for comparison. Keep this in your mind. It will obviously be useful in future
• 06-17-2014, 02:23 PM
Tolls
Re: how to compare double
Well, the whole try/catch is completely unnecessary as there's no possible exception that can be thrown from those statements.
• 06-17-2014, 02:30 PM
gimbal2
Re: how to compare double
Quote:

Originally Posted by Tolls
Well, the whole try/catch is completely unnecessary as there's no possible exception that can be thrown from those statements.

Except for the ParanoiaException and YouNeverKnowError
• 06-17-2014, 03:29 PM
jim829
Re: how to compare double
That works! But why write a method? Just do the comparison.

Code:

if(s_src == s_compareTo) {
// do something
} else {
// do something else
}

If you want to retain the comparison state, you can do the following:

Code:

boolean isEqualToD = s_src == s_compareTo;
//
if (isEqualToD) {
// do something
} else {
// do something else
}

Regards,
Jim
• 06-17-2014, 03:29 PM
JosAH
Re: how to compare double
Theoretically speaking you can compare two doubles for equality with the == operator, but be careful:

Code:

double a= 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;

System.out.println(a == 1.0);

This code snippet prints 'false' while mathematically 10*0.1 == 1.0; better test for an absolute error or relative error:

Code:

double absError= Math.abs(a-b);
double max= Math.max(Math.abs(a), Math.abs(b));
double relError= Math.abs((a-b)/max);

If both a and b are zero, take care not to use the second formula blindly, otherwise, check if relError and/or absError are less than a small value epsilon.

kind regards,

Jos
• 06-17-2014, 03:34 PM
jim829
Re: how to compare double
Good point. There is a link on this forum to an article about what everyone should know about floating point. I can't find it though. Wouldn't mind reading it again myself.

Regards,
Jim
• 06-17-2014, 03:39 PM
Tolls
Re: how to compare double
Apache's MathUtils has a compareTo for doubles which allows you to provide an error margin.
• 06-17-2014, 03:42 PM
JosAH
Re: how to compare double
Quote:

Originally Posted by jim829
Good point. There is a link on this forum to an article about what everyone should know about floating point. I can't find it though. Wouldn't mind reading it again myself.

Here it is: What Every Computer Scientist Should Know About Floating-Point Arithmetic

kind regards,

Jos
• 06-17-2014, 03:50 PM
jim829
Re: how to compare double
Great! Thanks!

Jim
• 06-17-2014, 04:27 PM
JosAH
Re: how to compare double
Quote:

Originally Posted by jim829
Great! Thanks!

You're welcome; after reading it (again), you'll know everything about ulps and other misery in the floating point world ;-)

kind regards,

Jos