Skip to content
Snippets Groups Projects
Commit 51648f65 authored by Raphael Grimm's avatar Raphael Grimm
Browse files

Fix compare functions to correctly deal with floats

parent 066a9e2b
No related branches found
No related tags found
No related merge requests found
......@@ -14,7 +14,15 @@ namespace simox::math
{
constexpr bool signed_l = std::is_signed_v<L>;
constexpr bool signed_r = std::is_signed_v<R>;
if constexpr(signed_l == signed_r)
constexpr bool floating_l = std::is_floating_point_v<L>;
constexpr bool floating_r = std::is_floating_point_v<R>;
if constexpr(floating_l != floating_r)
{
//same sign -> cast to bigger type and compare
using floating_t = std::conditional_t < floating_l, L, R >;
return static_cast<floating_t>(l) == static_cast<floating_t>(r);
}
else if constexpr(signed_l == signed_r)
{
//same sign -> cast to bigger type and compare
using big_t = std::conditional_t < (sizeof(L) > sizeof(R)), L, R >;
......
......@@ -14,7 +14,15 @@ namespace simox::math
{
constexpr bool signed_l = std::is_signed_v<L>;
constexpr bool signed_r = std::is_signed_v<R>;
if constexpr(signed_l == signed_r)
constexpr bool floating_l = std::is_floating_point_v<L>;
constexpr bool floating_r = std::is_floating_point_v<R>;
if constexpr(floating_l != floating_r)
{
//same sign -> cast to bigger type and compare
using floating_t = std::conditional_t < floating_l, L, R >;
return static_cast<floating_t>(l) < static_cast<floating_t>(r);
}
else if constexpr(signed_l == signed_r)
{
//same sign -> cast to bigger type and compare
using big_t = std::conditional_t < (sizeof(L) > sizeof(R)), L, R >;
......
......@@ -14,7 +14,15 @@ namespace simox::math
{
constexpr bool signed_l = std::is_signed_v<L>;
constexpr bool signed_r = std::is_signed_v<R>;
if constexpr(signed_l == signed_r)
constexpr bool floating_l = std::is_floating_point_v<L>;
constexpr bool floating_r = std::is_floating_point_v<R>;
if constexpr(floating_l != floating_r)
{
//same sign -> cast to bigger type and compare
using floating_t = std::conditional_t < floating_l, L, R >;
return static_cast<floating_t>(l) <= static_cast<floating_t>(r);
}
else if constexpr(signed_l == signed_r)
{
//same sign -> cast to bigger type and compare
using big_t = std::conditional_t < (sizeof(L) > sizeof(R)), L, R >;
......
......@@ -10,7 +10,7 @@ namespace simox::math
std::enable_if_t <simox::meta::are_arithmetic_v<TargetT, T>, bool>
value_in_limits_of_type(T v)
{
return simox::math::is_less_equal(std::numeric_limits<TargetT>::min(), v) &&
return simox::math::is_less_equal(std::numeric_limits<TargetT>::lowest(), v) &&
simox::math::is_greater_equal(std::numeric_limits<TargetT>::max(), v);
}
......
......@@ -18,36 +18,53 @@ BOOST_AUTO_TEST_CASE(test_normal_orth)
{
using namespace simox::math;
std::cout << "is_equal\n";
BOOST_CHECK(!is_equal(-1, 5u));
BOOST_CHECK(!is_equal(5u, -1));
BOOST_CHECK(is_equal(5u, 5u));
BOOST_CHECK(is_equal(-1, -1));
BOOST_CHECK(is_equal(-1.f, -1));
BOOST_CHECK(is_equal(-1, -1.));
std::cout << "is_greater\n";
BOOST_CHECK(!is_greater(-1, 5u));
BOOST_CHECK(is_greater(5u, -1));
BOOST_CHECK(!is_greater(5u, 5u));
BOOST_CHECK(!is_greater(-1, -1));
BOOST_CHECK(!is_greater(-1.f, -1));
BOOST_CHECK(!is_greater(-1, -1.));
std::cout << "is_greater_equal\n";
BOOST_CHECK(!is_greater_equal(-1, 5u));
BOOST_CHECK(is_greater_equal(5u, -1));
BOOST_CHECK(is_greater_equal(5u, 5u));
BOOST_CHECK(is_greater_equal(-1, -1));
std::cout << "is_inequal\n";
BOOST_CHECK(is_inequal(-1, 5u));
BOOST_CHECK(is_inequal(5u, -1));
BOOST_CHECK(!is_inequal(5u, 5u));
BOOST_CHECK(!is_inequal(-1, -1));
BOOST_CHECK(!is_inequal(-1.f, -1));
BOOST_CHECK(!is_inequal(-1, -1.));
std::cout << "is_less\n";
BOOST_CHECK(is_less(-1, 5u));
BOOST_CHECK(!is_less(5u, -1));
BOOST_CHECK(!is_less(5u, 5u));
BOOST_CHECK(!is_less(-1, -1));
BOOST_CHECK(!is_less(-1.f, -1));
BOOST_CHECK(!is_less(-1, -1.));
std::cout << "is_less_equal\n";
BOOST_CHECK(is_less_equal(-1, 5u));
BOOST_CHECK(!is_less_equal(5u, -1));
BOOST_CHECK(is_less_equal(5u, 5u));
BOOST_CHECK(is_less_equal(-1, -1));
BOOST_CHECK(is_less_equal(-1.f, -1));
BOOST_CHECK(is_less_equal(-1, -1.));
std::cout << "value_in_limits_of_type\n";
BOOST_CHECK(value_in_limits_of_type<int>(-1));
BOOST_CHECK(value_in_limits_of_type<int>(5u));
BOOST_CHECK(value_in_limits_of_type<int>(555u));
......@@ -55,4 +72,8 @@ BOOST_AUTO_TEST_CASE(test_normal_orth)
BOOST_CHECK(!value_in_limits_of_type<std::uint8_t>(-1));
BOOST_CHECK(value_in_limits_of_type<std::uint8_t>(5u));
BOOST_CHECK(!value_in_limits_of_type<std::uint8_t>(555u));
BOOST_CHECK(value_in_limits_of_type<float>(-1));
BOOST_CHECK(value_in_limits_of_type<float>(5u));
BOOST_CHECK(value_in_limits_of_type<float>(555u));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment