# comparison operator not working in for loop

#### mussa

Joined Jun 10, 2019
41
if(range == a[j]){

flag=1;
break;
}

the full code is attached below

#### Attachments

• 2.8 KB Views: 8

#### AlbertHall

Joined Jun 4, 2014
8,501
Comparing floats for equality is usually a bad idea as the representation is not exact.
The way out of this is to check that the difference between the two is less than some very small number, say 0.001.

#### 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
}