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

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.

Related

Math.floor () to round down to nearest 0.5 in javascript [duplicate]

This question already has answers here:
Javascript roundoff number to nearest 0.5
(10 answers)
Closed 3 years ago.
I have a function from excel that I use: =FLOOR(value,0.5)
With this function, I round DOWN to the nearest half value. For example:
10.55 becomes 10.5
10.99 becomes 10.5
10.2 becomes 10
Etc.
Is there an equivalent in javascript? I know that Math.floor () will round down to the nearest integer. However, does anyone know of a good way to round down to the nearest 0.5 instead?
Thank you in advance
Here's your function:
function slipFloor(num){
let f = Math.floor(num);
if(num-f < 0.5){
return f;
}
return f+0.5;
}
console.log(slipFloor(10.55));
console.log(slipFloor(10.99));
console.log(slipFloor(10.2));

Python/Javascript number precision [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
Probably this question is already existed somewhere but can't find my specific issue.
I have a two numbers that I want to add and I don't want to use round() function since I am working with money.
1.70 + 1.44 = 3.14 //Returns 3.1399999999999997
1.70 + 1.45 = 3.15 //Returns the correct answer
1.70 + 1.37 = 3.07 //Returns 3.0700000000000003
I tried it in Javascript but still the same issue.
Since you're working with money, always use the decimal.Decimal class in Python.
Use parseFloat to precision :
parseFloat(number).toFixed(precision);
E.g.
parseFloat(1.70 + 1.37).toFixed(2); // RETURN 3.07
one way you can achieve this is to multiply the values by (10 * precision) that you need and then divide the result by (10 * precision).
((1.70 * 100) + (1.37 * 100))/100 = 3.07
((1.70 * 100) + (1.45 * 100))/100 = 3.15
((1.70 * 100) + (1.44 * 100))/100 = 3.14
you can add a method to do this for you to keep it simple.

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

javascript math.cos error / Math.cos() delivers wrong result [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.
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.

How to get x of 2^x=8000? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the opposite of JavaScript's Math.pow?
2^x=i
Given i, how can we calculate x using Javascript?
You want to take the logarithm of 8000. JS has the Math.log function which uses base e, you want base 2 so you can write Math.log(8000) / Math.log(2) to get the logarithm of 8000 base 2, which is equal to x.
You need the logarithm from the Math object. It does not provide a base 2 log so do the conversion:
var x = Math.log(8000) / Math.log(2);
Reference to the javascript Math object.
In the more general case we calculate 2^x = i this way:
var i; // Some number
var x = Math.log(i) / Math.log(2);

Categories