Page 1 of 1

### controlling number of times loop is executed?

Posted: Tue May 28, 2019 6:57 pm
Hello everyone!

I just hit a mental block thinking about writing a piece of code, and hope someone can help -I know a fair amount of C but not too much python (but can't think of a solution in either language !) My goal is to send an equation that depends on time 't', and therefore outputs different values of voltage, such as this one: V = ((00.5*Vmax)*sin(((2*pi*t)/100)-0.5*pi))+(0.5*Vmax) through a digital pot. to a thruster to do some testing. I initially thought of a 'for' loop (amongst the other code) to cycle through the values of 't' to change the voltage values, but then realized this would just cycle through integer values. I need something that changes the value of 't' a little more often and I was told something about the 'code or hardware would determine the speed' but that doesn't make sense to me. Can someone explain how I can change 't' at smaller intervals at a given rate?

Thanks so much : )

### Re: controlling number of times loop is executed?

Posted: Tue May 28, 2019 7:03 pm
lezzlexx wrote:
Tue May 28, 2019 6:57 pm
Hello everyone!

I just hit a mental block thinking about writing a piece of code, and hope someone can help -I know a fair amount of C but not too much python (but can't think of a solution in either language !) My goal is to send an equation that depends on time 't', and therefore outputs different values of voltage, such as this one: V = ((00.5*Vmax)*sin(((2*pi*t)/100)-0.5*pi))+(0.5*Vmax) through a digital pot. to a thruster to do some testing. I initially thought of a 'for' loop (amongst the other code) to cycle through the values of 't' to change the voltage values, but then realized this would just cycle through integer values. I need something that changes the value of 't' a little more often and I was told something about the 'code or hardware would determine the speed' but that doesn't make sense to me. Can someone explain how I can change 't' at smaller intervals at a given rate?

Thanks so much : )
Why does t have to be an integer?
In C, the loop counter can be anything.
For example 64-bit floating-point ...

Code: Select all

``````for( double t = 0.0; t < Vmax; t += 0.001 )
V = ((00.5*Vmax)*sin(((2*pi*t)/100)-0.5*pi))+(0.5*Vmax);``````
I am certain the same is possible in Python.

### Re: controlling number of times loop is executed?

Posted: Tue May 28, 2019 7:04 pm

Code: Select all

``````print("start")
t = 0.0
while True:
t += 0.001
if t > 1000:
break
# some other stuff here
print ("complete")``````

### Re: controlling number of times loop is executed?

Posted: Tue May 28, 2019 7:23 pm
This works for me -

Code: Select all

``````def frange(fromVal, toVal, stepVal=1.0):
while fromVal < toVal:
yield fromVal
fromVal = fromVal + stepVal

for f in frange(1.0, 2.0, 0.1):
print f
``````

### Re: controlling number of times loop is executed?

Posted: Sat Jun 01, 2019 4:32 am
ghp wrote:
Tue May 28, 2019 7:04 pm

Code: Select all

``````print("start")
t = 0.0
while True:
t += 0.001
if t > 1000:
break
# some other stuff here
print ("complete")``````
This is simple and sufficient for your requirement in python

### Re: controlling number of times loop is executed?

Posted: Sat Jun 01, 2019 7:28 pm
ankith26 wrote:
Sat Jun 01, 2019 4:32 am

Code: Select all

``````print("start")
t = 0.0
while True:
t += 0.001
if t > 1000:
break
# some other stuff here
print ("complete")``````
This is simple and sufficient for your requirement in python
Horribly written though IMHO, and it doesn't present the initial 0.0 value to the "some other stuff here" code, which seems to me to be a bug.

If not going for what I consider my more elegant 'frange()' solution above, I would recommend -

Code: Select all

``````print("start")
t = 0.0
while t <= 1000.0:
# some other stuff here
t += 0.001
print ("complete")``````

### Re: controlling number of times loop is executed?

Posted: Sat Jun 01, 2019 8:01 pm
Or maybe...

Code: Select all

``````print ("start")
for t in range(0,1000,0.001):
# do stuff
print ("complete")``````

### Re: controlling number of times loop is executed?

Posted: Sat Jun 01, 2019 9:18 pm

Code: Select all

``````[email protected]:~/tmp \$ cat float-range.py
for f in range(1, 2, 0.1):
print(f)
``````

Code: Select all

``````[email protected]:~/tmp \$ python2 float-range.py
Traceback (most recent call last):
File "float-range.py", line 1, in <module>
for f in range(1, 2, 0.1):
TypeError: range() integer step argument expected, got float.
``````

Code: Select all

``````[email protected]:~/tmp \$ python3 float-range.py
Traceback (most recent call last):
File "float-range.py", line 1, in <module>
for f in range(1, 2, 0.1):
TypeError: 'float' object cannot be interpreted as an integer
``````

### Re: controlling number of times loop is executed?

Posted: Sun Jun 02, 2019 5:36 am
hippy wrote:
Sat Jun 01, 2019 7:28 pm
If not going for what I consider my more elegant 'frange()' solution above, I would recommend -

Code: Select all

``````print("start")
t = 0.0
while t <= 1000.0:
# some other stuff here
t += 0.001
print ("complete")``````
That is a good solution i agree

### Re: controlling number of times loop is executed?

Posted: Sun Jun 02, 2019 6:30 am
hippy,
I used your frange code like this in python 3

Code: Select all

`````` def frange(fromVal, toVal, stepVal=1.0):
while fromVal < toVal:
yield fromVal
fromVal = fromVal + stepVal

for f in frange(0.0, 20.0, 0.1):
print(f)
``````
It gave me this result

Code: Select all

``````0.0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
1.0999999999999999
1.2
1.3
1.4000000000000001
1.5000000000000002
1.6000000000000003
1.7000000000000004
1.8000000000000005
1.9000000000000006
2.0000000000000004
2.1000000000000005
2.2000000000000006
2.3000000000000007
2.400000000000001
2.500000000000001
2.600000000000001
2.700000000000001
2.800000000000001
2.9000000000000012
3.0000000000000013
3.1000000000000014
3.2000000000000015
3.3000000000000016
3.4000000000000017
3.5000000000000018
3.600000000000002
3.700000000000002
3.800000000000002
3.900000000000002
4.000000000000002
4.100000000000001
4.200000000000001
4.300000000000001
4.4
4.5
4.6
4.699999999999999
4.799999999999999
4.899999999999999
4.999999999999998
5.099999999999998
5.1999999999999975
5.299999999999997
5.399999999999997
5.4999999999999964
5.599999999999996
5.699999999999996
5.799999999999995
5.899999999999995
5.999999999999995
6.099999999999994
6.199999999999994
6.299999999999994
6.399999999999993
6.499999999999993
6.5999999999999925
6.699999999999992
6.799999999999992
6.8999999999999915
6.999999999999991
7.099999999999991
7.19999999999999
7.29999999999999
7.39999999999999
7.499999999999989
7.599999999999989
7.699999999999989
7.799999999999988
7.899999999999988
7.999999999999988
8.099999999999987
8.199999999999987
8.299999999999986
8.399999999999986
8.499999999999986
8.599999999999985
8.699999999999985
8.799999999999985
8.899999999999984
8.999999999999984
9.099999999999984
9.199999999999983
9.299999999999983
9.399999999999983
9.499999999999982
9.599999999999982
9.699999999999982
9.799999999999981
9.89999999999998
9.99999999999998
10.09999999999998
10.19999999999998
10.29999999999998
10.399999999999979
10.499999999999979
10.599999999999978
10.699999999999978
10.799999999999978
10.899999999999977
10.999999999999977
11.099999999999977
11.199999999999976
11.299999999999976
11.399999999999975
11.499999999999975
11.599999999999975
11.699999999999974
11.799999999999974
11.899999999999974
11.999999999999973
12.099999999999973
12.199999999999973
12.299999999999972
12.399999999999972
12.499999999999972
12.599999999999971
12.69999999999997
12.79999999999997
12.89999999999997
12.99999999999997
13.09999999999997
13.199999999999969
13.299999999999969
13.399999999999968
13.499999999999968
13.599999999999968
13.699999999999967
13.799999999999967
13.899999999999967
13.999999999999966
14.099999999999966
14.199999999999966
14.299999999999965
14.399999999999965
14.499999999999964
14.599999999999964
14.699999999999964
14.799999999999963
14.899999999999963
14.999999999999963
15.099999999999962
15.199999999999962
15.299999999999962
15.399999999999961
15.499999999999961
15.59999999999996
15.69999999999996
15.79999999999996
15.89999999999996
15.99999999999996
16.09999999999996
16.19999999999996
16.29999999999996
16.399999999999963
16.499999999999964
16.599999999999966
16.699999999999967
16.79999999999997
16.89999999999997
16.99999999999997
17.099999999999973
17.199999999999974
17.299999999999976
17.399999999999977
17.49999999999998
17.59999999999998
17.69999999999998
17.799999999999983
17.899999999999984
17.999999999999986
18.099999999999987
18.19999999999999
18.29999999999999
18.39999999999999
18.499999999999993
18.599999999999994
18.699999999999996
18.799999999999997
18.9
19.0
19.1
19.200000000000003
19.300000000000004
19.400000000000006
19.500000000000007
19.60000000000001
19.70000000000001
19.80000000000001
19.900000000000013
``````
I did not make any changes other than adding brackets for print..
When i use python 2 I get expected result

Code: Select all

``````0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2.0
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3.0
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4.0
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
5.0
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
6.0
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
7.0
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
8.0
8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
8.9
9.0
9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
10.0
10.1
10.2
10.3
10.4
10.5
10.6
10.7
10.8
10.9
11.0
11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.8
11.9
12.0
12.1
12.2
12.3
12.4
12.5
12.6
12.7
12.8
12.9
13.0
13.1
13.2
13.3
13.4
13.5
13.6
13.7
13.8
13.9
14.0
14.1
14.2
14.3
14.4
14.5
14.6
14.7
14.8
14.9
15.0
15.1
15.2
15.3
15.4
15.5
15.6
15.7
15.8
15.9
16.0
16.1
16.2
16.3
16.4
16.5
16.6
16.7
16.8
16.9
17.0
17.1
17.2
17.3
17.4
17.5
17.6
17.7
17.8
17.9
18.0
18.1
18.2
18.3
18.4
18.5
18.6
18.7
18.8
18.9
19.0
19.1
19.2
19.3
19.4
19.5
19.6
19.7
19.8
19.9
``````
Why does python 3 make such an error

### Re: controlling number of times loop is executed?

Posted: Sun Jun 02, 2019 6:39 am
What error?

### Re: controlling number of times loop is executed?

Posted: Sun Jun 02, 2019 12:46 pm
joan wrote:
Sun Jun 02, 2019 6:39 am
What error?
^ what he said

What you really mean is "why is it different" ?

Personally, no idea, but it undoubtedly comes down to implementation and/or internal representation of floating point numbers. Perhaps even the way the print processes or formats a number for display.

### Re: controlling number of times loop is executed?

Posted: Sun Jun 02, 2019 1:05 pm
I don't think python 2 has built in symbolic maths (but might be wrong) so it must be built in formatting in the implementation of print. Normally that's good but sometimes it might be bad so they changed it... The python3 version is probably what the floats actually are, so less scope for confusion.