Results 1 to 10 of 10
 10132009, 07:21 PM #1Member
 Join Date
 Oct 2009
 Posts
 1
 Rep Power
 0
 10132009, 07:51 PM #2Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 8
Forget about doing it in Java for a moment.
How would you do it on paper? Only move to the compiler if you get your algorithm right.
 10132009, 11:06 PM #3
you can use a loop but that's horribly inefficient. I can post you the algorithm once I find my notes from college on exponents. I think it's called like the medieval peasant addition formula or something if you want to look around.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
 10132009, 11:17 PM #4Member
 Join Date
 Oct 2009
 Posts
 13
 Rep Power
 0
in my college class, if u had 2^3 u would write
Java Code:total = 2 * 2 * 2;
 10142009, 04:22 PM #5
Break it down into smaller chunks and use a recursive formula to calculate it. take for example 2^24 which is (2(2^23) which is (2(2(2^22) which is (2(2(2(2^21) etc.
You end up with x^n = x(x^n1) = x(x(x^n2)) etc. I believe you can approximate this using a limit. lim i > n of x^i(x^ni) would be your general equation. from there you recursively calculate your answer. (x is your base number, n is your max exponent, i is an iterator)
After actually writing all this out I'm not sure how much faster it's going to be since you're still doing an exponential. Someone want to improve what I wrote? Or just say if I went the completely wrong direction and tell the OP to ignore everything I've just typed.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
 10142009, 05:03 PM #6Member
 Join Date
 Oct 2009
 Location
 Rotterdam
 Posts
 52
 Rep Power
 0
I've checked out the source code of the Math class recently and I found out that it delegates all these operations to another class called the StrictMath class.
When I checked that out it appeared that all these methods are 'natives', which probably means that it is done in the processor or something.
So I still don't know what kind of algorithm to use for broken exponents. It's one of the greatests mysteries in math to me.
 10142009, 05:10 PM #7
I know that multiplication by 2's can be done using simple hardware switches. Say for calculating 2^32 the compiler wouldn't actually calculate it the same way it would say 5^13. It would drop down and do byte level arithmetic and just shift the bits over(I think that's how you multiply by 2 with bits)
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.

Pardon my ignorance, but what is a "broken" exponent? Thanks in advance!
 10142009, 07:13 PM #9Member
 Join Date
 Oct 2009
 Location
 Rotterdam
 Posts
 52
 Rep Power
 0

Ah, gotcha. This would require logarithms or something similar to solve, I'd guess.
Similar Threads

Math Class
By ritwik07 in forum New To JavaReplies: 2Last Post: 09142009, 04:06 PM 
Math class question
By shendel in forum New To JavaReplies: 1Last Post: 03052009, 01:06 PM 
Java Math
By levent in forum Java TutorialReplies: 1Last Post: 05122008, 09:03 AM 
Math Class (ceil, floor, abs)
By Java Tip in forum Java TipReplies: 0Last Post: 12132007, 10:21 AM 
Help with math in java
By fernando in forum New To JavaReplies: 1Last Post: 08062007, 06:05 AM
Bookmarks