java - Comparing double for equality is not safe. So what to do if want to compare it to 0? -
i know because of binary double representation, comparison equality of 2 double
s not quite safe. need perform computation this:
double a; //initializing if(a != 0){ // <----------- here double b = 2 / a; //do other computation } throw new runtimeexception();
so, comparison of double
s not safe, not want to devide 0. in case?
i'd use bigdecimal
performance not quite acceptable.
well, if issue dividing zero, news can have since if value isn't 0, can divide it, if it's really, small.
you can use range comparison, comparing lowest value want allow, e.g. a >= 0.0000000000001
or similar (if it's going positive).
Comments
Post a Comment