From a8ca425e2d4b7b66b88d3961f06f0f1634d7b285 Mon Sep 17 00:00:00 2001 From: Mike Ford Date: Mon, 4 May 2020 15:34:57 +0100 Subject: [PATCH] Added absolute functions (abs, fabs) and epsilon. --- src/include/units/math.h | 63 ++++++++++++++++++---------- test/unit_test/runtime/math_test.cpp | 32 ++++++++++++++ 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/src/include/units/math.h b/src/include/units/math.h index b4d42429..745be47e 100644 --- a/src/include/units/math.h +++ b/src/include/units/math.h @@ -22,34 +22,53 @@ #pragma once +#include #include #include +#include namespace units { - template - requires (N == 0) - inline Rep pow(const quantity&) noexcept - { - return 1; - } +template +requires(N == 0) inline Rep pow(const quantity&) noexcept +{ + return 1; +} - template - inline Quantity AUTO pow(const quantity& q) noexcept - { - using dim = dimension_pow; - using ratio = ratio_pow; - using unit = downcast_unit; - return quantity(static_cast(std::pow(q.count(), N))); - } +template +inline Quantity AUTO pow(const quantity& q) noexcept +{ + using dim = dimension_pow; + using ratio = ratio_pow; + using unit = downcast_unit; + return quantity(static_cast(std::pow(q.count(), N))); +} - template - inline Quantity AUTO sqrt(const quantity& q) noexcept - { - using dim = dimension_sqrt; - using ratio = ratio_sqrt; - using unit = downcast_unit; - return quantity(static_cast(std::sqrt(q.count()))); - } +template +inline Quantity AUTO sqrt(const quantity& q) noexcept +{ + using dim = dimension_sqrt; + using ratio = ratio_sqrt; + using unit = downcast_unit; + return quantity(static_cast(std::sqrt(q.count()))); +} + +template +constexpr Quantity AUTO abs(const quantity& q) noexcept +{ + return quantity(std::abs(q.count())); +} + +template +constexpr Quantity AUTO fabs(const quantity& q) noexcept +{ + return quantity(std::fabs(q.count())); +} + +template +constexpr Quantity AUTO epsilon() noexcept +{ + return Q(std::numeric_limits::epsilon()); +} } // namespace units diff --git a/test/unit_test/runtime/math_test.cpp b/test/unit_test/runtime/math_test.cpp index 0123a174..74c3b48c 100644 --- a/test/unit_test/runtime/math_test.cpp +++ b/test/unit_test/runtime/math_test.cpp @@ -53,3 +53,35 @@ TEST_CASE("'sqrt()' on quantity changes the value and the dimension accordingly" { 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().count() == std::numeric_limits::epsilon()); + } + SECTION ("'epsilon' works as expected using integers") { + REQUIRE(epsilon().count() == std::numeric_limits::epsilon()); + } + SECTION ("'epsilon' works as expected using mixed Rep types") { + REQUIRE(epsilon().count() != std::numeric_limits::epsilon()); + } +}