Thread: Rounding Float- Return not happening ?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    11

    Rounding Float- Return not happening ?

    i wanted to Round off Float value,but the correct computed value is not returning to calling Function .
    i have following functions .
    init -- which calls 'compare' function.
    compare - which will call rounding function .
    My issue was when i call "compare" function the return value from "Round_Float" function is not visible in "compare" function .


    here is code ..

    Code:
    float  Util_Round_Float(float  data, unsigned int aft_dec_digits )
    {
        float              sign = 1.0;  
        unsigned int      index = 0;       
        unsigned int      local_var = 1;    
        unsigned int      temp_var = 0;     
        float             output = 0.0;
        float              X = 0;
          
        ..................
        ..................
        X = sign * output;
        return( X );
    }
     
    
    float Util_Float_Abs( float data )
    {
        float    ret_value = 0.0; 
     
        if( data > 0.0 )
        {
            ret_value = data;
        }
        else
        {
            ret_value = ( data * ( -1 ));
        }
        return( ret_value );
    } 
    
    void init ()
    {
       float value = 12.5;
       compare (value);
    }
    
    void Compare( float  Ratetype )
    {
         
       Ratetype = (float )Util_Round_Float( Ratetype, 5 );
    }
    when i debug "RoundFloat" function i am getting correct valuve.but the value returns to "compare" function it is not correct.
    is it becoz the return valve of "Util_Round_Float" is a local ??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Add a main() and some test code which we can compile and run "as is", rather than having to guess at what you might have done.

    Also, state your OS and compiler (with versions where appropriate)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    void Compare( float  Ratetype )
    {
         
       Ratetype = (float )Util_Round_Float( Ratetype, 5 );
    }
    Well, your Compare() function doesn't return any value so that's probably why no return is happening?

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Here is the code to compile/check .

    Code:
    float Util_Float_Abs( float data )
    {
        float    ret_value = 0.0; 
            
        if( data > 0.0 )
        {
            ret_value = data;
        }
        else
        {
            ret_value = ( data * ( -1 ));
        }
        return( ret_value );
    } 
    -------------- 
    float Util_Round_Float(float data, unsigned int aft_dec_digits )
    {
        float             sign = 1.0;    
        unsigned int      index = 0;      
        unsigned int      local_var = 1;    
        unsigned int      temp_var = 0;     
        float             output = 0.0;
        float             X = 0;
        
    
        /* Calculate the number of decimal places multiplied by 10 */
        for( index = 0; index < aft_dec_digits; index++ )
        {
            local_var = ( local_var * 10 );
        }
        if( data < 0.0 )
        {
            sign = -1.0;
        }
        /* Move the decimal point in the input argument number of places to the
           right */ 
        output = ( Util_Float_Abs( data ) * local_var );
    
        /* Adding 0.5 to the temporary value to round it off */
        temp_var = ( unsigned int )( output + 0.5 );
    
        /* Calculate the output which is rounded off to the input number of 
         significant digits */
        output = (FLOAT32)( temp_var ) / local_var ;
        X = sign * output;
        return( X );
    }
    
    ---------------------
    int main ()
    {
       float value = 12.5;
       compare (value);
       return 0; 
    }
    
    flaot Compare( float  Ratetype )
    {
       float Ratetype1;  
       float dummy = 12.5;
       Ratetype1 = (float )Util_Round_Float( Ratetype, 5 );
    
        if (dummy == Ratetype1)
          printf("  Floating point comparision");
    }
    -----------------------------
    when i check the value of Ratetype1 in "compare" function it is returing 5 instead of 12.5 .
    why is 12.5 not returning ?

    OS : Windows
    Compiler : Green Hill

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    5

    please post your code

    what you posted is not what you are compiling. please post the code you are compiling

    what you posted will not compile because you define
    Code:
    Compare(float)
    but call
    Code:
    compare(float)
    when i correct that and compile the code you posted,
    Code:
    Util_Round_Float(Ratetype, 5);
    does return 12.5

    also, its obvious you are not compiling this code since you have lines of '-' characters in there

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > flaot Compare( float Ratetype )
    Not to mention, typo's
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM