Joel Christensen wrote:
To be safe, whenever converting to int, always add a small epsilon.  I
think you can use float.epsilon, but I don't have any experience with
whether that is always reasonable.

-Steve

Thanks every one for the replies.

I still have problems. How do I use epsilon? 'real' helps in my example
program but not in my money program. I think there's a function out
there that does dollars and cents (eg. .89 -> 89c and 1.00 -> $1), but
I'm interested how to solve this problem.

If you're working with an accounting function, you should use bucket rounding. I don't think there's any standard routine for it, but basically first you round, and then you accumulate the error created by rounding in a separate variable. Then when you've processed the "column" or "batch" of numbers, you take the accumulated error and redistribute it over the numbers in a way that makes sense to you. (Perhaps proportional to the size of the number, or some such.) The exact procedure for redistributing the error is case specific, which is probably why there aren't any standard functions to handle this, but uniform distribution rarely works, because the goal is that the totals precisely equal the value. (Note that for this you don't want to use floating point values, but rather fixed point values...which can be emulated with integers that are converted to floats when it's time to export them.) Remember, accounting routines aren't allowed to lose or generate any extra pennies.

If you want to say "That's a silly way to thing about numbers", I'll agree with you. But the accountants who wrote the rules back in the 1800's (or earlier?) didn't use computers. They used abacuses, and pen and ink. And preventing cheating in various forms was one of their primary goals.

Reply via email to