What's the opposite of JavaScript's Math.pow?
Asked Answered
J

2

38

I'm having a mental block here, and algebra not really being my thing, can you tell me how to re-write the JavaScript code below to derive the variable, c, in terms of a and b?:

a = Math.pow(b, c);
c = ?

Thanks!

Juieta answered 25/10, 2010 at 15:37 Comment(1)
Slightly related question: #9309584Transitory
S
81
c = Math.log(a)/Math.log(b)
Shizue answered 25/10, 2010 at 15:41 Comment(4)
Might be worth mentioning that JavaScript is horrible at Math when dealing with precise numbers. var c = 3; var b = 10; var a = Math.pow(b,c) var d = Math.log(a)/Math.log(b); // d should equal 3 // d actually equals 2.9999999999999996Shaquitashara
@Shaquitashara is it safe to assume that if I'm working with integers and use Math.round() on the result it'll be accurate? Specifically I know my value is a power of 2, so let power = Math.round( Math.log(value) / Math.log(2) ); should be accurate in my case?Incarcerate
Ahh, it appears for my specific case Math has me covered with Math.log2(num)!Incarcerate
how do you obtain b if you have a & c?Aquavit
F
9

Logarithms. You want the logarithm of a. B is the base, c is the exponent, so

logb a = c

Finnigan answered 25/10, 2010 at 15:39 Comment(1)
Seems true, but this is not JavaScript, only pure math.Hartzel

© 2022 - 2024 — McMap. All rights reserved.