Author mark.dickinson
Recipients mark.dickinson
Date 2008-06-21.21:11:57
SpamBayes Score 0.0503535
Marked as misclassified No
Message-id <1214082720.03.0.30402071522.issue3166@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2008-06-21 21:12:00mark.dickinsonsetspambayes_score: 0.0503535 -> 0.0503535
recipients: + mark.dickinson
2008-06-21 21:12:00mark.dickinsonsetspambayes_score: 0.0503535 -> 0.0503535
messageid: <1214082720.03.0.30402071522.issue3166@psf.upfronthosting.co.za>
2008-06-21 21:11:59mark.dickinsonlinkissue3166 messages
2008-06-21 21:11:59mark.dickinsoncreate