Loop with float increments (frange)?

Discussion in 'Python' started by forum@anton.e4ward.com, Apr 14, 2006.

  1. 4ward.com

    4ward.com Guest

    Hi!

    what's the standard way for a "for" loop with float increments?

    Anton
     
    4ward.com, Apr 14, 2006
    #1
    1. Advertising

  2. 4ward.com

    Dan Sommers Guest

    On 14 Apr 2006 06:11:38 -0700,
    4ward.com wrote:

    > what's the standard way for a "for" loop with float increments?


    Use a while loop instead:

    f = initial_value
    while f <= final_value:
    process(f)
    f = f + increment

    Note that there is no general guarantee that f will actually be
    final_value; see also <http://docs.python.org/tut/node16.html>.

    Regards,
    Dan

    --
    Dan Sommers
    <http://www.tombstonezero.net/dan/>
    "I wish people would die in alphabetical order." -- My wife, the genealogist
     
    Dan Sommers, Apr 14, 2006
    #2
    1. Advertising

  3. 4ward.com wrote:

    > Hi!
    >
    > what's the standard way for a "for" loop with float increments?


    AFAIK there is no, but you should be easily able to write an frange
    yourself:


    def frange(from, to, step):
    while from < to:
    yield from
    from += step

    for x in frange(10.5, 23.4, 0.3):
    print x


    Diez
     
    Diez B. Roggisch, Apr 14, 2006
    #3
  4. 4ward.com

    Tim Peters Guest

    [4ward.com]
    >> what's the standard way for a "for" loop with float increments?


    [Dan Sommers]
    > Use a while loop instead:
    >
    > f = initial_value
    > while f <= final_value:
    > process(f)
    > f = f + increment
    >
    > Note that there is no general guarantee that f will actually be
    > final_value; see also <http://docs.python.org/tut/node16.html>.


    There's no guarantee that the `while` loop will execute "the expected"
    number of times either, and it's generally a Bad Idea to do "f +=
    increment" inside the loop: the value of f suffers an additional
    rounding error on each iteration that way, potentially making it drift
    away from "the expected" value more & more as the loop goes on.

    Standard careful numeric practice is this way:

    n = compute the number of iterations you want to make in total
    for i in xrange(n):
    process(initial_value + i * increment)

    Then each value computed suffers a total of only two rounding errors
    (one for the multiply, one for the add), independent of how large `n`
    may be, and doesn't get worse as the loop goes on.
     
    Tim Peters, Apr 14, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nicolla MacPherson
    Replies:
    4
    Views:
    395
    Karl Heinz Buchegger
    Aug 13, 2003
  2. bd
    Replies:
    0
    Views:
    634
  3. George Trojan

    frange() question

    George Trojan, Sep 20, 2007, in forum: Python
    Replies:
    9
    Views:
    300
    Carsten Haese
    Sep 22, 2007
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,554
    James Kanze
    Oct 8, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    383
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page