clang-tools 22.0.0git
clang::tidy::bugprone::IncorrectRoundingsCheck Class Reference

Checks the usage of patterns known to produce incorrect rounding. More...

#include <IncorrectRoundingsCheck.h>

Inheritance diagram for clang::tidy::bugprone::IncorrectRoundingsCheck:
[legend]

Public Member Functions

 IncorrectRoundingsCheck (StringRef Name, ClangTidyContext *Context)
void registerMatchers (ast_matchers::MatchFinder *Finder) override
void check (const ast_matchers::MatchFinder::MatchResult &Result) override

Detailed Description

Checks the usage of patterns known to produce incorrect rounding.

Programmers often use (int)(double_expression + 0.5) to round the double expression to an integer. The problem with this

  1. It is unnecessarily slow.
  2. It is incorrect. The number 0.499999975 (smallest representable float number below 0.5) rounds to 1.0. Even worse behavior for negative numbers where both -0.5f and -1.4f both round to 0.0.

Definition at line 24 of file IncorrectRoundingsCheck.h.

Constructor & Destructor Documentation

◆ IncorrectRoundingsCheck()

clang::tidy::bugprone::IncorrectRoundingsCheck::IncorrectRoundingsCheck ( StringRef Name,
ClangTidyContext * Context )
inline

Definition at line 26 of file IncorrectRoundingsCheck.h.

Member Function Documentation

◆ check()

void clang::tidy::bugprone::IncorrectRoundingsCheck::check ( const ast_matchers::MatchFinder::MatchResult & Result)
override

Definition at line 47 of file IncorrectRoundingsCheck.cpp.

◆ registerMatchers()

void clang::tidy::bugprone::IncorrectRoundingsCheck::registerMatchers ( ast_matchers::MatchFinder * Finder)
override

Definition at line 27 of file IncorrectRoundingsCheck.cpp.


The documentation for this class was generated from the following files: