[Tutor] re: range function

Doug Stanfield DOUGS@oceanic.com
Wed, 23 May 2001 06:31:32 -1000


[Mike Lange]
> how would I go about creating a range for float-type
> numbers?  I've tried range(1.0, 1000.0, 4.55) as well
> as range(float(1.0), float(1000), float(4.55)) and
> even float(range(1.0, 1000.0, 4.55)) but none of these work.   

First, I'm assuming you know all the shortcomings of using floats. ;-)

I've always found that working in the interpreter helps give me answers to
these kinds of questions.  I assume thats what you were doing above:

Python 1.5.2 (#1, Apr 18 1999, 16:03:16)  [GCC pgcc-2.91.60 19981201
(egcs-1.1.1  on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> range(1.0, 1000.0, 4.55)
[1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73,
77, 81, 85, 89, 93, 97, 10
1, 105, 109, 113, 117, 121, 125, 129, 133, 137, 141, 145, 149, 153, 157,
161, 165, 169, 
------snip------
985, 989, 993, 997]

Seems consistant with what the 'Python Library Reference' says ... "The
arguments must be plain integers. ... The full form returns a list of plain
integers."  Seems it was leniant that it didn't cough up on the non-integer
inputs.  From the above it seems you want numbers from 1 to nearly 1000 that
increment by 4.55.  Apparently that would require two decimal places of
precision:

>>> y = range(100,100000,455)
>>> y
[100, 555, 1010, 1465, 1920, 2375, 2830, 3285, 3740, 4195, 4650, 5105, 5560,
6015, 6470, 6925, 7380,
 7835, 8290, 8745, 9200, 9655, 10110, 10565, 11020, 11475, 11930, 12385,
12840, 13295, 13750, 14205,
-----------snip------------
465, 92920, 93375, 93830, 94285, 94740, 95195, 95650, 96105, 96560, 97015,
97470, 97925, 98380, 9883
5, 99290, 99745]
>>> for counter in range(len(y)):
...   y[counter] = float(y[counter])/100
...
>>> y
[1.0, 5.55, 10.1, 14.65, 19.2, 23.75, 28.3, 32.85, 37.4, 41.95, 46.5, 51.05,
55.6, 60.15, 64.7, 69.2
5, 73.8, 78.35, 82.9, 87.45, 92.0, 96.55, 101.1, 105.65, 110.2, 114.75,
119.3, 123.85, 128.4, 132.95
, 137.5, 142.05, 146.6, 151.15, 155.7, 160.25, 164.8, 169.35, 173.9, 178.45,
183.0, 187.55, 192.1, 1
-------------snip------------
.8, 988.35, 992.9, 997.45]
>>>

> Thanks in advance for the reply, 

HTH

-Doug-

Mike Lange
Developer
mlange@atmedicausa.com
Phone:   801-517-6944
Fax:        801-517-6990

He who controls vocabulary controls thought. 
-Luigi Wittgenstein