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 doubles 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 doubles 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

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -