1

我正在尝试制作一个基本工具,让我的日常生活更轻松,为我解决一些任务。不幸的是,当使用切线时,我无法弄清楚如何让它以度数计算。

我的代码:

import math


class Astro():
    def start(self):
        velocity = input("What is the galaxy's velocity? (m/s) \n")
        peculiar = (float(velocity) - 938600) ** 2
        mass = (3 * float(peculiar) * (10 ** 11) * 50 * (10 ** 6) * (8 * (180 / math.pi))
                * 9.46 * (10 ** 15)) / (2 * 6.67 * (10 ** -11))
        print("The galaxy's mass is " + str(mass) + " kg. \n")


if __name__ == '__main__':
    sup = Astro()
    sup.start()

编辑:抱歉缺少上下文;这是关于使用 2 个函数来计算星系的质量,第一个函数,第 7 行得到特殊速度,第二个函数在第 8-9 行得到所考虑星系的实际质量。

已解决:math.tan(8 * pi / 180)

谢谢你的帮助!

4

3 回答 3

7

计算机以弧度工作。尝试

answer = tan(angle * pi / 180)

将您的角度(以度为单位)用于三角函数。或者试试

answer = atan(number) * 180 / pi  

以度数得到答案。

于 2018-01-26T16:10:07.287 回答
3

math软件包具有功能radiansdegrees但在引擎盖下,这些只是:

def radians(deg):
    return deg * pi / 180

def degrees(rad):
    return rad * 180 / pi

这是一个包装器,您可以使用它来制作使用度数的三角函数(只是把它放在某个地方,尽管我使用numpy而不是math

import math
import itertools
import functools

def _use_deg(f, arc = False):
    if not arc:
        def df(*args):
            args = list(args)
            for index, value in enumerate(args):
                try:
                    args[index] = math.radians(value)
                except TypeError:
                    pass
            return f(*args)
    else:
        def df(*args):
            return math.degrees(f(*args))
    return functools.wraps(f)(df)


sind = _use_deg(math.sin)
cosd = _use_deg(math.cos)
tand = _use_deg(math.tan)
arcsind = _use_deg(math.asin, True)
arccosd = _use_deg(math.acos, True)
arctand = _use_deg(math.atan, True)
arctan2d = _use_deg(math.atan2, True)
于 2018-01-26T16:16:41.280 回答
1

您不想与数学库发生争执。让数学库给你一个弧度的答案,然后将它的答案乘以180/math.pi得到度数。

于 2018-01-26T16:13:47.193 回答