| 1 |
.. title:: clang-tidy - bugprone-integer-division |
| 2 |
|
| 3 |
bugprone-integer-division |
| 4 |
========================= |
| 5 |
|
| 6 |
Finds cases where integer division in a floating point context is likely to |
| 7 |
cause unintended loss of precision. |
| 8 |
|
| 9 |
No reports are made if divisions are part of the following expressions: |
| 10 |
|
| 11 |
- operands of operators expecting integral or bool types, |
| 12 |
- call expressions of integral or bool types, and |
| 13 |
- explicit cast expressions to integral or bool types, |
| 14 |
|
| 15 |
as these are interpreted as signs of deliberateness from the programmer. |
| 16 |
|
| 17 |
Examples: |
| 18 |
|
| 19 |
.. code-block:: c++ |
| 20 |
|
| 21 |
float floatFunc(float); |
| 22 |
int intFunc(int); |
| 23 |
double d; |
| 24 |
int i = 42; |
| 25 |
|
| 26 |
// Warn, floating-point values expected. |
| 27 |
d = 32 * 8 / (2 + i); |
| 28 |
d = 8 * floatFunc(1 + 7 / 2); |
| 29 |
d = i / (1 << 4); |
| 30 |
|
| 31 |
// OK, no integer division. |
| 32 |
d = 32 * 8.0 / (2 + i); |
| 33 |
d = 8 * floatFunc(1 + 7.0 / 2); |
| 34 |
d = (double)i / (1 << 4); |
| 35 |
|
| 36 |
// OK, there are signs of deliberateness. |
| 37 |
d = 1 << (i / 2); |
| 38 |
d = 9 + intFunc(6 * i / 32); |
| 39 |
d = (int)(i / 32) - 8; |