javascript math.cos error / Math.cos() delivers wrong result [duplicate] - javascript

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.

Related

Confusion over unexpected Math.cos behavior in Javascript [duplicate]

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.

Javascript Sinus not correct [duplicate]

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;
}

How do i use Math.sin() in javascript to get correct answer? [duplicate]

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))

finding value of theta using the equation of circle

Using the equation of circle I have
x = rCos(theta)
y = rSin(theta)
Now I want to calculate theta. I have
x = -87.91
r = 14.63
what will be the value of theta? using
Math.acos(x/r)
its giving me NaN...
From MDN
The acos method returns a numeric value between 0 and pi radians for
x between -1 and 1. If the value of number is outside this range, it
returns NaN.
You need to get it within the range.
If you have x = -87.91, than r >= 87.91, so you have something wrong.
The best method in such situations is usually theta = Math.atan2(y, x) http://en.wikipedia.org/wiki/Atan2
sine and cosine don't have true inverses unless you restrict their range. So the acos and asin methods are only defined over these restricted ranges (which become the domains of asin and acos).
You have to ensure that theta is a value in radians, and that sentiment has been echoed thoroughly. If you don't want to convert values before you enter them, then you can use a conversion formula while calculating x and y.
x = rCos(theta * (3.14/180))
y = rSin(theta * (3.14/180))

javascript Math.cos shows different answer compare to scientific calculator

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.

Categories