views:

114

answers:

3

Trying to figure out this pseudo code. The following is assumed.... I can only use unsigned and signed integers (or long). Division returns a real number with no remainder. MOD returns a real number. Fractions and decimals are not handled.

INT I = 41828;
INT C = 15;
INT D = 0;

D = (I / 65535) * C;

How would you handle a fraction (or decimal value) in this situation? Is there a way to use negative value to represent the remainder?

In this example I/65535 should be 0.638, however, with the limitations, I get 0 with a MOD of 638. How can I then multiply by C to get the correct answer?

Hope that makes sense.

MOD here would actually return 23707, not 638. (I hope I'm right on that :) )

+3  A: 

If you were to switch your order of operations on that last line, you would get the integer answer you're looking for (9, if my calculations are correct)

D = (I * C) / 65535
/* D == 9 */

Is that the answer you're looking for?

haldean
I think that might work for my purposes. If anyone does have an answer about how to handle decimals in a situation like I described, I'd be happy to hear it.
Senica Gonzalez
I posted a proposed mechanism to handle decimals. It's really ugly, though.
Brian
A: 
andand
A: 

Well, one way to handle decimals is this replacement division function. There are numerous obvious downsides to this technique.

ALT DIV (dividend, divisor) returns (decimal, point)
for point = 0 to 99
  if dividend mod divisor = 0 return dividend / divisor, point
  dividend = divident * 10
return dividend / divisor, 100
Brian