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

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

Related

How does JavaScript engine compute arithmetic expression? [duplicate]

This question already has answers here:
Why does changing the sum order returns a different result?
(7 answers)
In which order should floats be added to get the most precise result?
(11 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 5 months ago.
Quick summary - I was running a piece of code(in React application) where I am summing up decimal values entered by a user. One of the conditions to move to next page is if the sum equals 100
Problem - In one case, the sum of all values (a+b+c+d+e+f) is being computed to 99.99999999999997 even though the summation(over a calculator) is 100. But when I change the summation order to a+d+e+f+b+c, the sum is correctly computed to 100.
Please find below code samples -
Wrong summation -
const result = [15+10.4+1.9+1.9+4.9+2.8+6.8+2+4.9+2.8+2.5+2.8+4.8+6.5+15+15].reduce((sum, val) => {return sum+val}, 0)
console.log(result) //99.99999999999997
Correct summation -
const result = [15+10.4+4.9+2.8+6.8+2+4.9+2.8+2.5+2.8+4.8+6.5+15+15+1.9+1.9].reduce((sum, val) => {return sum+val}, 0)
console.log(result) //100
Questions -
How does JavaScript engine compute?
How to compute to 100 always (without using ceil, floor or round methods because Math.round(99.6) is also equal to 100.

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

Excel formula calculations into JavaScript [duplicate]

This question already has answers here:
JavaScript exponents
(8 answers)
Closed 4 years ago.
I have a formula into my excel sheet, now I am making that formula using JavaScript. I made a code exact like written on excel sheet but in JavaScript I am getting different result, I mean wrong result.
Excel formula
= H4*((((1+H7)^H8-1)/H7)*(1+H7))
JavaScript
var contribute = 1000;
var cum_rate = 0.001666667;
var num_periods = 480;
var fvc = contribute * ((((1 + cum_rate) ^ num_periods - 1) / cum_rate) * (1 + cum_rate));
console.log(fvc);
Result of this calculations should be 735659.68 but here I am getting wrong result, can you guys help me out what I am doing wrong here?
The karat doesn't mean exponent. You need to use Math.pow(base, exp) to evaluate the expression correctly:
var fvc = contribute*( ((Math.pow( (1+cum_rate), num_periods ) - 1)/cum_rate)*(1+cum_rate) )

Extra numbers being added when decrementing value from number with JavaScript [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 5 years ago.
I'm trying to decrement "0.01" from a number, it works fine the first time, but when I try to decrement one more time it adds some extra numbers.
Here is my JavaScript code:
function decrement() {
var credits_sidebar = $('#credits_sidebar').html();
var credits_update = credits_sidebar - 0.01;
$("#credits_sidebar").fadeOut('');
$("#credits_sidebar").html(credits_update);
$("#credits_sidebar").fadeIn('');
}
If you click once on the decrement button it works, but if you click another time, the number will be "95.97999999999999" it should be 95.98 instead.
Here's an example JsFiddle:
https://jsfiddle.net/rozvnay1/
var credits_update = (credits_sidebar - 0.01).toFixed(2)
JSFiddle demo: https://jsfiddle.net/8eakhn4L/1/
This is a problem with floating point value representation.
You should consider using
Math.round((credits_sidebar - 0.01)* 100)) / 100
This behavior is normal and expected because of the way floating point math is done in JavaScript.
However what you simply can do is multiply your number by a factor of 100 to make it a whole number that needs to be decremented then you can do the decrement and divide the result by 100 to get the correct decimal answer.
You need to update this for working of code:
var credits_update = (credits_sidebar - 0.01).toFixed(2);

Convert equation string to equation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running an equation with Javascript from a text field
How can I convert the following:
var n = "2x^3+3x+6";
To
var x = a number
var n = 2x^3+3x+6;
In JavaScript?
Quite hard to guess what the exact requirements and the context are, but if you want to roughly stick to the grammar demonstrated by your variable I'd suggest using a math expression parser.
Using js-Expression-eval, it could look like this:
var formula = "2*x^3+3*x+6";
var expression = Parser.parse(formula);
var result = expression.evaluate({ x: 3 });
Run the Fiddle
Should you want to have your own grammar - to leave out the * symbols for multiplication with variables, for example - you'll have to roll your own parser, for example using something like jison.
var x = a number;
var n = eval("2*Math.pow(x,3)+3*x+6")

Categories