Using floats in conditional statements

This is a discussion on Using floats in conditional statements within the C++ Programming forums, part of the General Programming Boards category; I have found that I am unable to use floats in conditional statements. I wish to compare two float values, ...

  1. #1
    DBB
    DBB is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Question 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?

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    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;
    Making error is human, but for messing things thoroughly it takes a computer

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    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.

  4. #4
    DBB
    DBB is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Thanks

    Thanks, I think I have sorted out the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Conditional Statements
    By strokebow in forum C Programming
    Replies: 30
    Last Post: 11-22-2006, 06:08 PM
  3. problem with floats in conditional statements
    By Desolation in forum C Programming
    Replies: 3
    Last Post: 07-08-2006, 02:56 AM
  4. multi conditional if statements?
    By chaser in forum Game Programming
    Replies: 3
    Last Post: 07-26-2002, 11:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21