Nasty typo in PEP 238 (revised)

Tim Peters tim.one at home.com
Fri Jul 27 00:59:07 EDT 2001


[David Eppstein]
> floor converts ints to floats????  This seems exactly backwards
> from what I would expect.

Virtually all the functions in the math module wrap C library functions of
the same names.  floor() is a required double->double function in standard
C, and Python's math.floor() exposes the platform C's floor() function.

> And while it is certainly documented that way in the library
> reference (section 5), it is contradicted by the footnote in section 2
> about using floor and ceil for "well-defined conversions" from float to
> int,

C89 didn't define the rounding behavior of float->int conversions, so floor
and ceil were needed to force the issue as desired; as of earlier this
afternoon, Python 2.2 now defines float->int as truncating ("round to 0")
regardless of what the platform C happens to do.

> as well as the claim in the reference manual that integer-division is
> the same as applying floor to the result of mathematical division.

The mathematical floor, yes; not the math.floor() function.

> I have to consider this a big wart.

The audience for math.floor() is floating-point users; you've already
testified repeatedly you're not one of them <wink>.

> Especially with the proposed changes to /, I would likely want to write
> code like i = floor(a/b) and j = ceil(c/d) but apparently this is
> going to need added coercions to get an integral type.

Yes.  But if you want the floor, use a // b instead:  that's what it's there
for.  ceil is neither harder nor easier to get than before, and the best way
to get the ceiling of an integer quotient in Python has always been

def ceiling_of_quotient(i, j):
    q, r = divmod(i, j)
    return q + (r != 0)

> Anyway, back to the PEP, I don't think calling floor(x) is a very
> clear way of writing an int-to-float coercion, and it's still a bug
> because it will give you incorrect results if x is a non-integral float.

math.floor() isn't intended for int-to-float coercion, and I believe you
were right the first time (that the specific occurrence of "floor" in the
PEP that you pointed out was just a typo, and that "float" was intended
there instead).





More information about the Python-list mailing list