On a en rêvé, Python l’a fait : atan2(y,x)

Bonjour à tous

fortuitement, je suis tombé sur la fonction atan2 du module math de Python.

Cette fonction prend deux arguments comme indiqué par la docstring peu illuminante (et surtout fausse) :

———————-

atan2(y, x)

Return the arc tangent (measured in radians) of y/x.

Unlike atan(y/x), the signs of both x and y are considered.

———————-

Si on lit bien la deuxième ligne est contredite par la troisième :

atan2(y,x) n’est pas la même chose que atan2(-y,-x)

 

En fait ce que fait VRAIMENT atan2 est très pratique : elle retourne VRAIMENT l’argument de x+iy. (Même pour x nul , et y non nul quand même !).

Comme je le dis toujours avec insistance en sup : pour calculer l’argument theta de x+iy

on sait que si x n’est pas nul, tan(theta) =y/x mais on ne se jette pas sur atan(y/x) !

Les arctan vivent du côté droit du cercle trigonométrique, là où x est positif (strict). Ainsi :

Si x>0, theta=atan(y/x) mod 2pi mais

si x<0 , theta=atan(y/x)+pi mod 2pi

Eh bien, devinez quoi ? atan2(y,x) renvoie exactement cela !

Autrement dit atan2(y,x) renvoie :  atan(y/x) si x>0, atan(y/x)+pi si x<0 (enfin d’après ce que j’ai testé), et même +pi/2 ou -pi/2 pour x=0 suivant le signe de y non nul.

(Bon il renvoie 0 pour atan2(0,0) bof là c’est plutôt mal).

On aurait pu souhaiter que cette fonction s’appelât arg(x,y) plutôt mais bon… le lien avec atan a un côté sympathique non ?

 

 

 

 

 

Laisser un commentaire