Getting fractional part from a float without using string operations

James Harris james.harris.1 at
Thu Nov 20 16:47:59 CET 2008

On 20 Nov, 06:01, srinivasan srinivas <sri_anna... at> wrote:
> Yes it works for most of the cases.  But it doesn't for the following case:
> >>> str(abs(int(1234567.89)-1234567.89))
> '0.889999999898'

Well, that is 0.89 or about as near to it as the calculation can
represent. Like other numbers 0.89 evidently cannot be exactly
represented as a float in binary. The nearest number above it is

>>> 0.89

This is not a Python issue but a result of storing numbers in floating
binary form.

BTW, please post comments below existing ones rather than above them.
It is more familiar on Usenet and, as a consequence, easier to read.


More information about the Python-list mailing list