0

In Processing I have this code:

int numberOfSteps = 20;
float numberOfStepsCalculated = (1/20);
println(numberOfStepsCalculated);

But my print keeps returning 0.0

I can not figure out why!

Thanks for my dumb question

aName
  • 257
  • 2
  • 8

1 Answers1

2

Integer divison.

float numberOfStepsCalculated = (1.0f / numberOfSteps); // 20

When you divide two int(s) the result is an int (or 0). You then widen the result to a float (or 0.0). Promote one of the values in the calculation to a float and you'll get a float result.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Ah that was it! I knew it was something like that! I have upvoted you, but is there anything else I should do to this thread (i.e. mark resolved, e.t.c.) – aName Apr 07 '15 at 01:05
  • @user2122995 I suggest that you [edit your profile](/users/edit/current) and give yourself a name! Also, yes - you can [accept an answer](http://meta.stackexchange.com/q/5234/243725). – Elliott Frisch Apr 07 '15 at 01:08