mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
replace "modulo arithmetic" with "modular arithmetic" (#2088)
This commit is contained in:
@@ -13430,8 +13430,8 @@ Unsigned types support bit manipulation without surprises from sign bits.
|
|||||||
|
|
||||||
##### Note
|
##### Note
|
||||||
|
|
||||||
Unsigned types can also be useful for modulo arithmetic.
|
Unsigned types can also be useful for modular arithmetic.
|
||||||
However, if you want modulo arithmetic add
|
However, if you want modular arithmetic add
|
||||||
comments as necessary noting the reliance on wraparound behavior, as such code
|
comments as necessary noting the reliance on wraparound behavior, as such code
|
||||||
can be surprising for many programmers.
|
can be surprising for many programmers.
|
||||||
|
|
||||||
@@ -13445,7 +13445,7 @@ can be surprising for many programmers.
|
|||||||
##### Reason
|
##### Reason
|
||||||
|
|
||||||
Because most arithmetic is assumed to be signed;
|
Because most arithmetic is assumed to be signed;
|
||||||
`x - y` yields a negative number when `y > x` except in the rare cases where you really want modulo arithmetic.
|
`x - y` yields a negative number when `y > x` except in the rare cases where you really want modular arithmetic.
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
@@ -13475,7 +13475,7 @@ but if you had seen `us - (s + 2)` or `s += 2; ...; us - s`, would you reliably
|
|||||||
|
|
||||||
##### Exception
|
##### Exception
|
||||||
|
|
||||||
Use unsigned types if you really want modulo arithmetic - add
|
Use unsigned types if you really want modular arithmetic - add
|
||||||
comments as necessary noting the reliance on overflow behavior, as such code
|
comments as necessary noting the reliance on overflow behavior, as such code
|
||||||
is going to be surprising for many programmers.
|
is going to be surprising for many programmers.
|
||||||
|
|
||||||
@@ -13534,7 +13534,7 @@ Incrementing a value beyond a maximum value can lead to memory corruption and un
|
|||||||
|
|
||||||
##### Exception
|
##### Exception
|
||||||
|
|
||||||
Use unsigned types if you really want modulo arithmetic.
|
Use unsigned types if you really want modular arithmetic.
|
||||||
|
|
||||||
**Alternative**: For critical applications that can afford some overhead, use a range-checked integer and/or floating-point type.
|
**Alternative**: For critical applications that can afford some overhead, use a range-checked integer and/or floating-point type.
|
||||||
|
|
||||||
@@ -13559,7 +13559,7 @@ Decrementing a value beyond a minimum value can lead to memory corruption and un
|
|||||||
|
|
||||||
##### Exception
|
##### Exception
|
||||||
|
|
||||||
Use unsigned types if you really want modulo arithmetic.
|
Use unsigned types if you really want modular arithmetic.
|
||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
@@ -13609,7 +13609,7 @@ This also applies to `%`.
|
|||||||
|
|
||||||
##### Reason
|
##### Reason
|
||||||
|
|
||||||
Choosing `unsigned` implies many changes to the usual behavior of integers, including modulo arithmetic,
|
Choosing `unsigned` implies many changes to the usual behavior of integers, including modular arithmetic,
|
||||||
can suppress warnings related to overflow,
|
can suppress warnings related to overflow,
|
||||||
and opens the door for errors related to signed/unsigned mixes.
|
and opens the door for errors related to signed/unsigned mixes.
|
||||||
Using `unsigned` doesn't actually eliminate the possibility of negative values.
|
Using `unsigned` doesn't actually eliminate the possibility of negative values.
|
||||||
@@ -13631,7 +13631,7 @@ Consider:
|
|||||||
auto a = area(height, 2); // if the input is -2 a becomes 4294967292
|
auto a = area(height, 2); // if the input is -2 a becomes 4294967292
|
||||||
|
|
||||||
Remember that `-1` when assigned to an `unsigned int` becomes the largest `unsigned int`.
|
Remember that `-1` when assigned to an `unsigned int` becomes the largest `unsigned int`.
|
||||||
Also, since unsigned arithmetic is modulo arithmetic the multiplication didn't overflow, it wrapped around.
|
Also, since unsigned arithmetic is modular arithmetic the multiplication didn't overflow, it wrapped around.
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user