In general case, to compare double
s with ==
, you may consider error margin in their representation by using an epsilon value.
But this solution is not perfect either.
You can refer to this post.
Default float
value is 0.0F
.
So, you could so use this condition in the if
statement :
float epsilon = 0.00000001F;
float currentValue = priceArr[x];
if (Math.abs(currentValue) < epsilon && smallest > currentValue ) {
smallest = currentValue;
}
Note that using a local variable to store the priceArr[x]
value reduces the duplication.
But beware as comparing float
s by using an epsilon may give matching while the value is not really the 0
value.
For example this :
float[] array = new float[5];
array[0] = 0.000000001F;
float epsilon = 0.00000001F;
if (Math.abs(array[0]) < epsilon) {
System.out.println("match");
}
will produce "match".
If your requirement is only ignoring elements of the array that were not explicitly valued, you should favor Float
over float
.
Float
are by default null
in an array and you could so test the nullity to ignore the element :
Float currentValue = priceArr[x];
if(currentValue != null && smallest > currentValue ) {
smallest = currentValue;
}