Problem with simple float arithmetic in APDE

edited July 2016 in Questions about Code

Hi all,

Well this has left me feeling like a noob! I'm having weird behaviour with what should be very simple arithmetic in APDE. No matter what I do this calculation is coming out with 0.0. Yet the example code on the Processing float wiki page runs ok, and I can directly assign a floating point number without a problem.

Screenshot_2016-07-12-07-10-02

float h;
//h=0.2361111111;
h=255 / width;
println("width="+width) ;
println(h) ;

Any ideas, before I start pulling my hair out?? I haven't got much left!

Tagged:

Answers

  • edited July 2016 Answer ✓

    Dividing an int by an int gives you another int - I think if you change to h=255.0 / width you get a float & it should work.

    edit - sorry, float at start already does this for you. It does give a result - trying it in openprocessing gives h = 2.55

  • Thank you!! I'm pretty sure other languages I've used would automatically convert to a float so I clearly assumed the same with Processing. Thanks for saving my hair!

  • Processing isn't a language but a framework library mainly for Java. L-)

Sign In or Register to comment.