| 1 |
.. title:: clang-tidy - bugprone-incorrect-roundings |
| 2 |
|
| 3 |
bugprone-incorrect-roundings |
| 4 |
============================ |
| 5 |
|
| 6 |
Checks the usage of patterns known to produce incorrect rounding. |
| 7 |
Programmers often use:: |
| 8 |
|
| 9 |
(int)(double_expression + 0.5) |
| 10 |
|
| 11 |
to round the double expression to an integer. The problem with this: |
| 12 |
|
| 13 |
1. It is unnecessarily slow. |
| 14 |
2. It is incorrect. The number 0.499999975 (smallest representable float |
| 15 |
number below 0.5) rounds to 1.0. Even worse behavior for negative |
| 16 |
numbers where both -0.5f and -1.4f both round to 0.0. |