[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)
2.9514790517935283e+20
>>> long(float(n))
295147905179352825856L
>>> n - long(float(n))
65535L
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>
<http://bugs.python.org/issue3166>
_______________________________________
More information about the Python-bugs-list
mailing list