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.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
Rehmanali Momin
  • 199
  • 1
  • 1
  • 14
  • 4
    "it is not accurate" what do you mean by that? – Stultuske Nov 03 '17 at 08: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. – tevemadar Nov 03 '17 at 09:09

3 Answers3

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.

Nam Tran
  • 643
  • 4
  • 14
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.

Noixes
  • 1,158
  • 12
  • 25
  • "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? – Stultuske Nov 03 '17 at 08:17
  • The numerical nature of computers is, that all floatingpoint numbers are inacurate. This is a way to remove numerical errors teached in university – Noixes Nov 03 '17 at 08:18
  • Correct answer; `if (bonusScore > 0.00001)` would do here. – Joop Eggen Nov 03 '17 at 08:19
  • and if the point of the code was to remove numerical errors, I would agree, but the point is to make an exact comparison between the current score and zero. Which is not what your solution does – Stultuske Nov 03 '17 at 08:20
  • OP sayed: "how should i do, because if done in above way it is not accurate and I do not know why.". You cant compare floatingpoint numbers acurately. The answer "if (bonusScore > 0.00001) is a correct solution but does not explain why. I think, my explanation can be used for serveral cases where floatingpoint numbers should be compared, for example in divide by zero possibilities – Noixes Nov 03 '17 at 08:23
  • 1
    @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. – Joop Eggen Nov 03 '17 at 08:24
  • 1
    @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. – Joop Eggen Nov 03 '17 at 08:27
  • @Joop Eggen Thank you :) – Noixes Nov 03 '17 at 08:29
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.

oginski
  • 354
  • 5
  • 16