|
| 1 | +// Copyright 2025 Christopher Kormanyos |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | +// |
| 5 | +// See: https://github.com/cppalliance/decimal/issues/1110 |
| 6 | + |
| 7 | +#include <boost/decimal/decimal128_t.hpp> |
| 8 | +#include <boost/decimal/cmath.hpp> |
| 9 | +#include <boost/decimal/iostream.hpp> |
| 10 | + |
| 11 | +#include <boost/core/lightweight_test.hpp> |
| 12 | + |
| 13 | +#include <iomanip> |
| 14 | +#include <iostream> |
| 15 | +#include <sstream> |
| 16 | + |
| 17 | +using namespace boost::decimal; |
| 18 | + |
| 19 | +namespace local { |
| 20 | + |
| 21 | +auto test() -> void; |
| 22 | + |
| 23 | +auto test() -> void |
| 24 | +{ |
| 25 | + const boost::decimal::decimal128_t one { 1 }; |
| 26 | + const boost::decimal::decimal128_t del { 1, -32 }; |
| 27 | + const boost::decimal::decimal128_t sum { one + del }; |
| 28 | + |
| 29 | + const boost::decimal::decimal128_t sqr { sqrt(sum) }; |
| 30 | + |
| 31 | + { |
| 32 | + std::stringstream strm { }; |
| 33 | + |
| 34 | + strm << std::setprecision(std::numeric_limits<boost::decimal::decimal128_t>::digits10) << sqr; |
| 35 | + |
| 36 | + BOOST_TEST_CSTR_EQ(strm.str().c_str(), "1.000000000000000000000000000000005"); |
| 37 | + } |
| 38 | + |
| 39 | + const boost::decimal::decimal128_t cbr { cbrt(sum) }; |
| 40 | + |
| 41 | + { |
| 42 | + std::stringstream strm { }; |
| 43 | + |
| 44 | + strm << std::setprecision(std::numeric_limits<boost::decimal::decimal128_t>::digits10)<< cbr; |
| 45 | + |
| 46 | + // TODO: This tolerance isn't the best |
| 47 | + BOOST_TEST_CSTR_EQ(strm.str().c_str(), "1.000000000000000000000000000000641"); |
| 48 | + } |
| 49 | + |
| 50 | + const boost::decimal::decimal128_t lgt { log10(sum) }; |
| 51 | + |
| 52 | + { |
| 53 | + std::stringstream strm { }; |
| 54 | + |
| 55 | + strm << std::setprecision(std::numeric_limits<boost::decimal::decimal128_t>::digits10)<< lgt; |
| 56 | + |
| 57 | + BOOST_TEST_CSTR_EQ(strm.str().c_str(), "4.4e-33"); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace local |
| 62 | + |
| 63 | +auto main() -> int |
| 64 | +{ |
| 65 | + local::test(); |
| 66 | + |
| 67 | + return boost::report_errors(); |
| 68 | +} |
0 commit comments