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
  • Scaling fixed point math takes some understanding. Perhaps you could start by reviewing this article or Google for fixed point math: http://www.wwnet.net/~stevelim/fixed.html

    Perhaps you should also review a "C" tutorial. If you want to perform floating point operations then you need to have your equation representative of a floating point calculation.

    So as Dan has suggested "cast" the divide operation or change your constants to be float values.

      for(delay=82; delay < 920; delay += 20) {
        Temp = (delay-82.0)/(920.0-82.0);
        Temp = 680.0 * Temp;
        printf("result is %f\n", Temp);
      }
    

Reply
  • Scaling fixed point math takes some understanding. Perhaps you could start by reviewing this article or Google for fixed point math: http://www.wwnet.net/~stevelim/fixed.html

    Perhaps you should also review a "C" tutorial. If you want to perform floating point operations then you need to have your equation representative of a floating point calculation.

    So as Dan has suggested "cast" the divide operation or change your constants to be float values.

      for(delay=82; delay < 920; delay += 20) {
        Temp = (delay-82.0)/(920.0-82.0);
        Temp = 680.0 * Temp;
        printf("result is %f\n", Temp);
      }
    

Children
No data