comparison operator not working in for loop

ErnieM

Joined Apr 24, 2011
8,005
Another point to think about is how you represent the frequency. The FM band goes from 88,000,000 to 108,000,000 Hz, and you chose to represent these with constants spaced 1,000,000 Hz apart.

However if you space your constants 100,000 apart they can be held in simple integer types and not floating types, as long as you remember to simply divide by 10 when you use them with your radio object.
 

MrChips

Joined Oct 2, 2009
19,377
Never use equality test when comparing floats. Use ≤ or ≥. If checking for equality you need to specify a tolerance window using both ≤ and ≥.
 

djsfantasi

Joined Apr 11, 2010
5,682
Code when comparing float values for equality work best like this.

Code:
#define closeEnuff 0.01
...
float value1;
float value2;
...
if(abs(value1-value2) <= closeEnuff) {
    // values are close enuff
    // consider them equal
} else {
    // values are not equal
}
 
Top