Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python cos(90) and cos(270) not 0

What is going on??

Testing out sin and cos functions to find out why I get so beautiful positioning on the wrong places when outputting my coordinates into a SVG file. So I made this test code, which I can predict what the answer is to find out why. Oddly nothing that effects the calculation it self adds this behavior but simply the position of where I'm about to stay. If the position is 0 and will become 0 after the calculation it won't work, but if position is 1 and will become 1 after the calculation it works.

First Test:

import math

cX = 2
cY = 2
r = 2

rcX = cX + (r * math.cos(math.radians(0)))
rcY = cY + (r * math.sin(math.radians(0)))

print rcX #4
print rcY #2
r = 1

rlX = rcX + (r * math.cos(math.radians(90)))
rlY = rcY + (r * math.sin(math.radians(90)))

print rlX #4
print rlY #3
r = 4

flX = rlX + (r * math.cos(math.radians(180)))
flY = rlY + (r * math.sin(math.radians(180)))

print flX #0
print flY #3
r = 2

print r * math.cos(math.radians(270))
print flX + (r * math.cos(math.radians(270))) #-3.67394039744e-16 should be 0
print flY + (r * math.sin(math.radians(270))) #1

Now I change cX to 3 and it works even if it doesn't effect the calculation which is:

r * math.cos(math.radians(270))

The result of that calculation is added to the x coordination

import math

cX = 3
cY = 2
r = 2

rcX = cX + (r * math.cos(math.radians(0)))
rcY = cY + (r * math.sin(math.radians(0)))

print rcX #5
print rcY #2
r = 1

rlX = rcX + (r * math.cos(math.radians(90)))
rlY = rcY + (r * math.sin(math.radians(90)))

print rlX #5
print rlY #3
r = 4

flX = rlX + (r * math.cos(math.radians(180)))
flY = rlY + (r * math.sin(math.radians(180)))

print flX #1
print flY #3
r = 2

print r * math.cos(math.radians(270))
print flX + (r * math.cos(math.radians(270))) #1
print flY + (r * math.sin(math.radians(270))) #1
like image 970
Andreas Avatar asked Nov 06 '12 22:11

Andreas


People also ask

Is cos 90 equal to zero?

Cos 90 degrees is the value of cosine trigonometric function for an angle equal to 90 degrees. The value of cos 90° is 0.

Is Python cos in radians or degrees?

In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math. cos() function returns the cosine of value passed as argument. The value passed in this function should be in radians.

How do you solve cos in Python?

How to Find Cosine in Python. To calculate the cosine function with the python language, it use the cos() function of the math library. The argument x is the radian value of the angle. The cos(x) function returns the cosine of x in output.

What does cos mean in Python?

The math. acos() method returns the arc cosine value of a number. Note: The parameter passed in math. acos() must lie between -1 to 1.


1 Answers

Indeed, it is a very low number, awfully close to zero. Here's a great article which can help you understand the common challenges and pitfalls of floats: "What Every Computer Scientist Should Know About Floating-Point Arithmetic"

like image 75
Brian Cain Avatar answered Sep 22 '22 14:09

Brian Cain