Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Floating point range (http://www.programmingforums.org/showthread.php?t=12962)

kurt Apr 8th, 2007 10:33 PM

Floating point range
 
I search my book, searched Google and this forum, but still can't find a way to do this:

:

range(1,2, 10e-9)

How can I create a range that increments in a value that is small?

Thanks in advance!

andro Apr 8th, 2007 10:57 PM

Pretty sure you need to implement your own frange() function. Be careful about the inaccuracies of floating point numbers especially on the order of magnitude you are suggesting.

DaWei Apr 8th, 2007 10:58 PM

Not sure of this, but I don't think the step can be between -1 and 1. You can, of course, roll your own.

ptmcg Apr 9th, 2007 3:27 AM

Don't forget range() creates a list of all of the entries, so range(1,2,1e-9) would create a billion-entry list. If instead you are trying to iterate a billion times (and I hope you have some time on your hands!), you may be better off with "for i in xrange(0,1e9): f = float(i)/1e9" and then use f as your 0-to-1-by-billionths counter.
-- Paul

ptmcg Apr 10th, 2007 5:13 PM

Out of curiosity, I timed how long a do-nothing for loop takes to iterate 1e9 times. On my 2GHz CPU system, running Python 2.4.1:

:

for i in xrange(1e9): pass

takes 127.5 seconds. So it is feasible to do something a billion times in Python, but I wouldn't put much processing in the body of that loop.

-- Paul


All times are GMT -5. The time now is 2:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC