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

Incorporate cast simplifications from https://github.com/boostorg/math/pull/880

Add tests and make sure everything is covered, and fix resulting errors.
This commit is contained in:
jzmaddock
2023-10-17 17:09:44 +01:00
parent e3e79b51c2
commit c6f4a88809
5 changed files with 5233 additions and 71 deletions

View File

@@ -918,62 +918,6 @@ namespace std
using boost::math::cstdfloat::detail::isunordered;
// end more functions
//
// Very basic iostream operator:
//
inline std::ostream& operator << (std::ostream& os, __float128 m_value)
{
std::streamsize digits = os.precision();
std::ios_base::fmtflags f = os.flags();
std::string s;
char buf[100];
std::unique_ptr<char[]> buf2;
std::string format = "%";
if (f & std::ios_base::showpos)
format += "+";
if (f & std::ios_base::showpoint)
format += "#";
format += ".*";
if (digits == 0)
digits = 36;
format += "Q";
if (f & std::ios_base::scientific)
format += "e";
else if (f & std::ios_base::fixed)
format += "f";
else
format += "g";
int v = quadmath_snprintf(buf, 100, format.c_str(), digits, m_value);
if ((v < 0) || (v >= 99))
{
int v_max = v;
buf2.reset(new char[v + 3]);
v = quadmath_snprintf(&buf2[0], v_max + 3, format.c_str(), digits, m_value);
if (v >= v_max + 3)
{
BOOST_MATH_THROW_EXCEPTION(std::runtime_error("Formatting of float128_type failed."));
}
s = &buf2[0];
}
else
s = buf;
std::streamsize ss = os.width();
if (ss > static_cast<std::streamsize>(s.size()))
{
char fill = os.fill();
if ((os.flags() & std::ios_base::left) == std::ios_base::left)
s.append(static_cast<std::string::size_type>(ss - s.size()), fill);
else
s.insert(static_cast<std::string::size_type>(0), static_cast<std::string::size_type>(ss - s.size()), fill);
}
return os << s;
}
} // namespace std
// We will now remove the preprocessor symbols representing quadruple-precision <cmath>

View File

@@ -32,8 +32,7 @@
#include <boost/math/tools/nothrow.hpp>
#include <boost/math/tools/throw_exception.hpp>
// #if (0)
#if defined(__GNUC__)
#if defined(__GNUC__) && !defined(BOOST_MATH_TEST_IO_AS_INTEL_QUAD)
// Forward declarations of quadruple-precision string functions.
extern "C" int quadmath_snprintf(char *str, size_t size, const char *format, ...) BOOST_MATH_NOTHROW;
@@ -96,7 +95,7 @@
// So we have to use dynamic memory allocation for the output
// string buffer.
char* my_buffer2 = static_cast<char*>(0U);
char* my_buffer2 = nullptr;
#ifndef BOOST_NO_EXCEPTIONS
try
@@ -160,8 +159,7 @@
}
}
// #elif defined(__GNUC__)
#elif defined(__INTEL_COMPILER)
#elif defined(__INTEL_COMPILER) || defined(BOOST_MATH_TEST_IO_AS_INTEL_QUAD)
// The section for I/O stream support for the ICC compiler is particularly
// long, because these functions must be painstakingly synthesized from
@@ -172,6 +170,7 @@
// used in Boost.Multiprecision by John Maddock and Christopher Kormanyos.
// This methodology has been slightly modified here for boost::float128_t.
#include <cstring>
#include <cctype>
@@ -266,7 +265,7 @@
{
// Pad out the end with zero's if we need to.
int chars = static_cast<int>(str.size());
std::ptrdiff_t chars = static_cast<std::ptrdiff_t>(str.size());
chars = digits - chars;
if(scientific)
@@ -507,6 +506,8 @@
eval_subtract(t, digit);
eval_multiply(t, ten);
}
if (result.size() == 0)
result = "0";
// Possibly round the result.
if(digits >= 0)
@@ -522,11 +523,13 @@
if((static_cast<int>(*result.rbegin() - '0') & 1) != 0)
{
round_string_up_at(result, static_cast<int>(result.size() - 1U), expon);
if (digits == 0) digits = 1;
}
}
else if(cdigit >= 5)
{
round_string_up_at(result, static_cast<int>(result.size() - 1), expon);
round_string_up_at(result, static_cast<int>(result.size() - 1u), expon);
if (digits == 0) digits = 1;
}
}
}
@@ -569,7 +572,7 @@
{
value = 0;
if((p == static_cast<const char*>(0U)) || (*p == static_cast<char>(0)))
if((p == nullptr) || (*p == '\0'))
{
return false;
}
@@ -584,11 +587,11 @@
constexpr int max_digits = std::numeric_limits<float_type>::max_digits10 + 1;
if(*p == static_cast<char>('+'))
if(*p == '+')
{
++p;
}
else if(*p == static_cast<char>('-'))
else if(*p == '-')
{
is_neg = true;
++p;
@@ -632,7 +635,7 @@
++digits_seen;
}
if(*p == static_cast<char>('.'))
if(*p == '.')
{
// Grab everything after the point, stop when we've seen
// enough digits, even if there are actually more available.
@@ -659,15 +662,15 @@
}
// Parse the exponent.
if((*p == static_cast<char>('e')) || (*p == static_cast<char>('E')))
if((*p == 'e') || (*p == 'E'))
{
++p;
if(*p == static_cast<char>('+'))
if(*p == '+')
{
++p;
}
else if(*p == static_cast<char>('-'))
else if(*p == '-')
{
is_neg_expon = true;
++p;
@@ -718,7 +721,7 @@
value = -value;
}
return (*p == static_cast<char>(0));
return (*p == '\0');
}
} } } } // boost::math::cstdfloat::detail

View File

@@ -909,6 +909,8 @@ test-suite new_floats :
[ compile compile_test/float32.cpp ]
[ compile compile_test/float64.cpp ]
[ compile compile_test/float128.cpp ]
[ run test_float_io.cpp : : : [ check-target-builds ../config//has_float128 "GCC libquadmath and __float128 support" : <define>BOOST_MATH_TEST_FLOAT128 <linkflags>"-Bstatic -lquadmath -Bdynamic" ] ]
[ run test_float_io.cpp : : : <define>BOOST_MATH_TEST_IO_AS_INTEL_QUAD=1 [ check-target-builds ../config//has_float128 "GCC libquadmath and __float128 support" : <define>BOOST_MATH_TEST_FLOAT128 <linkflags>"-Bstatic -lquadmath -Bdynamic" ] : test_float_io_quad ]
;
test-suite mp :

4765
test/string_data.ipp Normal file

File diff suppressed because it is too large Load Diff

448
test/test_float_io.cpp Normal file
View File

@@ -0,0 +1,448 @@
// Copyright John Maddock 2023.
// 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)
#ifdef _MSC_VER
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <boost/cstdfloat.hpp>
#include <boost/math/special_functions/next.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/array.hpp>
#include <iostream>
#include <sstream>
#include <iomanip>
#if defined(__INTEL_COMPILER) || defined(BOOST_MATH_TEST_IO_AS_INTEL_QUAD)
bool has_bad_bankers_rounding(const boost::float128_t&)
{
return true;
}
#endif
template <class T>
bool has_bad_bankers_rounding(const T&)
{
return false;
}
enum
{
warn_on_fail,
error_on_fail,
abort_on_fail
};
inline std::ostream& report_where(const char* file, int line, const char* function)
{
if (function)
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: " << function << std::endl;
BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
}
#define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
inline void report_severity(int severity)
{
if (severity == error_on_fail)
++boost::detail::test_errors();
else if (severity == abort_on_fail)
{
++boost::detail::test_errors();
abort();
}
}
#define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
template <class E>
void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
{
report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl;
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl;
BOOST_MP_REPORT_SEVERITY(severity);
}
#ifdef BOOST_HAS_INT128
std::ostream& operator<<(std::ostream& os, boost::int128_type val)
{
std::stringstream ss;
ss << std::hex << "0x" << static_cast<std::uint64_t>(static_cast<boost::uint128_type>(val) >> 64) << static_cast<std::uint64_t>(val);
return os << ss.str();
}
std::ostream& operator<<(std::ostream& os, boost::uint128_type val)
{
std::stringstream ss;
ss << std::hex << "0x" << static_cast<std::uint64_t>(val >> 64) << static_cast<std::uint64_t>(val);
return os << ss.str();
}
#endif
#ifndef BOOST_NO_EXCEPTIONS
#define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \
catch (const std::exception& e) \
{ \
report_unexpected_exception(e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
} \
catch (...) \
{ \
std::cout << "Exception of unknown type was thrown" << std::endl; \
report_severity(severity); \
}
#define BOOST_MP_TEST_TRY try
#else
#define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_MP_TEST_TRY
#endif
#define BOOST_CHECK_IMP(x, severity) \
BOOST_MP_TEST_TRY \
{ \
if (x) \
{ \
} \
else \
{ \
BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_CHECK(x) BOOST_CHECK_IMP(x, error_on_fail)
#define BOOST_WARN(x) BOOST_CHECK_IMP(x, warn_on_fail)
#define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, abort_on_fail)
#define BOOST_CLOSE_IMP(x, y, tol, severity) \
BOOST_MP_TEST_TRY \
{ \
if (relative_error(x, y) > tol) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for closeness: \n" \
<< std::setprecision(std::numeric_limits<decltype(x)>::max_digits10) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(5) << std::fixed \
<< "Relative error was: " << relative_error(x, y) << "eps\n" \
<< "Tolerance was: " << tol << "eps" << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_EQUAL_IMP(x, y, severity) \
BOOST_MP_TEST_TRY \
{ \
if (!((x) == (y))) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \
<< std::setprecision(std::numeric_limits<decltype(x)>::max_digits10) << std::scientific \
<< "Value of LHS was: " << (x) << "\n" \
<< "Value of RHS was: " << (y) << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_NE_IMP(x, y, severity) \
BOOST_MP_TEST_TRY \
{ \
if (!(x != y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \n" \
<< std::setprecision(std::numeric_limits<decltype(x)>::max_digits10) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_LT_IMP(x, y, severity) \
BOOST_MP_TEST_TRY \
{ \
if (!(x < y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \
<< std::setprecision(std::numeric_limits<decltype(x)>::max_digits10) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_GT_IMP(x, y, severity) \
BOOST_MP_TEST_TRY \
{ \
if (!(x > y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \
<< std::setprecision(std::numeric_limits<decltype(x)>::max_digits10) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_LE_IMP(x, y, severity) \
BOOST_MP_TEST_TRY \
{ \
if (!(x <= y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \
<< std::setprecision(std::numeric_limits<decltype(x)>::max_digits10) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_GE_IMP(x, y, severity) \
BOOST_MP_TEST_TRY \
{ \
if (!(x >= y)) \
{ \
BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \
<< std::setprecision(std::numeric_limits<decltype(x)>::max_digits10) << std::scientific \
<< "Value of LHS was: " << x << "\n" \
<< "Value of RHS was: " << y << "\n" \
<< std::setprecision(3) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#ifndef BOOST_NO_EXCEPTIONS
#define BOOST_MT_CHECK_THROW_IMP(x, E, severity) \
BOOST_MP_TEST_TRY \
{ \
x; \
BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl; \
BOOST_MP_REPORT_SEVERITY(severity); \
} \
catch (const E&) {} \
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#else
#define BOOST_MT_CHECK_THROW_IMP(x, E, severity)
#endif
#define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, error_on_fail)
#define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, abort_on_fail)
void print_flags(std::ios_base::fmtflags f)
{
std::cout << "Formatting flags were: ";
if (f & std::ios_base::scientific)
std::cout << "scientific ";
if (f & std::ios_base::fixed)
std::cout << "fixed ";
if (f & std::ios_base::showpoint)
std::cout << "showpoint ";
if (f & std::ios_base::showpos)
std::cout << "showpos ";
std::cout << std::endl;
}
bool is_bankers_rounding_error(const std::string& s, const char* expect)
{
// This check isn't foolproof: that would require *much* more sophisticated code!!!
std::string::size_type l = std::strlen(expect);
if (l != s.size())
return false;
std::string::size_type len = s.find('e');
if (len == std::string::npos)
len = l - 1;
else
--len;
if (s.compare(0, len, expect, len))
return false;
if (s[len] != expect[len] + 1)
return false;
return true;
}
template <class T>
bool is_bankers_rounding_error(T new_val, T val)
{
// This check isn't foolproof: that would require *much* more sophisticated code!!!
auto n = boost::math::float_distance(new_val, val) == 1;
std::cout << "Distance was: " << n << std::endl;
return std::abs(n) <= 1;
}
template <class T>
void test()
{
typedef T mp_t;
boost::array<std::ios_base::fmtflags, 9> f =
{{std::ios_base::fmtflags(0), std::ios_base::showpoint, std::ios_base::showpos, std::ios_base::scientific, std::ios_base::scientific | std::ios_base::showpos,
std::ios_base::scientific | std::ios_base::showpoint, std::ios_base::fixed, std::ios_base::fixed | std::ios_base::showpoint,
std::ios_base::fixed | std::ios_base::showpos}};
boost::array<boost::array<const char*, 13 * 9>, 40> string_data = {{
#include "libs/math/test/string_data.ipp"
}};
double num = 123456789.0;
double denom = 1;
double val = num;
for (unsigned j = 0; j < 40; ++j)
{
unsigned col = 0;
for (unsigned prec = 1; prec < 14; ++prec)
{
for (unsigned i = 0; i < f.size(); ++i, ++col)
{
std::stringstream ss;
ss.precision(prec);
ss.flags(f[i]);
ss << mp_t(val);
const char* expect = string_data[j][col];
if (ss.str() != expect)
{
if (has_bad_bankers_rounding(mp_t()) && is_bankers_rounding_error(ss.str(), expect))
{
std::cout << "Ignoring bankers-rounding error with Intel _Quad.\n";
}
else
{
std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
print_flags(f[i]);
std::cout << "Precision is: " << prec << std::endl;
std::cout << "Got: " << ss.str() << std::endl;
std::cout << "Expected: " << expect << std::endl;
++boost::detail::test_errors();
}
}
}
}
num = -num;
if (j & 1)
denom *= 8;
val = num / denom;
}
boost::array<const char*, 13 * 9> zeros =
{{"0", "0.", "+0", "0.0e+00", "+0.0e+00", "0.0e+00", "0.0", "0.0", "+0.0", "0", "0.0", "+0", "0.00e+00", "+0.00e+00", "0.00e+00", "0.00", "0.00", "+0.00", "0", "0.00", "+0", "0.000e+00", "+0.000e+00", "0.000e+00", "0.000", "0.000", "+0.000", "0", "0.000", "+0", "0.0000e+00", "+0.0000e+00", "0.0000e+00", "0.0000", "0.0000", "+0.0000", "0", "0.0000", "+0", "0.00000e+00", "+0.00000e+00", "0.00000e+00", "0.00000", "0.00000", "+0.00000", "0", "0.00000", "+0", "0.000000e+00", "+0.000000e+00", "0.000000e+00", "0.000000", "0.000000", "+0.000000", "0", "0.000000", "+0", "0.0000000e+00", "+0.0000000e+00", "0.0000000e+00", "0.0000000", "0.0000000", "+0.0000000", "0", "0.0000000", "+0", "0.00000000e+00", "+0.00000000e+00", "0.00000000e+00", "0.00000000", "0.00000000", "+0.00000000", "0", "0.00000000", "+0", "0.000000000e+00", "+0.000000000e+00", "0.000000000e+00", "0.000000000", "0.000000000", "+0.000000000", "0", "0.000000000", "+0", "0.0000000000e+00", "+0.0000000000e+00", "0.0000000000e+00", "0.0000000000", "0.0000000000", "+0.0000000000", "0", "0.0000000000", "+0", "0.00000000000e+00", "+0.00000000000e+00", "0.00000000000e+00", "0.00000000000", "0.00000000000", "+0.00000000000", "0", "0.00000000000", "+0", "0.000000000000e+00", "+0.000000000000e+00", "0.000000000000e+00", "0.000000000000", "0.000000000000", "+0.000000000000", "0", "0.000000000000", "+0", "0.0000000000000e+00", "+0.0000000000000e+00", "0.0000000000000e+00", "0.0000000000000", "0.0000000000000", "+0.0000000000000"}};
unsigned col = 0;
val = 0;
for (unsigned prec = 1; prec < 14; ++prec)
{
for (unsigned i = 0; i < f.size(); ++i, ++col)
{
std::stringstream ss;
ss.precision(prec);
ss.flags(f[i]);
ss << mp_t(val);
const char* expect = zeros[col];
if (ss.str() != expect)
{
std::cout << std::setprecision(20) << "Testing value " << val << std::endl;
print_flags(f[i]);
std::cout << "Precision is: " << prec << std::endl;
std::cout << "Got: " << ss.str() << std::endl;
std::cout << "Expected: " << expect << std::endl;
++boost::detail::test_errors();
}
}
}
}
template <class T>
T generate_random()
{
typedef int e_type;
static boost::random::mt19937 gen;
T val = gen();
T prev_val = -1;
while (val != prev_val)
{
val *= (gen.max)();
prev_val = val;
val += gen();
}
e_type e;
val = frexp(val, &e);
static boost::random::uniform_int_distribution<e_type> ui(0, std::numeric_limits<T>::max_exponent - 10);
return ldexp(val, ui(gen));
}
template <class T>
void do_round_trip(const T& val, std::ios_base::fmtflags f)
{
std::stringstream ss;
ss << std::setprecision(std::numeric_limits<T>::max_digits10);
ss.flags(f);
ss << val;
T new_val;
ss >> new_val;
if (new_val != val)
{
if (has_bad_bankers_rounding(T()) && is_bankers_rounding_error(new_val, val))
{
std::cout << "Ignoring bankers-rounding error with Intel _Quad mp_f.\n";
}
else
{
BOOST_CHECK_EQUAL(new_val, val);
}
}
}
template <class T>
void do_round_trip(const T& val)
{
do_round_trip(val, std::ios_base::fmtflags(0));
do_round_trip(val, std::ios_base::fmtflags(std::ios_base::scientific));
if ((fabs(val) > 1) && (fabs(val) < 1e100))
do_round_trip(val, std::ios_base::fmtflags(std::ios_base::fixed));
}
template <class T>
void test_round_trip()
{
for (unsigned i = 0; i < 1000; ++i)
{
T val = generate_random<T>();
do_round_trip(val);
do_round_trip(T(-val));
do_round_trip(T(1 / val));
do_round_trip(T(-1 / val));
}
}
int main()
{
test<double>();
test<boost::float64_t>();
test_round_trip<double>();
test_round_trip<boost::float64_t>();
#ifdef BOOST_FLOAT128_C
test<boost::float128_t>();
test_round_trip<boost::float128_t>();
#endif
return boost::report_errors();
}