This is from a response to a question about rational division on the Edu-SIG list. On Sat, 5 Feb 2000, Ka-Ping Yee wrote: > > In E, there are two operators for division: > > 2 / 3 yields 0.666666 > > 2 _/ 3 yields 0 > > Read "_/" as "floor-divide". > (See http://www.eright.org/elang/grammar/expr.html.) > > Note that in the current Python, if a and b are integers and > f is the floating-point result of dividing a by b, then > a/b is *not* equal to int(f). It's equal to math.floor(f). Then i realized that math.floor returns a float. So, really, in order to replace the old /, a / b becomes import math int(math.floor(a / b)) As long as we're going to push forward into the realm of sanity (and i'm in support of this change!) do we need an easier way to spell this to help folks convert? I rather like E's "_/" operator, but that may be harder to swallow here since "_" goes in Python identifiers. E allows "_" in identifiers too, but you can use spaces to avoid ambiguity, e.g.: a _/ b # parses as 'a' floor-divide 'b' a_/b # parses as 'a_' float-divide 'b' But perhaps that is just too arcane for Python. "//" was also considered for a floor-divide operator for a while, but Java-style comments won out. So do we go for a new operator, or put a 'div()' in __builtins__, or what? > int() truncates, always rounding towards zero (an abomination > in my opinion!), while floor() rounds down. This is also possibly an Edu-SIG question, but don't you think it would be nice to have a round() built-in? I can see it being invaluable for classroom and scientific work. Something like: def round(x): return int(math.floor(x + 0.5)) or even: def round(x, unit=1): return int(math.floor(x / unit + 0.5))*unit -- ?!ng "If I have not seen as far as others, it is because giants were standing on my shoulders." -- Hal Abelson
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4