90 lines · plain
1// (C) Copyright Matt Borland 2022.2// Use, modification and distribution are subject to the3// Boost Software License, Version 1.0. (See accompanying file4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_CCMATH_FMIN_HPP7#define BOOST_MATH_CCMATH_FMIN_HPP8 9#include <boost/math/ccmath/detail/config.hpp>10 11#ifdef BOOST_MATH_NO_CCMATH12#error "The header <boost/math/fmin.hpp> can only be used in C++17 and later."13#endif14 15#include <boost/math/tools/promotion.hpp>16#include <boost/math/ccmath/isnan.hpp>17 18namespace boost::math::ccmath {19 20namespace detail {21 22template <typename T>23constexpr T fmin_impl(const T x, const T y) noexcept24{25 if (x < y)26 {27 return x;28 }29 else30 {31 return y;32 }33}34 35} // Namespace detail36 37template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>38constexpr Real fmin(Real x, Real y) noexcept39{40 if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))41 {42 if (boost::math::ccmath::isnan(x))43 {44 return y;45 }46 else if (boost::math::ccmath::isnan(y))47 {48 return x;49 }50 51 return boost::math::ccmath::detail::fmin_impl(x, y);52 }53 else54 {55 using std::fmin;56 return fmin(x, y);57 }58}59 60template <typename T1, typename T2>61constexpr auto fmin(T1 x, T2 y) noexcept62{63 if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))64 {65 using promoted_type = boost::math::tools::promote_args_t<T1, T2>;66 return boost::math::ccmath::fmin(static_cast<promoted_type>(x), static_cast<promoted_type>(y));67 }68 else69 {70 using std::fmin;71 return fmin(x, y);72 }73}74 75constexpr float fminf(float x, float y) noexcept76{77 return boost::math::ccmath::fmin(x, y);78}79 80#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS81constexpr long double fminl(long double x, long double y) noexcept82{83 return boost::math::ccmath::fmin(x, y);84}85#endif86 87} // Namespace boost::math::ccmath88 89#endif // BOOST_MATH_CCMATH_FMIN_HPP90