Variable devient négative

Résolu
SerialCookie -  
Dalfab Messages postés 706 Date d'inscription   Statut Membre Dernière intervention   - 19 nov. 2020 à 20:58
Bonjour,

Je suis en train de créer un simple programme python pour tracer la trajectoire d'un objet en chute libre. Le problème est que j'utilise 2 variables locales qui deviennent négatives lorsque je les utilise dans des calculs (même avec la fonction abs()). Pourtant, lorsque je les print, ils sont positifs... Voici le code
from math import sin, cos
import matplotlib.pyplot as p
def graph(x,y):
    p.xlabel('Coordonnées x')
    p.ylabel('Coordonnées y')
    p.title('Chute libre d\'un corps')
    p.plot(x,y)
def genxy(theta,v):
    print(v,theta)
    x=[]
    y=[]
    v=abs(v)
    theta=abs(theta)
    tvol=2*v*sin(theta)/9.8
    for i in inter(0,tvol,0.001):
        y.append(v*sin(theta)*i-(0.5*9.8*i**2))
        x.append(v*cos(theta)*i)
    graph(x,y)
def inter(d,f,i):
    n=[]
    while d<f:
        n.append(d)
        d+=i
    return n
def again():
    choix=input('Voulez-vous recommencer ? O ou N :\n')
    if choix=='O':
        main()
    elif choix=='N':
        print('Bye bye')
    else:
        print('Syntaxe invalide')
        again()
def main():
    ng=1
    try:
        while True:
            v=int(input('Vitesse {0}:\n'.format(ng)))
            theta=int(input('Angle {0}:\n'.format(ng)))
            genxy(v,theta)
            ng+=1
    except ValueError:
        if ng==1:
            print('Syntaxe invalide')
            main()
        else:
            p.show()
            again()
if __name__ == "__main__":
    main()

Merci d'avance
Configuration: Windows / Firefox 82.0

3 réponses

SerialCookie
 
Petite précision : en cherchant, j'ai trouvé que le problème venait de sin() et de cos()

theta=abs(theta)
print(sin(theta))
print(sin(45))

et pourtant le 1er print() renvoie -0.9...
0
SerialCookie
 
Okkkkkkkkkkkkk...
Excusez-moi pour cette discussion inutile, j'avais inversé les variables v et t. Je le ferme tout de suite
0
Dalfab Messages postés 706 Date d'inscription   Statut Membre Dernière intervention   101
 
Bonjour,
un sinus comme un cosinus ça peut être négatif.
exemple: cos(3.14159) ça vaut -1
0