2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Add rsqrt test and update type traits for control path

This commit is contained in:
Matt Borland
2023-05-22 13:08:10 +02:00
parent c4c8c6936a
commit aef57135b3
2 changed files with 25 additions and 10 deletions

View File

@@ -11,10 +11,10 @@
#include <boost/math/tools/is_standalone.hpp>
#ifndef BOOST_MATH_STANDALONE
#include <boost/config.hpp>
#ifdef BOOST_NO_CXX17_IF_CONSTEXPR
#error "The header <boost/math/norms.hpp> can only be used in C++17 and later."
#endif
# include <boost/config.hpp>
# ifdef BOOST_NO_CXX17_IF_CONSTEXPR
# error "The header <boost/math/rqrt.hpp> can only be used in C++17 and later."
# endif
#endif
namespace boost::math {
@@ -23,7 +23,7 @@ template<typename Real>
inline Real rsqrt(Real const & x)
{
using std::sqrt;
if constexpr (std::is_same_v<Real, float> || std::is_same_v<Real, double> || std::is_same_v<Real, long double>)
if constexpr (std::is_arithmetic_v<Real> && !std::is_integral_v<Real>)
{
return 1/sqrt(x);
}

View File

@@ -13,6 +13,11 @@
#include <boost/core/demangle.hpp>
#include <boost/math/special_functions/rsqrt.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#if __has_include(<stdfloat>)
# include <stdfloat>
#endif
#ifdef BOOST_HAS_FLOAT128
#include <boost/multiprecision/float128.hpp>
using boost::multiprecision::float128;
@@ -27,7 +32,7 @@ void test_rsqrt()
using std::sqrt;
Real x = (std::numeric_limits<Real>::min)();
while (x < 10000*std::numeric_limits<Real>::epsilon()) {
Real expected = 1/sqrt(x);
Real expected = Real(1)/sqrt(x);
Real computed = rsqrt(x);
if(!CHECK_ULP_CLOSE(expected, computed, 2)) {
std::cerr << " 1/sqrt(" << x << ") is computed incorrectly.\n";
@@ -38,7 +43,7 @@ void test_rsqrt()
// x ~ 1:
x = 1;
while (x < 1 + 1000*std::numeric_limits<Real>::epsilon()) {
Real expected = 1/sqrt(x);
Real expected = Real(1)/sqrt(x);
Real computed = rsqrt(x);
if(!CHECK_ULP_CLOSE(expected, computed, 2)) {
std::cerr << " 1/sqrt(" << x << ") is computed incorrectly.\n";
@@ -49,7 +54,7 @@ void test_rsqrt()
// x ~ 1000:
x = 1000;
while (x < 1000 + 1000*1000*std::numeric_limits<Real>::epsilon()) {
Real expected = 1/sqrt(x);
Real expected = Real(1)/sqrt(x);
Real computed = rsqrt(x);
if(!CHECK_ULP_CLOSE(expected, computed, 2)) {
std::cerr << " 1/sqrt(" << x << ") is computed incorrectly.\n";
@@ -58,14 +63,14 @@ void test_rsqrt()
}
x = std::numeric_limits<Real>::infinity();
Real expected = 1/sqrt(x);
Real expected = Real(1)/sqrt(x);
Real computed = rsqrt(x);
if (!CHECK_ULP_CLOSE(expected, computed, 0)) {
std::cerr << "Reciprocal square root of infinity not correctly computed.\n";
}
x = (std::numeric_limits<Real>::max)();
expected = 1/sqrt(x);
expected = Real(1)/sqrt(x);
computed = rsqrt(x);
if (!CHECK_EQUAL(expected, computed)) {
std::cerr << "Reciprocal square root of std::numeric_limits<Real>::max() not correctly computed.\n";
@@ -79,8 +84,18 @@ void test_rsqrt()
int main()
{
#ifdef __STDCPP_FLOAT32_T__
test_rsqrt<std::float32_t>();
#else
test_rsqrt<float>();
#endif
#ifdef __STDCPP_FLOAT64_T__
test_rsqrt<std::float64_t>();
#else
test_rsqrt<double>();
#endif
test_rsqrt<long double>();
test_rsqrt<boost::multiprecision::cpp_bin_float_50>();
test_rsqrt<boost::multiprecision::cpp_bin_float_100>();