Skip to content

Commit 60b433d

Browse files
committed
Update comparison functions to be more standard conforming
Signed-off-by: Ian <[email protected]>
1 parent 388e680 commit 60b433d

File tree

6 files changed

+42
-54
lines changed

6 files changed

+42
-54
lines changed

include/ccmath/math/compare/isgreater.hpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <type_traits>
1414

1515
#include "ccmath/internal/math/generic/builtins/compare/isgreater.hpp"
16-
16+
#include "ccmath/math/compare/isunordered.hpp"
1717

1818
namespace ccm
1919
{
@@ -24,29 +24,26 @@ namespace ccm
2424
* @param y A floating-point or integer value.
2525
* @return true if the first argument is greater than the second, false otherwise.
2626
*/
27-
template <typename T>
27+
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
2828
constexpr bool isgreater(T x, T y) noexcept
2929
{
3030
if constexpr (ccm::builtin::has_constexpr_isgreater<T>) { return ccm::builtin::isgreater(x, y); }
31-
else
32-
{
33-
return x > y;
34-
}
31+
else { return !ccm::isunordered(x, y) && (x > y); }
3532
}
3633

3734
/**
3835
* @brief Checks if the first argument is greater than the second.
39-
* @tparam T Type of the left-hand side.
40-
* @tparam U Type of the right-hand side.
36+
* @tparam Arithmetic1 Arithmetic type of the left-hand side.
37+
* @tparam Arithmetic2 Arithmetic type of the right-hand side.
4138
* @param x Value of the left-hand side of the comparison.
4239
* @param y Value of the right-hand side of the comparison.
4340
* @return true if the first argument is greater than the second, false otherwise.
4441
*/
45-
template <typename T, typename U>
46-
constexpr bool isgreater(T x, U y) noexcept
42+
template <typename Arithmetic1, typename Arithmetic2, std::enable_if_t<std::is_arithmetic_v<Arithmetic1> && std::is_arithmetic_v<Arithmetic2>, bool> = true>
43+
constexpr bool isgreater(Arithmetic1 x, Arithmetic2 y) noexcept
4744
{
4845
// Find the common type of the two arguments
49-
using shared_type = std::common_type_t<T, U>;
46+
using shared_type = std::common_type_t<Arithmetic1, Arithmetic2>;
5047

5148
// Then cast the arguments to the common type and call the single argument version
5249
return static_cast<shared_type>(isgreater<shared_type>(static_cast<shared_type>(x), static_cast<shared_type>(y)));

include/ccmath/math/compare/isgreaterequal.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#pragma once
1212

1313
#include "ccmath/internal/math/generic/builtins/compare/isgreaterequal.hpp"
14+
#include "ccmath/math/compare/isunordered.hpp"
1415

1516
#include <type_traits>
1617

@@ -23,29 +24,29 @@ namespace ccm
2324
* @param y A floating-point or integer value.
2425
* @return true if the first argument is greater than or equal to the second, false otherwise.
2526
*/
26-
template <typename T>
27+
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
2728
constexpr bool isgreaterequal(T x, T y) noexcept
2829
{
2930
if constexpr (ccm::builtin::has_constexpr_isgreaterequal<T>) { return ccm::builtin::isgreaterequal(x, y); }
3031
else
3132
{
32-
return x >= y;
33+
return !ccm::isunordered(x,y) && (x >= y);
3334
}
3435
}
3536

3637
/**
3738
* @brief Checks if the first argument is greater than or equal to the second.
38-
* @tparam T Type of the left-hand side.
39-
* @tparam U Type of the right-hand side.
39+
* @tparam Arithmetic1 Arithmetic type of the left-hand side.
40+
* @tparam Arithmetic2 Arithmetic type of the right-hand side.
4041
* @param x Value of the left-hand side of the comparison.
4142
* @param y Value of the right-hand side of the comparison.
4243
* @return true if the first argument is greater than or equal to the second, false otherwise.
4344
*/
44-
template <typename T, typename U>
45-
constexpr bool isgreaterequal(T x, U y) noexcept
45+
template <typename Arithmetic1, typename Arithmetic2, std::enable_if_t<std::is_arithmetic_v<Arithmetic1> && std::is_arithmetic_v<Arithmetic2>, bool> = true>
46+
constexpr bool isgreaterequal(Arithmetic1 x, Arithmetic2 y) noexcept
4647
{
4748
// Find the common type of the two arguments
48-
using shared_type = std::common_type_t<T, U>;
49+
using shared_type = std::common_type_t<Arithmetic1, Arithmetic2>;
4950

5051
// Then cast the arguments to the common type and call the single argument version
5152
return static_cast<shared_type>(isgreaterequal<shared_type>(static_cast<shared_type>(x), static_cast<shared_type>(y)));

include/ccmath/math/compare/isless.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#pragma once
1212

1313
#include "ccmath/internal/math/generic/builtins/compare/isless.hpp"
14+
#include "ccmath/math/compare/isunordered.hpp"
1415

1516
#include <type_traits>
1617

@@ -23,29 +24,26 @@ namespace ccm
2324
* @param y A floating-point or integer value.
2425
* @return true if the first argument is less than the second, false otherwise.
2526
*/
26-
template <typename T>
27+
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
2728
constexpr bool isless(T x, T y) noexcept
2829
{
2930
if constexpr (ccm::builtin::has_constexpr_isless<T>) { return ccm::builtin::isless(x, y); }
30-
else
31-
{
32-
return x < y;
33-
}
31+
else { return !ccm::isunordered(x, y) && (x < y); }
3432
}
3533

3634
/**
3735
* @brief Checks if the first argument is less than the second.
38-
* @tparam T Type of the left-hand side.
39-
* @tparam U Type of the right-hand side.
36+
* @tparam Arithmetic1 Arithmetic type of the left-hand side.
37+
* @tparam Arithmetic2 Arithmetic type of the right-hand side.
4038
* @param x Value of the left-hand side of the comparison.
4139
* @param y Value of the right-hand side of the comparison.
4240
* @return true if the first argument is less than the second, false otherwise.
4341
*/
44-
template <typename T, typename U>
45-
constexpr bool isless(T x, U y) noexcept
42+
template <typename Arithmetic1, typename Arithmetic2, std::enable_if_t<std::is_arithmetic_v<Arithmetic1> && std::is_arithmetic_v<Arithmetic2>, bool> = true>
43+
constexpr bool isless(Arithmetic1 x, Arithmetic2 y) noexcept
4644
{
4745
// Find the common type of the two arguments
48-
using shared_type = std::common_type_t<T, U>;
46+
using shared_type = std::common_type_t<Arithmetic1, Arithmetic2>;
4947

5048
// Then cast the arguments to the common type and call the single argument version
5149
return static_cast<shared_type>(isless<shared_type>(static_cast<shared_type>(x), static_cast<shared_type>(y)));

include/ccmath/math/compare/islessequal.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#pragma once
1212

1313
#include "ccmath/internal/math/generic/builtins/compare/islessequal.hpp"
14+
#include "ccmath/math/compare/isunordered.hpp"
1415

1516
#include <type_traits>
1617

@@ -23,29 +24,26 @@ namespace ccm
2324
* @param y A floating-point or integer value.
2425
* @return true if the first argument is less than or equal to the second, false otherwise.
2526
*/
26-
template <typename T>
27+
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
2728
constexpr bool islessequal(T x, T y) noexcept
2829
{
2930
if constexpr (ccm::builtin::has_constexpr_islessequal<T>) { return ccm::builtin::islessequal(x, y); }
30-
else
31-
{
32-
return x <= y;
33-
}
31+
else { return !ccm::isunordered(x, y) && (x <= y); }
3432
}
3533

3634
/**
3735
* @brief Checks if the first argument is less than or equal to the second.
38-
* @tparam T Type of the left-hand side.
39-
* @tparam U Type of the right-hand side.
36+
* @tparam Arithmetic1 Arithmetic type of the left-hand side.
37+
* @tparam Arithmetic2 Arithmetic type of the right-hand side.
4038
* @param x Value of the left-hand side of the comparison.
4139
* @param y Value of the right-hand side of the comparison.
4240
* @return true if the first argument is less than or equal to the second, false otherwise.
4341
*/
44-
template <typename T, typename U>
45-
constexpr bool islessequal(T x, U y) noexcept
42+
template <typename Arithmetic1, typename Arithmetic2, std::enable_if_t<std::is_arithmetic_v<Arithmetic1> && std::is_arithmetic_v<Arithmetic2>, bool> = true>
43+
constexpr bool islessequal(Arithmetic1 x, Arithmetic2 y) noexcept
4644
{
4745
// Find the common type of the two arguments
48-
using shared_type = std::common_type_t<T, U>;
46+
using shared_type = std::common_type_t<Arithmetic1, Arithmetic2>;
4947

5048
// Then cast the arguments to the common type and call the single argument version
5149
return static_cast<shared_type>(islessequal<shared_type>(static_cast<shared_type>(x), static_cast<shared_type>(y)));

include/ccmath/math/compare/islessgreater.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#pragma once
1212

1313
#include "ccmath/internal/math/generic/builtins/compare/islessgreater.hpp"
14+
#include "ccmath/math/compare/isunordered.hpp"
1415

1516
#include <type_traits>
1617

@@ -23,28 +24,25 @@ namespace ccm
2324
* @param y A floating-point or integer value.
2425
* @return true if the first argument is less than the second or greater than the second, false otherwise.
2526
*/
26-
template <typename T>
27+
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
2728
constexpr bool islessgreater(T x, T y) noexcept
2829
{
2930
if constexpr (ccm::builtin::has_constexpr_islessgreater<T>) { return ccm::builtin::islessgreater(x, y); }
30-
else
31-
{
32-
return x < y || x > y;
33-
}
31+
else { return !ccm::isunordered(x, y) && (x < y || x > y); }
3432
}
3533

3634
/**
3735
* @brief Checks if the first argument is less than the second or greater than the second.
38-
* @tparam T Type of the left-hand side.
39-
* @tparam U Type of the right-hand side.
36+
* @tparam Arithmetic1 Arithmetic type of the left-hand side.
37+
* @tparam Arithmetic2 Arithmetic type of the right-hand side.
4038
* @param x Value of the left-hand side of the comparison.
4139
* @param y Value of the right-hand side of the comparison.
4240
* @return true if the first argument is less than the second or greater than the second, false otherwise.
4341
*/
44-
template <typename T, typename U>
45-
constexpr bool islessgreater(T x, U y) noexcept
42+
template <typename Arithmetic1, typename Arithmetic2, std::enable_if_t<std::is_arithmetic_v<Arithmetic1> && std::is_arithmetic_v<Arithmetic2>, bool> = true>
43+
constexpr bool islessgreater(Arithmetic1 x, Arithmetic2 y) noexcept
4644
{
47-
using shared_type = std::common_type_t<T, U>;
45+
using shared_type = std::common_type_t<Arithmetic1, Arithmetic2>;
4846
return static_cast<shared_type>(islessgreater<shared_type>(static_cast<shared_type>(x), static_cast<shared_type>(y)));
4947
}
5048
} // namespace ccm

include/ccmath/math/compare/isunordered.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
#pragma once
1212

13-
#include "ccmath/math/compare/isnan.hpp"
1413
#include "ccmath/internal/math/generic/builtins/compare/isunordered.hpp"
15-
14+
#include "ccmath/math/compare/isnan.hpp"
1615

1716
#include <type_traits>
1817

@@ -29,10 +28,7 @@ namespace ccm
2928
constexpr bool isunordered(T x, T y) noexcept
3029
{
3130
if constexpr (ccm::builtin::has_constexpr_isunordered<T>) { return ccm::builtin::isunordered(x, y); }
32-
else
33-
{
34-
return ccm::isnan(x) || ccm::isnan(y);
35-
}
31+
else { return ccm::isnan(x) || ccm::isnan(y); }
3632
}
3733

3834
/**

0 commit comments

Comments
 (0)