Float from int divided by int

I’m trying to dynamically trying to generate a spacing value which depends on the window size and the number of elements in a vector.

This is what I’m doing:

spacing = ofGetWidth()/drawBuffer.size();

I have spacing declared as a float in my header file, but the results I keep getting is a int - how do I get the value of spacing as a float while dividing an int by an int?

Seems to work properly if I do:

spacing = float(ofGetWindowWidth())/float(drawBuffer.size());

Seems a little stupid if this is how I’m supposed to make it work. Is it?

that’s how division works in c/c++ divinding an int by an int will give an int which kind of makes sense the problem is that when assigning to a float you don’t get any warning or similar. the way you’ve done it is the proper solution, you can also only cast the divisor or assign it to a float variable first

1 Like

Thanks! I’m still quite new to programming so I run into these issues that are rather trivial, I hope you guys on the forum don’t mind these basic questions! :sweat_smile:

no worries, i just had this same issue a couple of days ago it’s a really annoying thing in c++