2

I am trying to make a basic tool to make my everyday easier, solving some assignments for me. Unfortunately, I can't figure out how to make it calculate in degrees when tangent is being used.

My code:

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()

EDIT: Sorry for the lack of context; this is about calculating the masses of galaxies using 2 functions, the first one, line 7 to get the peculiar velocity, and the second one in lines 8-9 to get the actual mass of the considered galaxy.

SOLVED: math.tan(8 * pi / 180)

Thank you for all your help!

foot enjoyer
  • 63
  • 1
  • 1
  • 6
  • It looks like you have an error in your code, masse should be mass on line 10. – Qwerty Jan 26 '18 at 16:09
  • 1
    Please update the question with all the information as text in the question. Links to other sites are not a sustainable way to build Stackoverflow. – quamrana Jan 26 '18 at 16:09
  • https://docs.python.org/3/library/math.html#angular-conversion – Georgy Jan 26 '18 at 16:16

3 Answers3

8

Computers work in radians. Try

answer = tan(angle * pi / 180)

to use your angle in degrees into a trig function. Or try

answer = atan(number) * 180 / pi  

to get answer in degrees.

Dan Sp.
  • 1,419
  • 1
  • 14
  • 21
3

The math package has the functions radians and degrees but under the hood these are just:

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

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

Here is a wrapper you can use to make degree-using trig functions (just had it lying around somewhere, although I use numpy instead of 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)
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
1

You don't want to get in a fight with the math library. Let the math library give you an answer in radians, then multiply it's answer by 180/math.pi to get degrees.

quamrana
  • 37,849
  • 12
  • 53
  • 71
Tom Riley
  • 21
  • 2