Solved Math issues

Discussion in 'Spigot Plugin Development' started by MrDienns, Apr 16, 2017.

  1. I have a starting value of 1000

    This value must be multiplied by 1.5 (or 50%) for an x given amount of times.

    So for example:

    I want to multiply 1000 by 50% for 34 times.

    Example: 1000 > 1500 > 2250 > ....

    Please note that I'm using BigIntegers for this instead of normal Integers.

    I'm unable to figger this out in Java code. Any help would be great.
  2. 1000*1.5^n where N is the amount of times
  3. Photon


    1000 * Math.pow(1.5, x), given x as your variable. Use a long for this.
  4. Just make a for loop and do this calculation in there. Something like this:
    Code (Text):

    double base = 1000;
    for(int i = 0; i < x; i++) base *= 1.5;
    If this is wrong tell me, it's a quick draft.
    • Like Like x 1
  5. You're not wrong but see the previous replies to see why you're not right either. :p
  6. This isn't the way you would go about it. What @DizMizzer & @Photon suggests would be the appropriate way.
  7. I would assume so. And easier and more convenient, but I'm checking it now
  8. I'm quite sure that their way under the hood also uses a for loop. I could be wrong though.
    In my little time as a developer one of the few things I understood is that performance is everything! :)
    • Like Like x 1
  9. Our way is faster because we aren't storing the variable over and over. This is also basic math not really programming
  10. Could be, the thing is this is a more direct approach with indeed more code (which is a bad thing).
  11. Test finished. Actually not as expected.
    Code (Text):
    1 - Result: 1.9175105923288408E8. Operation took 768 nano seconds.
    2 - Result: 1.9175105923288408E8. Operation took 13056 nano seconds.
    1 = Your method @xTimPugz.
    2 = @Photon 's method

    Code if you wanna try it yourself.

    EDIT: So basically, double as fast. Considerably faster for a computer, but not noticeable to a human eye.

    EDIT 2: @xTimPugz take a look at the edit. That's pretty odd. Your method proved to be faster. I just confused the results with my ignorance.

    EDIT 3: @DizMizzer , @xTimPugz 's method is actually faster :p
    #12 ExpDev, Apr 16, 2017
    Last edited: Apr 16, 2017
    • Informative Informative x 1
  12. Thanks for all the help! I got it working.

    I'll have to think of a better formula to generate a required exp amount. When a player reaches level 90, it needs 9223372036854775807 (actual number) exp points to become level 91...
  13. Well yeah I guess that's because under the hood 5 to the power of 5 is just the same as 5 * 5 * 5 *5 *5.
    • Like Like x 2
  14. Haha, glad you proved them wrong :p ;) (me, as well)
  15. I don't really care about that haha :p I just want to enjoy my time on the forums and help around where I can hehe. Just thought it'd be helpful.
    • Like Like x 1