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
What Is the Purpose of Std::Make_Pair VS the Constructor of Std::Pair
Is It Possible for the Executable to Ask for Administrator Rights? (Windows 7)
Array of Size Defined by Not Constant Variable
What Is the Motivation Behind Static Polymorphism in C++
Do I Get a Finished Slot If I Start Qprocess Using Startdetached
Passing Std::String by Value or Reference
How to Make a Recursive Rule in Boost Spirit X3 in VS2017
When to Use Template VS Inheritance
Stl Random Distributions and Portability
Sf::Texture as Class Member Doesn't Work
Why Use #Define Instead of a Variable
C++: Class Declaration and Definition Separated Inside Header Causes Duplicate Symbol
Can the Hwnd from Createwindow/Createdialog Be Getmessage'D from Another Thread
How to Print Out the Size of a C++ Class at Compile-Time
Cin >> Fails with Bigger Numbers But Works with Smaller Ones