Using pow() for non-integers

I am trying to raise X ^ 0.026 using

float X = 2.0;
float Y = pow(X,0.026);

This returns 0.0 when it should return 1.018.

Does pow() require an integer? Is there a way around this? I have also tried

float Y = exp(0.026*log(X));

and I still return 0.0.

float X = 2.0;
float Y = pow(X,0.026);
println( Y ); // Prints 1.0181853

I’m not having a problem with it. How are you seeing the value of Y?

1 Like

I have no idea why it works now. In my code I had

float X = 2;

and I changed it to

float X = 2.0;

and now it works…sigh.

I know it works now, but can I ask if this is Processing.js or regular Java Processing?

Processing is open-source, so you can view its source here. The pow() function is defined on line 4469.

static public final float pow(float n, float e) {
    return (float)Math.pow(n, e);

This defers to the Math.pow() function in Java, which you can read about here. The Math.pow() function takes double values as parameters, so no, pow() does not require integers.

This also works okay for me:

float x = 2;
float y = pow(x, 0.026);

My best guess is you maybe had something like this:

float x = 1/2;
float y = pow(x, 0.026);

With this code, the 1/2 calculation is done first, and since they’re both int primitives, the result is 0.

Or maybe something like this:

float x = 2;
float y = pow(X, 0.026);

Note the upper-case X in the second line of code. This X is a predefined value used internally by Processing, and its value is 0.


Up in the top corner it just says Java

This sounds like it may be your culprit.