feat: first draft ceil

This commit is contained in:
Markus Hofbauer authored and Mateusz Pusz committed 2021-11-13 16:50:55 +01:00
1 parent e82bfa23a5
commit b881b56480
3 files changed
+98 -1

No files matched your search

+36
View File
@@ -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