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

Re: [Python-Dev] Why don't range and xrange threat floats as floats?

P: n/a
Matthieu Brucher wrote:
2008/11/5 L V <so******>:
>Why don't range and xrange threat floats as floats?
Is there any good reason range and xrange don't threat floats as floats but
as integers?
When I enter float arguments in a range, the floats are treated as integers.
(+ some warning)

This is how I think it should work:
>>>>range(0, 1, 0.1)
[0.0, 0.10000000000000001, 0.20000000000000001, 0.29999999999999999,
0.40000000000000002, 0.5, 0.59999999999999998, 0.69999999999999996,
0.80000000000000004, 0.90000000000000002]

I got these results by:
>>>>[x/10. for x in xrange(10)]
I'm not looking for alternatives, just for the reason that it works this
It works this way because you can't be sure that you won't get an extra
increment. When you repeatedly add fractions together, the desired final
result might be just less or just more than the actual integral value.
If it's just less you will get an extra increment. It just doesn't work.

Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC

Nov 5 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.