Android java testing float negative and positive values doesn't work as it should?

StackOverflow https://stackoverflow.com/questions/23549220

  •  18-07-2023
  •  | 
  •  

Вопрос

I am testing float values

    public boolean deviceInGoodPosition() {
    if (filteredSampling.fmagnet_x > 0.0 && filteredSampling.fmagnet_y < 0.0 &&  filteredSampling.fmagnet_y > 0.0 ) { 
        return true;
    } else {
        System.out.println("fmagnet_x " +  filteredSampling.fmagnet_x + " > 0.0  AND  fmagnet_y " + filteredSampling.fmagnet_y  + " < 0.0 AND  fmagnet_z " + filteredSampling.fmagnet_z + " > 0.0");
        return false;
    }
}

but running it, with correct values return false .. I print the values for checking

Starting analyzeThread
analyzeThread requesting calibration
fmagnet_x 19.119263 > 0.0  AND  fmagnet_y -44.880676 < 0.0 AND  fmagnet_z 3.5110474 > 0.0
 Wrong device positioning
Это было полезно?

Решение

Your issue is that filteredSampling.fmagnet_y < 0.0 && filteredSampling.fmagnet_y > 0.0 will never return true, as a number cannot be both positive and negative.

Based on your print statement, I assume you meant the last part to be filteredSampling.fmagnet_z > 0.0 (note the z instead of the y).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top