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

417 lines
15 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
#include <mp-units/bits/external/hacks.h>
#include <mp-units/bits/value_cast.h>
#include <mp-units/quantity.h>
#include <mp-units/systems/angular/angular.h>
#include <mp-units/systems/isq/space_and_time.h>
#include <mp-units/systems/si/units.h>
#include <mp-units/unit.h>
2021-03-30 13:21:05 +02:00
// IWYU pragma: begin_exports
#include <cmath>
2021-03-30 13:21:05 +02:00
#include <cstdint>
// IWYU pragma: end_exports
#include <limits>
namespace mp_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]] constexpr 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;
return make_quantity<reference<pow<Num, Den>(Q::quantity_spec), pow<Num, Den>(Q::unit)>{}>(
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]] constexpr 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;
return make_quantity<reference<pow<1, 2>(Q::quantity_spec), pow<1, 2>(Q::unit)>{}>(
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]] constexpr 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;
return make_quantity<reference<pow<1, 3>(Q::quantity_spec), pow<1, 3>(Q::unit)>{}>(
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
*/
template<QuantityOf<dimension_one> Q>
[[nodiscard]] constexpr Q exp(const Q& q)
requires requires { exp(q.number()); } || requires { std::exp(q.number()); }
{
using std::exp;
return value_cast<Q::unit>(make_quantity<dimensionless[one]>(exp(value_cast<one>(q).number())));
}
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]] constexpr Q abs(const Q& q) noexcept
requires requires { abs(q.number()); } || requires { std::abs(q.number()); }
{
using std::abs;
return make_quantity<Q::reference>(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
{
return make_quantity<r>(std::numeric_limits<Rep>::epsilon());
}
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<get_quantity_spec(R), To>{}, Rep> floor(const quantity<R, Rep>& q) noexcept
requires((!treat_as_floating_point<Rep>) || requires { floor(q.number()); } ||
requires { std::floor(q.number()); }) &&
(To == get_unit(R) || requires {
::mp_units::value_cast<To>(q);
quantity<reference<get_quantity_spec(R), To>{}, Rep>::one();
2022-12-16 18:15:48 +01:00
})
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;
if constexpr (To == get_unit(R)) {
return make_quantity<reference<get_quantity_spec(R), To>{}>(floor(q.number()));
} else {
return handle_signed_results(
make_quantity<reference<get_quantity_spec(R), To>{}>(floor(value_cast<To>(q).number())));
2021-11-13 12:17:43 +01:00
}
} else {
if constexpr (To == get_unit(R)) {
return value_cast<To>(q);
} else {
return handle_signed_results(value_cast<To>(q));
2021-11-13 12:17:43 +01:00
}
}
}
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<get_quantity_spec(R), To>{}, Rep> ceil(const quantity<R, Rep>& q) noexcept
requires((!treat_as_floating_point<Rep>) || requires { ceil(q.number()); } || requires { std::ceil(q.number()); }) &&
(To == get_unit(R) || requires {
::mp_units::value_cast<To>(q);
quantity<reference<get_quantity_spec(R), To>{}, Rep>::one();
2022-12-16 18:15:48 +01:00
})
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;
if constexpr (To == get_unit(R)) {
return make_quantity<reference<get_quantity_spec(R), To>{}>(ceil(q.number()));
} else {
return handle_signed_results(
make_quantity<reference<get_quantity_spec(R), To>{}>(ceil(value_cast<To>(q).number())));
2021-11-13 12:51:04 +01:00
}
} else {
if constexpr (To == get_unit(R)) {
return value_cast<To>(q);
} else {
return handle_signed_results(value_cast<To>(q));
2021-11-13 12:51:04 +01:00
}
}
}
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<get_quantity_spec(R), To>{}, Rep> round(const quantity<R, Rep>& q) noexcept
requires((!treat_as_floating_point<Rep>) || requires { round(q.number()); } ||
requires { std::round(q.number()); }) &&
(To == get_unit(R) || requires {
::mp_units::floor<To>(q);
quantity<reference<get_quantity_spec(R), To>{}, Rep>::one();
2022-12-16 18:15:48 +01:00
})
2021-11-15 18:53:15 +01:00
{
if constexpr (To == get_unit(R)) {
if constexpr (treat_as_floating_point<Rep>) {
2021-11-15 18:53:15 +01:00
using std::round;
return make_quantity<reference<get_quantity_spec(R), To>{}>(round(q.number()));
} else {
return value_cast<To>(q);
2021-11-15 18:53:15 +01:00
}
} else {
const auto res_low = mp_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>
[[nodiscard]] constexpr QuantityOf<common_quantity_spec(Q1::quantity_spec, Q2::quantity_spec)> auto hypot(
const Q1& x, const Q2& y) noexcept
2022-12-16 18:15:48 +01:00
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 make_quantity<type::reference>(hypot(type{x}.number(), type{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>
[[nodiscard]] constexpr QuantityOf<common_quantity_spec(Q1::quantity_spec, Q2::quantity_spec, Q3::quantity_spec)> auto
hypot(const Q1& x, const Q2& y, const Q3& z) noexcept
2022-12-16 18:15:48 +01:00
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 make_quantity<type::reference>(hypot(type{x}.number(), type{y}.number(), type{z}.number()));
}
namespace isq {
template<QuantityOf<angular_measure> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<dimensionless> auto sin(const Q& q) noexcept
requires requires { sin(q.number()); } || requires { std::sin(q.number()); }
{
using std::sin;
return make_quantity<one>(sin(q[si::radian].number()));
}
template<QuantityOf<angular_measure> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<dimensionless> auto cos(const Q& q) noexcept
requires requires { cos(q.number()); } || requires { std::cos(q.number()); }
{
using std::cos;
return make_quantity<one>(cos(q[si::radian].number()));
}
template<QuantityOf<angular_measure> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<dimensionless> auto tan(const Q& q) noexcept
requires requires { tan(q.number()); } || requires { std::tan(q.number()); }
{
using std::tan;
return make_quantity<one>(tan(q[si::radian].number()));
}
template<QuantityOf<dimension_one> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<angular_measure> auto asin(const Q& q) noexcept
requires requires { asin(q.number()); } || requires { std::asin(q.number()); }
{
using std::asin;
return make_quantity<angular_measure[si::radian]>(asin(value_cast<one>(q).number()));
}
template<QuantityOf<dimension_one> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<angular_measure> auto acos(const Q& q) noexcept
requires requires { acos(q.number()); } || requires { std::acos(q.number()); }
{
using std::acos;
return make_quantity<angular_measure[si::radian]>(acos(value_cast<one>(q).number()));
}
template<QuantityOf<dimension_one> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<angular_measure> auto atan(const Q& q) noexcept
requires requires { atan(q.number()); } || requires { std::atan(q.number()); }
{
using std::atan;
return make_quantity<angular_measure[si::radian]>(atan(value_cast<one>(q).number()));
}
} // namespace isq
namespace angular {
template<QuantityOf<angle> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<dimensionless> auto sin(const Q& q) noexcept
requires requires { sin(q.number()); } || requires { std::sin(q.number()); }
{
using std::sin;
return sin(q[radian].number()) * one;
}
template<QuantityOf<angle> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<dimensionless> auto cos(const Q& q) noexcept
requires requires { cos(q.number()); } || requires { std::cos(q.number()); }
{
using std::cos;
return cos(q[radian].number()) * one;
}
template<QuantityOf<angle> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<dimensionless> auto tan(const Q& q) noexcept
requires requires { tan(q.number()); } || requires { std::tan(q.number()); }
{
using std::tan;
return tan(q[radian].number()) * one;
}
template<QuantityOf<dimension_one> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<angle> auto asin(const Q& q) noexcept
requires requires { asin(q.number()); } || requires { std::asin(q.number()); }
{
using std::asin;
return asin(value_cast<one>(q).number()) * angle[radian];
}
template<QuantityOf<dimension_one> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<angle> auto acos(const Q& q) noexcept
requires requires { acos(q.number()); } || requires { std::acos(q.number()); }
{
using std::acos;
return acos(value_cast<one>(q).number()) * angle[radian];
}
template<QuantityOf<dimension_one> Q>
requires treat_as_floating_point<typename Q::rep>
[[nodiscard]] inline QuantityOf<angle> auto atan(const Q& q) noexcept
requires requires { atan(q.number()); } || requires { std::atan(q.number()); }
{
using std::atan;
return atan(value_cast<one>(q).number()) * angle[radian];
}
} // namespace angular
} // namespace mp_units