JG.9872

Floating point to integer conversion error

Discussion created by JG.9872 on Apr 20, 2011
Latest reply on Apr 20, 2011 by JG.9872

I am getting unexpected results when I am converting a double to an unsigned integer using a BF548M and Compiler version 5.0 ( -double-size-64 switch is on).  My code is pretty simple; I'm trying to set the TWI CLKDIV register based on a frequency passed into the function, so it looks like this:

 

 

// freq_in = 100000 (integer)

double temp = (10000000.0 / (double)freq_in) ;

clk_hi = (unsigned short) ( ( temp / 2 )+ .5 );

clk_low = (unsigned short)( temp / 2 );

The problem is that clk_hi and clk_lo always come back as zero.  I've stepped through the code when it's running, and it is calling the ___float64_to_unsigned_int32 library function.

 

What am I doing wrong?

 

Thanks

Outcomes