Skip to content

Commit

Permalink
feat: abs(quantity) exposed for conforming freestanding implementat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
mpusz committed Nov 29, 2024
1 parent 51bc3ea commit 0cc26f4
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/core/include/mp-units/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#pragma once

#include <mp-units/bits/requires_hosted.h>
//
#include <mp-units/bits/module_macros.h>
#include <mp-units/framework/customization_points.h>
#include <mp-units/framework/quantity.h>
Expand All @@ -35,14 +33,38 @@
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <limits>
#endif
#endif
#if MP_UNITS_HOSTED
#include <cmath>
#endif // MP_UNITS_HOSTED
#endif // MP_UNITS_IMPORT_STD
#endif // MP_UNITS_IN_MODULE_INTERFACE

MP_UNITS_EXPORT
namespace mp_units {

#if MP_UNITS_HOSTED || __cpp_lib_freestanding_cstdlib >= 202306L

/**
* @brief Computes the absolute value of a quantity
*
* @param q Quantity being the base of the operation
* @return Quantity The absolute value of a provided quantity
*/
template<auto R, typename Rep>
requires requires(Rep v) { abs(v); } || requires(Rep v) { std::abs(v); }
[[nodiscard]] constexpr quantity<R, Rep> abs(const quantity<R, Rep>& q) noexcept
{
using std::abs;
return {static_cast<Rep>(abs(q.numerical_value_ref_in(q.unit))), R};
}

#endif

#if MP_UNITS_HOSTED

/**
* @brief Computes the value of a quantity raised to the `Num/Den` power
*
Expand Down Expand Up @@ -122,20 +144,6 @@ template<ReferenceOf<dimensionless> auto R, typename Rep>
quantity{static_cast<Rep>(exp(q.force_numerical_value_in(q.unit))), detail::clone_reference_with<one>(R)});
}

/**
* @brief Computes the absolute value of a quantity
*
* @param q Quantity being the base of the operation
* @return Quantity The absolute value of a provided quantity
*/
template<auto R, typename Rep>
requires requires(Rep v) { abs(v); } || requires(Rep v) { std::abs(v); }
[[nodiscard]] constexpr quantity<R, Rep> abs(const quantity<R, Rep>& q) noexcept
{
using std::abs;
return {static_cast<Rep>(abs(q.numerical_value_ref_in(q.unit))), R};
}

/**
* @brief Determines if a quantity is finite.
*
Expand Down Expand Up @@ -481,4 +489,6 @@ template<auto R1, typename Rep1, auto R2, typename Rep2, auto R3, typename Rep3>
return quantity{hypot(x.numerical_value_in(unit), y.numerical_value_in(unit), z.numerical_value_in(unit)), ref};
}

#endif // MP_UNITS_HOSTED

} // namespace mp_units

0 comments on commit 0cc26f4

Please sign in to comment.