Re: Conversion error



dada wrote:

Consider the following function...


/**
***********************************************************************
* COMM_convertFreqIntToDouble
***********************************************************************
*
* \Description: Converts a frequency (integer in Hz) to a
* double in mHz.
*
* \Parameters: in int freqHz - the frequency in Hz
*
* \Returns: double - the representation(in mHz) of the frequency as
a double
*
* \Limitations:
*
***********************************************************************
*/

double COMM_convertFreqIntToDouble ( UINT32 freqHz )
{
double temp = freqHz / 1000.0 / 1000.0 ;
int tempint ;


Is this check even necessary ? Should the above work correctly ?
This check is in here because in the past we had seen different values
than we expected and we were attempting to try and capture where the
error was introduced
Thanks !

/* Convert it back and see if they match ! */
tempint = (int) ( temp * 1000 * 1000 ) ;
if ( freqHz != tempint )
{
LOGMSG ( "WARNING", __FUNCTION__,"No Match",
"%d != %d", (unsigned int) freqHz, tempint ) ;
}

return ( temp ) ;
}


LOGMSG is a macro that performs some formatting and writes a message to the console.

When this functin is called with
130075000
tempint = 130074999

Which causes the warning to be issued.
As I write this I see that I should be debugging and looking at the
double to see if it did indeed convert correctly...

Well, I'll ask my question anyways...
Is the difference being introduced in the conversion TO double or FROM double, and, is there a better way to perform this operation ?

Thanks !
Joe




.



Relevant Pages

  • Conversion error
    ... in int freqHz - the frequency in Hz ... double COMM_convertFreqIntToDouble (UINT32 freqHz) ... if (freqHz!= tempint) ... Is the difference being introduced in the conversion TO double or FROM double, and, is there a better way to perform this operation? ...
    (comp.lang.c)
  • Re: Conversion error
    ... in int freqHz - the frequency in Hz ... double COMM_convertFreqIntToDouble (UINT32 freqHz) ... if (freqHz!= tempint) ... No-one ever won a game by resigning. ...
    (comp.lang.c)