Using floats in conditional statements

• 09-12-2001
DBB
Using floats in conditional statements
I have found that I am unable to use floats in conditional statements. I wish to compare two float values, one of which is an element of an array of floats.
Example:

for(int i=0;i<n;i++)
{
gfreq=freq[i]*count;//count being a float value also

//The compiler seems to be telling me taht the * is an indirection operator

for(int j=0;j<n;j++)
{
if(gfreq<freq[j]) //I am told that this is an illegal use of floats

..........

I had not realised that this is the case with floats. Is there any way I can get around this problem?
• 09-12-2001
kitten
I didn't find anything catastrophic in your code, but I put a sample strip here:
Code:

```const int ARR_SIZE = 40; float  fl; float* fl_array = new float[ARR_SIZE];  // this could also be float fl_array[ARR_SIZE]; for(int i=0; i<ARR_SIZE; i++) {   fl = fl_array[i] * some_const_float;   for(int j=0; j<ARR_SIZE; j++)   {     if (fl < fl_array[j]) // do whatever here   } } delete [] fl_array;```
• 09-12-2001
Brown Drake
I may get corrected here, but you can't use a float value as the subscript in an array.
Code:

```float j, k, someArray[100]; int x; . . .  //initialize j, k, x . someArray[j] = k;  // wrong someArray[x] = k;  // right```
The array subscript is an int, although the value held in the array can be a float.
I could be wrong here, too, but don't think you can or at least should not, use a float in a for statement. Float values are only approximate, so incrementing & testing the end condition can be inaccurate.
• 09-12-2001
DBB
Thanks
Thanks, I think I have sorted out the problem:)