From 8b112960586fce4aad791f9cf52c94a70b476ce0 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Tue, 25 Feb 2025 23:30:03 +0100 Subject: Fix float comparison with 0 warning --- asl/formatting/format_float.cpp | 12 +++++++++++- todo.txt | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/asl/formatting/format_float.cpp b/asl/formatting/format_float.cpp index eca1d92..fb37064 100644 --- a/asl/formatting/format_float.cpp +++ b/asl/formatting/format_float.cpp @@ -19,6 +19,16 @@ static constexpr char kZeros[kZeroCount] = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', }; +static constexpr bool is_zero(float x) +{ + return (asl::bit_cast(x) & 0x7fff'ffff) == 0; +} + +static constexpr bool is_zero(double x) +{ + return (asl::bit_cast(x) & 0x7fff'ffff'ffff'ffff) == 0; +} + template static void format_float(asl::Formatter& f, T value) { @@ -35,7 +45,7 @@ static void format_float(asl::Formatter& f, T value) return; } - if (value == static_cast(0)) + if (is_zero(value)) { f.write("0"_sv); return; diff --git a/todo.txt b/todo.txt index 2c010fa..8b13789 100644 --- a/todo.txt +++ b/todo.txt @@ -1 +1 @@ -warning compare with 0 in format_float.cpp + -- cgit