|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
The opposite of Exponent in Javascript?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Intel Overclocker
Join Date: Jan 2007
Location: Springfield, Oregon
Posts: 2,778
Rep: 229
![]() ![]() ![]() Unique Rep: 184
Trader Rating: 4
|
So I'm working on a project in javascript, and I wrote a short function to compute x^y. Now I need to do the opposite, and I'm not sure how. Here's the original function.
Code:
function exp(a,b)
{
t = 1;
for(x = 1; x <= b; x++)
{
t = t * a;
}
EXP = t;
return(EXP);
}
__________________
Hotel Da Volta - The Enigma: Rising Tide Add-On The Gigabyte GA-P35-DS3L Thread 101% OC on E6300!
Last edited by SgtSpike : 02-19-07 at 03:02 PM. |
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
I AM NEHALEM
|
So you would be looking for the root? You could just put in a fraction as the exponent value, like x^1/2 to get the square root.
__________________
FAQ: What are FB-DIMMs? FAQ: How does ECC (Error Correcting Code) work? The definitive 360 vs PS3 guide Have an X1900/1950, X1800 or X1600 series card? Fold with it!
|
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
Intel Overclocker
Join Date: Jan 2007
Location: Springfield, Oregon
Posts: 2,778
Rep: 229
![]() ![]() ![]() Unique Rep: 184
Trader Rating: 4
|
Not exactly. I would need to find the opposite of what you are talking about. Instead of finding x, I already know it. I would need to find the 1/2.
__________________
Hotel Da Volta - The Enigma: Rising Tide Add-On The Gigabyte GA-P35-DS3L Thread 101% OC on E6300!
|
|||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
WaterCooler
|
I believe you are looking to do a logarithm. you may need to find a .jar file to help you do that.
__________________edit: I haven't coded in java for a year so it may be off by a little bit, but I think this is what you were looking for. that or upon thinking about it, just take the value as it is. y=2 z=8 for(x=1; x<<put in a number here that you would want it to stop incase it isn't exact>;x++) { if(y==z) break; else <call function for your exponential function and put in x,y and return that value> }
Last edited by Janus67 : 02-19-07 at 03:41 PM. |
|||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||
|
110100001101001111000
|
Well the actually Java function uses log(x) which is the natural logarithm (normally denoted ln(x)) it can be found in the java.lang.Math class.
For your project, the inverse of multiplication is division, so why not do: Code:
public static int log(double a, int b){ // computes log of a, base b
int exponent=0;
double remainder=a;
do{
remainder /= b;
exponent++;
}while(remainder > 1);
return exponent;
}
a = b^exponent Note: My method will only work when a is a perfect power of b. In other words, exponent must be an integer. You're going to have to do some working if you want to integrate decimal exponents. In other words log(8,2) = 3 log(16,2) = 4 But for a number where the exponent is not an integer, the value gets rounded up. log(9,2) = 4 rather than 3.1699 You must define your accuracy and consider if this method will suffice for your program constraints.
__________________
Last edited by C-bro : 02-20-07 at 04:10 PM. |
||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Intel Overclocker
Join Date: Jan 2007
Location: Springfield, Oregon
Posts: 2,778
Rep: 229
![]() ![]() ![]() Unique Rep: 184
Trader Rating: 4
|
Hmmm... Great starts Janus and C-Bro.
C-Bro - I like your method, it's exactly what I need except I do need decimal-level precision to 8 or 9 places... Any other thoughts on how to do that? Could I have a +1 loop to find the closest integer digit, and then a +0.1 loop to find the closest first decimal digit, and then a +0.01 loop to find the next decimal digit, etc. Would that work? What would it look like?
__________________
Hotel Da Volta - The Enigma: Rising Tide Add-On The Gigabyte GA-P35-DS3L Thread 101% OC on E6300!
|
|||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
Dunno if you still need this - but if so
Code:
public static double log(double a, int b, int places){ // computes log of a, base b to <places> decimal places
double exponent=0;
double increment = 1.;
double remainder=a/b;
for(int i = 0; i < places; i++)
{
while(remainder>1)
{
exponent += increment;
remainder /= b;
}
increment /= 10;
}
return exponent;
}
|
|||||||||||||
|
|
|
|
#8 (permalink) | ||||||||||||
|
110100001101001111000
|
I'm almost sure you won't need this now, but I used this method on test today for implementing the ln() function. It was said that we had an exp function available to us.
The basis behind the code is the Newton-Raphson method of approximation. You're basically finding the zeros for the inverse function to solve for the answer. Say we want to find ln a. x = ln a exp(x) = a exp(x) - a = 0 Hopefully none of that is foreign so far. You can then take the NR method which is: Xi+1 = Xi - f(Xi)/f'(Xi) I used a fixed guess of X0 = 1, and it produces accurate results to the maximum I can display in 8 iterations. This finds ln a. I tried it for around 10 or 15 different values and they were correct to 14 decimal places (the most I could view). Also note this was done in base e, but can be modified to work for any base. Code:
int guess = 1;
for (int i=0;i<7;i++)
guess=guess - (exp(guess)-a)/exp(guess);
answer = guess;
__________________
|
||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
Intel Overclocker
Join Date: Jan 2007
Location: Springfield, Oregon
Posts: 2,778
Rep: 229
![]() ![]() ![]() Unique Rep: 184
Trader Rating: 4
|
Actually, thanks for the great replies! I can still use this definitely. I had just put the project on hold, but I'll probably resume it here shortly. I'll implement those algorithms and see what I come up with. Thanks and Rep+!
__________________
Hotel Da Volta - The Enigma: Rising Tide Add-On The Gigabyte GA-P35-DS3L Thread 101% OC on E6300!
|
|||||||||||||
|
|
|
|
|
#10 (permalink) | ||||||||||||
|
110100001101001111000
|
I have found a flaw in my method. It works perfectly for the natural logarithm like I posted there, but it falls apart when working with other bases. Why is that?
For a base b, the method would go as follows: logb a = x b^x = a b^x - a = 0 However, when you try and differentiate the b^x term you get stuck with d/dx(b^x) = b^x * 1 * ln b The reason it worked so well with base e is because the derivative of e^x = e^x. If you want to use the Newton-Raphson approximation, you'll have to include both a ln(a) and log(a,b) function. I've been working with these methods and it really doesn't seem to be a great algorithm for log with any base. In fact when the numbers get to high, it downright crashes. log(2048,2) produces NaN when it should be 11. For large difference between base and product, a huge number of iterations are also needed. I'd say abandon this approach. It's smart on paper but doesn't seem practical. Here's what I finished with when I gave up. Every thing "works" but only up to a limit. Code:
public static double log(double a, double b){
double guess = 1;
for (int i=0;i<1000;i++)
guess=guess - (pow(b,guess)-a)/(ln(b)*pow(b,guess));
return guess;
}
public static double ln(double a){
double guess = 1;
for (int i=0;i<7;i++)
guess=guess - (java.lang.Math.exp(guess)-a)/java.lang.Math.exp(guess);
return guess;
}
__________________
|
||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|