mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-04 12:54:25 +02:00
feat: first draft ceil
This commit is contained in:
committed by
Mateusz Pusz
parent
e82bfa23a5
commit
b881b56480
@@ -183,4 +183,40 @@ template<Unit To, typename D, typename U, typename Rep>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Computes the smallest quantity with integer representation and unit type To with its number not less 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> ceil(const quantity<D, U, Rep>& q) noexcept
|
||||||
|
requires (!treat_as_floating_point<Rep>) ||
|
||||||
|
requires { ceil(q.number()); } ||
|
||||||
|
requires { std::ceil(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::ceil;
|
||||||
|
if constexpr(std::is_same_v<To, U>) {
|
||||||
|
return quantity<D, To, Rep>(ceil(q.number()));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return handle_signed_results(quantity<D, To, Rep>(ceil(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
|
} // namespace units
|
||||||
|
@@ -133,7 +133,7 @@ TEST_CASE("floor functions", "[floor]")
|
|||||||
SECTION ("floor 1.3 seconds with target unit second should be 1 second") {
|
SECTION ("floor 1.3 seconds with target unit second should be 1 second") {
|
||||||
REQUIRE(floor<si::second>(1.3_q_s) == 1_q_s);
|
REQUIRE(floor<si::second>(1.3_q_s) == 1_q_s);
|
||||||
}
|
}
|
||||||
SECTION ("floor -1.3 seconds with target unit second should be -1 second") {
|
SECTION ("floor -1.3 seconds with target unit second should be -2 seconds") {
|
||||||
REQUIRE(floor<si::second>(-1.3_q_s) == -2_q_s);
|
REQUIRE(floor<si::second>(-1.3_q_s) == -2_q_s);
|
||||||
}
|
}
|
||||||
SECTION ("floor 1001. milliseconds with target unit second should be 1 second") {
|
SECTION ("floor 1001. milliseconds with target unit second should be 1 second") {
|
||||||
@@ -150,6 +150,47 @@ TEST_CASE("floor functions", "[floor]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("ceil functions", "[ceil]")
|
||||||
|
{
|
||||||
|
SECTION ("ceil 1 second with target unit second should be 1 second") {
|
||||||
|
REQUIRE(ceil<si::second>(1_q_s) == 1_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil 1000 milliseconds with target unit second should be 1 second") {
|
||||||
|
REQUIRE(ceil<si::second>(1000_q_ms) == 1_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil 1001 milliseconds with target unit second should be 2 seconds") {
|
||||||
|
REQUIRE(ceil<si::second>(1001_q_ms) == 2_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil 1999 milliseconds with target unit second should be 2 seconds") {
|
||||||
|
REQUIRE(ceil<si::second>(1999_q_ms) == 2_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil -1000 milliseconds with target unit second should be -1 second") {
|
||||||
|
REQUIRE(ceil<si::second>(-1000_q_ms) == -1_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil -999 milliseconds with target unit second should be 0 seconds") {
|
||||||
|
REQUIRE(ceil<si::second>(-999_q_ms) == 0_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil 1.3 seconds with target unit second should be 2 seconds") {
|
||||||
|
REQUIRE(ceil<si::second>(1.3_q_s) == 2_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil -1.3 seconds with target unit second should be -1 second") {
|
||||||
|
REQUIRE(ceil<si::second>(-1.3_q_s) == -1_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil 1001. milliseconds with target unit second should be 2 seconds") {
|
||||||
|
REQUIRE(ceil<si::second>(1001._q_ms) == 2_q_s);
|
||||||
|
}
|
||||||
|
SECTION ("ceil 1999. milliseconds with target unit second should be 2 seconds") {
|
||||||
|
REQUIRE(ceil<si::second>(1999._q_ms) == 2_q_s);
|
||||||
|
}
|
||||||
|
// TODO does not work, probably due to a bug in fpow10() see #311
|
||||||
|
// SECTION ("ceil -1000. milliseconds with target unit second should be -1 second") {
|
||||||
|
// REQUIRE(ceil<si::second>(-1000._q_ms) == -1_q_s);
|
||||||
|
// }
|
||||||
|
SECTION ("ceil -999. milliseconds with target unit second should be 0 seconds") {
|
||||||
|
REQUIRE(ceil<si::second>(-999._q_ms) == 0_q_s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEMPLATE_TEST_CASE_SIG("pow<N>() implementation exponentiates values to power N", "[math][pow][exp]",
|
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)
|
(std::intmax_t N, N), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25)
|
||||||
{
|
{
|
||||||
|
@@ -77,6 +77,26 @@ 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>(1999._q_ms) == 1_q_s);
|
||||||
static_assert(floor<si::second>(-1000._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);
|
static_assert(floor<si::second>(-999._q_ms) == -1_q_s);
|
||||||
|
|
||||||
|
// ceil
|
||||||
|
// integral types
|
||||||
|
static_assert(compare<decltype(ceil<si::second>(1_q_s)), decltype(1_q_s)>);
|
||||||
|
|
||||||
|
static_assert(compare<decltype(ceil<si::second>(1000_q_ms)), decltype(1_q_s)>);
|
||||||
|
static_assert(compare<decltype(ceil<si::second>(1001_q_ms)), decltype(2_q_s)>);
|
||||||
|
static_assert(compare<decltype(ceil<si::second>(1999_q_ms)), decltype(2_q_s)>);
|
||||||
|
static_assert(compare<decltype(ceil<si::second>(-1000_q_ms)), decltype(-1_q_s)>);
|
||||||
|
static_assert(compare<decltype(ceil<si::second>(-999_q_ms)), decltype(0_q_s)>);
|
||||||
|
|
||||||
|
// floating-point
|
||||||
|
static_assert(ceil<si::second>(1.3_q_s) == 2_q_s);
|
||||||
|
static_assert(ceil<si::second>(-1.3_q_s) == -1_q_s);
|
||||||
|
|
||||||
|
static_assert(ceil<si::second>(1000._q_ms) == 1_q_s);
|
||||||
|
static_assert(ceil<si::second>(1001._q_ms) == 2_q_s);
|
||||||
|
static_assert(ceil<si::second>(1999._q_ms) == 2_q_s);
|
||||||
|
static_assert(ceil<si::second>(-1000._q_ms) == -1_q_s);
|
||||||
|
static_assert(ceil<si::second>(-999._q_ms) == 0_q_s);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
Reference in New Issue
Block a user