Short answer
With Python 3, the ceil
and floor
return integers (as said by delnan's comment).
See here for details: http://www.afpy.org/doc/python/2.7/whatsnew/2.6.html
It "should" be an integer
I do not think returning a floating point here is a sensible thing to do.
The fact that integer 8 is also a real number does mean that we should return a floating point value after doing floor(8.2)
, exactly because we would not return a complex value with a zero imaginary part (8 is a complex number too).
This has to do with the mathematical definitions of the operations, not the possible machine representations of values: floor and ceiling mathematical functions are defined to return integers, whereas multiplication is a ring where we expect the product of x and y from set A to belong to set A too.
I would not expect 8.2 * 10
to return the integer 82
and similarly I do not expect floor(8.2)
to return 8.0
.
By the way, I disagree with some parts of Robert Harvey's answer.
It makes perfect sense to return a value of a different type depending on an input parameter, especially with mathematical operations.
I don't think the return type should be based on a presupposed common usage of the value and I don't see how convenient it would be. And if it was relevant, I'd probably expect to be given an integer: I generally do not combine the result of
floor
with a floating point.
Why return floats?
Probably, Python was historically not explicitely designed to formally conform to some of the properties of mathematical operations (that would not happen by accident). Guido Von Rossum has acknowledged some early design mistakes and explained the rationale behind the types used in Python, notably why he preferred C
types instead of reusing the ones in ABC. Note that in C
, the ceil
function takes and returns a double
. See for examples:
The Problem with Integer Division. The division operator used to perform truncation, which was generally unexpected and error-prone: see Changing the Division Operator.
The language is supposed to evolve, though, and people tried to incorporate numeric type systems from other languages. For example, Reworking Python's Numeric Model and A Type Hierarchy for Numbers.