Integer separation in C ++ 11

I noticed some changes in the edition of section 5.6 for C ++ 11. (I look at the draft C ++ N3242 standard from 2011-02-28.) The new (draft) standard includes a sentence:

"For integral operands, the operator / gives an algebraic relation with any fractional part dropped:"

This statement is not specified in 5.6 of standard 03 (ISO-IEC-14882-2003), but I do not think this is a change, right? This is how C and C ++ have worked for years, if I haven’t lost my mind (which could happen anyway).

+6
source share
2 answers

You will not lose your mind.

The footnote to 5.6/4 says:

[C++03 footnote 74]: In accordance with the work of revising ISO C, the preferred algorithm for integer division follows the rules defined in the ISO Fortran standard, ISO / IEC 1539: 1991, in which the factor is always rounded to zero.

In C ++ 11, this behavior is explicitly required, and not "preferred"; the change is indicated in the compatibility section:

[C++11: C.2.2]:
Edit: Specify rounding for integer / and % results
Rationale: Increase portability, compatibility with C99.
Impact on the original function: Valid C ++ 2003 code that uses integer division rounds the result to 0 or to negative infinity, whereas this International Standard always rounds the result to 0.

+9
source

Nearly. In C ++ 03, the remainder sign for % (in which both conditions are indicated) was unspecified, since such rounding could go from scratch and in certain situations. Compare with C ++ 03 Note:

In accordance with the work being done regarding the revision of ISO C, the preferred algorithm for integer division follows the rules defined in ISO Fortran Standard, ISO / IEC 1539: 1991, in which the factor is always rounded to zero.

In practice, however, this almost never mattered.

+1
source

Source: https://habr.com/ru/post/904390/


All Articles