# Looping using iterators with fractional values

Discussion in 'Python' started by drife, Jan 1, 2005.

1. ### drifeGuest

Hello,

Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?

In Perl this construct quite easy:

for (my \$i=0.25; \$i<=2.25; \$i+=0.25) {
printf "%9.2f\n", \$i;
}

Daran Rife

drife, Jan 1, 2005

2. ### Mark McEahernGuest

drife wrote:

> Hello,
>
> Making the transition from Perl to Python, and have a
> question about constructing a loop that uses an iterator
> of type float. How does one do this in Python?
>
>

Use a generator:

>>> def iterfloat(start, stop, inc):

.... f = start
.... while f <= stop:
.... yield f
.... f += inc
....
>>> for x in iterfloat(0.25, 2.25, 0.25):

.... print '%9.2f' % x
....
0.25
0.50
0.75
1.00
1.25
1.50
1.75
2.00
2.25
>>>

Mark McEahern, Jan 1, 2005

3. ### Reinhold BirkenfeldGuest

drife wrote:
> Hello,
>
> Making the transition from Perl to Python, and have a
> question about constructing a loop that uses an iterator
> of type float. How does one do this in Python?
>
> In Perl this construct quite easy:
>
> for (my \$i=0.25; \$i<=2.25; \$i+=0.25) {
> printf "%9.2f\n", \$i;
> }

<=Py2.3:

for i in [x/4.0 for x in xrange(1, 10)]:
print "%9.2f" % i

Py2.4:

for i in (x/4.0 for x in xrange(1, 20)):
print "%9.2f" % i

Reinhold

Reinhold Birkenfeld, Jan 1, 2005
4. ### Steven BethardGuest

Mark McEahern wrote:
> drife wrote:
>
>> Hello,
>>
>> Making the transition from Perl to Python, and have a
>> question about constructing a loop that uses an iterator
>> of type float. How does one do this in Python?
>>
>>

> Use a generator:
>
> >>> def iterfloat(start, stop, inc):

> ... f = start
> ... while f <= stop:
> ... yield f
> ... f += inc
> ...
> >>> for x in iterfloat(0.25, 2.25, 0.25):

> ... print '%9.2f' % x
> ...
> 0.25
> 0.50
> 0.75
> 1.00
> 1.25
> 1.50
> 1.75
> 2.00
> 2.25
> >>>

>

Or use the numarray module:

py> import numarray as na
py> for f in na.arange(0.25, 2.25, 0.25):
.... print '%9.2f' % f
....
0.25
0.50
0.75
1.00
1.25
1.50
1.75
2.00

Steve

Steven Bethard, Jan 1, 2005
5. ### Mike MeyerGuest

"drife" <> writes:

> Hello,
>
> Making the transition from Perl to Python, and have a
> question about constructing a loop that uses an iterator
> of type float. How does one do this in Python?
>
> In Perl this construct quite easy:
>
> for (my \$i=0.25; \$i<=2.25; \$i+=0.25) {
> printf "%9.2f\n", \$i;
> }

Generally, you don't loop incrementing floating point values, as
you're liable to be be surprised. This case will work as you expect
because .25 can be represented exactly, but that's not always the
case.

Python loops are designed to iterate over things, not as syntactic
sugar for while. So you can either do the while loop directly:

i = 0.25
while i <= 2.25:
print "%9.2f" % i
i += 0.25

Or - and much safer when dealing with floating point numbers - iterate
over integers and generate your float values:

for j in range(1, 9):
i = j * .25
print "%9.2f" % i

<mike
--
Mike Meyer <> http://www.mired.org/home/mwm/

Mike Meyer, Jan 1, 2005
6. ### Reinhold BirkenfeldGuest

Mike Meyer wrote:

> Or - and much safer when dealing with floating point numbers - iterate
> over integers and generate your float values:
>
> for j in range(1, 9):
> i = j * .25
> print "%9.2f" % i

There's a glitch there, though - should be range(1, 10).

Reinhold

PS: I'm wondering whether my genexp approach or this one is preferable.

Brought up a few timeits:

Python 2.3
----------

for i in [x/4.0 for x in range(1, 10)]: 36,9 sec

for j in range(1, 10): i = j * 0.25: 33,7 sec

Python 2.4
----------

for i in (x/4.0 for x in range(1, 10)): 32,5 sec

for j in range(1, 10): i = j * 0.25: 28,4 sec

So what does that tell us?
(a) don't use genexps where there is a simpler approach
(b) Py2.4 rocks!

Reinhold

Reinhold Birkenfeld, Jan 1, 2005
7. ### Guest

Mike Meyer wrote:

>Or - and much safer when dealing with floating point numbers - iterate
>over integers and generate your float values:

>for j in range(1, 9):
> i = j * .25
> print "%9.2f" % i

I agree with this suggestion. As an historical aside, Fortran had loops
with floating point variables for decades, but in 1995, the first
standard in a while to REMOVE features, this was one of the few things
deleted. The Fortran standards committee is very conservative about
creating backwards incompatibilities, but they must have thought loops
with floating point variables are so error-prone -- and alternatives
with integer counters are so easy to write -- that they put their foot
down. I know the OP is asking about Python, but the principle is the
same.

, Jan 1, 2005