refactor: dead ratio code removed

This commit is contained in:
Mateusz Pusz
2022-09-06 23:45:47 +02:00
parent 1238cc17c2
commit d0eceefe80
7 changed files with 0 additions and 249 deletions

View File

@@ -25,7 +25,6 @@
#include <gsl/gsl-lite.hpp>
#include <units/bits/external/hacks.h>
#include <units/bits/math_concepts.h>
#include <units/bits/pow.h>
#include <units/bits/ratio_maths.h>
namespace units::detail {

View File

@@ -1,46 +0,0 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <cassert>
#include <cstdint>
namespace units::detail {
template<std::intmax_t N, typename T>
constexpr T pow_impl(const T& v) noexcept
{
if constexpr (N == 0) {
return T(1);
} else if constexpr (N == 1) {
return v;
} else if constexpr (N < 0) {
return 1 / pow_impl<-N>(v);
} else if constexpr (N % 2 == 0) { // even
return pow_impl<N / 2>(v * v);
} else { // odd
return v * pow_impl<(N - 1) / 2>(v * v);
}
}
} // namespace units::detail

View File

@@ -1,99 +0,0 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <gsl/gsl-lite.hpp>
#include <units/bits/constexpr_math.h>
#include <units/bits/math_concepts.h>
#include <units/bits/pow.h>
#include <units/bits/ratio_maths.h>
#include <cmath>
namespace units::detail {
template<std::intmax_t N, typename F>
requires gt_zero<N>
[[nodiscard]] constexpr std::intmax_t iroot_impl(std::intmax_t v, const F& pow_function) noexcept
{
if constexpr (N == 1) {
return v;
} else {
gsl_Expects(v >= 0);
if (v == 0) {
return 0;
}
constexpr double exponent = 1.0 / static_cast<double>(N);
const auto root = static_cast<std::intmax_t>(pow_function(static_cast<double>(v), exponent));
// integer roots may be truncated down by 1 or in even rarer cases up by 1 due to finite precision of pow and
// exponent, check both cases
if (v == pow_impl<N>(root + 1)) {
return root + 1;
}
if (v < pow_impl<N>(root)) {
return root - 1;
}
return root;
}
}
// maximum v is std::numeric_limits<std::intmax_t>::max() which is the worst case for exp convergence
// ExpOrder = 12 and Factor = 64 give a precision of about O(1e-15) for a wide range of 1 / N exponents
// Factor = 32 needs quite a few more terms to converge
// https://godbolt.org/z/odWq1o
template<std::intmax_t N, std::size_t ExpOrder = 12, std::intmax_t Factor = 64>
requires gt_zero<N>
[[nodiscard]] constexpr std::intmax_t iroot_compile(std::intmax_t v) noexcept
{
return iroot_impl<N>(v, [](double x, double exponent) { return constexpr_pow<ExpOrder, Factor>(x, exponent); });
}
template<std::intmax_t N>
requires gt_zero<N>
[[nodiscard]] std::intmax_t iroot_runtime(std::intmax_t v) noexcept
{
return iroot_impl<N>(v, [](double x, [[maybe_unused]] double exponent) {
if constexpr (N == 2) {
return std::sqrt(x);
} else if constexpr (N == 3) {
return std::cbrt(x);
} else {
return std::pow(x, exponent);
}
});
}
template<std::intmax_t N>
requires gt_zero<N>
[[nodiscard]] constexpr std::intmax_t iroot(std::intmax_t v) noexcept
{
// compile time version is much slower, use faster version at runtime
if (std::is_constant_evaluated()) {
return iroot_compile<N>(v);
}
return iroot_runtime<N>(v);
}
} // namespace units::detail

View File

@@ -24,7 +24,6 @@
#include <units/bits/dimension_op.h>
#include <units/bits/external/type_traits.h>
#include <units/bits/pow.h>
#include <units/concepts.h>
#include <units/customization_points.h>
#include <units/magnitude.h>

View File

@@ -24,9 +24,7 @@
// IWYU pragma: begin_exports
#include <units/bits/math_concepts.h>
#include <units/bits/pow.h>
#include <units/bits/ratio_maths.h>
#include <units/bits/root.h>
#include <cstdint>
// IWYU pragma: end_exports
@@ -84,24 +82,6 @@ struct ratio {
[[nodiscard]] constexpr bool is_integral(const ratio& r) { return r.num % r.den == 0; }
template<std::intmax_t Num>
[[nodiscard]] constexpr ratio pow(const ratio& r)
{
if constexpr (Num == 0) {
return ratio(1);
} else if constexpr (Num == 1) {
return r;
} else {
const ratio result = detail::pow_impl<Num>(r);
if constexpr (Num < 0) { // account for negative exponent
return inverse(result);
} else {
return result;
}
}
}
// common_ratio
[[nodiscard]] constexpr ratio common_ratio(const ratio& r1, const ratio& r2)
{

View File

@@ -294,79 +294,6 @@ TEST_CASE("round functions", "[round]")
}
}
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)
{
auto v = GENERATE(range(0.5, 20.0, 0.5));
REQUIRE(detail::pow_impl<N>(v) == Catch::Approx(std::pow(v, N)).epsilon(1e-15).margin(0));
}
template<std::intmax_t N>
struct Pow {
constexpr static std::intmax_t exponent = N;
template<typename T>
[[nodiscard]] constexpr static auto pow(const T& v) noexcept
{
return detail::pow_impl<N>(v);
}
};
template<std::intmax_t N>
struct CompileRoot : Pow<N> {
[[nodiscard]] constexpr static std::intmax_t root(std::intmax_t v) noexcept { return detail::iroot_compile<N>(v); }
};
template<std::intmax_t N>
struct RuntimeRoot : Pow<N> {
[[nodiscard]] static std::intmax_t root(std::intmax_t v) noexcept { return detail::iroot_runtime<N>(v); }
};
// test to make sure precision is not lost when rounding what should be integer roots
template<typename TestType>
static void root_test()
{
SECTION("Roots are truncated down")
{
auto base = GENERATE(range(1.0, 10.0, 1.0)); // doubles to guard against overflow
if (TestType::pow(base) < static_cast<double>(std::numeric_limits<std::intmax_t>::max())) {
const std::intmax_t x = TestType::pow(static_cast<std::intmax_t>(base));
const auto expect = static_cast<std::intmax_t>(base);
REQUIRE(TestType::root(x - 1) == expect - 1);
REQUIRE(TestType::root(x) == expect);
}
}
SECTION("Roots are truncated correctly for very large inputs")
{
auto exponent = GENERATE(range(10, std::numeric_limits<std::intmax_t>::digits10, 1));
const auto large_val = static_cast<std::intmax_t>(std::pow(10, exponent));
const auto expected = static_cast<std::intmax_t>(std::pow(10, exponent / static_cast<double>(TestType::exponent)));
REQUIRE(TestType::root(large_val) == expected);
}
}
/* Catch2 uses int for indexing in TEMPLATE_PRODUCT_TEST_CASE_SIG so it does not compile with -Werror=sign-conversion
* https://github.com/catchorg/Catch2/pull/2074
TEMPLATE_PRODUCT_TEST_CASE_SIG("detail::iroot<N>()", "[math][pow][iroot]", (std::intmax_t N, N),
(CompileRoot, RuntimeRoot), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25))
{
root_test<TestType>();
}
*/
#define ROOT_TEST_CASE(Type) \
TEMPLATE_TEST_CASE_SIG("detail::iroot<N>() - " #Type, "[math][pow][iroot]", (std::intmax_t N, N), 1, 2, 3, 4, 5, 6, \
7, 8, 9, 10, 15, 20, 25) \
{ \
root_test<Type<N>>(); \
}
ROOT_TEST_CASE(CompileRoot)
ROOT_TEST_CASE(RuntimeRoot)
TEST_CASE("hypot functions", "[hypot]")
{
using namespace units::aliases::isq::si;

View File

@@ -46,15 +46,6 @@ static_assert(ratio(2) / ratio(8) == ratio(1, 4));
static_assert(ratio(1, 8) / ratio(2) == ratio(1, 16));
static_assert(ratio(6) / ratio(3) == ratio(2));
static_assert(pow<0>(ratio(2)) == ratio(1));
static_assert(pow<1>(ratio(2)) == ratio(2));
static_assert(pow<2>(ratio(2)) == ratio(4));
static_assert(pow<3>(ratio(2)) == ratio(8));
static_assert(pow<0>(ratio(1, 2)) == ratio(1));
static_assert(pow<1>(ratio(1, 2)) == ratio(1, 2));
static_assert(pow<2>(ratio(1, 2)) == ratio(1, 4));
static_assert(pow<3>(ratio(1, 2)) == ratio(1, 8));
// common_ratio
static_assert(common_ratio(ratio(1), ratio(1000)) == ratio(1));
static_assert(common_ratio(ratio(1000), ratio(1)) == ratio(1));