Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming these are the values you're always using for this computation, then I would do something like:</p> <pre> D = I / (65535 / C);</pre> <p><p>or</P></p> <pre> D = I / 4369;</pre> <p>Since C is a factor of 65535. This will help to reduce the possibility of overruning the available range of integers (i.e. if you've only got 16 bit unsigned ints).</p> <p>In the more general case you, if you think there's a risk that the multiplication of I and C will result in a value outside the allowed range of the integer type you're using (even if the final result will be inside that range) you can factor out the <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm" rel="nofollow noreferrer">GCD</a> of the numerator and denominator as in:</p> <pre> INT I = 41828; INT C = 15; INT DEN = 65535; INT GCDI = GCD(I, DEN); DEN = DEN / GCDI; I = I / GCDI; INT GCDC = GCD(C, DEN); DEN = DEN / GCDC; C = C / GCDC; INT D = (I * C) / DEN; </pre> <p>Where DEN is your denominator (65535 in this case). This will not provide you with the correct answer in all cases, especially if I and C are both mutually prime to DEN and I*C > MAX_INT.</p> <p> As to the larger question you raise, division of integer values will always loose the decimal component (equivalent to the floor function). The only way to preserve the information contained in what we think of as the "decimal" part is through the remainder which can be derived from the modulus. I highly encourage you to not mix the meanings of these different number systems. Integers are just that integers. If you need them to be floating point numbers, you should really be using floats, not ints. If all you're interested in doing is displaying the decimal part to the user (i.e. you're not really using it for further computation) then you could write a routine to convert the remainder into a character string representing the remainder.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload