Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in the standard (C++11) is it said that the remainder operator works for integral type only?

I would like a pointer in the standard (n3242/3291/3290) about where it is defined that the remainder operator doesn't apply to floating point type.

The remainder operator % is defined in 5.6.2

The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined. For integral operands the / operator yields the algebraic quotient with any fractional part discarded if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a.

I can't find where it is forbidden to use a floating point type. And the fact the standard explicitly treats integral operands as a special case could be interpreted as: the operator exists for other non-integral types.

I know that MSVC and GCC doesn't accept it, and I looked at lots of answers about this issue, but I can't get a valid pointer in the C++ 11 standard.

like image 769
fjardon Avatar asked Dec 19 '22 11:12

fjardon


1 Answers

C++11, 5.6/2:

The operands of * and / shall have arithmetic or unscoped enumeration type; the operands of % shall have integral or unscoped enumeration type. The usual arithmetic conversions are performed on the operands and determine the type of the result.

(Emphasis mine)

like image 161
Angew is no longer proud of SO Avatar answered Feb 23 '23 00:02

Angew is no longer proud of SO