This question already has answers here:
Javascript sin function issue
(4 answers)
Closed 8 years ago.
I am working with a friend on a little port from the bukkit plugin WorldEdit to the minecraft pocket edition. The scripts are in JavaScript (so forget jQuery).
I was testing my theoretical calculations to check if I can calculate the height of c (hc) if I know the length of a and the beta angle.
Example data:
a: 20 cm
beta: 40°
Formula to calculate hc:
sin(40)*20
Result:
12.85575219374
I tested the formula at Chrome and IE and I got
14.902263209586957
Am I doing something wrong or has my calculator/pc problems?
Greetings
miny
There are different ways of representing angles (degrees or radians). In your calculation you are using degrees, but the function sin expects radians. So you need to convert degrees to radians.
function degToRad(deg) {
return deg * Math.PI / 180;
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I'm trying to understand the math behind the Math.tan method but it doesn't make any sense. Can someone please explain to me how it works?
Firstly the mathematical formula to solve for a tangent angle is Tangent = Opposite/Adjacent. Which means I need to know two sides of the triangle to figure out the tangent angle. However the Math.tan method only accept a single argument in radians, not the length of two sides, so I don't understand how it's figuring out the angle of the tangent.
Next in examples they show passing impossibly huge radian values into the method and getting back a value. For example, W3 schools shows the example of Math.tan(90) but 90 radians equals 5,156.6 degrees which is an impossible angle for a corner of a right triangle.
How does this method work, what's happening behind the scenes that turns 90 radians into a tangent angle of -1.995200412208242
First let's talk about what a tangent is: Tangent is y/x for the coordinate at a specific point (see this YouTube video for an example).
So if you want the tangent of pi over 2, on a graph, that's at a 90 degree angle, so the coordinate is (0, 1). Then you just divide 1/0.
However, Math.tan isn't very precise. In Chrome 52, Math.tan(Math.PI / 2) (from the video above) is 16331239353195370, even though it should evaluate to the Infinity value in JavaScript (the mathematical value is actually undefined since it works out to 1/0).
According to this answer, the V8 implementation is:
function MathTan(x) {
return MathSin(x) / MathCos(x);
}
(As a side note: The value that Chrome actually outputs is larger than Number.MAX_SAFE_INTEGER, which you can prove by running Number.MAX_SAFE_INTEGER < Math.tan(Math.PI / 2) // => true, so Chrome takes it to be "close to infinity".)
Edit
The reason for the lack of a precise value is that pi is generally represented as a fixed value (since we have limitations in computer memory), even though it should be "Infinity" or "undefined" outside of the context of programming. For example, in my browser, Math.PI is fixed at 3.141592653589793.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
The cosine of 90 degrees is 0.
I understand Javascript's Math.cos takes radians, so I've tried:
Math.cos(90 * Math.PI / 180)
Why does this yield 6.123233995736766e-17 instead of 0?
6.123233995736766e-17 is essentially 0. It's 0.00000000000000006123233995736766. That kind of minor error is completely normal when working with IEEE floating point numbers.
The solution is to never compare numbers exactly, but compare if they are within a range that you expect. EG.
var result = Math.cos(90 * Math.PI / 180);
if ( Math.abs( result - 0 ) < 1e-16 ) {
// Test passed, result is effectively 0
}
The - 0 does nothing, but more generally - x is how you would compare to x. You are trying to compare to 0, so I used - 0, but you could leave that off and get the same result.
This question already has answers here:
How can I get sin, cos, and tan to use degrees instead of radians?
(6 answers)
Closed 8 years ago.
the actual value of cos(15) is 0.9659258262890682867497431997289. but when i entered Math.cos(15) int the Google Chrome console, it's showing -0.7596879128588205 as the result. Why would it show this error? Why can't JavaScript return 0.9659258262890682867497431997289?
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);
-.759 is when the argument is in radians, .965 is when it's in degrees. All you have to do is convert to radians to get the result you want.
This question already has answers here:
How can I get sin, cos, and tan to use degrees instead of radians?
(6 answers)
Closed 8 years ago.
When I use Math.sin(90) for calculating Sine of 90 degrees in javascript it returns 0.8939966636005565, but sin(90) is 1. Is there a way to fix that? I need accurate values for any angle.
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button calculate value of 90 degrees.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction(){
document.getElementById("demo").innerHTML=Math.sin(90);
}
</script>
Math.sin expects the input to be in radian, but you are expecting the result of 90 degree. Convert it to radian, like this
console.log(Math.sin(90 * Math.PI / 180.0));
# 1
As per the wikipedia's Radian to Degree conversion formula,
Angle in Radian = Angle in Degree * Math.PI / 180
The sin function in Javascript takes radians, not degrees. You need to convert 90 to radians to get the correct answer:
Math.sin(90 * (Math.PI / 180))
i created a program in javascript that computes vector coordinates, everything was smooth since i have the correct formula, but when i try to conpute for the cosine of 143.1301 using Math.cos in javascript it returns 0.1864 instead of 0.7999 from the scientific calculator why is that? can anyone explain to me why? and also please give me the solution for this problem... thanks in advance... :) here;s a sample of my code
function cyltoxec(a)
{
ans = Math.cos(a);
return ans.toFixed(4);
}
var = x;
return cyltoxec(x);
Trigonometric functions in JavaScript (and indeed, in most mathematical parlance and programming) use radians as the angular unit, not degrees.
There are 2 * Pi radians in 360 Degrees. Thus, the cosine of a degrees is
Math.cos(a * Math.PI/180)
Math.cos expects its argument to be in radians, not degrees. Try Math.cos(a * Math.PI/180) instead.