modf.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // (C) Copyright Matt Borland 2021.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_CCMATH_MODF_HPP
  6. #define BOOST_MATH_CCMATH_MODF_HPP
  7. #include <boost/math/ccmath/detail/config.hpp>
  8. #ifdef BOOST_MATH_NO_CCMATH
  9. #error "The header <boost/math/modf.hpp> can only be used in C++17 and later."
  10. #endif
  11. #include <boost/math/ccmath/abs.hpp>
  12. #include <boost/math/ccmath/isinf.hpp>
  13. #include <boost/math/ccmath/isnan.hpp>
  14. #include <boost/math/ccmath/trunc.hpp>
  15. namespace boost::math::ccmath {
  16. namespace detail {
  17. template <typename Real>
  18. inline constexpr Real modf_error_impl(Real x, Real* iptr)
  19. {
  20. *iptr = x;
  21. return boost::math::ccmath::abs(x) == Real(0) ? x :
  22. x > Real(0) ? Real(0) : -Real(0);
  23. }
  24. template <typename Real>
  25. inline constexpr Real modf_nan_impl(Real x, Real* iptr)
  26. {
  27. *iptr = x;
  28. return x;
  29. }
  30. template <typename Real>
  31. inline constexpr Real modf_impl(Real x, Real* iptr)
  32. {
  33. *iptr = boost::math::ccmath::trunc(x);
  34. return (x - *iptr);
  35. }
  36. } // Namespace detail
  37. template <typename Real>
  38. inline constexpr Real modf(Real x, Real* iptr)
  39. {
  40. if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  41. {
  42. return boost::math::ccmath::abs(x) == Real(0) ? detail::modf_error_impl(x, iptr) :
  43. boost::math::ccmath::isinf(x) ? detail::modf_error_impl(x, iptr) :
  44. boost::math::ccmath::isnan(x) ? detail::modf_nan_impl(x, iptr) :
  45. boost::math::ccmath::detail::modf_impl(x, iptr);
  46. }
  47. else
  48. {
  49. using std::modf;
  50. return modf(x, iptr);
  51. }
  52. }
  53. inline constexpr float modff(float x, float* iptr)
  54. {
  55. return boost::math::ccmath::modf(x, iptr);
  56. }
  57. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  58. inline constexpr long double modfl(long double x, long double* iptr)
  59. {
  60. return boost::math::ccmath::modf(x, iptr);
  61. }
  62. #endif
  63. } // Namespaces
  64. #endif // BOOST_MATH_CCMATH_MODF_HPP