[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master a6f2830d 01/10: Explain the necessity of a ce
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master a6f2830d 01/10: Explain the necessity of a certain cast |
Date: |
Mon, 20 Jun 2022 19:16:17 -0400 (EDT) |
branch: master
commit a6f2830d6e33438082e9f0fece7cf1b36f64f4da
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Explain the necessity of a certain cast
Without the cast, 'std::uint8_t' is promoted to 'signed int': i.e., both
its width and its signedness change. The conditional ('?:') operator's
third operand is likewise promoted.
Assigning the 'signed int' return value to a 'std::int16_t' loop counter
in the unit test engenders a gcc '-Wconversion' warning where 'int' is
at least 32 bits wide. Arguably that warning is too aggressive because
the compiler can deduce that the value is representable in 16 bits, but
the cast makes the intention explicit.
---
math_functions.hpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/math_functions.hpp b/math_functions.hpp
index e1eded1a..372cf74e 100644
--- a/math_functions.hpp
+++ b/math_functions.hpp
@@ -167,6 +167,12 @@ T signum(T t)
/// Asserts that both integer types have no padding, to rule out the
/// UINT_MAX == INT_MAX == -(INT_MIN+1)
/// case that Daniel Fischer points out somewhere on the web.
+///
+/// The cast on the last line may appear superfluous, but it is not:
+/// unary '-' performs integral promotion on its operand and returns a
+/// result of the promoted type, which is (signed) 'int' if type T is
+/// narrower than 'int'. Without this cast, gcc would issue a warning
+/// in the accompanying unit test.
template<typename T>
constexpr auto u_abs(T t)
- [lmi-commits] [lmi] master updated (876d4b5b -> 50009ece), Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master e986f0c9 02/10: Specify types more explicitly in unit test, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master a6f2830d 01/10: Explain the necessity of a certain cast,
Greg Chicares <=
- [lmi-commits] [lmi] master 3c9845b4 06/10: State the purpose of a particular unit test, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 8f5b6960 04/10: Demonstrate object sizes in unit test, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 1744d568 07/10: Regularize every mention of nextafter(), Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master e711587c 08/10: Expunge a workaround for an obsolete compiler, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 168e9a70 03/10: Specify a constant more clearly, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 7258e4ae 05/10: Establish and test explicit postconditions, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 50009ece 10/10: x⋅(1+ε) ≢ nextafter(x) [264], Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master e7278f90 09/10: Add some functions to complement std::nextafter(), Greg Chicares, 2022/06/20