Dividing a round number doesn't work. Dividing as a variable does?

Hi, I am having a question why something doesn’t work one way, but it does the other way.

I was sketching something out in processing with this variable:

float k = 5 / 8;

It didn’t want to work. But when I did it like this:

float d = 8;
float n = 5;
float k = n / d;

Somehow it did work.

What is the difference?

1 Like

need to force a floating point division.

float k = 5/8;
float l = 5/8.0;
float m = 5f/8;

Because 5 and 8 are integers so 5 / 8 is zero remainder five hence the answer is 0 it is called integer division. On the other hand n / d is dividing one float by another so you get 0.625


I find this annoying too but it’s JAVA thing.

I produces errors that are hard to find.

This doesn’t work:

float k = 5 / 8;

But this does work (giving 0.625) :

float k = 5 / 8.0;

Because one number is float (8.0).

Crazy but true.



Applies to all statically typed languages, not just Java


It’s like a implicit / tacit conversion from a float result to integer and copying this result into a float variable. It’s confusing. :wink:

@quark and @Chrisir
Thanks a lot for the explanations. In one of Daniel Shiffman’s videos I noticed that he also used the .0 trick to make it work but I didn’t know why he did that.

The above line was something I started doing in P5 which worked fine, but in Processing it didn’t and I couldn’t find out why!