Getting fractional part from a float without using string operations

James Harris james.harris.1 at googlemail.com
Thu Nov 20 10:47:59 EST 2008


On 20 Nov, 06:01, srinivasan srinivas <sri_anna... at yahoo.co.in> 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
0.89000000000000001
>>>

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.

James



More information about the Python-list mailing list