Double comparison with zero special case?

I initialize a double array:

double [] foo = new double[n]; 

I understand that the specification of the java language initializes all the values ​​in the array to zero.

When I look through my algorithm, some of the elements in the array get a positive value.

So, to check if a particular element has a nonzero value, is it safe to just use

 if (foo[i] > 0.0) 

or should i use epsilon. Or similarly, if I want to know if a value is set, can I use == , given that the zeros were not calculated values, but the initial initialized zeros? Normally, of course, I would never use == to compare floating point numbers, but I wonder if this is a special case?

Thanks!

+2
source share
2 answers

Consider this code

 public static void main(String[] args) { double [] foo = new double[10]; foo[5] = 10; // Sets the sixth element to 10 for (int i = 0; i < foo.length; i++) { double val = foo[i]; if (val != 0) { System.out.printf("foo[%d] = %f\n", i, val); } } } 

He outputs

  foo[5] = 10.000000 
+2
source

Unable to use

 if (foo[i] > 0.0) 
+1
source

All Articles