5

Possible Duplicate:
Why does modulus division (%) only work with integers?

This code doesn't work in C and C++ but works in C# and Java:

float x = 3.4f % 1.1f;
double x = 3.4 % 1.1;

Also, division remainder is defined for reals in Python.

What is the reason this operation is not defined for floats and doubles in C and C++?

Community
  • 1
  • 1
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111

1 Answers1

20

The C committee explained its position of why there is no remainder operator for floating types in the Rationale document:

(6.5.5 Multiplicative operators) The C89 Committee rejected extending the % operator to work on floating types as such usage would duplicate the facility provided by fmod (see ยง7.12.10.1).

ouah
  • 142,963
  • 15
  • 272
  • 331