0

I'm making a program that calculates distance moved of an object however when I run the program using 45 as an angle (sin45 and cos45 are equivalent) the output is different for vertical height and horizontal height

import math

angle = float(input("Enter Angle of Movement(In Degrees): "))
print("Angle is: ",angle,"°")

horizontal_distance = (abs(overall_distance*math.sin(90-angle)))
print("Horizontal Distance:",horizontal_distance,"m")

vertical_distance = (abs(overall_distance*math.cos(90-angle)))
print("Vertical Distance:",vertical_distance,"m")
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065

3 Answers3

5

The input values for sine and cosine are in radians, not degrees.

ag2718
  • 101
  • 2
  • 9
  • thanks for the reply, i am working to a specification and one requirement is that user should input the angle in degrees. Is there any way around this? thanks again –  Nov 01 '20 at 18:56
  • Yeah, you can always convert the value from the input from degrees to radians before you calculate the trigonometric functions for it. – ag2718 Nov 01 '20 at 18:58
  • @HarrisonBowers there is simple way to do it. `from numpy import deg2rad` and use it like this: `deg2rad(angle_in_degrees)` (I think) – Matiiss Nov 01 '20 at 19:06
  • You can also just multiply the angle in degrees by `math.pi / 180` – ag2718 Nov 01 '20 at 19:07
  • even if the expression is in radian, sin and cos 45 may still be different because there's no way to represent pi exactly in floating-point, and because transcendental operations isn't required to be properly rounded in IEEE-754 [Why do sin(45) and cos(45) give different results?](https://stackoverflow.com/q/31509019/995714) – phuclv Nov 03 '20 at 04:39
1

The input is in radian units, not degree. Here's the documentation - https://docs.python.org/3/library/math.html.

Instead, you should replace sin(90-angle) with sin(math.pi/2 - angle).

UPDATE: Just saw your post about converting degree to radian. You can use math.radians(degree)

fendy3d
  • 363
  • 3
  • 9
  • much appreciated :) im very new to python so i appreciate the help. EDIT: still seems to output different values for me even though they are equivalent –  Nov 01 '20 at 18:59
  • Can you write down the code which converts degree to radian and computing the sin of it? – fendy3d Nov 01 '20 at 19:04
  • Try replace "90-angle" with "math.pi/2 - math.radians(angle)" – fendy3d Nov 01 '20 at 19:07
  • I think i've got it now! Thank you so much for your help (and other people's comments). I appreciate you taking time to help a new user:) –  Nov 01 '20 at 19:09
  • Also note that the output of this: `print math.sin(math.radians(45)) == math.cos(math.radians(45));` returns `False`. So yes, they are equivalent, but they are not the same! – Luuk Nov 02 '20 at 08:56
0

For the second question

import math
x = math.radians(float(input()))
y = math.radians(float(input()))
SIN = math.sin(x)
COS = math.cos(y)
print(x)
print(y)
print(SIN)
print(COS)


45 
45
0.7853981633974483
0.7853981633974483
0.7071067811865476
0.7071067811865476
Ohad Forman
  • 64
  • 13