From 4c47bc0e9ac170e93bfbb7ea4a385dd6a0498a5d Mon Sep 17 00:00:00 2001 From: Keith O'Hara Date: Sun, 31 Jul 2022 17:08:24 -0400 Subject: [PATCH] add tests and minor reformat --- include/gcem.hpp | 10 +++++----- include/gcem_incl/ceil.hpp | 33 +++++++++++++++------------------ include/gcem_incl/floor.hpp | 33 +++++++++++++++------------------ include/gcem_incl/round.hpp | 35 ++++++++++++++++------------------- include/gcem_incl/trunc.hpp | 33 +++++++++++++++------------------ tests/rounding.cpp | 4 ++++ 6 files changed, 70 insertions(+), 78 deletions(-) diff --git a/include/gcem.hpp b/include/gcem.hpp index 11519e5..6aa367d 100644 --- a/include/gcem.hpp +++ b/include/gcem.hpp @@ -30,6 +30,11 @@ namespace gcem #include "gcem_incl/is_inf.hpp" #include "gcem_incl/is_nan.hpp" #include "gcem_incl/is_finite.hpp" + + #include "gcem_incl/signbit.hpp" + #include "gcem_incl/copysign.hpp" + #include "gcem_incl/neg_zero.hpp" + #include "gcem_incl/sgn.hpp" #include "gcem_incl/abs.hpp" #include "gcem_incl/ceil.hpp" @@ -42,11 +47,6 @@ namespace gcem #include "gcem_incl/sqrt.hpp" #include "gcem_incl/inv_sqrt.hpp" - #include "gcem_incl/signbit.hpp" - #include "gcem_incl/copysign.hpp" - #include "gcem_incl/neg_zero.hpp" - #include "gcem_incl/sgn.hpp" - #include "gcem_incl/find_exponent.hpp" #include "gcem_incl/find_fraction.hpp" #include "gcem_incl/find_whole.hpp" diff --git a/include/gcem_incl/ceil.hpp b/include/gcem_incl/ceil.hpp index 0ca8d26..e8570ab 100644 --- a/include/gcem_incl/ceil.hpp +++ b/include/gcem_incl/ceil.hpp @@ -57,12 +57,11 @@ float ceil_check_internal(const float x) noexcept { - //threshold = 8388608.f; - - return ( - (abs(x) >= 8388608.f) ? \ - x : \ - ceil_int(x, float(static_cast(x))) ); + return( abs(x) >= 8388608.f ? \ + // if + x : \ + // else + ceil_int(x, float(static_cast(x))) ); } template<> @@ -71,12 +70,11 @@ double ceil_check_internal(const double x) noexcept { - //threshold = 4503599627370496.; - - return ( - (abs(x) >= 4503599627370496.) ? \ - x : \ - ceil_int(x, double(static_cast(x))) ); + return( abs(x) >= 4503599627370496. ? \ + // if + x : \ + // else + ceil_int(x, double(static_cast(x))) ); } template<> @@ -85,12 +83,11 @@ long double ceil_check_internal(const long double x) noexcept { - //threshold = 9223372036854775808.l; - - return ( - (abs(x) >= 9223372036854775808.l) ? \ - x : \ - ceil_int(x, ((long double)static_cast(abs(x))) * ((x < 0) ? -1.l : 1.l)) ); + return( abs(x) >= 9223372036854775808.l ? \ + // if + x : \ + // else + ceil_int(x, ((long double)static_cast(abs(x))) * sgn(x)) ); } template diff --git a/include/gcem_incl/floor.hpp b/include/gcem_incl/floor.hpp index cdf1c82..c60ff6a 100644 --- a/include/gcem_incl/floor.hpp +++ b/include/gcem_incl/floor.hpp @@ -57,12 +57,11 @@ float floor_check_internal(const float x) noexcept { - //threshold = 8388608.f; - - return ( - (abs(x) >= 8388608.f) ? \ - x : \ - floor_int(x, float(static_cast(x))) ); + return( abs(x) >= 8388608.f ? \ + // if + x : \ + // else + floor_int(x, float(static_cast(x))) ); } template<> @@ -71,12 +70,11 @@ double floor_check_internal(const double x) noexcept { - //threshold = 4503599627370496.; - - return ( - (abs(x) >= 4503599627370496.) ? \ - x : \ - floor_int(x, double(static_cast(x))) ); + return( abs(x) >= 4503599627370496. ? \ + // if + x : \ + // else + floor_int(x, double(static_cast(x))) ); } template<> @@ -85,12 +83,11 @@ long double floor_check_internal(const long double x) noexcept { - //threshold = 9223372036854775808.l; - - return ( - (abs(x) >= 9223372036854775808.l) ? \ - x : \ - floor_int(x, ((long double)static_cast(abs(x))) * ((x < 0) ? -1.l : 1.l)) ); + return( abs(x) >= 9223372036854775808.l ? \ + // if + x : \ + // else + floor_int(x, ((long double)static_cast(abs(x))) * sgn(x)) ); } template diff --git a/include/gcem_incl/round.hpp b/include/gcem_incl/round.hpp index 5b71298..43d7a5e 100644 --- a/include/gcem_incl/round.hpp +++ b/include/gcem_incl/round.hpp @@ -52,12 +52,11 @@ float round_check_internal(const float x) noexcept { - //threshold = 8388608.f; - - return ( - (abs(x) >= 8388608.f) ? \ - x : \ - round_int(x) ); + return( abs(x) >= 8388608.f ? \ + // if + x : \ + //else + round_int(x) ); } template<> @@ -66,12 +65,11 @@ double round_check_internal(const double x) noexcept { - //threshold = 4503599627370496.; - - return ( - (abs(x) >= 4503599627370496.) ? \ - x : \ - round_int(x) ); + return( abs(x) >= 4503599627370496. ? \ + // if + x : \ + // else + round_int(x) ); } template<> @@ -80,12 +78,11 @@ long double round_check_internal(const long double x) noexcept { - //threshold = 9223372036854775808.l; - - return ( - (abs(x) >= 9223372036854775808.l) ? \ - x : \ - round_int(x) ); + return( abs(x) >= 9223372036854775808.l ? \ + // if + x : \ + // else + round_int(x) ); } template @@ -104,7 +101,7 @@ noexcept GCLIM::min() > abs(x) ? \ x : // else - sgn(x) * round_int(abs(x)) ); + sgn(x) * round_check_internal(abs(x)) ); } } diff --git a/include/gcem_incl/trunc.hpp b/include/gcem_incl/trunc.hpp index ad5492f..4e19ef9 100644 --- a/include/gcem_incl/trunc.hpp +++ b/include/gcem_incl/trunc.hpp @@ -48,12 +48,11 @@ float trunc_check_internal(const float x) noexcept { - //threshold = 8388608.f; - - return ( - (abs(x) >= 8388608.f) ? \ - x : \ - trunc_int(x) ); + return( abs(x) >= 8388608.f ? \ + // if + x : \ + // else + trunc_int(x) ); } template<> @@ -62,12 +61,11 @@ double trunc_check_internal(const double x) noexcept { - //threshold = 4503599627370496.; - - return ( - (abs(x) >= 4503599627370496.) ? \ - x : \ - trunc_int(x) ); + return( abs(x) >= 4503599627370496. ? \ + // if + x : \ + // else + trunc_int(x) ); } template<> @@ -76,12 +74,11 @@ long double trunc_check_internal(const long double x) noexcept { - //threshold = 9223372036854775808.l; - - return ( - (abs(x) >= 9223372036854775808.l) ? \ - x : \ - ((long double)static_cast(abs(x))) * ((x < 0) ? -1.l : 1.l) ); + return( abs(x) >= 9223372036854775808.l ? \ + // if + x : \ + // else + ((long double)static_cast(abs(x))) * sgn(x) ); } template diff --git a/tests/rounding.cpp b/tests/rounding.cpp index 21451e7..a4f820d 100644 --- a/tests/rounding.cpp +++ b/tests/rounding.cpp @@ -39,6 +39,7 @@ int main() GCEM_TEST_COMPARE_VALS(gcem::floor,std::floor, -4.7); GCEM_TEST_COMPARE_VALS(gcem::floor,std::floor, -5.0); GCEM_TEST_COMPARE_VALS(gcem::floor,std::floor, 42e32); + GCEM_TEST_COMPARE_VALS(gcem::floor,std::floor, -42e32); GCEM_TEST_COMPARE_VALS(gcem::floor,std::floor, std::numeric_limits::max()); GCEM_TEST_COMPARE_VALS(gcem::floor,std::floor, -std::numeric_limits::infinity()); @@ -57,6 +58,7 @@ int main() GCEM_TEST_COMPARE_VALS(gcem::ceil,std::ceil, -4.7); GCEM_TEST_COMPARE_VALS(gcem::ceil,std::ceil, -5.0); GCEM_TEST_COMPARE_VALS(gcem::ceil,std::ceil, 42e32); + GCEM_TEST_COMPARE_VALS(gcem::ceil,std::ceil, -42e32); GCEM_TEST_COMPARE_VALS(gcem::ceil,std::ceil, std::numeric_limits::max()); GCEM_TEST_COMPARE_VALS(gcem::ceil,std::ceil, -std::numeric_limits::infinity()); @@ -75,6 +77,7 @@ int main() GCEM_TEST_COMPARE_VALS(gcem::trunc,std::trunc, -4.7); GCEM_TEST_COMPARE_VALS(gcem::trunc,std::trunc, -5.0); GCEM_TEST_COMPARE_VALS(gcem::trunc,std::trunc, 42e32); + GCEM_TEST_COMPARE_VALS(gcem::trunc,std::trunc, -42e32); GCEM_TEST_COMPARE_VALS(gcem::trunc,std::trunc, std::numeric_limits::max()); GCEM_TEST_COMPARE_VALS(gcem::trunc,std::trunc, -std::numeric_limits::infinity()); @@ -94,6 +97,7 @@ int main() GCEM_TEST_COMPARE_VALS(gcem::round,std::round, -4.7); GCEM_TEST_COMPARE_VALS(gcem::round,std::round, -5.0); GCEM_TEST_COMPARE_VALS(gcem::round,std::round, 42e32); + GCEM_TEST_COMPARE_VALS(gcem::round,std::round, -42e32); GCEM_TEST_COMPARE_VALS(gcem::round,std::round, std::numeric_limits::max()); GCEM_TEST_COMPARE_VALS(gcem::round,std::round, -std::numeric_limits::infinity());