Tom said:
How about forcing integer % implementation?
int mult = 100000;
int remainder = (Int32)(1.2 * mult) % (Int32)(0.01 * mult);
Because it _doesn't solve the problem_.
The binary representation of .01 may be a little more or less than .01.
When you multiply by 100000, the result may be a little more or less than
1000.00. If it is less, when you cast to integer without any rounding, you
will get 999. Same goes for the other operand.
Furthermore, you are still doing more operations than necessary. You've
eliminated the FP division, but you have two FP multiplies. You're better
off (at least in the case of constants) multiplying by the reciprocal,
rounding, subtracting -- cheap FP operations, probably cheaper than integer
modulo -- and performing a equal-with-tolerance test.
I.e.
double x = 1.2;
const double divisor = 0.01;
const double divisor_recip = 1.0 / divisor; // evaluated at compile-time
double divided = x * divisor_recip;
double remainderoverdivisor = divided - Math.Round(divided); // may replace
with Math.Floor(divided + .5) if faster
if (Math.Abs(remainderoverdivisor) < (.0001 * divisor_recip)) ... //
compile-time expression again, introduce another "const" variable as needed
to force compile-time evaluation
Even when divisor is not known at compile-time, this method may still yield
considerable savings if the reciprocal operation can be moved outside a loop
for example.