atan2 weirdness

N

narutocanada

hi

atan2 is supposed to return the angle to x-axis when given y and x, I
suppose if I take [x,y] to one full circle, I should get 0-360 degree
back---- but no, I get 3 full revolutions!
maybe my understanding is wrong.

from math import *

def f(ang):
a=ang
if a>360: a-=360
if a>360: a-=360
if a<0: a+=360
if a<0: a+=360
return round(a)

for i in range(0,360):
t=2*pi*i/360.0
print i,f(atan2(sin(t),cos(t))*180.0)

0 0.0
1 3.0
2 6.0
3 9.0
4 13.0
5 16.0
6 19.0
7 22.0
8 25.0
9 28.0
10 31.0
11 35.0
12 38.0
13 41.0
14 44.0
15 47.0
16 50.0
17 53.0
18 57.0
19 60.0
20 63.0
21 66.0
22 69.0
23 72.0
24 75.0
25 79.0
26 82.0
27 85.0
28 88.0
29 91.0
30 94.0
31 97.0
32 101.0
33 104.0
34 107.0
35 110.0
36 113.0
37 116.0
38 119.0
39 123.0
40 126.0
41 129.0
42 132.0
43 135.0
44 138.0
45 141.0
46 145.0
47 148.0
48 151.0
49 154.0
50 157.0
51 160.0
52 163.0
53 167.0
54 170.0
55 173.0
56 176.0
57 179.0
58 182.0
59 185.0
60 188.0
61 192.0
62 195.0
63 198.0
64 201.0
65 204.0
66 207.0
67 210.0
68 214.0
69 217.0
70 220.0
71 223.0
72 226.0
73 229.0
74 232.0
75 236.0
76 239.0
77 242.0
78 245.0
79 248.0
80 251.0
81 254.0
82 258.0
83 261.0
84 264.0
85 267.0
86 270.0
87 273.0
88 276.0
89 280.0
90 283.0
91 286.0
92 289.0
93 292.0
94 295.0
95 298.0
96 302.0
97 305.0
98 308.0
99 311.0
100 314.0
101 317.0
102 320.0
103 324.0
104 327.0
105 330.0
106 333.0
107 336.0
108 339.0
109 342.0
110 346.0
111 349.0
112 352.0
113 355.0
114 358.0
115 1.0
116 4.0
117 8.0
118 11.0
119 14.0
120 17.0
121 20.0
122 23.0
123 26.0
124 30.0
125 33.0
126 36.0
127 39.0
128 42.0
129 45.0
130 48.0
131 52.0
132 55.0
133 58.0
134 61.0
135 64.0
136 67.0
137 70.0
138 74.0
139 77.0
140 80.0
141 83.0
142 86.0
143 89.0
144 92.0
145 96.0
146 99.0
147 102.0
148 105.0
149 108.0
150 111.0
151 114.0
152 118.0
153 121.0
154 124.0
155 127.0
156 130.0
157 133.0
158 136.0
159 140.0
160 143.0
161 146.0
162 149.0
163 152.0
164 155.0
165 158.0
166 162.0
167 165.0
168 168.0
169 171.0
170 174.0
171 177.0
172 180.0
173 183.0
174 187.0
175 190.0
176 193.0
177 196.0
178 199.0
179 202.0
180 205.0
181 158.0
182 161.0
183 164.0
184 167.0
185 170.0
186 173.0
187 177.0
188 180.0
189 183.0
190 186.0
191 189.0
192 192.0
193 195.0
194 198.0
195 202.0
196 205.0
197 208.0
198 211.0
199 214.0
200 217.0
201 220.0
202 224.0
203 227.0
204 230.0
205 233.0
206 236.0
207 239.0
208 242.0
209 246.0
210 249.0
211 252.0
212 255.0
213 258.0
214 261.0
215 264.0
216 268.0
217 271.0
218 274.0
219 277.0
220 280.0
221 283.0
222 286.0
223 290.0
224 293.0
225 296.0
226 299.0
227 302.0
228 305.0
229 308.0
230 312.0
231 315.0
232 318.0
233 321.0
234 324.0
235 327.0
236 330.0
237 334.0
238 337.0
239 340.0
240 343.0
241 346.0
242 349.0
243 352.0
244 356.0
245 359.0
246 2.0
247 5.0
248 8.0
249 11.0
250 14.0
251 18.0
252 21.0
253 24.0
254 27.0
255 30.0
256 33.0
257 36.0
258 40.0
259 43.0
260 46.0
261 49.0
262 52.0
263 55.0
264 58.0
265 62.0
266 65.0
267 68.0
268 71.0
269 74.0
270 77.0
271 80.0
272 84.0
273 87.0
274 90.0
275 93.0
276 96.0
277 99.0
278 102.0
279 106.0
280 109.0
281 112.0
282 115.0
283 118.0
284 121.0
285 124.0
286 128.0
287 131.0
288 134.0
289 137.0
290 140.0
291 143.0
292 146.0
293 150.0
294 153.0
295 156.0
296 159.0
297 162.0
298 165.0
299 168.0
300 172.0
301 175.0
302 178.0
303 181.0
304 184.0
305 187.0
306 190.0
307 193.0
308 197.0
309 200.0
310 203.0
311 206.0
312 209.0
313 212.0
314 215.0
315 219.0
316 222.0
317 225.0
318 228.0
319 231.0
320 234.0
321 237.0
322 241.0
323 244.0
324 247.0
325 250.0
326 253.0
327 256.0
328 259.0
329 263.0
330 266.0
331 269.0
332 272.0
333 275.0
334 278.0
335 281.0
336 285.0
337 288.0
338 291.0
339 294.0
340 297.0
341 300.0
342 303.0
343 307.0
344 310.0
345 313.0
346 316.0
347 319.0
348 322.0
349 325.0
350 329.0
351 332.0
352 335.0
353 338.0
354 341.0
355 344.0
356 347.0
357 351.0
358 354.0
359 357.0
 
C

Carsten Haese

hi

atan2 is supposed to return the angle to x-axis when given y and x, I
suppose if I take [x,y] to one full circle, I should get 0-360 degree
back---- but no, I get 3 full revolutions!
maybe my understanding is wrong.

from math import *

def f(ang):
a=ang
if a>360: a-=360
if a>360: a-=360
if a<0: a+=360
if a<0: a+=360
return round(a)

for i in range(0,360):
t=2*pi*i/360.0
print i,f(atan2(sin(t),cos(t))*180.0)

Your understanding of atan2 is correct, but your conversion from radians
to degrees is off by a factor of pi. Since pi is close to 3, you're
getting what appears to be three full revolutions.

HTH,
 
R

Raymond Hettinger

hi

atan2 is supposed to return the angle to x-axis when given y and x, I
suppose if I take [x,y] to one full circle, I should get 0-360 degree
back---- but no, I get 3 full revolutions!
maybe my understanding is wrong.

from math import *

def f(ang):
 a=ang
 if a>360: a-=360
 if a>360: a-=360
 if a<0: a+=360
 if a<0: a+=360
 return round(a)

for i in range(0,360):
 t=2*pi*i/360.0
 print i,f(atan2(sin(t),cos(t))*180.0)

The "*180.0" part should be "*180/pi".

Do yourself some favors. First use the degrees and radians functions
in the math module. Second, when in doubt, print the intermediate
results (that would have made the error obvious). Third, pick just one
line of the result and compute it with your calculator by hand (to
make sure you know what your program is doing. Lastly, when working
with trig functions, always check the docs to make sure you know the
ranges of values returned (i.e. atan2 returns from -pi to +pi).
Here's a proposed revision:

from math import pi, atan2, radians, degrees, cos, sin

for i in range(360):
t = radians(i)
x = cos(t)
y = sin(t)
a = degrees(atan2(y, x))
print i, t, x, y, a


Raymond
 

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top