By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

Re: Getting fractional part from a float without using stringoperations

P: n/a
On Wed, Nov 19, 2008 at 10:01 PM, srinivasan srinivas
<sr*********@yahoo.co.inwrote:
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'
Since you really care about significant figures here, have you
considered using decimal rather than float as, IIRC, it handles this
correctly?

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Thanks,
Srini
----- Original Message ----
From: Tino Wildenhain <ti**@wildenhain.de>
To: srinivasan srinivas <sr*********@yahoo.co.in>
Cc: Jeremiah Dodds <je************@gmail.com>; py*********@python.org
Sent: Wednesday, 19 November, 2008 7:33:46 PM
Subject: Re: Getting fractional part from a float without using string operations

srinivasan srinivas wrote:
>Yes. But it didn't give only the expected decimals.
For ex:
> >>a = 1.23
abs(int(a) -a)
0.22999999999999998
I would like to get the result '0.23' only.

well, thats what get stored internally - there
is no way around it if you are using floating
point numbers:
>>>0.23
0.23000000000000001

but str() handles the rounding correctly:
>>>print 0.23
0.23
>>>print abs(int(a) -a)
0.23

See also http://en.wikipedia.org/wiki/Floating_point
for the problems with FP figures.

Regards
Tino

Get perfect Email ID for your Resume. Grab now http://in.promos.yahoo.com/address
--
http://mail.python.org/mailman/listinfo/python-list
Nov 20 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.