diff --git a/doc/sf/ccmath.qbk b/doc/sf/ccmath.qbk index 65e10c897..a152ebd64 100644 --- a/doc/sf/ccmath.qbk +++ b/doc/sf/ccmath.qbk @@ -91,6 +91,18 @@ All of the following functions require C++17 or greater. template inline constexpr double scalbln(Integer x, long exp) noexcept + template + inline constexpr Real floor(Real arg) noexcept + + template + inline constexpr double floor(Integer arg) noexcept + + template + inline constexpr Real ceil(Real arg) noexcept + + template + inline constexpr double ceil(Integer arg) noexcept + } // Namespaces [endsect] [/section:ccmath Constexpr CMath] diff --git a/include/boost/math/ccmath/ccmath.hpp b/include/boost/math/ccmath/ccmath.hpp index 04e47e1b7..8a6f379d7 100644 --- a/include/boost/math/ccmath/ccmath.hpp +++ b/include/boost/math/ccmath/ccmath.hpp @@ -21,5 +21,7 @@ #include #include #include +#include +#include #endif // BOOST_MATH_CCMATH_HPP diff --git a/include/boost/math/ccmath/ceil.hpp b/include/boost/math/ccmath/ceil.hpp new file mode 100644 index 000000000..34ab2bb6b --- /dev/null +++ b/include/boost/math/ccmath/ceil.hpp @@ -0,0 +1,75 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_CCMATH_CEIL_HPP +#define BOOST_MATH_CCMATH_CEIL_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace boost::math::ccmath { + +namespace detail { + +template +inline constexpr T ceil_impl(T arg) noexcept +{ + T result = boost::math::ccmath::floor(arg); + + if(result == arg) + { + return result; + } + else + { + return result + 1; + } +} + +} // Namespace detail + +template , bool> = true> +inline constexpr Real ceil(Real arg) noexcept +{ + if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg)) + { + return boost::math::ccmath::abs(arg) == Real(0) ? arg : + boost::math::ccmath::isinf(arg) ? arg : + boost::math::ccmath::isnan(arg) ? arg : + boost::math::ccmath::detail::ceil_impl(arg); + } + else + { + using std::ceil; + return ceil(arg); + } +} + +template , bool> = true> +inline constexpr double ceil(Z arg) noexcept +{ + return boost::math::ccmath::ceil(static_cast(arg)); +} + +inline constexpr float ceilf(float arg) noexcept +{ + return boost::math::ccmath::ceil(arg); +} + +#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS +inline constexpr long double ceill(long double arg) noexcept +{ + return boost::math::ccmath::ceil(arg); +} +#endif + +} // Namespaces + +#endif // BOOST_MATH_CCMATH_CEIL_HPP diff --git a/include/boost/math/ccmath/floor.hpp b/include/boost/math/ccmath/floor.hpp new file mode 100644 index 000000000..d866a35c3 --- /dev/null +++ b/include/boost/math/ccmath/floor.hpp @@ -0,0 +1,121 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_CCMATH_FLOOR_HPP +#define BOOST_MATH_CCMATH_FLOOR_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace boost::math::ccmath { + +namespace detail { + +template +inline constexpr T floor_pos_impl(T arg) noexcept +{ + T result = 1; + + if(result < arg) + { + while(result < arg) + { + result *= 2; + } + while(result > arg) + { + --result; + } + + return result; + } + else + { + return T(0); + } +} + +template +inline constexpr T floor_neg_impl(T arg) noexcept +{ + T result = -1; + + if(result > arg) + { + while(result > arg) + { + result *= 2; + } + while(result < arg) + { + ++result; + } + if(result != arg) + { + --result; + } + } + + return result; +} + +template +inline constexpr T floor_impl(T arg) noexcept +{ + if(arg > 0) + { + return floor_pos_impl(arg); + } + else + { + return floor_neg_impl(arg); + } +} + +} // Namespace detail + +template , bool> = true> +inline constexpr Real floor(Real arg) noexcept +{ + if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg)) + { + return boost::math::ccmath::abs(arg) == Real(0) ? arg : + boost::math::ccmath::isinf(arg) ? arg : + boost::math::ccmath::isnan(arg) ? arg : + boost::math::ccmath::detail::floor_impl(arg); + } + else + { + using std::floor; + return floor(arg); + } +} + +template , bool> = true> +inline constexpr double floor(Z arg) noexcept +{ + return boost::math::ccmath::floor(static_cast(arg)); +} + +inline constexpr float floorf(float arg) noexcept +{ + return boost::math::ccmath::floor(arg); +} + +#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS +inline constexpr long double floorl(long double arg) noexcept +{ + return boost::math::ccmath::floor(arg); +} +#endif + +} // Namespaces + +#endif // BOOST_MATH_CCMATH_FLOOR_HPP diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 6b95e4b13..a77c00bac 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -135,6 +135,8 @@ test-suite special_fun : [ run ccmath_ilogb_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] [ run ccmath_scalbn_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] [ run ccmath_scalbln_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] + [ run ccmath_floor_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] + [ run ccmath_ceil_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] [ run log1p_expm1_test.cpp test_instances//test_instances pch_light ../../test/build//boost_unit_test_framework ] [ run powm1_sqrtp1m1_test.cpp test_instances//test_instances pch_light ../../test/build//boost_unit_test_framework ] [ run special_functions_test.cpp ../../test/build//boost_unit_test_framework ] diff --git a/test/ccmath_ceil_test.cpp b/test/ccmath_ceil_test.cpp new file mode 100644 index 000000000..9540ea99d --- /dev/null +++ b/test/ccmath_ceil_test.cpp @@ -0,0 +1,63 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_FLOAT128 +#include +#endif + +template +constexpr void test() +{ + if constexpr (std::numeric_limits::has_quiet_NaN) + { + static_assert(boost::math::ccmath::isnan(boost::math::ccmath::ceil(std::numeric_limits::quiet_NaN())), "If x is NaN, NaN is returned"); + } + + static_assert(!boost::math::ccmath::ceil(T(0)), "If x is +- 0, it is returned, unmodified"); + static_assert(!boost::math::ccmath::ceil(T(-0)), "If x is +- 0, it is returned, unmodified"); + + static_assert(boost::math::ccmath::isinf(boost::math::ccmath::ceil(std::numeric_limits::infinity())), + "If x is +- inf, it is returned, unmodified"); + static_assert(boost::math::ccmath::isinf(boost::math::ccmath::ceil(-std::numeric_limits::infinity())), + "If x is +- inf, it is returned, unmodified"); + + static_assert(boost::math::ccmath::ceil(T(2)) == T(2)); + static_assert(boost::math::ccmath::ceil(T(2.4)) == T(3)); + static_assert(boost::math::ccmath::ceil(T(2.9)) == T(3)); + static_assert(boost::math::ccmath::ceil(T(-2.7)) == T(-2)); + static_assert(boost::math::ccmath::ceil(T(-2)) == T(-2)); +} + +#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P) +int main() +{ + test(); + test(); + + #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS + test(); + #endif + + #ifdef BOOST_HAS_FLOAT128 + test(); + #endif + + return 0; +} +#else +int main() +{ + return 0; +} +#endif diff --git a/test/ccmath_floor_test.cpp b/test/ccmath_floor_test.cpp new file mode 100644 index 000000000..aa907fab1 --- /dev/null +++ b/test/ccmath_floor_test.cpp @@ -0,0 +1,63 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_FLOAT128 +#include +#endif + +template +constexpr void test() +{ + if constexpr (std::numeric_limits::has_quiet_NaN) + { + static_assert(boost::math::ccmath::isnan(boost::math::ccmath::floor(std::numeric_limits::quiet_NaN())), "If x is NaN, NaN is returned"); + } + + static_assert(!boost::math::ccmath::floor(T(0)), "If x is +- 0, it is returned, unmodified"); + static_assert(!boost::math::ccmath::floor(T(-0)), "If x is +- 0, it is returned, unmodified"); + + static_assert(boost::math::ccmath::isinf(boost::math::ccmath::floor(std::numeric_limits::infinity())), + "If x is +- inf, it is returned, unmodified"); + static_assert(boost::math::ccmath::isinf(boost::math::ccmath::floor(-std::numeric_limits::infinity())), + "If x is +- inf, it is returned, unmodified"); + + static_assert(boost::math::ccmath::floor(T(2)) == T(2)); + static_assert(boost::math::ccmath::floor(T(2.4)) == T(2)); + static_assert(boost::math::ccmath::floor(T(2.9)) == T(2)); + static_assert(boost::math::ccmath::floor(T(-2.7)) == T(-3)); + static_assert(boost::math::ccmath::floor(T(-2)) == T(-2)); +} + +#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P) +int main() +{ + test(); + test(); + + #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS + test(); + #endif + + #ifdef BOOST_HAS_FLOAT128 + test(); + #endif + + return 0; +} +#else +int main() +{ + return 0; +} +#endif diff --git a/test/compile_test/ccmath_ceil_incl_test.cpp b/test/compile_test/ccmath_ceil_incl_test.cpp new file mode 100644 index 000000000..13cd29ad6 --- /dev/null +++ b/test/compile_test/ccmath_ceil_incl_test.cpp @@ -0,0 +1,16 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include "test_compile_result.hpp" + +void compile_and_link_test() +{ + check_result(boost::math::ccmath::ceil(1.0f)); + check_result(boost::math::ccmath::ceil(1.0)); +#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS + check_result(boost::math::ccmath::ceil(1.0l)); +#endif +} diff --git a/test/compile_test/ccmath_floor_incl_test.cpp b/test/compile_test/ccmath_floor_incl_test.cpp new file mode 100644 index 000000000..859303a44 --- /dev/null +++ b/test/compile_test/ccmath_floor_incl_test.cpp @@ -0,0 +1,16 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include "test_compile_result.hpp" + +void compile_and_link_test() +{ + check_result(boost::math::ccmath::floor(1.0f)); + check_result(boost::math::ccmath::floor(1.0)); +#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS + check_result(boost::math::ccmath::floor(1.0l)); +#endif +}