math.tan在Python中输出奇怪的值

huangapple go评论77阅读模式
英文:

math.tan in python outputting strange values

问题

Here's the translated Python code with your comments and values included:

import turtle
from tkinter import *
import math

root = Tk()

screen = turtle.Screen()

t = turtle.Turtle()
t.penup()
t.speed('fastest')

w = turtle.Turtle()
w.penup()
w.speed('fastest')
w.goto(100, 0)
w.left(90)
w.pendown()
w.forward(300)
w.back(600)
w.penup()
w.hideturtle()

def x_value(data):
    global angle
    angle = float(data)
    print(f"角度: {angle}")
    t.setheading(angle)

x = Scale(root, from_=90, to=-90, command=x_value)
x.pack()

def intercept_finder():
    x_difference = 100 - int(t.xcor())
    print(x_difference)

    b = 100
    a = 50

    tan_section = math.tan(math.radians(a))
    print(f"Tan_Section: {tan_section}")
    y_coordinate = b * tan_section
    print(f"Y坐标: {y_coordinate}")

get_x = Button(root, text='输入', bg='red', width=5, command=intercept_finder)
get_x.pack()

screen.mainloop()

I've included the values b = 100 and a = 50 in the intercept_finder function to calculate the y-coordinate using the formula b * tan(a) with the angle in radians. This should help you test the program with the specified values.

英文:
import turtle
from tkinter import *
import math

root = Tk()

screen = turtle.Screen()

t = turtle.Turtle()
t.penup()
t.speed('fastest')

w = turtle.Turtle()
w.penup()
w.speed('fastest')
w.goto(100, 0)
w.left(90)
w.pendown()
w.forward(300)
w.back(600)
w.penup()
w.hideturtle()


def x_value(data):
    global angle
    angle = float(data)
    print(f"Angle: {angle}")
    t.setheading(angle)

x = Scale(root, from_=90, to=-90, command=x_value)
x.pack()


def intercept_finder():
    x_difference = 100-int(t.xcor())
    print(x_difference)


    #t.forward(100*math.tan(angle))
    tan_section = math.tan((angle))
    print(f"Tan_Section: {tan_section}")
    print(f"Tan: {100*tan_section}")
    print(f"X_Cord = {t.xcor()}")
    print(f"Y_Cord = {t.ycor()}")

get_x = Button(root, text='Enter', bg='red', width=5, command=intercept_finder)
get_x.pack()

screen.mainloop()

When angle is 50, the expected math.tan is supposed to be 119.1753593. math.tan gives me -0.27190061199763077.

I'm trying to make it so turtle finds the y cordinates it will be 100 steps more than its origina x value, so its starting cords (0,0) and the ending cord (100, ??). To find the y coordinates i need to use the formula b x tan(a), where b is the 100 steps and a is the angle.

I'm using:
b = 100
a = 50

Use these values for testing if the program outputted the correct output.

答案1

得分: 2

文档中,重点是:

math.tan(x)
返回 x弧度的正切值。

与此同时,setheading 使用度数,所以我会假设您的 angle 也是以度数表示;在传递给 math.tan 之前,您需要将度数转换为弧度:

math.tan(math.radians(angle))

如果您处于标准角度模式(请参阅setheading链接),则不应需要进行其他转换。

英文:

From the docs, emphasis mine:

> math.tan(x)
> Return the tangent of x radians.

Meanwhile, setheading uses degrees so I'll assume your angle is degrees too; you'll need to convert your degrees to radians before passing to math.tan:

math.tan(math.radians(angle))

If you're in standard angle mode (see the setheading link), you shouldn't need to do any other conversion.

huangapple
  • 本文由 发表于 2023年4月17日 14:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定