i'm writing a code in Python 3 to do some calculus with some parameters that user enters with keyboard.
When I was testing the program I faced and isolated the next problem:
Evaluating the angle in radians in a sin() function, converted from a degree angle entered by user, there is some trouble with the value, I suppose that is due to the decimals proximations in the operation.
Heres is the part of the code:
import math
degrees = int(input())
radians = degrees*(math.pi/180)
print(math.sin(radians))
So, most of the values you enter en degrees, seems ok, but when you enter 180º (Corresponds to PI and the resul must be 0), the output value is a very tiny value (1.2246467991473532e-16), but not 0.
How about casting to
round()
?Which outputs
0
when you enter180
. But you are correct that it's going to round to the nearest integer no matter what.math
hasmath.radians()
which will easily convert to radians, but won't cure your "false 0."I agree with the other commenter that you may need to set a threshold and anything below that prints 0.