Unable to generate proper float value in Processing

I am converting p5js to Processing. I’ve actually used Processing for years but I specialize in Javascript. I am surprised at this issue because I’ve never noticed before. Not sure how I overlooked it.

When calculating a float value in P5js I get this expected result:

let k = kMax * sqrt(i/n);

0.653496871297773

When calculating a float value in Processing I get this unexpected result:
float k = kMax * sqrt(i/n);

0.0

What do I need to do in order to generate a proper float value in Processing?

At least 1 of the variables i or n needs to be of datatype float for the operator / to evaluate its operands as a float value. :face_with_monocle:

Alternatively you can temporarily upcast 1 of the variables to float:
float k = kMax * sqrt( (float) i / n );

5 Likes

That was it. I overlooked the fact that one of my variables didn’t have a proper datatype. Glad the solution makes sense. Wasn’t sure if I was misunderstanding something. Thanks.

1 Like