2008-10-18

Modulus accepts floating type ?

Honda asks me a statement about C++ as like below:

float a, b;
a = 10.5;
b = a % 2;


First ball, there is a statement error, "2" is a integer data-type, and b is a lvalue expression as a floating type, so that they doesn't match in type manner. The line sould be modify to this:

b = a % 2.0f;


Second, modulus (%) only accept the integral computing, refer from the C++ Standard draft, Sec. 5.6, p. 124.

The conclusion is, modulus doesn't accept the floating data.

The next question is, how to get the remainder as like the modulus does?