This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem with calculation

Hi

I have been trying to use the equation:

int Temp;
   Temp = 680*((N-82)/(920-82));
   return Temp;


N=920 - I would expect temp would be 680 and the result is 680 which is correct


But when I tried to use other values below, I keep getting zero.

Then N - 900
Result: 0

adc_result - 700
result:0

adc_result - 500
result:0

adc_result - 300
result:0

adc_result - 100
result:0

I don't understand why this happened.

Arron

Parents
  • It's often possible to re-order the operations to avoid early truncation, without resorting to floating point.

    Temp = 680*((N-82)/(920-82));

    is equivalent to

    t = N - 82;
    t = t / (920-82);
    Temp = 680 * t;

    The problem is in step 2, where t goes to zero. (Even when N > 920, there's a big loss of accuracy.)

    Algebraically, it doesn't matter whether you multiply or divide first. So moving the parens to force a different order of operation results in:

    Temp = (680*(N-82))/(920-82);

    is equivalent to

    t = N - 82;
    t = 680 * t;
    Temp = t / (920 - 82);

    The problem to watch out for here is the opposite of the truncation problem. "680 * t" might overflow, depending on the possible input range and size of integers used. Handling overflow is often easy, though -- just use a larger int for those steps -- compared to the expense of floating point or scaling fixed point results.

Reply
  • It's often possible to re-order the operations to avoid early truncation, without resorting to floating point.

    Temp = 680*((N-82)/(920-82));

    is equivalent to

    t = N - 82;
    t = t / (920-82);
    Temp = 680 * t;

    The problem is in step 2, where t goes to zero. (Even when N > 920, there's a big loss of accuracy.)

    Algebraically, it doesn't matter whether you multiply or divide first. So moving the parens to force a different order of operation results in:

    Temp = (680*(N-82))/(920-82);

    is equivalent to

    t = N - 82;
    t = 680 * t;
    Temp = t / (920 - 82);

    The problem to watch out for here is the opposite of the truncation problem. "680 * t" might overflow, depending on the possible input range and size of integers used. Handling overflow is often easy, though -- just use a larger int for those steps -- compared to the expense of floating point or scaling fixed point results.

Children