forked from mpusz/mp-units
Added absolute functions (abs, fabs) and epsilon.
This commit is contained in:
@@ -22,14 +22,15 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <units/concepts.h>
|
||||||
#include <units/quantity.h>
|
#include <units/quantity.h>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
template<std::intmax_t N, typename D, typename U, typename Rep>
|
template<std::intmax_t N, typename D, typename U, typename Rep>
|
||||||
requires (N == 0)
|
requires(N == 0) inline Rep pow(const quantity<D, U, Rep>&) noexcept
|
||||||
inline Rep pow(const quantity<D, U, Rep>&) noexcept
|
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -52,4 +53,22 @@ namespace units {
|
|||||||
return quantity<dim, unit, Rep>(static_cast<Rep>(std::sqrt(q.count())));
|
return quantity<dim, unit, Rep>(static_cast<Rep>(std::sqrt(q.count())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename D, typename U, typename Rep>
|
||||||
|
constexpr Quantity AUTO abs(const quantity<D, U, Rep>& q) noexcept
|
||||||
|
{
|
||||||
|
return quantity<D, U, Rep>(std::abs(q.count()));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename D, typename U, typename Rep>
|
||||||
|
constexpr Quantity AUTO fabs(const quantity<D, U, Rep>& q) noexcept
|
||||||
|
{
|
||||||
|
return quantity<D, U, Rep>(std::fabs(q.count()));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<Quantity Q>
|
||||||
|
constexpr Quantity AUTO epsilon() noexcept
|
||||||
|
{
|
||||||
|
return Q(std::numeric_limits<typename Q::rep>::epsilon());
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace units
|
} // namespace units
|
||||||
|
@@ -53,3 +53,35 @@ TEST_CASE("'sqrt()' on quantity changes the value and the dimension accordingly"
|
|||||||
{
|
{
|
||||||
REQUIRE(sqrt(4q_m2) == 2q_m);
|
REQUIRE(sqrt(4q_m2) == 2q_m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("absolute functions on quantity returns the absolute value", "[math][abs][fabs]")
|
||||||
|
{
|
||||||
|
SECTION ("'abs()' on a negative quantity returns the abs") {
|
||||||
|
REQUIRE(abs(-1q_m) == 1q_m);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION ("'abs()' on a positive quantity returns the abs") {
|
||||||
|
REQUIRE(abs(1q_m) == 1q_m);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION ("'fabs()' on a negative quantity returns the abs") {
|
||||||
|
REQUIRE(fabs(-1.q_m) == 1.q_m);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION ("'fabs()' on a positive quantity returns the abs") {
|
||||||
|
REQUIRE(fabs(1.q_m) == 1.q_m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("numeric_limits functions", "[limits]")
|
||||||
|
{
|
||||||
|
SECTION ("'epsilon' works as expected using default floating type") {
|
||||||
|
REQUIRE(epsilon<decltype(1.q_m)>().count() == std::numeric_limits<decltype(1.q_m)::rep>::epsilon());
|
||||||
|
}
|
||||||
|
SECTION ("'epsilon' works as expected using integers") {
|
||||||
|
REQUIRE(epsilon<decltype(1q_m)>().count() == std::numeric_limits<decltype(1q_m)::rep>::epsilon());
|
||||||
|
}
|
||||||
|
SECTION ("'epsilon' works as expected using mixed Rep types") {
|
||||||
|
REQUIRE(epsilon<decltype(1q_m)>().count() != std::numeric_limits<decltype(1.q_m)::rep>::epsilon());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user