Results 1 to 10 of 10
  1. #1
    BlueJ Coder is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default Performing Exponentiation in Java without the Math Class

    How does one carry out exponentiation in java without using the math class. If you can help, please provide an example to help pick up the concept. Thanks for all your input.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  4. #4
    Jnoobs is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    in my college class, if u had 2^3 u would write

    Java Code:
    total = 2 * 2 * 2;

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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^n-1) = x(x(x^n-2)) etc. I believe you can approximate this using a limit. lim i -> n of x^i(x^n-i) 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.

  6. #6
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    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.

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Pardon my ignorance, but what is a "broken" exponent? Thanks in advance!

  9. #9
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by Fubarable View Post
    Pardon my ignorance, but what is a "broken" exponent? Thanks in advance!
    Something like this: 2^2.3
    Yoe can't just use a loop or a recursive method for that.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Math Class
    By ritwik07 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 04:06 PM
  2. Math class question
    By shendel in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 12:06 PM
  3. Java Math
    By levent in forum Java Tutorial
    Replies: 1
    Last Post: 05-12-2008, 09:03 AM
  4. Math Class (ceil, floor, abs)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-13-2007, 09:21 AM
  5. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •