Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 02-19-07   #1 (permalink)
Intel Overclocker
 
SgtSpike's Avatar
 
intel nvidia

Join Date: Jan 2007
Location: Springfield, Oregon
Posts: 2,778

Rep: 229 SgtSpike is acknowledged by manySgtSpike is acknowledged by manySgtSpike is acknowledged by many
Unique Rep: 184
Trader Rating: 4
Default The opposite of Exponent in Javascript?

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);
}
So if I put in exp(2, 3) (which would be 2^3), I will get 8 as the returned value. Then, in a new function I need to do the opposite. Given 8 and 2, I need to figure out what y would be (in this case 3). How can I do this?

System: Must...Go...Faster...
CPU
E6300 @ 3.74ghz - 1.44v
Motherboard
Gigabyte P35-DS3L @ 535FSB, +.3v MCH/FSB
Memory
2x1GB G'Skill 800mhz @ 1070mhz - 2.2v
Graphics Card
EVGA 8800GT 512MB
Hard Drive
2x 250GB WDC 7200rpm, OC'd to 8100rpm
Sound Card
X-Fi Xtreme Gamer (2.1 CA with 12" 500w sub)
Power Supply
500w XClio Goodpower
Case
Custom
CPU cooling
Sycthe Mini-Ninja
GPU cooling
VF-900
OS
Windows Vista Home Premium
Monitor
19" Widescreen HannsG

Last edited by SgtSpike : 02-19-07 at 03:02 PM.
SgtSpike is offline   Reply With Quote
Old 02-19-07   #2 (permalink)
I AM NEHALEM
 
HrnyGoat's Avatar
 
intel ati

Join Date: Feb 2005
Location: Colorado Springs, CO
Posts: 5,893

Rep: 337 HrnyGoat is a proven memberHrnyGoat is a proven memberHrnyGoat is a proven memberHrnyGoat is a proven member
Unique Rep: 232
FAQs Submitted: 4
Hardware Reviews: 11
Trader Rating: 4
Default

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.

System: Desktop
CPU
Core 2 E2140 @2.67GHz
Motherboard
Abit IP35 Pro
Memory
2x1GB GSkill HZ
Graphics Card
Radeon X1900XT 512MB
Hard Drive
2x500GB Samsung HD501LJ + 2x80GB (1.16TB total)
Sound Card
X-Fi Xtreme Music
Power Supply
PC P&C Silencer 610W
Case
AeroCool AeroEngine II w/ 2x140mm intake fans
CPU cooling
AeroCool Dominator 140mm
GPU cooling
Stock
OS
Windows XP Home
Monitor
24" Acer LCD (AL2324W)
HrnyGoat is offline Overclocked Account HrnyGoat's Gallery   Reply With Quote
Old 02-19-07   #3 (permalink)
Intel Overclocker
 
SgtSpike's Avatar
 
intel nvidia

Join Date: Jan 2007
Location: Springfield, Oregon
Posts: 2,778

Rep: 229 SgtSpike is acknowledged by manySgtSpike is acknowledged by manySgtSpike is acknowledged by many
Unique Rep: 184
Trader Rating: 4
Default

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.

System: Must...Go...Faster...
CPU
E6300 @ 3.74ghz - 1.44v
Motherboard
Gigabyte P35-DS3L @ 535FSB, +.3v MCH/FSB
Memory
2x1GB G'Skill 800mhz @ 1070mhz - 2.2v
Graphics Card
EVGA 8800GT 512MB
Hard Drive
2x 250GB WDC 7200rpm, OC'd to 8100rpm
Sound Card
X-Fi Xtreme Gamer (2.1 CA with 12" 500w sub)
Power Supply
500w XClio Goodpower
Case
Custom
CPU cooling
Sycthe Mini-Ninja
GPU cooling
VF-900
OS
Windows Vista Home Premium
Monitor
19" Widescreen HannsG
SgtSpike is offline   Reply With Quote
Old 02-19-07   #4 (permalink)
WaterCooler
 
amd nvidia

Join Date: Feb 2007
Posts: 7

Rep: 1 Janus67 Unknown
Unique Rep: 1
Trader Rating: 0
Default

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>
}
__________________
System: Watercooled
CPU
3700+ San Diego @ 2.7ghz
Motherboard
DFI Ultra-D
Memory
2x1GB G.Skill HZ's PC4000 @250mhz
Graphics Card
eVGA 7800gtx
Hard Drive
3 for 720gb of space
Sound Card
Onboard :-(
Power Supply
Seasonic 600W
Case
Modded Antec P180
CPU cooling
Swiftech Storm
GPU cooling
DangerDen Maze4
OS
XP Corporate
Monitor
Dell 2407

Last edited by Janus67 : 02-19-07 at 03:41 PM.
Janus67 is offline   Reply With Quote
Old 02-20-07   #5 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

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;
}
What this does is figure out the value exponent such that

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.

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N

Last edited by C-bro : 02-20-07 at 04:10 PM.
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 02-20-07   #6 (permalink)
Intel Overclocker
 
SgtSpike's Avatar
 
intel nvidia

Join Date: Jan 2007
Location: Springfield, Oregon
Posts: 2,778

Rep: 229 SgtSpike is acknowledged by manySgtSpike is acknowledged by manySgtSpike is acknowledged by many
Unique Rep: 184
Trader Rating: 4
Default

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?

System: Must...Go...Faster...
CPU
E6300 @ 3.74ghz - 1.44v
Motherboard
Gigabyte P35-DS3L @ 535FSB, +.3v MCH/FSB
Memory
2x1GB G'Skill 800mhz @ 1070mhz - 2.2v
Graphics Card
EVGA 8800GT 512MB
Hard Drive
2x 250GB WDC 7200rpm, OC'd to 8100rpm
Sound Card
X-Fi Xtreme Gamer (2.1 CA with 12" 500w sub)
Power Supply
500w XClio Goodpower
Case
Custom
CPU cooling
Sycthe Mini-Ninja
GPU cooling
VF-900
OS
Windows Vista Home Premium
Monitor
19" Widescreen HannsG
SgtSpike is offline   Reply With Quote
Old 03-01-07   #7 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Thumbs up

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;
}
You may want to test it for bugs but that should work
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 03-07-07   #8 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

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;

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 03-08-07   #9 (permalink)
Intel Overclocker
 
SgtSpike's Avatar
 
intel nvidia

Join Date: Jan 2007
Location: Springfield, Oregon
Posts: 2,778

Rep: 229 SgtSpike is acknowledged by manySgtSpike is acknowledged by manySgtSpike is acknowledged by many
Unique Rep: 184
Trader Rating: 4
Default

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+!

System: Must...Go...Faster...
CPU
E6300 @ 3.74ghz - 1.44v
Motherboard
Gigabyte P35-DS3L @ 535FSB, +.3v MCH/FSB
Memory
2x1GB G'Skill 800mhz @ 1070mhz - 2.2v
Graphics Card
EVGA 8800GT 512MB
Hard Drive
2x 250GB WDC 7200rpm, OC'd to 8100rpm
Sound Card
X-Fi Xtreme Gamer (2.1 CA with 12" 500w sub)
Power Supply
500w XClio Goodpower
Case
Custom
CPU cooling
Sycthe Mini-Ninja
GPU cooling
VF-900
OS
Windows Vista Home Premium
Monitor
19" Widescreen HannsG
SgtSpike is offline   Reply With Quote
Old 03-08-07   #10 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

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

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 01:30 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.32830 seconds with 8 queries