title: prevent arithmetic overflow
body: <p>I want to calculate a voltage using ADC peripheral of PIC18F14K50. The result ranges between 0-1023 (10-bit). So I used this simple calculation:</p>
  
  <pre class="lang-c prettyprint-override"><code>uint16_t voltage = ADC_Result * 5000 / 1023;
  </code></pre>
  
  <p>However, the results are incorrect. I guess an arithmetic overflow happened. I tried many combination of parentheses, changing order of elements, etc.<br>
  The best result was 4088 when <code>ADC_Result</code> was 1023 using below code; which is really far from 5000.</p>
  
  <pre class="lang-c prettyprint-override"><code>uint16_t voltage = ADC_Result * (5000 / 1023);
  </code></pre>
  
  <p>What should I do to get better results in above calculation? Please don't suggest floating points as they cause disaster in MCUs! They use a lot of resources without any real benefit.</p>