1

folks,

If I have a list of floats and I want to make them within the range 0 to 2pi by adding or subtracting 2pi. What is a good way to do it?

Thanks very much.

nos
  • 19,875
  • 27
  • 98
  • 134

3 Answers3

3

Use % operator:

>>> pi = 3.1415

>>> angle = 2*pi+0.5
>>> angle % (2*pi)
0.5

>>> angle = -4*pi + 0.5
>>> angle % (2*pi)
0.5

For a list of angles just use list comprehensions:

>>> L = [2*pi + 0.5, 4*pi + 0.6]
>>> [i % (2*pi) for i in L]
[0.5, 0.5999999999999996]
>>> 
ovgolovin
  • 13,063
  • 6
  • 47
  • 78
2

You can take the answers mod 2 pi:

>>> import random
>>> from math import pi
>>> xx = list(random.uniform(-10,10) for i in range(4))
>>> xx
[-3.652068894375777, -6.357128588604748, 9.896564215080154, -6.298659336390939]
>>> yy = list(x % (2*pi) for x in xx)
>>> yy
[2.6311164128038094, 6.209242025754424, 3.613378907900568, 6.267711277968234]
DSM
  • 342,061
  • 65
  • 592
  • 494
  • I'm newish to python myself, but is there a reason for the calls to list() instead of just making them lists in the first place? i.e. xx = [random.uniform(-10, 10) for i in range(4)] – Free Monica Cellio Dec 17 '11 at 16:44
  • @ChadMiller I think `list(...)` and `[...]` does the same in this case. The other thoght is that `list(...)` creates a temporary **generator** expression, and then calls `list` constructor. While list comprehension (your solution) may avoid creating a temporary object, thus it might be more effective. – ovgolovin Dec 17 '11 at 16:49
  • @ChadMiller: two reasons. One is historical: list comprehensions leak variables in python 2.7, and that's caused me problems in the past, so I learned to avoid it. (In the above example, if I used [random etc], then we wind up with a variable i assigned to 3.) The second is that I naturally think in terms of generator expressions -- the underlying (random.uniform(-10, 10) for i in range(4)) -- and I use next a lot, so that's also force of habit. – DSM Dec 17 '11 at 16:57
  • @DSM It may be of some iterest to you. I read an article by Guido Van Rossum (the author of Python), where he wrote that they made list comprehensions leakable to make them more efficient and then faster. He also wrote that it's the reason why list comprehensions leak while generator expressions don't. He wrote that as of Python 3 list comprehensions don't leak. So, for Python 2.x it may be more efficient (in terms of speed) to use list comprehension (while more risky in terms of safety (because of leaking)). – ovgolovin Dec 18 '11 at 19:22
  • @DSM Here is the link to the article: http://python-history.blogspot.com/2010/06/from-list-comprehensions-to-generator.html Citation: "This was an artifact of the original implementation of list comprehensions; it was one of Python's "dirty little secrets" for years. It started out as an intentional compromise to make list comprehensions blindingly fast, and while it was not a common pitfall for beginners, it definitely stung people occasionally." – ovgolovin Dec 18 '11 at 19:28
0

Consider using math.fmod() since it's better at handling rounding of floats than the % operator. See discussion here.

Community
  • 1
  • 1
jmn
  • 1,254
  • 1
  • 8
  • 7