When you compare signed and unsigned integers, you may not get the result you expect. Thanks to the six std::cmp_
* functions, there is a cure in C++20.
Maybe, you remember the rule "ES.100 Don't mix signed and unsigned arithmetic" from the C++ Core Guidelines. I wrote a few words about it in my previous post to "Arithmetic Rules". Today, I want to dig deeper into this issue and compare signed and unsigned integers.
Let's start with an unsafe comparison.
Unsafe Comparison of Integrals
Of course, there is a reason for the program name unsafeComparison.cpp
.
// unsafeComparison.cpp
#include <iostream>
int main() {
std::cout << std::endl;
std::cout << std::boolalpha;
int x = -3; // (1)
unsigned int y = 7; // (2)
std::cout << "-3 < 7: " << (x < y) << std::endl;
std::cout << "-3 <= 7: " << (x <= y) << std::endl;
std::cout << "-3 > 7: " << (x > y) << std::endl;
std::cout << "-3 => 7: " << (x >= y) << std::endl;
std::cout << std::endl;
}
When I execute the program, the output may not meet your expectations.
When you read the output of the program, you recognize -3 should be bigger than 7. You presumably know the reason. I compared a signed x
(line (1)) with an unsigned y
(line (2)). What is happening under the hood? The following program provides the answer.
// unsafeComparison2.cpp
int main() {
int x = -3;
unsigned int y = 7;
bool val = x < y; // (1)
static_assert(static_cast<unsigned int>(-3) == 4'294'967'293);
}
In the example, I'm focusing on the less-than operator. C++ Insights gives me the following output:
Here is what's happening:
- The compiler transforms the expression
x < y
(line 1) into static_cast<unsigned int>(x) < y
. In particular, the signed
x
is converted to an unsigned int.
- Due to the conversion,
-3
becomes 4'294'967'293.
4'294'967'293
is equal to (-3) modulo (2 to the power of 32).
- 32 is the number of bits of an unsigned
int
on C++ Insights.
Thanks to C++20, we have a safe comparison of integrals.
Safe Comparison of Integrals
C++20 supports the six comparison functions for integrals:
Thanks to the six comparison functions, I can easily transform the previous program unsafeComparison.cpp
into the program safeComparison.cpp.
The new comparison functions require the header <utility
>.
// safeComparison.cpp
#include <iostream>
#include <utility>
int main() {
std::cout << std::endl;
std::cout << std::boolalpha;
int x = -3;
unsigned int y = 7;
std::cout << "3 == 7: " << std::cmp_equal(x, y) << std::endl;
std::cout << "3 != 7: " << std::cmp_not_equal(x, y) << std::endl;
std::cout << "-3 < 7: " << std::cmp_less(x, y) << std::endl;
std::cout << "-3 <= 7: " << std::cmp_less_equal(x, y) << std::endl;
std::cout << "-3 > 7: " << std::cmp_greater(x, y) << std::endl;
std::cout << "-3 => 7: " << std::cmp_greater_equal(x, y) << std::endl;
std::cout << std::endl;
}
I also used in this program the equal and not equal operator.
Thanks to GCC 10, here is the expected result:
Invoking a comparison function a non-integral value would causes a compile-time error.
// safeComparison2.cpp
#include <iostream>
#include <utility>
int main() {
double x = -3.5; // (1)
unsigned int y = 7; // (2)
std::cout << "-3.5 < 7: " << std::cmp_less(x, y) << std::endl;
}
Trying to compare a double
(line (1)) and an unsigned int
(line (2)) gives with the GCC 10 compiler a lengthy error message. Here is the crucial line of the error message:
The internal type-traits __is_standard_integer failed. I was curious about what that means and looked it up in the GCC type-traits implementation on GitHub. Here are the relevant lines from the header type-traits:
// Check if a type is one of the signed or unsigned integer types.
template<typename _Tp>
using __is_standard_integer
= __or_<__is_signed_integer<_Tp>, __is_unsigned_integer<_Tp>>;
// Check if a type is one of the signed integer types.
template<typename _Tp>
using __is_signed_integer = __is_one_of<__remove_cv_t<_Tp>,
signed char, signed short, signed int, signed long,
signed long long
// Check if a type is one of the unsigned integer types.
template<typename _Tp>
using __is_unsigned_integer = __is_one_of<__remove_cv_t<_Tp>,
unsigned char, unsigned short, unsigned int, unsigned long,
unsigned long long
__remove_cv_t
is the internal function of GCC to remove const
or volatile
from a type.
Maybe, you are now curious what happens when you compare a double
and an unsigned int
the classical way.
Here is the modified program safeComparison2.cpp.
// classicalComparison.cpp
int main() {
double x = -3.5;
unsigned int y = 7;
auto res = x < y; // true
}
It works. The crucial unsigned int
is floating-point promoted to double
. C++ Insights shows the truth:
After so many comparisons, I want to end this post with the new mathematical constants we have with C++20.
Mathematical Constants
First, the constants require the header <numbers>
and the namespace std::numbers
. The following tables give you the first overview.
The program mathematicConstants.cpp
applies the mathematical constants.
// mathematicConstants.cpp
#include <iomanip>
#include <iostream>
#include <numbers>
int main() {
std::cout << std::endl;
std::cout<< std::setprecision(10);
std::cout << "std::numbers::e: " << std::numbers::e << std::endl;
std::cout << "std::numbers::log2e: " << std::numbers::log2e << std::endl;
std::cout << "std::numbers::log10e: " << std::numbers::log10e << std::endl;
std::cout << "std::numbers::pi: " << std::numbers::pi << std::endl;
std::cout << "std::numbers::inv_pi: " << std::numbers::inv_pi << std::endl;
std::cout << "std::numbers::inv_sqrtpi: " << std::numbers::inv_sqrtpi << std::endl;
std::cout << "std::numbers::ln2: " << std::numbers::ln2 << std::endl;
std::cout << "std::numbers::sqrt2: " << std::numbers::sqrt2 << std::endl;
std::cout << "std::numbers::sqrt3: " << std::numbers::sqrt3 << std::endl;
std::cout << "std::numbers::inv_sqrt3: " << std::numbers::inv_sqrt3 << std::endl;
std::cout << "std::numbers::egamma: " << std::numbers::egamma << std::endl;
std::cout << "std::numbers::phi: " << std::numbers::phi << std::endl;
std::cout << std::endl;
}
Here is the output of the program with the MSVC compiler 19.27.
The mathematical constants are available for float
, double
, and long double
. Per-default double
is used but you can also specify float
(std::numbers::pi_v<float>
) or long double
(std::numbers::pi_v<long double>
).
What's next?
C++20 offers more useful utilities. For example, you can ask your compiler which C++ feature it supports, can easily create function objects with std::bind_front,
or perform different actions in a function whether the function runs a compile-time or at runtime.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, and Peter Ware.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, and Sudhakar Belagurusamy.
Seminars
I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.
Bookable (Online)
Deutsch
Standard Seminars
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
New
Contact Me
Modernes C++,
Comments
This name is a big source of trouble. Let me quote the standard:
3.9.1 Fundamental types [basic.fundamental]
Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type.[...]
By the way, I used the term Integral in my German translation of this post and got more than 20 complaints. This happens when you exactly use the names of the standard.
RSS feed for comments to this post