Re: [Tutor] Loop over floating point values

2013-12-07 Thread Amit Saha
On Mon, Dec 2, 2013 at 4:49 PM, eryksun wrote: > On Mon, Dec 2, 2013 at 1:28 AM, Amit Saha wrote: >> Indeed, that's a good point. Surprisingly, C does it just fine: >> >> # include >> >> int main(int argc, char **argv) >> { >> float x = 0.0; >> while(x<1) >> { >> x += 0.1; >>

Re: [Tutor] Loop over floating point values

2013-12-02 Thread Mark Lawrence
On 02/12/2013 12:27, Dave Angel wrote: On Mon, 2 Dec 2013 16:28:38 +1000, Amit Saha wrote: Indeed, that's a good point. Surprisingly, C does it just fine: # include int main(int argc, char **argv) { float x = 0.0; while(x<1) { x += 0.1; printf("%f\n", x); }

Re: [Tutor] Loop over floating point values

2013-12-02 Thread Dave Angel
On Mon, 2 Dec 2013 22:57:30 +1000, Amit Saha wrote: You missed the fact that I am printing the value of x *after* incrementing it. You're quite right, sorry. I'm too accustomed to the usual c idiom, which would increment the value at the end of the loop. -- DaveA

Re: [Tutor] Loop over floating point values

2013-12-02 Thread Amit Saha
On Mon, Dec 2, 2013 at 10:27 PM, Dave Angel wrote: > On Mon, 2 Dec 2013 16:28:38 +1000, Amit Saha wrote: >> >> Indeed, that's a good point. Surprisingly, C does it just fine: > > > >> # include > > > >> int main(int argc, char **argv) >> { >> float x = 0.0; >> while(x<1) >> { >> x

Re: [Tutor] Loop over floating point values

2013-12-02 Thread Dave Angel
On Mon, 2 Dec 2013 16:28:38 +1000, Amit Saha wrote: Indeed, that's a good point. Surprisingly, C does it just fine: # include int main(int argc, char **argv) { float x = 0.0; while(x<1) { x += 0.1; printf("%f\n", x); } return 0; } gives the following ou

Re: [Tutor] Loop over floating point values

2013-12-02 Thread Steven D'Aprano
On Mon, Dec 02, 2013 at 04:28:38PM +1000, Amit Saha wrote: > On Sun, Dec 1, 2013 at 7:26 PM, Steven D'Aprano wrote: > > Such floating point loops are tricky to get right, thanks to rounding of > > floats. Observe: > > > > py> x = 0.0 > > py> while x < 1.0: > > ... x += 0.1 > > ... > > py> x =

Re: [Tutor] Loop over floating point values

2013-12-01 Thread eryksun
On Mon, Dec 2, 2013 at 1:28 AM, Amit Saha wrote: > Indeed, that's a good point. Surprisingly, C does it just fine: > > # include > > int main(int argc, char **argv) > { > float x = 0.0; > while(x<1) > { > x += 0.1; > printf("%f\n", x); > } > > return 0; > } Python uses

Re: [Tutor] Loop over floating point values

2013-12-01 Thread Amit Saha
On Mon, Dec 2, 2013 at 4:36 PM, Asokan Pichai wrote: > On Mon, Dec 2, 2013 at 11:58 AM, Amit Saha wrote: >> >> On Sun, Dec 1, 2013 at 7:26 PM, Steven D'Aprano >> wrote: >> > On Sun, Dec 01, 2013 at 07:03:15PM +1000, Amit Saha wrote: >> >> Hello, >> >> >> >> Much to my disbelief, I realized I had

Re: [Tutor] Loop over floating point values

2013-12-01 Thread Amit Saha
On Sun, Dec 1, 2013 at 7:47 PM, spir wrote: > On 12/01/2013 10:03 AM, Amit Saha wrote: >> >> Hello, >> >> Much to my disbelief, I realized I hadn't written a program in Python >> as far as I can recall which required me to do something like this, in >> psuedocode: >> >> x = 0.1 >> >> for i = 0 to

Re: [Tutor] Loop over floating point values

2013-12-01 Thread Amit Saha
On Sun, Dec 1, 2013 at 7:14 PM, Dominik George wrote: > Hi, > >> - Do not create a list of the floating point values as i=[0.01, 0.02, >> 0.03..] - either like that or by using a suitable mathematical formula >> combined with a list comprehension > > You could simply write your own version of xran

Re: [Tutor] Loop over floating point values

2013-12-01 Thread Amit Saha
On Sun, Dec 1, 2013 at 7:26 PM, Steven D'Aprano wrote: > On Sun, Dec 01, 2013 at 07:03:15PM +1000, Amit Saha wrote: >> Hello, >> >> Much to my disbelief, I realized I hadn't written a program in Python >> as far as I can recall which required me to do something like this, in >> psuedocode: >> >> x

Re: [Tutor] Loop over floating point values

2013-12-01 Thread spir
On 12/01/2013 10:03 AM, Amit Saha wrote: Hello, Much to my disbelief, I realized I hadn't written a program in Python as far as I can recall which required me to do something like this, in psuedocode: x = 0.1 for i = 0 to x step 0.01 # do something with i end i Simply stated, I want to start

Re: [Tutor] Loop over floating point values

2013-12-01 Thread Mark Lawrence
On 01/12/2013 09:14, Dominik George wrote: Hi, - Do not create a list of the floating point values as i=[0.01, 0.02, 0.03..] - either like that or by using a suitable mathematical formula combined with a list comprehension You could simply write your own version of xrange that does it, as a g

Re: [Tutor] Loop over floating point values

2013-12-01 Thread Steven D'Aprano
On Sun, Dec 01, 2013 at 07:03:15PM +1000, Amit Saha wrote: > Hello, > > Much to my disbelief, I realized I hadn't written a program in Python > as far as I can recall which required me to do something like this, in > psuedocode: > > x = 0.1 > > for i = 0 to x step 0.01 > # do something with i >

Re: [Tutor] Loop over floating point values

2013-12-01 Thread Dominik George
Hi, > - Do not create a list of the floating point values as i=[0.01, 0.02, > 0.03..] - either like that or by using a suitable mathematical formula > combined with a list comprehension You could simply write your own version of xrange that does it, as a generator: def xrange_f(start, stop, st

[Tutor] Loop over floating point values

2013-12-01 Thread Amit Saha
Hello, Much to my disbelief, I realized I hadn't written a program in Python as far as I can recall which required me to do something like this, in psuedocode: x = 0.1 for i = 0 to x step 0.01 # do something with i end i Simply stated, I want to start from say a value, 0 and go upto 0.1 in incr