mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-05 13:14:29 +02:00
feat: hypot
support added
This commit is contained in:
@@ -157,9 +157,8 @@ public:
|
||||
using legs = std::vector<leg>;
|
||||
|
||||
template<std::ranges::input_range R>
|
||||
requires std::same_as<std::ranges::range_value_t<R>,
|
||||
waypoint> explicit task(const R& r) :
|
||||
waypoints_(std::ranges::begin(r), std::ranges::end(r))
|
||||
requires std::same_as<std::ranges::range_value_t<R>, waypoint>
|
||||
explicit task(const R& r) : waypoints_(std::ranges::begin(r), std::ranges::end(r))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -215,8 +214,7 @@ constexpr height agl(altitude glider_alt, altitude terrain_level) { return glide
|
||||
|
||||
inline units::isq::si::length<units::isq::si::kilometre> length_3d(distance dist, height h)
|
||||
{
|
||||
// TODO support for hypot(q, q)
|
||||
return sqrt(pow<2>(dist.common()) + pow<2>(h.common()));
|
||||
return hypot(dist.common(), h.common());
|
||||
}
|
||||
|
||||
distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt);
|
||||
|
@@ -301,4 +301,50 @@ template<Quantity To, std::same_as<typename To::dimension> D, typename U, std::s
|
||||
return ::units::round<typename To::unit>(q);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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]] inline std::common_type_t<Q1, Q2> hypot(const Q1& x, const Q2& y) noexcept
|
||||
requires requires { typename std::common_type_t<Q1, Q2>; } &&
|
||||
requires(std::common_type_t<Q1, Q2> q) {
|
||||
pow<2>(x);
|
||||
pow<2>(y);
|
||||
sqrt(pow<2>(x) + pow<2>(y));
|
||||
requires requires { hypot(q.number(), q.number()); } || requires { std::hypot(q.number(), q.number()); };
|
||||
}
|
||||
{
|
||||
using type = std::common_type_t<Q1, Q2>;
|
||||
type xx = x;
|
||||
type yy = y;
|
||||
using std::hypot;
|
||||
return type(hypot(xx.number(), yy.number()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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]] inline std::common_type_t<std::common_type_t<Q1, Q2>, Q3> hypot(const Q1& x, const Q2& y,
|
||||
const Q3& z) noexcept
|
||||
requires requires { typename std::common_type_t<std::common_type_t<Q1, Q2>, Q3>; } &&
|
||||
requires(std::common_type_t<std::common_type_t<Q1, Q2>, Q3> q) {
|
||||
pow<2>(x);
|
||||
pow<2>(y);
|
||||
pow<2>(z);
|
||||
sqrt(pow<2>(x) + pow<2>(y) + pow<2>(z));
|
||||
requires requires { hypot(q.number(), q.number(), q.number()); } ||
|
||||
requires { std::hypot(q.number(), q.number(), q.number()); };
|
||||
}
|
||||
{
|
||||
using type = std::common_type_t<std::common_type_t<Q1, Q2>, Q3>;
|
||||
type xx = x;
|
||||
type yy = y;
|
||||
type zz = z;
|
||||
using std::hypot;
|
||||
return type(hypot(xx.number(), yy.number(), zz.number()));
|
||||
}
|
||||
|
||||
} // namespace units
|
||||
|
Reference in New Issue
Block a user