[Python-Dev] Rounding float to int directly (Re: struct module and coercing floats to integers) (original) (raw)

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Aug 3 04:30:12 CEST 2006


M.-A. Lemburg wrote:

Believe me: you have to if you want to do more advanced calculus related to pricing and risk analysis of derivatives.

When you do things like that, you're treating money as though it were a continuous quantity. This is an approximation, so you can tolerate having small inaccuracies in the result.

But when adding up actual, real amounts of money, where the result must be exact, using binary fractions is a very bad idea.

This is not the same: if you round both value and then compare, you test a different interval than by taking the difference and applying a tolerance value comparison:

That's exactly my point. Chopping decimal places is not a substitute for doing tolerance testing properly.

Most typical uses of round() don't use the optional argument, true, but I still fail to see what returning an integer instead of a float would buy you.

It saves you a function call in the vast majority of cases, where an int is what you ultimately want.

-- Greg



More information about the Python-Dev mailing list