From 064e59e2791b2b6fa9b0e4d8564cdced80c19f09 Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Sun, 15 Sep 2024 20:15:38 +0200 Subject: [PATCH 1/2] add static test checking for overflow in comparison operators --- test/static/international_test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/static/international_test.cpp b/test/static/international_test.cpp index 755eea73c..d2d26f656 100644 --- a/test/static/international_test.cpp +++ b/test/static/international_test.cpp @@ -38,6 +38,8 @@ using namespace mp_units::international::unit_symbols; // Mass static_assert(100'000'000 * isq::mass[lb] == 45'359'237 * isq::mass[si::kilogram]); +// 10^14 easily fits into int64 (which has about a range 10^19); but converted to the gcd, it doesn't! +static_assert(100'000'000'000'000 * isq::mass[lb] == 45'359'237'000'000 * isq::mass[si::kilogram]); static_assert(1 * isq::mass[lb] == 16 * isq::mass[oz]); static_assert(1 * isq::mass[oz] == 16 * isq::mass[dr]); static_assert(7'000 * isq::mass[gr] == 1 * isq::mass[lb]); From 10444cc409a511bdcbdb692c15b89673f7f7cd67 Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Mon, 16 Sep 2024 17:19:20 +0200 Subject: [PATCH 2/2] added one more test proving that the previous pound to kilogram conversion test passed just accidentally --- test/static/international_test.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/static/international_test.cpp b/test/static/international_test.cpp index d2d26f656..40d9aed8e 100644 --- a/test/static/international_test.cpp +++ b/test/static/international_test.cpp @@ -38,6 +38,9 @@ using namespace mp_units::international::unit_symbols; // Mass static_assert(100'000'000 * isq::mass[lb] == 45'359'237 * isq::mass[si::kilogram]); +// 47 * 45'359'237 = 2'131'884'139 = 0x7F11'F86B; the largest value that fits a signed int32 in this comparison +// 47 * 100'000'000 = 4'700'000'000 = 0x1'1824'4F00 will be truncated to 0x1824'4F00. +static_assert(47 * isq::mass[lb] < 47 * isq::mass[si::kilogram]); // 10^14 easily fits into int64 (which has about a range 10^19); but converted to the gcd, it doesn't! static_assert(100'000'000'000'000 * isq::mass[lb] == 45'359'237'000'000 * isq::mass[si::kilogram]); static_assert(1 * isq::mass[lb] == 16 * isq::mass[oz]);