Files
mp-units/src/core/include/units/math.h
T

297 lines
10 KiB
C++
Raw Normal View History

// 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
2021-03-30 13:21:05 +02:00
#include <units/bits/external/hacks.h>
#include <units/quantity.h>
2021-03-30 13:21:05 +02:00
#include <units/unit.h>
// IWYU pragma: begin_exports
#include <cmath>
2021-03-30 13:21:05 +02:00
#include <cstdint>
// IWYU pragma: end_exports
#include <limits>
namespace units {
2020-05-13 15:33:29 +02:00
/**
2022-12-16 18:15:48 +01:00
* @brief Computes the value of a quantity raised to the `Num/Den` power
2020-10-17 20:50:58 +01:00
*
2022-12-16 18:15:48 +01:00
* Both the quantity value and its quantity specification are the base of the operation.
2020-10-17 20:50:58 +01:00
*
* @tparam Num Exponent numerator
* @tparam Den Exponent denominator
2020-05-13 15:33:29 +02:00
* @param q Quantity being the base of the operation
2020-10-17 20:50:58 +01:00
* @return Quantity The result of computation
2020-05-13 15:33:29 +02:00
*/
2020-10-17 20:50:58 +01:00
template<std::intmax_t Num, std::intmax_t Den = 1, Quantity Q>
2020-11-02 13:28:13 +00:00
requires detail::non_zero<Den>
[[nodiscard]] inline auto pow(const Q& q) noexcept
requires requires { pow(q.number(), 1.0); } || requires { std::pow(q.number(), 1.0); }
{
2020-09-04 23:00:57 +02:00
using rep = TYPENAME Q::rep;
2020-10-17 20:50:58 +01:00
if constexpr (Num == 0) {
return rep(1);
2022-12-16 18:15:48 +01:00
} else if constexpr (ratio{Num, Den} == 1) {
return q;
2020-10-17 20:50:58 +01:00
} else {
using std::pow;
2022-12-16 18:15:48 +01:00
return quantity<reference<pow<Num, Den>(Q::quantity_spec), pow<Num, Den>(Q::unit)>{}, rep>(
static_cast<rep>(pow(q.number(), static_cast<double>(Num) / static_cast<double>(Den))));
}
2020-05-13 15:33:29 +02:00
}
/**
* @brief Computes the square root of a quantity
2020-10-17 20:50:58 +01:00
*
2022-12-16 18:15:48 +01:00
* Both the quantity value and its quantity specification are the base of the operation.
2020-10-17 20:50:58 +01:00
*
2020-05-13 15:33:29 +02:00
* @param q Quantity being the base of the operation
2020-10-17 20:50:58 +01:00
* @return Quantity The result of computation
2020-05-13 15:33:29 +02:00
*/
template<Quantity Q>
[[nodiscard]] inline Quantity auto sqrt(const Q& q) noexcept
requires requires { sqrt(q.number()); } || requires { std::sqrt(q.number()); }
{
2020-09-04 23:00:57 +02:00
using rep = TYPENAME Q::rep;
using std::sqrt;
2022-12-16 18:15:48 +01:00
return quantity<reference<pow<1, 2>(Q::quantity_spec), pow<1, 2>(Q::unit)>{}, rep>(
static_cast<rep>(sqrt(q.number())));
}
2020-10-17 20:50:58 +01:00
/**
* @brief Computes the cubic root of a quantity
*
2022-12-16 18:15:48 +01:00
* Both the quantity value and its quantity specification are the base of the operation.
2020-10-17 20:50:58 +01:00
*
* @param q Quantity being the base of the operation
* @return Quantity The result of computation
*/
template<Quantity Q>
[[nodiscard]] inline Quantity auto cbrt(const Q& q) noexcept
requires requires { cbrt(q.number()); } || requires { std::cbrt(q.number()); }
2020-10-17 20:50:58 +01:00
{
using rep = TYPENAME Q::rep;
using std::cbrt;
2022-12-16 18:15:48 +01:00
return quantity<reference<pow<1, 3>(Q::quantity_spec), pow<1, 3>(Q::unit)>{}, rep>(
static_cast<rep>(cbrt(q.number())));
2020-10-17 20:50:58 +01:00
}
/**
* @brief Computes Euler's raised to the given power
2021-11-13 12:17:43 +01:00
*
* @note Such an operation has sense only for a dimensionless quantity.
2021-11-13 12:17:43 +01:00
*
* @param q Quantity being the base of the operation
* @return Quantity The value of the same quantity type
*/
2022-12-16 18:15:48 +01:00
template<quantity_of<dimensionless> Q, typename Rep>
[[nodiscard]] inline Q exp(const Q& q)
requires requires { exp(q.number()); } || requires { std::exp(q.number()); }
{
using std::exp;
2022-12-16 18:15:48 +01:00
return quantity_cast<Q::unit>(exp(quantity_cast<one>(q).number()) * dimensionless[one]);
}
2020-05-13 15:33:29 +02:00
/**
* @brief Computes the absolute value of a quantity
2021-11-13 12:17:43 +01:00
*
2020-05-13 15:33:29 +02:00
* @param q Quantity being the base of the operation
* @return Quantity The absolute value of a provided quantity
*/
2022-12-16 18:15:48 +01:00
template<Quantity Q>
[[nodiscard]] inline Q abs(const Q& q) noexcept
requires requires { abs(q.number()); } || requires { std::abs(q.number()); }
{
using std::abs;
2022-12-16 18:15:48 +01:00
return Q(abs(q.number()));
}
2020-05-13 15:33:29 +02:00
/**
* @brief Returns the epsilon of the quantity
2021-11-13 12:17:43 +01:00
*
2020-05-13 15:33:29 +02:00
* The returned value is defined by a <tt>std::numeric_limits<typename Q::rep>::epsilon()</tt>.
2021-11-13 12:17:43 +01:00
*
2020-05-13 15:33:29 +02:00
* @tparam Q Quantity type being the base of the operation
* @return Quantity The epsilon value for quantity's representation type
*/
2022-12-16 18:15:48 +01:00
template<Representation Rep, Reference R>
requires requires { std::numeric_limits<Rep>::epsilon(); }
[[nodiscard]] constexpr Quantity auto epsilon(R r) noexcept
{
2022-12-16 18:15:48 +01:00
return std::numeric_limits<Rep>::epsilon() * r;
}
2021-11-13 12:17:43 +01:00
/**
* @brief Computes the largest quantity with integer representation and unit type To with its number not greater than q
*
* @tparam q Quantity being the base of the operation
* @return Quantity The rounded quantity with unit type To
*/
2022-12-16 18:15:48 +01:00
template<Unit auto To, auto R, typename Rep>
[[nodiscard]] constexpr quantity<reference<R.quantity_spec, To>{}, Rep> floor(const quantity<R, Rep>& q) noexcept
requires((!treat_as_floating_point<Rep>) || requires { floor(q.number()); } ||
requires { std::floor(q.number()); }) &&
2022-12-16 18:15:48 +01:00
(To == R.unit || requires {
::units::quantity_cast<To>(q);
quantity<reference<R.quantity_spec, To>{}, Rep>::one();
})
2021-11-13 12:17:43 +01:00
{
const auto handle_signed_results = [&]<typename T>(const T& res) {
2021-11-15 18:53:15 +01:00
if (res > q) {
2021-11-13 12:17:43 +01:00
return res - T::one();
2021-11-15 18:53:15 +01:00
}
2021-11-13 12:17:43 +01:00
return res;
};
if constexpr (treat_as_floating_point<Rep>) {
2021-11-13 12:17:43 +01:00
using std::floor;
2022-12-16 18:15:48 +01:00
if constexpr (To == R.unit) {
return quantity<reference<R.quantity_spec, To>{}, Rep>(floor(q.number()));
} else {
2022-12-16 18:15:48 +01:00
return handle_signed_results(
quantity<reference<R.quantity_spec, To>{}, Rep>(floor(quantity_cast<To>(q).number())));
2021-11-13 12:17:43 +01:00
}
} else {
2022-12-16 18:15:48 +01:00
if constexpr (To == R.unit) {
return quantity_cast<To>(q);
} else {
2021-11-13 12:17:43 +01:00
return handle_signed_results(quantity_cast<To>(q));
}
}
}
2021-11-13 12:51:04 +01:00
/**
* @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
*/
2022-12-16 18:15:48 +01:00
template<Unit auto To, auto R, typename Rep>
[[nodiscard]] constexpr quantity<reference<R.quantity_spec, To>{}, Rep> ceil(const quantity<R, Rep>& q) noexcept
requires((!treat_as_floating_point<Rep>) || requires { ceil(q.number()); } || requires { std::ceil(q.number()); }) &&
2022-12-16 18:15:48 +01:00
(To == R.unit || requires {
::units::quantity_cast<To>(q);
quantity<reference<R.quantity_spec, To>{}, Rep>::one();
})
2021-11-13 12:51:04 +01:00
{
const auto handle_signed_results = [&]<typename T>(const T& res) {
2021-11-15 18:53:15 +01:00
if (res < q) {
2021-11-13 12:51:04 +01:00
return res + T::one();
2021-11-15 18:53:15 +01:00
}
2021-11-13 12:51:04 +01:00
return res;
};
if constexpr (treat_as_floating_point<Rep>) {
2021-11-13 12:51:04 +01:00
using std::ceil;
2022-12-16 18:15:48 +01:00
if constexpr (To == R.unit) {
return quantity<reference<R.quantity_spec, To>{}, Rep>(ceil(q.number()));
} else {
2022-12-16 18:15:48 +01:00
return handle_signed_results(
quantity<reference<R.quantity_spec, To>{}, Rep>(ceil(quantity_cast<To>(q).number())));
2021-11-13 12:51:04 +01:00
}
} else {
2022-12-16 18:15:48 +01:00
if constexpr (To == R.unit) {
return quantity_cast<To>(q);
} else {
2021-11-13 12:51:04 +01:00
return handle_signed_results(quantity_cast<To>(q));
}
}
}
2021-11-15 18:53:15 +01:00
/**
* @brief Computes the nearest quantity with integer representation and unit type To to q
*
* Rounding halfway cases away from zero, regardless of the current rounding mode.
*
* @tparam q Quantity being the base of the operation
* @return Quantity The rounded quantity with unit type To
*/
2022-12-16 18:15:48 +01:00
template<Unit auto To, auto R, typename Rep>
[[nodiscard]] constexpr quantity<reference<R.quantity_spec, To>{}, Rep> round(const quantity<R, Rep>& q) noexcept
requires((!treat_as_floating_point<Rep>) || requires { round(q.number()); } ||
requires { std::round(q.number()); }) &&
2022-12-16 18:15:48 +01:00
(To == R.unit || requires {
::units::floor<To>(q);
quantity<reference<R.quantity_spec, To>{}, Rep>::one();
})
2021-11-15 18:53:15 +01:00
{
2022-12-16 18:15:48 +01:00
if constexpr (To == R.unit) {
if constexpr (treat_as_floating_point<Rep>) {
2021-11-15 18:53:15 +01:00
using std::round;
2022-12-16 18:15:48 +01:00
return quantity<reference<R.quantity_spec, To>{}, Rep>(round(q.number()));
} else {
2022-12-16 18:15:48 +01:00
return quantity_cast<To>(q);
2021-11-15 18:53:15 +01:00
}
} else {
2021-11-15 18:53:15 +01:00
const auto res_low = units::floor<To>(q);
2022-12-16 18:15:48 +01:00
const auto res_high = res_low + res_low.one();
2021-11-15 18:53:15 +01:00
const auto diff0 = q - res_low;
const auto diff1 = res_high - q;
if (diff0 == diff1) {
if (static_cast<int>(res_low.number()) & 1) {
return res_high;
}
return res_low;
}
if (diff0 < diff1) {
return res_low;
}
return res_high;
}
}
2022-08-03 12:29:49 +02:00
/**
* @brief Computes the square root of the sum of the squares of x and y,
* without undue overflow or underflow at intermediate stages of the computation
*/
template<Quantity Q1, Quantity Q2>
2022-12-16 18:15:48 +01:00
[[nodiscard]] inline quantity_of<common_reference(Q1::reference, Q2::reference)> auto hypot(const Q1& x,
const Q2& y) noexcept
requires requires { common_reference(Q1::reference, Q2::reference); } &&
(
requires { hypot(x.number(), y.number()); } || requires { std::hypot(x.number(), y.number()); })
2022-08-03 12:29:49 +02:00
{
using std::hypot;
2022-12-16 18:15:48 +01:00
using type = quantity<common_reference(Q1::reference, Q2::reference), decltype(hypot(x.number(), y.number()))>;
return type{hypot(x.number(), y.number())};
2022-08-03 12:29:49 +02:00
}
/**
* @brief Computes the square root of the sum of the squares of x, y, and z,
* without undue overflow or underflow at intermediate stages of the computation
*/
template<Quantity Q1, Quantity Q2, Quantity Q3>
2022-12-16 18:15:48 +01:00
[[nodiscard]] inline quantity_of<common_reference(Q1::reference, Q2::reference, Q3::reference)> auto hypot(
const Q1& x, const Q2& y, const Q3& z) noexcept
requires requires { common_reference(Q1::reference, Q2::reference, Q3::reference); } &&
(
requires { hypot(x.number(), y.number(), z.number()); } ||
requires { std::hypot(x.number(), y.number(), z.number()); })
2022-08-03 12:29:49 +02:00
{
using std::hypot;
2022-12-16 18:15:48 +01:00
using type = quantity<common_reference(Q1::reference, Q2::reference, Q3::reference),
decltype(hypot(x.number(), y.number(), z.number()))>;
return type{hypot(x.number(), y.number(), z.number())};
}
} // namespace units