C/C++: Force floating point math

In C/C++ if you want to get a floating point result from integer division you need to cast at least one of the values as a float to force floating point division (not integer):

int a = 1;
int b = 7;

float c = (float)a / b;

If you don't cast one of the variables as a float you will get an integer result. This is true even if your resulting variable is cast as a float.

Leave A Reply
All content licensed under the Creative Commons License