Skip to content

Commit

Permalink
[clang-tidy] Avoid float compare in bugprone-incorrect-roundings
Browse files Browse the repository at this point in the history
Using APFloat to compare floating numbers instead of float/double.

Fixes: #46424

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D147906
  • Loading branch information
PiotrZSL committed Apr 15, 2023
1 parent caa9d6e commit a73b87b
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ using namespace clang::ast_matchers;

namespace clang::tidy::bugprone {

static llvm::APFloat getHalf(const llvm::fltSemantics &Semantics) {
return llvm::APFloat(Semantics, 1U) / llvm::APFloat(Semantics, 2U);
}

namespace {
AST_MATCHER(FloatingLiteral, floatHalf) {
const auto &Literal = Node.getValue();
const llvm::APFloat Literal = Node.getValue();
if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle())
return Literal.convertToFloat() == 0.5f;
return Literal == getHalf(llvm::APFloat::IEEEsingle());
if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble())
return Literal.convertToDouble() == 0.5;
return Literal == getHalf(llvm::APFloat::IEEEdouble());
return false;
}
} // namespace
Expand Down

0 comments on commit a73b87b

Please sign in to comment.