AMY LIAO

problem with float conversion in CW3.1

Discussion created by AMY LIAO on Mar 30, 2006
Latest reply on Mar 30, 2006 by bigmac
Hi,
 
I have a function which does some multiplication and division all in float. Then it casts the result from float to unsigned long.
 
The result in float is 225000000, which is correct. For some reason, when it gets casted into unsigned long, it becomes 224999984. Is there any way this could be solved?
 
Below is the code:
 
float calibration = 2000;
unsigned int time_rate = 1;
unsigned int dist_rate = 2000;
const float TIM0_INC = 1.6e-5;
 
unsigned long get_xover_pw (void)
{
  float pw;
 
  pw = 3600.0 / calibration / (float)time_rate * (float)dist_rate / TIM0_INC;
  return (unsigned long)pw;
}
 
void main(void) {
 unsigned long y;
  y = get_xover_pw ();
}

Outcomes