0
 double bonusScore = 10;

 if (bonusScore > 0) {
//code body
}

I want to compare bonusScore which is double with 0, how should i do, because if done in above way it is not accurate and I do not know why.

2
  • 4
    "it is not accurate" what do you mean by that? Commented Nov 3, 2017 at 8:08
  • @VasilisG. that would not make any difference here, as OP is most likely facing a non-0 result where they expect 0. Double.compare adds some extras for comparing 0-s (+/-) and NaN. Commented Nov 3, 2017 at 9:09

3 Answers 3

1

In java, we need to be careful when comparing floating point number. Example:

double a = 1.3;
double b = 3.0;
double bonusScore = a * b;

System.out.println(bonusScore); // expect bonusScore = 3.9

bonusScore = bonusScore - 3.9;
System.out.println(bonusScore); // expect bonusScore = 0

I have ouput:

3.9000000000000004         // expect 3.9
4.440892098500626E-16      // expect 0

So clearly, we have to compare them with the allowed tolerance. Here we need compare bonusScore to zero with tolerance 0.00001:

if (bonusScore > 0.00001)

I have read a very useful article about floating point number. Hope this help.

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what i meant. Thank you for posting another example :)
Very appropriate explanation. Thank you. This has cleared my doubt.
0

Define an epsilon that is very close to zero. Then you get max( bonusScore, epsilon ). if this max returns the value of epsilon, bonusscore is very close to zero. keep in mind to remove any signs from your bonusScore.

8 Comments

"very close to zero", "remove any signs from your bonusScore". two reasons why this is a bad solution, if it is a solution at all. Either it is zero, or it isn't, this way, you won't know. so, you would give someone with negative points (penalties of some sort) -1 to have a 1 point?
The numerical nature of computers is, that all floatingpoint numbers are inacurate. This is a way to remove numerical errors teached in university
Correct answer; if (bonusScore > 0.00001) would do here.
@Stultuske the OP seems to fear that (here) > 0 could capture a bonusScore almost 0 which should be considered 0. The OP's question is maybe not asking exact for an eps comparison, but seems to mean just that. Not a technical function to make an int from a double or such.
@Noixes you are right. I meant in this case taking an abs would be a bit of an overhead, and I missed a concrete solution here. Gave +1 btw.
|
0

For non exact comparasion:

You need to know your epsilon (measure of equality) and compare double like that:

public static boolean equals(double x, double y, double epsilon){
    return Math.abs(x-y) < epsilon;
}

For exact comparasion Try something in lower level:

public static boolean equals(double x, double y) {
    return Double.doubleToLongBits(x) == Double.doubleToLongBits(y);
}

Be careful with exact comparasions on double.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.