[New-bugs-announce] [issue3166] Make conversions from long to float correctly rounded.

Mark Dickinson report at bugs.python.org
Sat Jun 21 23:12:00 CEST 2008

New submission from Mark Dickinson <dickinsm at gmail.com>:

If n is a Python long, then one might expect float(n) to return the 
closest float to n.  Currently it doesn't do this.  For example (with 
Python 2.6, on OS X 10.5.2/Intel):

>>> n = 295147905179352891391L

The closest float to n is equal to n+1.  But float(n) returns the 
further of the two floats bracketing n, equal to n-65535:

>>> float(n)
>>> long(float(n))
>>> n - long(float(n))

It's fairly straightforward to fix PyLong_AsDouble to return the closest 
double to a given long integer n (using the round-half-to-even rule in 
the case of a tie).  The attached patch does this.

Having a correctly rounded float(n) can be useful for testing other 
floating-point routines that are supposed to be correctly rounded.

components: Interpreter Core
files: long_as_double.patch
keywords: patch
messages: 68545
nosy: marketdickinson
severity: normal
status: open
title: Make conversions from long to float correctly rounded.
type: feature request
versions: Python 2.6
Added file: http://bugs.python.org/file10694/long_as_double.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list