Search code examples
javapythonnancurvealgebra

The equation -e**-((-log(7)/100.0)*(100-x))+7 returns NaN


I'm trying to implement this curve as part of the leveling system of a small game I'm currently working on. The equation is as follows

f(x) = -e^-((-log(7)/100)*(100-x))+7

Which in python can be defined as

f=lambda x:-e**-((-log(7)/100.0)*(100-x))+7

Running this function in the Python console returns the values expected. I ported it over to Java, where it takes the form of:

public static double f(float x) {
    return (Math.pow(-Math.E, -((-Math.log(7)/100)*(100-x)))+7);
}

However, mysteriously, this always returns NaN. I've never encountered this problem before, what is going on?


Solution

  • (Putting my comment as the answer)

    The expressions are not the same. In python it looks like -(e**someNum). In java it looks like (-e)**someNum.

    Think about it (in a pretty raw fashion), what would you get when you mutliply a negative number some irrational number of times. Thats why you get a NaN.


    What you want in Java is this:

    public static double f(float x) {
        return 7 - Math.pow(Math.E, -((-Math.log(7)/100)*(100-x)));
    }