mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
ES.100
new example
This commit is contained in:
@@ -10779,9 +10779,14 @@ Avoid wrong results.
|
|||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
unsigned x = 100;
|
int x = -3;
|
||||||
unsigned y = 102;
|
unsigned int y = 7;
|
||||||
cout << abs(x-y) << '\n'; // wrong result
|
|
||||||
|
cout << x-y << '\n'; // unsigned result, possibly 4294967286
|
||||||
|
cout << x+y << '\n'; // unsiged result: 4
|
||||||
|
cout << x*y << '\n'; // unsigned result, possibly 4294967275
|
||||||
|
|
||||||
|
It is harder to spot the problem in more realistic examples.
|
||||||
|
|
||||||
##### Note
|
##### Note
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user