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

M.-A. Lemburg mal at egenix.com
Thu Aug 3 10:32:54 CEST 2006


Greg Ewing wrote:
> 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.

Agreed. Those are different use cases, though.

>> 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.

As I said: those two are different ways of doing
comparisons.

If you are eventually rounding to say 2 decimal
places in the end of the calculation, you won't
want to find yourself presenting the user 1.12
and 1.13 as equal values :-)

>> 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.

-1 on that. Just saving a single function call
isn't really enough to break all sorts of
applications out there that use round() in
calculations (with or without argument). Most
such calculations do work with floats, so having
round() return an int would just add another
coercion to a float for those use-cases.

Seriously, rounding is a complicated business.
round() just implements one way of doing it.
Perhaps a module providing different rounding
models would be of use ?!

The decimal module already has a few of those
implemented, so it could benefit from such a
module as well.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Aug 03 2006)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::


More information about the Python-Dev mailing list