Cos Returns Wrong Values

python3: math.cos() returns wrong value

Welcome to floats.

If anyone tells you that 6e-17 is not 0 then ignore them. Some calculators will actually round for you.

You can use the round() to make it show 0. But by no means that answer is wrong

Not that I disagree with @Kevin comment about Mars, but the math functions are only estimation they don't use the full extent of Taylor's Series in this case. I'm sure, and I hope NASA implements them differently (different algorithm is possible) than what you see in a basic calculator/computer.

Why is Math.Cos returning the wrong value?

The trigonometric functions expect radians, not degrees.

cos giving out the wrong value

cos(90) (degrees) is approximately zero, as in 0.0. Worrying about getting a number back that is e-17 vs e-9 is not meaningful, they are both basically zero.

How close it the result actually is depends on how they represented pi, 90.0, 180.0, and the implementation of the cos function in math.h.

javascript math.cos error / Math.cos() delivers wrong result

Math.cos takes its argument in radians. Thus, you first need to convert the 15 degrees to radians before calling Math.cos

var degrees = 15;
var radians = degrees * Math.PI / 180;
var cosOf15 = Math.cos(radians);

Math.cos() gives wrong results

deg(90) is approximately equal to 90*Math.PI/180 and your result is aproximately equal to 0.

So, everything is fine ;)

Note that it has to be approximate, because there is no way to represent π precisely.

Math.cos() gives wrong result

Math.cos() expects the parameter to be in radians. This will return the result you need:

Math.cos(Math.toRadians(50));


Related Topics



Leave a reply



Submit