Looping using iterators with fractional values

D

drife

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;
}

Thanks in advance for your help.


Daran Rife
(e-mail address removed)
 
M

Mark McEahern

drife said:
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:
.... f = start
.... while f <= stop:
.... yield f
.... f += inc
........ print '%9.2f' % x
....
0.25
0.50
0.75
1.00
1.25
1.50
1.75
2.00
2.25
 
R

Reinhold Birkenfeld

drife said:
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
 
S

Steven Bethard

Mark said:
Use a generator:

... f = start
... while f <= stop:
... yield f
... f += inc
...
... 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
 
M

Mike Meyer

drife said:
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
 
R

Reinhold Birkenfeld

Mike said:
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.
Readability is equal, I would say, but how about speed?

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
 
B

beliavsky

Mike said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top