mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-04 12:54:25 +02:00
Feat: Floor (#309)
* feat: first draft floor * apply review feedback and move to desired files * implement floor considering float types * reduce code duplication * apply review feedback and improve docstring * enable static floor tests for gcc only * Use recommended feature-test macro * add floor runtime unittests * apply review feedback * make lambda const
This commit is contained in:
@@ -147,4 +147,40 @@ template<Quantity Q>
|
||||
return Q(std::numeric_limits<typename Q::rep>::epsilon());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Computes the largest quantity with integer representation and unit type To with its number not greater than q
|
||||
*
|
||||
* @tparam q Quantity being the base of the operation
|
||||
* @return Quantity The rounded quantity with unit type To
|
||||
*/
|
||||
template<Unit To, typename D, typename U, typename Rep>
|
||||
[[nodiscard]] constexpr quantity<D, To, Rep> floor(const quantity<D, U, Rep>& q) noexcept
|
||||
requires (!treat_as_floating_point<Rep>) ||
|
||||
requires { floor(q.number()); } ||
|
||||
requires { std::floor(q.number()); }
|
||||
{
|
||||
const auto handle_signed_results = [&]<typename T>(const T& res) {
|
||||
if (res > q)
|
||||
return res - T::one();
|
||||
return res;
|
||||
};
|
||||
if constexpr(treat_as_floating_point<Rep>) {
|
||||
using std::floor;
|
||||
if constexpr(std::is_same_v<To, U>) {
|
||||
return quantity<D, To, Rep>(floor(q.number()));
|
||||
}
|
||||
else {
|
||||
return handle_signed_results(quantity<D, To, Rep>(floor(quantity_cast<To>(q).number())));
|
||||
}
|
||||
}
|
||||
else {
|
||||
if constexpr(std::is_same_v<To, U>) {
|
||||
return q;
|
||||
}
|
||||
else {
|
||||
return handle_signed_results(quantity_cast<To>(q));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace units
|
||||
|
@@ -24,10 +24,12 @@
|
||||
#include <units/isq/si/area.h>
|
||||
#include <units/isq/si/length.h>
|
||||
#include <units/isq/si/volume.h>
|
||||
#include <units/isq/si/time.h>
|
||||
#include <catch2/catch.hpp>
|
||||
#include <limits>
|
||||
|
||||
using namespace units;
|
||||
using namespace units::isq;
|
||||
using namespace units::isq::si;
|
||||
|
||||
// classical
|
||||
@@ -108,6 +110,46 @@ TEST_CASE("numeric_limits functions", "[limits]")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("floor functions", "[floor]")
|
||||
{
|
||||
SECTION ("floor 1 second with target unit second should be 1 second") {
|
||||
REQUIRE(floor<si::second>(1_q_s) == 1_q_s);
|
||||
}
|
||||
SECTION ("floor 1000 milliseconds with target unit second should be 1 second") {
|
||||
REQUIRE(floor<si::second>(1000_q_ms) == 1_q_s);
|
||||
}
|
||||
SECTION ("floor 1001 milliseconds with target unit second should be 1 second") {
|
||||
REQUIRE(floor<si::second>(1001_q_ms) == 1_q_s);
|
||||
}
|
||||
SECTION ("floor 1999 milliseconds with target unit second should be 1 second") {
|
||||
REQUIRE(floor<si::second>(1999_q_ms) == 1_q_s);
|
||||
}
|
||||
SECTION ("floor -1000 milliseconds with target unit second should be -1 second") {
|
||||
REQUIRE(floor<si::second>(-1000_q_ms) == -1_q_s);
|
||||
}
|
||||
SECTION ("floor -999 milliseconds with target unit second should be -1 second") {
|
||||
REQUIRE(floor<si::second>(-999_q_ms) == -1_q_s);
|
||||
}
|
||||
SECTION ("floor 1.3 seconds with target unit second should be 1 second") {
|
||||
REQUIRE(floor<si::second>(1.3_q_s) == 1_q_s);
|
||||
}
|
||||
SECTION ("floor -1.3 seconds with target unit second should be -1 second") {
|
||||
REQUIRE(floor<si::second>(-1.3_q_s) == -2_q_s);
|
||||
}
|
||||
SECTION ("floor 1001. milliseconds with target unit second should be 1 second") {
|
||||
REQUIRE(floor<si::second>(1001._q_ms) == 1_q_s);
|
||||
}
|
||||
SECTION ("floor 1999. milliseconds with target unit second should be 1 second") {
|
||||
REQUIRE(floor<si::second>(1999._q_ms) == 1_q_s);
|
||||
}
|
||||
SECTION ("floor -1000. milliseconds with target unit second should be -1 second") {
|
||||
REQUIRE(floor<si::second>(-1000._q_ms) == -1_q_s);
|
||||
}
|
||||
SECTION ("floor -999. milliseconds with target unit second should be -1 second") {
|
||||
REQUIRE(floor<si::second>(-999._q_ms) == -1_q_s);
|
||||
}
|
||||
}
|
||||
|
||||
TEMPLATE_TEST_CASE_SIG("pow<N>() implementation exponentiates values to power N", "[math][pow][exp]",
|
||||
(std::intmax_t N, N), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25)
|
||||
{
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include <units/isq/si/length.h>
|
||||
#include <units/isq/si/area.h>
|
||||
#include <units/isq/si/volume.h>
|
||||
#include <units/isq/si/time.h>
|
||||
#include <units/isq/si/international/length.h>
|
||||
#include <units/isq/si/international/area.h>
|
||||
#include <units/isq/si/international/volume.h>
|
||||
@@ -32,6 +33,7 @@
|
||||
namespace {
|
||||
|
||||
using namespace units;
|
||||
using namespace units::isq;
|
||||
using namespace units::isq::si::literals;
|
||||
using namespace units::isq::si::international::literals;
|
||||
|
||||
@@ -55,4 +57,26 @@ static_assert(compare<decltype(pow<1, 4>(4_q_m2)), decltype(sqrt(2_q_m))>);
|
||||
static_assert(compare<decltype(pow<1, 4>(4_q_km2)), decltype(sqrt(2_q_km))>);
|
||||
static_assert(compare<decltype(pow<1, 4>(4_q_ft2)), decltype(sqrt(2_q_ft))>);
|
||||
|
||||
#if __cpp_lib_constexpr_cmath // TODO remove once std::floor is constexpr for all compilers
|
||||
// floor
|
||||
// integral types
|
||||
static_assert(compare<decltype(floor<si::second>(1_q_s)), decltype(1_q_s)>);
|
||||
|
||||
static_assert(compare<decltype(floor<si::second>(1000_q_ms)), decltype(1_q_s)>);
|
||||
static_assert(compare<decltype(floor<si::second>(1001_q_ms)), decltype(1_q_s)>);
|
||||
static_assert(compare<decltype(floor<si::second>(1999_q_ms)), decltype(1_q_s)>);
|
||||
static_assert(compare<decltype(floor<si::second>(-1000_q_ms)), decltype(-1_q_s)>);
|
||||
static_assert(compare<decltype(floor<si::second>(-999_q_ms)), decltype(-1_q_s)>);
|
||||
|
||||
// floating-point
|
||||
static_assert(floor<si::second>(1.3_q_s) == 1_q_s);
|
||||
static_assert(floor<si::second>(-1.3_q_s) == -2_q_s);
|
||||
|
||||
// static_assert(floor<si::second>(1000._q_ms) == 1_q_s); // does not work due to a bug in fpow10() see #311
|
||||
static_assert(floor<si::second>(1001._q_ms) == 1_q_s);
|
||||
static_assert(floor<si::second>(1999._q_ms) == 1_q_s);
|
||||
static_assert(floor<si::second>(-1000._q_ms) == -1_q_s);
|
||||
static_assert(floor<si::second>(-999._q_ms) == -1_q_s);
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
Reference in New Issue
Block a user