I'm writing this function that takes in 2 numbers ref and data and checks if data is within 5% of ref.
Example: if ref is 100 and data is 102, it returns 1.
int within_5_percent(int ref, int data)
{
int result = 0;
int lower_bound = (ref - 0.05 * ref);
int upper_bound = (ref + 0.05 * ref);
// printf("Upper: %d\n",upper_bound);
// printf("Lower: %d\n", lower_bound);
if(data >= lower_bound && data <= upper_bound)
{
result = 1;
}
else
{
result = 0;
}
return result;
}
The problem I'm having is at lower_bound. When I pass 100 as ref, the upper_bound is 105 but for some reason lower_bound is 94 when it should really be 95.