xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/bugprone/integer-division.rst (revision 6e566bc5523f743bc34a7e26f050f1f2b4d699a8)
1.. title:: clang-tidy - bugprone-integer-division
2
3bugprone-integer-division
4=========================
5
6Finds cases where integer division in a floating point context is likely to
7cause unintended loss of precision.
8
9No 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
15as these are interpreted as signs of deliberateness from the programmer.
16
17Examples:
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;
40