Mathematicians required!

Discussion in 'Plugin Development' started by Jnorr44, Sep 17, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Jnorr44

    Hey guys, I did some curve fitting for something I am doing, and I need to get this working, although I am not familiar enough with the "Math" class, because plugins don't normally require a lot of math, lol. So I was wondering if anyone could tell me how to get the exponent working, and also if I am doing anything wrong for curve fitting...

    Code:
        protected double amountVariable() {
            double x = game.getPlayers().size() * 1.5;
            double a = 5.150202692369555E-4;
            double b = 0.009694737048487135;
            double c = 2.0740001246685478;
            double d = 1.5771190606123884;
            return d + (c * x) + (b * x)^2 - (a * x)^3;
        }
     
  2. Offline

    Courier

    Math.pow(double base, double exponent)

    EDIT: Although it might be faster to just use *, at least for ^2. Like this:
    Code:java
    1. double bx = b * x;
    2. double ax = a * x;
    3. return d + c * x + bx * bx - ax * ax * ax;
     
  3. Offline

    Jnorr44

    ahh okay, thanks. That makes complete sense!
     
  4. Offline

    gofish8195

    Yeeah, the ^ sign in Java is a bitwise XOR operator and nothing to do with exponents, I've made the same mistake before
     
  5. Offline

    slam5000

    Surprisingly, it's faster to do exponents on a bitwise level than multiplications, so
    in this case, Java's Math.pow would be the faster method. But unless you care about a one
    nanosecond difference... or are slightly insane, you should just go with the one that is the
    most easy for you to understand. (I would learn the Math class's primary utility methods though
    as they are extremely necessary for doing... well... like... everything)
     
  6. Offline

    md_5

    All java math ops are pretty cheap except for the trig ones (which can be done better with a lookup table) and the horrendous square root.
     
  7. Offline

    slam5000

    They don't use a lookup table :O oh dear... why java why! :/

    Also, never looked at their Sqrt implementation, what's so terrible about it?
     
  8. Offline

    Courier

    This is their implementation of sin. It is in C.
    This is their implementation of pow. It might call this. It is also in C.

    The documentation doesn't explicitly say what their implementation of sqrt is. It probably depends on the JVM. It might be this, if it is software.
    There is this comment: "Note that hardware sqrt instructions frequently can be directly used by JITs and should be much faster than doing Math.sqrt in software."
     
Thread Status:
Not open for further replies.

Share This Page