[Python-ideas] What math.floor(inf) should return? Was: Make `float('inf') //1 == float('inf')`

Case Van Horsen casevh at gmail.com
Sat Sep 20 06:08:22 CEST 2014


On Fri, Sep 19, 2014 at 5:44 AM, Mark Dickinson <dickinsm at gmail.com> wrote:
> On Fri, Sep 19, 2014 at 3:16 AM, Alexander Belopolsky
> <alexander.belopolsky at gmail.com> wrote:
>>
>> The standards are influenced by the limitation inherent in many languages
>> where ints have finite range and cannot represent floor() of many finite
>> floating point values.  Python does not have this limitation.  (Granted -
>> PEP 3141 could do a better job explaining why floor, ceil, round, //, etc.
>> should return Integer rather than Real.)
>
>
> Indeed.  FWIW, I think it was a mistake to change the return type of
> math.floor and math.ceil in Python 3.  There's no longer any way to spell
> the simple, fast, float->float floor operation.  It would be nice to have
> those basic floating-point math operations accessible again.

It looks like the Scheme standards committee agree with you.

PEP-3141 references:

http://groups.csail.mit.edu/mac/ftpdir/scheme-reports/r5rs-html/r5rs_8.html#SEC50

There is a later version that specifically discusses the behavior for
+-Inf and Nan.

http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.7.2

Here is the pertinent section:

Although infinities and NaNs are not integer objects, these procedures
return an infinity when given an infinity as an argument, and a NaN
when given a NaN.

(floor -4.3)                           ‌⇒  -5.0
(ceiling -4.3)                         ‌⇒  -4.0
(truncate -4.3)                        ‌⇒  -4.0
(round -4.3)                           ‌⇒  -4.0
(floor 3.5)                            ‌⇒  3.0
(ceiling 3.5)                          ‌⇒  4.0
(truncate 3.5)                         ‌⇒  3.0
(round 3.5)                            ‌⇒  4.0
(round 7/2)                            ‌⇒  4
(round 7)                              ‌⇒  7
(floor +inf.0)                         ‌⇒  +inf.0
(ceiling -inf.0)                       ‌⇒  -inf.0
(round +nan.0)                         ‌⇒  +nan.0

Also note that a floating point representation is used for the results
when the argument is floating point number. Scheme does not use the
internal representation (IEEE-754 or long int or....) to determine if
a number is an integer or real but rather its value. Here is an
example from a Scheme session:

(integer? 2)
;Value: #t

(integer? 2.0)
;Value: #t

(integer? 2.1)
;Value: #f

(#t and #f are equivalent to True and False.)

I think PEP-3141 incorrectly interpreted the meaning of "integer" to
imply the use of an "integer representation" while I think the Scheme
standard implies an "integer value"; i.e. x is an "integer" iff
x-round(x) == 0.


>
> --
> Mark
>


More information about the Python-ideas mailing list