2

I know that because of binary double representation, comparison for equality of two doubles is not quite safe. But I need to perform some computation like this:

double a;
//initializing
if(a != 0){       // <----------- HERE
    double b = 2 / a;
   //do other computation
}
throw new RuntimeException();

So, comparison of doubles is not safe, but I definitely do not want to to devide by 0. What to do in this case?

I'd use BigDecimal but its performance is not quite acceptable.

user3663882
  • 6,957
  • 10
  • 51
  • 92
  • related: http://stackoverflow.com/questions/12114498/can-a-near-zero-floating-value-cause-a-divide-by-zero-error/12115243#12115243 –  Jul 25 '16 at 17:27
  • Dividing by zero is often a surprisingly reasonable thing to do with doubles. – user2357112 Jul 25 '16 at 17:27
  • try catch the divide by zero exception – LuKenneth Jul 25 '16 at 17:28
  • If `a` is a method variable or parameter, or it is declared `final` or `volatile`, it will be thread-safe. – ControlAltDel Jul 25 '16 at 17:30
  • @ControlAltDel I don't think that's the question –  Jul 25 '16 at 17:30
  • when checking if a floating point value like double or float is 0, an error threshold is used to detect if the value is near 0, but not quite 0. public boolean checkZero(double value, double threshold){ return value >= -threshold && value <= threshold; } – Rishal Jul 25 '16 at 17:31

2 Answers2

5

Well, if your issue is dividing by zero, the good news is that you can do what you have since if the value isn't actually 0, you can divide by it, even if it's really, really small.

You can use a range comparison, comparing it to the lowest value you want to allow, e.g. a >= 0.0000000000001 or similar (if it's always going to be positive).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Actually, in this particular case very very small values do not cause DevisionByZero-related `Exception`, and it's safe. Thank you. – user3663882 Jul 25 '16 at 17:30
  • Not even dividing by zero causes an exception in double arithmetic; it just returns a (valid) infinite result. – Louis Wasserman Jul 25 '16 at 19:14
  • @LouisWasserman: I thought that too, thanks for confirming. I wasn't sure enough of my facts (my Java is rusty) to say it in an answer I was about to leave unattended, even after reading [*Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.*](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.2). – T.J. Crowder Jul 26 '16 at 07:35
0

What about using the Static method compare of the Double class wrapper??

double a = 0.0;
// initializing
if (Double.compare(a, 0.0) != 0) {

}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • hm... but is it actually different from the original example? – user3663882 Jul 25 '16 at 17:28
  • 3
    This provides no benefit over a regular `== 0` comparison. – user2357112 Jul 25 '16 at 17:29
  • 1
    yes... you delegate the comparing operation to the class Double... so how its done is none of your bussines anymore.... – ΦXocę 웃 Пepeúpa ツ Jul 25 '16 at 17:30
  • 2
    It still is your problem if the method doesn't do what you semantically require. Calling a method doesn't absolve you of the duty to ensure code correctness. – biziclop Jul 25 '16 at 18:44
  • 1
    @ΦXocę웃Пepeúpa: That's like saying that shooting people is bad, so you're going to delegate to a hitman and how it's done is none of your business any more. The problems with exact comparisons to 0 don't go away just because they're hidden behind an abstraction layer. – user2357112 Jul 25 '16 at 19:02