1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
|
// Copyright 2023 - 2025 Matt Borland
// Copyright 2023 - 2025 Christopher Kormanyos
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
// Some parts of this test file have been taken from the Boost.Decimal project.
#if !defined(TEST_CPP_DOUBLE_FLOAT)
#define TEST_CPP_DOUBLE_FLOAT
#endif
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_double_fp.hpp>
#include <test_traits.hpp> // Note: include this AFTER the test-backends are defined
#include <chrono>
#include <limits>
#include <random>
#include <sstream>
#include <string>
#include <vector>
#if (defined(__clang__) || defined(__GNUC__))
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
#include <boost/core/lightweight_test.hpp>
template<typename FloatType> auto my_zero() noexcept -> FloatType&;
template<typename FloatType> auto my_one () noexcept -> FloatType&;
template<typename FloatType> auto my_inf () noexcept -> FloatType&;
template<typename FloatType> auto my_nan () noexcept -> FloatType&;
template<typename FloatType> auto my_generator_overflow() noexcept -> FloatType;
template<typename FloatType> auto my_generator_underflow() noexcept -> FloatType;
namespace local
{
template<typename IntegralTimePointType,
typename ClockType = std::chrono::high_resolution_clock>
auto time_point() noexcept -> IntegralTimePointType
{
using local_integral_time_point_type = IntegralTimePointType;
using local_clock_type = ClockType;
typename local_clock_type::time_point tp_zero { };
const auto my_now = local_clock_type::now();
const auto duration { my_now - tp_zero };
const std::uintmax_t
value
{
static_cast<std::uintmax_t>
(
std::chrono::duration_cast<std::chrono::nanoseconds>(my_now - tp_zero).count()
)
};
return static_cast<local_integral_time_point_type>(value);
}
template<typename NumericType>
auto is_close_fraction(const NumericType& a,
const NumericType& b,
const NumericType& tol) noexcept -> bool
{
using std::fabs;
auto result_is_ok = bool { };
NumericType delta { };
if(b == static_cast<NumericType>(0))
{
delta = fabs(a - b); // LCOV_EXCL_LINE
result_is_ok = (delta <= tol); // LCOV_EXCL_LINE
}
else
{
delta = fabs(1 - (a / b));
result_is_ok = (delta <= tol);
}
return result_is_ok;
}
template<class AnyFloatType, class OtherFloatType>
bool test_convert_and_back(const float epsilon_factor)
{
using any_float_type = AnyFloatType;
using other_float_type = OtherFloatType;
std::mt19937_64 gen { time_point<typename std::mt19937_64::result_type>() };
auto dis =
std::uniform_real_distribution<float>
{
static_cast<float>(-1.5F),
static_cast<float>(1.5F)
};
bool result_is_ok { true };
for(int n_loop = static_cast<int>(INT8_C(0)); n_loop < static_cast<int>(INT8_C(64)); ++n_loop)
{
static_cast<void>(n_loop);
using string_data_array_type = std::array<std::string, std::size_t { UINT8_C(5) }>;
// Table[N[Exp[Pi EulerGamma*m], 100], {m, 1, 41, 10}]
// 6.131114182422604828954743181715560166328314533478636289880930665602805209787080979043183175178343525
// 4.601860472890328982970020344164597095017995523702194983334509372565826509463730150117899613177592949*10^8
// 3.454041008184686240269652058630805927805404685705614002929256804267342980143530922708514273650048389*10^16
// 2.592516517287682590319488890844344913780990651072642232646840608443538857001634005575916140667342752*10^24
// 1.945877850460679347837790546862626024227287564971475316486097406542338497232351222296834286913627889*10^32
const string_data_array_type
float_number_strings
{
std::string("6.131114182422604828954743181715560166328314533478636289880930665602805209787080979043183175178343525"),
std::string("4.601860472890328982970020344164597095017995523702194983334509372565826509463730150117899613177592949E8"),
std::string("3.454041008184686240269652058630805927805404685705614002929256804267342980143530922708514273650048389E16"),
std::string("2.592516517287682590319488890844344913780990651072642232646840608443538857001634005575916140667342752E24"),
std::string("1.945877850460679347837790546862626024227287564971475316486097406542338497232351222296834286913627889E32")
};
for(std::size_t index { std::size_t { UINT8_C(0) }}; index < std::tuple_size<string_data_array_type>::value; ++index)
{
const any_float_type start(boost::lexical_cast<any_float_type>(float_number_strings[index]) * (dis(gen)* dis(gen)));
const other_float_type other(static_cast<other_float_type>(start));
const any_float_type backto(static_cast<any_float_type>(other));
const bool
result_of_trip_is_ok
{
local::is_close_fraction
(
start,
backto,
any_float_type(std::numeric_limits<any_float_type>::epsilon() * epsilon_factor)
)
};
BOOST_TEST(result_of_trip_is_ok);
result_is_ok = (result_of_trip_is_ok && result_is_ok);
}
}
return result_is_ok;
}
template<class FloatType>
bool test_edges()
{
using float_type = FloatType;
std::mt19937_64 gen { time_point<typename std::mt19937_64::result_type>() };
auto dis =
std::uniform_real_distribution<float>
{
static_cast<float>(1.01F),
static_cast<float>(1.04F)
};
bool result_is_ok { true };
using std::isinf;
using std::isnan;
using std::signbit;
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
static_cast<void>(index);
float_type flt_non_finite { };
long double ldbl_non_finite { };
bool result_non_finite_is_ok { };
flt_non_finite = std::numeric_limits<float_type>::quiet_NaN() * dis(gen);
ldbl_non_finite = static_cast<long double>(flt_non_finite);
result_non_finite_is_ok = (isnan)(ldbl_non_finite);
BOOST_TEST(result_non_finite_is_ok);
result_is_ok = (result_non_finite_is_ok && result_is_ok);
flt_non_finite = std::numeric_limits<float_type>::infinity() * dis(gen);
ldbl_non_finite = static_cast<long double>(flt_non_finite);
result_non_finite_is_ok = (isinf)(ldbl_non_finite);
BOOST_TEST(result_non_finite_is_ok);
result_is_ok = (result_non_finite_is_ok && result_is_ok);
flt_non_finite = std::numeric_limits<float_type>::infinity() * -dis(gen);
ldbl_non_finite = static_cast<long double>(flt_non_finite);
result_non_finite_is_ok = ((isinf)(ldbl_non_finite) && signbit(ldbl_non_finite));
BOOST_TEST(result_non_finite_is_ok);
result_is_ok = (result_non_finite_is_ok && result_is_ok);
flt_non_finite = float_type { (std::numeric_limits<long double>::max)() }
* float_type { (std::numeric_limits<long double>::max)() }
* dis(gen);
ldbl_non_finite = static_cast<long double>(flt_non_finite);
result_non_finite_is_ok = (isinf)(ldbl_non_finite);
BOOST_TEST(result_non_finite_is_ok);
result_is_ok = (result_non_finite_is_ok && result_is_ok);
flt_non_finite = float_type { (std::numeric_limits<long double>::max)() }
* float_type { (std::numeric_limits<long double>::max)() }
* -dis(gen);
ldbl_non_finite = static_cast<long double>(flt_non_finite);
result_non_finite_is_ok = ((isinf)(ldbl_non_finite) && signbit(ldbl_non_finite));
BOOST_TEST(result_non_finite_is_ok);
result_is_ok = (result_non_finite_is_ok && result_is_ok);
}
}
#ifdef BOOST_HAS_INT128
// TBD: Is this supposed to work for cpp_double_double?
BOOST_IF_CONSTEXPR(!::has_poor_exp_range_or_precision_support<float_type>::value)
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(64)); ++index)
{
float_type
flt_n128
{
float_type { (std::numeric_limits<signed long long>::max)() }
* float_type { dis(gen) * 12345.0F }
* float_type { dis(gen) * dis(gen) }
};
const bool is_neg { ((index & 1U) != 0U) };
if(is_neg)
{
flt_n128 = -flt_n128;
}
boost::int128_type val_n128 { static_cast<boost::int128_type>(flt_n128) };
bool result_val_n128_is_ok { };
const auto
str128_maker
{
[](const float_type& flt)
{
std::stringstream strm { };
strm << std::fixed << flt;
std::string str_local { strm.str() };
str_local = str_local.substr(std::size_t { UINT8_C(0) }, str_local.find('.'));
return str_local;
}
};
const std::string str_ctrl { str128_maker(flt_n128) };
const std::string str_n128 { str128_maker(static_cast<float_type>(val_n128)) };
// This test assumes that boost::int128_type is as wide or wider than signed long long.
// Consider using a a kind of if-constexpr to verify this "up front".
result_val_n128_is_ok =
(
(str_n128 == str_ctrl)
&& (
(!is_neg) ? (val_n128 >= static_cast<boost::int128_type>((std::numeric_limits<signed long long>::max)()))
: (val_n128 <= static_cast<boost::int128_type>(std::numeric_limits<signed long long>::lowest()))
)
);
BOOST_TEST(result_val_n128_is_ok);
result_is_ok = (result_val_n128_is_ok && result_is_ok);
}
}
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(32)); ++index)
{
constexpr boost::int128_type my_max_val_n128 = (((static_cast<boost::int128_type>(1) << (sizeof(boost::int128_type) * CHAR_BIT - 2)) - 1) << 1) + 1;
constexpr boost::int128_type my_min_val_n128 = static_cast<boost::int128_type>(-my_max_val_n128 - 1);
const float_type
flt_n128
{
float_type { -my_max_val_n128 }
- ::my_one<float_type>()
+ float_type { ::my_zero<float_type>() + float_type { dis(gen) } }
- ::my_one<float_type>()
- ::my_one<float_type>()
};
const boost::int128_type val_n128 { static_cast<boost::int128_type>(flt_n128) };
const bool result_val_n128_is_ok { (val_n128 == my_min_val_n128) };
BOOST_TEST(result_val_n128_is_ok);
result_is_ok = (result_val_n128_is_ok && result_is_ok);
}
}
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
static_cast<void>(index);
const float_type flt_under_one { float_type { ::my_one<float_type>() / dis(gen) } / dis(gen) };
const bool
result_under_one_is_ok
{
(static_cast<boost::int128_type>(flt_under_one) == static_cast<boost::int128_type>(INT8_C(0)))
};
BOOST_TEST(result_under_one_is_ok);
result_is_ok = (result_under_one_is_ok && result_is_ok);
}
}
#endif
{
const std::initializer_list<std::string>
funky_strings_list
{
std::string("3.14u"), std::string("3.14l"), std::string("3.14U"), std::string("3.14L"),
std::string("3.14ul"), std::string("3.14Ul"), std::string("3.14uL"), std::string("3.14UL"),
std::string("3.14ull"), std::string("3.14Ull"), std::string("3.14ULl"), std::string("3.14ULL"), std::string("3.14uLL"), std::string("3.14ulL")
};
const std::vector<std::string> funky_strings(funky_strings_list);
std::size_t funky_count { };
for(const auto& str : funky_strings)
{
try
{
const float_type flt_from_bad_string(str);
static_cast<void>(flt_from_bad_string);
}
catch(const std::runtime_error& excp)
{
static_cast<void>(excp.what());
++funky_count;
}
}
const bool result_funky_strings_is_ok { (funky_count == funky_strings.size()) };
BOOST_TEST(result_funky_strings_is_ok);
result_is_ok = (result_funky_strings_is_ok && result_is_ok);
}
{
const std::initializer_list<std::string>
infty_strings_list
{
std::string("inf"), std::string("INF"), std::string("infinity"), std::string("INFINITY"),
std::string("+inf"), std::string("+INF"), std::string("+infinity"), std::string("+INFINITY"),
std::string("-inf"), std::string("-INF"), std::string("-infinity"), std::string("-INFINITY")
};
const std::vector<std::string> infty_strings(infty_strings_list);
std::size_t infty_count { };
for(const auto& str : infty_strings)
{
const float_type flt_from_inf_string(str);
const bool
result_infty_strings_is_ok
{
(boost::multiprecision::isinf)(flt_from_inf_string)
&& ((infty_count < std::size_t { UINT8_C(8) }) ? (!signbit(flt_from_inf_string)) : signbit(flt_from_inf_string))
};
++infty_count;
BOOST_TEST(result_infty_strings_is_ok);
result_is_ok = (result_infty_strings_is_ok && result_is_ok);
}
}
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
static_cast<void>(index);
float_type flt_nrm { ::my_one<float_type>() * dis(gen) };
const bool is_neg { ((index & 1U) != 0U) };
if(is_neg)
{
flt_nrm = -flt_nrm;
}
const float_type flt_zer { ::my_zero<float_type>() * dis(gen) };
flt_nrm /= flt_zer;
bool result_div_zero_is_ok { (boost::multiprecision::isinf)(flt_nrm) };
if(is_neg)
{
result_div_zero_is_ok = ((boost::multiprecision::signbit)(flt_nrm) && result_div_zero_is_ok);
}
BOOST_TEST(result_div_zero_is_ok);
result_is_ok = (result_div_zero_is_ok && result_is_ok);
}
}
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
static_cast<void>(index);
float_type flt_nrm { ::my_one<float_type>() + 0.25F };
float_type flt_one { ::my_one<float_type>() + ::my_zero<float_type>() * dis(gen) };
const bool is_neg { ((index & 1U) != 0U) };
if(is_neg)
{
flt_one = -flt_one;
}
flt_nrm /= flt_one;
bool result_div_one_is_ok { (is_neg ? (flt_nrm == -1.25F) : (flt_nrm == 1.25F)) };
BOOST_TEST(result_div_one_is_ok);
result_is_ok = (result_div_one_is_ok && result_is_ok);
}
}
constexpr unsigned max_index { 1024U };
{
using std::ldexp;
float_type flt_near_min { ldexp((std::numeric_limits<float_type>::min)(), +64) };
unsigned index { };
while((index < max_index) && (!((boost::multiprecision::fpclassify)(flt_near_min) == FP_ZERO)))
{
flt_near_min /= static_cast<unsigned long long>(0xDEADBEEF);
++index;
}
const bool result_unf_is_ok { ((index > 1U) && (index < max_index)) };
BOOST_TEST(result_unf_is_ok);
result_is_ok = (result_unf_is_ok && result_is_ok);
}
{
using std::ldexp;
float_type flt_near_min { ldexp((std::numeric_limits<float_type>::min)(), +64) };
unsigned index { };
while((index < max_index) && (!((boost::multiprecision::fpclassify)(flt_near_min) == FP_ZERO)))
{
flt_near_min /= static_cast<unsigned long long>(100000000ULL - 3ULL);
++index;
}
const bool result_unf_is_ok { ((index > 1U) && (index < max_index)) };
BOOST_TEST(result_unf_is_ok);
result_is_ok = (result_unf_is_ok && result_is_ok);
}
{
using std::ldexp;
float_type flt_near_max { ldexp((std::numeric_limits<float_type>::max)(), -1) };
const float_type flt_less_near_max { ldexp((std::numeric_limits<float_type>::max)(), -4) };
unsigned index { };
while((index < max_index) && (!(boost::multiprecision::isinf)(flt_near_max)))
{
flt_near_max += (flt_less_near_max * dis(gen));
++index;
}
const bool result_ovf_is_ok { ((index > 1U) && (index < max_index)) };
BOOST_TEST(result_ovf_is_ok);
result_is_ok = (result_ovf_is_ok && result_is_ok);
}
{
using std::ldexp;
float_type flt_near_lowest { -ldexp((std::numeric_limits<float_type>::max)(), -1) };
const float_type flt_less_near_max { ldexp((std::numeric_limits<float_type>::max)(), -4) };
unsigned index { };
while((index < max_index) && (!(boost::multiprecision::isinf)(flt_near_lowest)))
{
flt_near_lowest -= (flt_less_near_max * dis(gen));
++index;
}
const bool result_ovf_is_ok { ((index > 1U) && (index < max_index)) && signbit(flt_near_lowest) };
BOOST_TEST(result_ovf_is_ok);
result_is_ok = (result_ovf_is_ok && result_is_ok);
}
{
using std::ldexp;
float_type flt_near_lowest { -ldexp((std::numeric_limits<float_type>::max)(), -1) };
const float_type neg_flt_less_near_max { -ldexp((std::numeric_limits<float_type>::max)(), -4) };
unsigned index { };
while((index < max_index) && (!(boost::multiprecision::isinf)(flt_near_lowest)))
{
flt_near_lowest += (neg_flt_less_near_max * dis(gen));
++index;
}
const bool result_ovf_is_ok { ((index > 1U) && (index < max_index)) && signbit(flt_near_lowest) };
BOOST_TEST(result_ovf_is_ok);
result_is_ok = (result_ovf_is_ok && result_is_ok);
}
for(int n_loop = static_cast<int>(INT8_C(-16)); n_loop < static_cast<int>(INT8_C(-8)); ++n_loop)
{
using std::ldexp;
using std::fabs;
float_type flt_near_max { ldexp((std::numeric_limits<float_type>::max)(), n_loop) * dis(gen) };
unsigned index { };
while((index < max_index) && (!(boost::multiprecision::isinf)(flt_near_max)))
{
flt_near_max *= static_cast<unsigned long long>(INT32_C(2));
++index;
}
const bool result_ovf_is_ok { ((index > 1U) && (index < max_index)) };
BOOST_TEST(result_ovf_is_ok);
result_is_ok = (result_ovf_is_ok && result_is_ok);
}
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
static_cast<void>(index);
const float_type flt_nan_denominator { float_type { std::numeric_limits<float>::quiet_NaN() } * dis(gen) };
float_type flt_zero_numerator { ::my_zero<float_type>() * dis(gen) };
const bool
result_div_zero_by_nan_is_ok
{
(boost::multiprecision::isnan)(flt_nan_denominator)
&& (boost::multiprecision::isnan)(flt_zero_numerator /= flt_nan_denominator)
};
BOOST_TEST(result_div_zero_by_nan_is_ok);
result_is_ok = (result_div_zero_by_nan_is_ok && result_is_ok);
}
}
{
const float_type flt_nan { ::my_nan<float_type>() };
const float_type flt_inf { ::my_inf<float_type>() };
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
static_cast<void>(index);
flt_nan * dis(gen);
flt_inf * dis(gen);
const float_type finite { float_type { 123 } / 100 };
const float_type eq_nan = finite + flt_nan;
const float_type eq_inf = finite + flt_inf;
const bool result_nan_is_ok = (boost::multiprecision::isnan)(eq_nan);
const bool result_inf_is_ok = (boost::multiprecision::isinf)(eq_inf);
BOOST_TEST(result_nan_is_ok);
BOOST_TEST(result_inf_is_ok);
}
}
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
static_cast<void>(index);
float_type flt_zer_numerator { ::my_zero<float_type>() * dis(gen) };
float_type flt_nrm_numerator { ::my_one<float_type>() * dis(gen) };
float_type flt_nan_numerator { ::my_nan<float_type>() * dis(gen) };
float_type flt_inf_numerator { ::my_inf<float_type>() * dis(gen) };
const float_type flt_inf_result_neg_01 { -flt_nrm_numerator /= static_cast<signed long long>(INT8_C(0)) };
const float_type flt_inf_result_neg_02 { -flt_inf_numerator *= static_cast<signed long long>(index + 2U) };
const bool result_edge_00 { (boost::multiprecision::isnan)(flt_zer_numerator /= static_cast<signed long long>(INT8_C(0))) };
const bool result_edge_01 { (boost::multiprecision::isinf)(flt_nrm_numerator /= static_cast<signed long long>(INT8_C(0))) };
const bool result_edge_02 { (boost::multiprecision::isinf)(flt_inf_result_neg_01) && (boost::multiprecision::signbit)(flt_inf_result_neg_01) };
const bool result_edge_03 { (boost::multiprecision::isnan)(flt_nan_numerator /= static_cast<signed long long>(index + 2U)) };
const bool result_edge_04 { (boost::multiprecision::isnan)(flt_nan_numerator *= static_cast<signed long long>(index + 2U)) };
const bool result_edge_05 { (boost::multiprecision::isnan)(flt_inf_numerator *= static_cast<signed long long>(0)) };
const bool result_edge_06 { (boost::multiprecision::isinf)(flt_inf_result_neg_02) && (boost::multiprecision::signbit)(flt_inf_result_neg_02) };
const bool
result_divs_are_ok
{
(
result_edge_00
&& result_edge_01
&& result_edge_02
&& result_edge_03
&& result_edge_04
&& result_edge_05
&& result_edge_06
)
};
BOOST_TEST(result_divs_are_ok);
result_is_ok = (result_divs_are_ok && result_is_ok);
}
}
BOOST_IF_CONSTEXPR((std::numeric_limits<float>::digits >= 24) && std::numeric_limits<float_type>::is_specialized)
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
// This test is specifically designed to pick up hard-to-reach lines
// in cpp_dec_float. Notice how the printed string is expected to have
// (or not have) rounding at the 8th fixed-point digit.
// N[1 + 1/2^4 + 1/2^5 + 1/2^7 + 1/2^9, 20]
const float flt_builtin { 1.1035156250000000000F };
const float_type delta = std::numeric_limits<float_type>::epsilon() * dis(gen) * static_cast<float>(index + 1U);
const float_type flt_val_base { flt_builtin };
const float_type flt_val_close_upper { flt_builtin + delta };
const float_type flt_val_close_lower { flt_builtin - delta };
{
std::stringstream strm { };
strm << std::fixed << std::setprecision(std::streamsize { INT8_C(8) }) << flt_val_base;
BOOST_TEST(strm.str() == "1.10351562");
}
{
std::stringstream strm { };
strm << std::fixed << std::setprecision(std::streamsize { INT8_C(8) }) << flt_val_close_upper;
BOOST_TEST(strm.str() > "1.10351562");
}
{
std::stringstream strm { };
strm << std::fixed << std::setprecision(std::streamsize { INT8_C(8) }) << flt_val_close_lower;
BOOST_TEST(strm.str() == "1.10351562");
}
}
}
return result_is_ok;
}
template<class FloatType>
bool test_edges_ovf_und()
{
using float_type = FloatType;
std::mt19937_64 gen { time_point<typename std::mt19937_64::result_type>() };
auto dis =
std::uniform_real_distribution<float>
{
static_cast<float>(1.01F),
static_cast<float>(1.04F)
};
bool result_is_ok { true };
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(16)); ++index)
{
static_cast<void>(index);
const float_type flt_ovf = ::my_generator_overflow<float_type>() * dis(gen);
const float_type flt_und = ::my_generator_underflow<float_type>() * dis(gen);
const bool
result_ovf_und_is_ok
{
((boost::multiprecision::isinf)(flt_ovf) && ((boost::multiprecision::fpclassify)(flt_und) == FP_ZERO))
};
BOOST_TEST(result_ovf_und_is_ok);
result_is_ok = (result_ovf_und_is_ok && result_is_ok);
}
}
return result_is_ok;
}
template<class FloatType>
bool test_edges_trig()
{
using float_type = FloatType;
std::mt19937_64 gen { time_point<typename std::mt19937_64::result_type>() };
auto dis =
std::uniform_real_distribution<float>
{
static_cast<float>(1.01F),
static_cast<float>(1.04F)
};
bool result_is_ok { true };
using std::isinf;
using std::isnan;
using std::signbit;
BOOST_IF_CONSTEXPR(std::numeric_limits<float_type>::has_quiet_NaN)
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(8)); ++index)
{
static_cast<void>(index);
float_type flt_nan = std::numeric_limits<float_type>::quiet_NaN() * dis(gen);
const float_type atan_nan = atan(flt_nan);
const bool result_atan_nan_is_ok { (boost::multiprecision::isnan)(atan_nan) && ((boost::multiprecision::fpclassify)(atan_nan) == FP_NAN) };
BOOST_TEST(result_atan_nan_is_ok);
result_is_ok = (result_atan_nan_is_ok && result_is_ok);
}
}
return result_is_ok;
}
template<class OtherFloatType>
auto test_convert_and_back_caller(const float epsilon_factor = 0.0F) -> bool
{
using other_float_type = OtherFloatType;
std::cout << "Testing type (in test_convert_and_back): " << typeid(other_float_type).name() << std::endl;
const bool
result_of_trip_is_ok
{
local::test_convert_and_back<boost::multiprecision::cpp_double_double, other_float_type>(epsilon_factor)
};
BOOST_TEST(result_of_trip_is_ok);
return result_of_trip_is_ok;
}
template<class CppDecFloatType>
auto test_cpp_dec_float_rd_ovf_unf() -> void
{
using local_cpp_dec_float_type = CppDecFloatType;
{
std::string str_max_pos_01("+1.0E+");
std::string str_max_pos_02("+2.0E+");
std::string str_max_pos_03("+3.0E+");
std::string str_max_pos_04("+0.9E+");
std::string str_max_neg_01("-1.0E+");
std::string str_max_neg_02("-2.0E+");
std::string str_max_neg_03("-3.0E+");
std::string str_max_neg_04("-0.9E+");
{
std::stringstream strm { };
strm << std::numeric_limits<local_cpp_dec_float_type>::max_exponent10;
str_max_pos_01 += strm.str();
str_max_pos_02 += strm.str();
str_max_pos_03 += strm.str();
str_max_pos_04 += strm.str();
str_max_neg_01 += strm.str();
str_max_neg_02 += strm.str();
str_max_neg_03 += strm.str();
str_max_neg_04 += strm.str();
}
const local_cpp_dec_float_type max_pos_01(str_max_pos_01);
const local_cpp_dec_float_type max_pos_02(str_max_pos_02);
const local_cpp_dec_float_type max_pos_03(str_max_pos_03);
const local_cpp_dec_float_type max_pos_04(str_max_pos_04);
const local_cpp_dec_float_type max_neg_01(str_max_neg_01);
const local_cpp_dec_float_type max_neg_02(str_max_neg_02);
const local_cpp_dec_float_type max_neg_03(str_max_neg_03);
const local_cpp_dec_float_type max_neg_04(str_max_neg_04);
using std::signbit;
BOOST_TEST(max_pos_01 == (std::numeric_limits<local_cpp_dec_float_type>::max)());
BOOST_TEST((boost::multiprecision::isinf)(max_pos_02) && (!signbit(max_pos_02)));
BOOST_TEST((boost::multiprecision::isinf)(max_pos_03) && (!signbit(max_pos_03)));
BOOST_TEST(((boost::multiprecision::fpclassify)(max_pos_04) == FP_NORMAL) && (!signbit(max_pos_04)));
BOOST_TEST(max_neg_01 == (std::numeric_limits<local_cpp_dec_float_type>::lowest)());
BOOST_TEST((boost::multiprecision::isinf)(max_neg_02) && signbit(max_neg_02));
BOOST_TEST((boost::multiprecision::isinf)(max_neg_03) && signbit(max_neg_03));
BOOST_TEST(((boost::multiprecision::fpclassify)(max_neg_04) == FP_NORMAL) && signbit(max_neg_04));
}
{
std::string str_min_pos_01("+1.0E");
std::string str_min_pos_02("+1.1E");
std::string str_min_pos_03("+0.9E");
std::string str_min_neg_01("-1.0E");
std::string str_min_neg_02("-1.1E");
std::string str_min_neg_03("-0.9E");
{
std::stringstream strm { };
strm << std::numeric_limits<local_cpp_dec_float_type>::min_exponent10;
str_min_pos_01 += strm.str();
str_min_pos_02 += strm.str();
str_min_pos_03 += strm.str();
str_min_neg_01 += strm.str();
str_min_neg_02 += strm.str();
str_min_neg_03 += strm.str();
}
const local_cpp_dec_float_type min_pos_01(str_min_pos_01);
const local_cpp_dec_float_type min_pos_02(str_min_pos_02);
const local_cpp_dec_float_type min_pos_03(str_min_pos_03);
const local_cpp_dec_float_type min_neg_01(str_min_neg_01);
const local_cpp_dec_float_type min_neg_02(str_min_neg_02);
const local_cpp_dec_float_type min_neg_03(str_min_neg_03);
BOOST_TEST(min_pos_01 == (std::numeric_limits<local_cpp_dec_float_type>::min)());
BOOST_TEST(((boost::multiprecision::fpclassify)(min_pos_02) == FP_NORMAL) && (!signbit(min_pos_02)));
BOOST_TEST(((boost::multiprecision::fpclassify)(min_pos_03) == FP_ZERO) && (!signbit(min_pos_03)));
BOOST_TEST(min_neg_01 == -(std::numeric_limits<local_cpp_dec_float_type>::min)());
BOOST_TEST(((boost::multiprecision::fpclassify)(min_neg_02) == FP_NORMAL) && signbit(min_neg_02));
BOOST_TEST(((boost::multiprecision::fpclassify)(min_neg_03) == FP_ZERO) && (!signbit(min_neg_03)));
}
}
template<class AnyFloatType>
auto test_frexp_edge() -> void
{
using float_type = AnyFloatType;
using float_backend_ctrl_type = boost::multiprecision::cpp_bin_float<50, boost::multiprecision::digit_base_10, void, std::int32_t>;
using float_ctrl_type = boost::multiprecision::number<float_backend_ctrl_type, boost::multiprecision::et_off>;
std::mt19937_64 gen { time_point<typename std::mt19937_64::result_type>() };
auto dis =
std::uniform_real_distribution<float>
{
static_cast<float>(1.01F),
static_cast<float>(1.04F)
};
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(32)); ++index)
{
static_cast<void>(index);
using local_exponent_type = typename float_type::backend_type::exponent_type;
local_exponent_type exp_val { };
std::int32_t exp_val_ctrl { };
float_type arg_near_max { (std::numeric_limits<float_type>::max)() / dis(gen) };
const bool is_neg { ((index & 1U) != 0U) };
if(is_neg)
{
arg_near_max = -arg_near_max;
}
float_type frexp_near_max { frexp(arg_near_max, &exp_val) };
const float_ctrl_type frexp_near_max_ctrl { frexp(float_ctrl_type(arg_near_max), &exp_val_ctrl) };
BOOST_TEST((boost::multiprecision::isfinite)(frexp_near_max));
BOOST_TEST(exp_val >= (std::numeric_limits<float_type>::max_exponent - 8));
BOOST_TEST(exp_val == exp_val_ctrl);
BOOST_TEST(is_close_fraction(float_type(frexp_near_max_ctrl), frexp_near_max, std::numeric_limits<float_type>::epsilon() * 8));
}
}
}
auto test_double_fp_string_gt_max() -> void
{
using float_backend_larger_type = boost::multiprecision::cpp_bin_float<50, boost::multiprecision::digit_base_10, void, std::int32_t>;
using float_larger_type = boost::multiprecision::number<float_backend_larger_type, boost::multiprecision::et_off>;
std::mt19937_64 gen { time_point<typename std::mt19937_64::result_type>() };
auto dis =
std::uniform_real_distribution<double>
{
static_cast<double>(1.01),
static_cast<double>(1.04)
};
{
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(32)); ++index)
{
const bool make_neg { ((index % unsigned { UINT8_C(2) }) != unsigned { UINT8_C(0) }) };
const double dbl_fuzz { 1.0 - dis(gen) * std::numeric_limits<double>::epsilon() };
float_larger_type
flt_larger
(
float_larger_type((std::numeric_limits<boost::multiprecision::cpp_double_double>::max)())
/ dbl_fuzz
);
if(make_neg)
{
flt_larger = -flt_larger;
}
std::stringstream strm { };
strm << std::setprecision(std::numeric_limits<boost::multiprecision::cpp_double_double>::max_digits10)
<< flt_larger;
const std::string str_ovf { strm.str() };
const boost::multiprecision::cpp_double_double val_ovf(str_ovf.c_str());
BOOST_TEST((boost::multiprecision::isinf)(val_ovf));
if(make_neg)
{
BOOST_TEST((boost::multiprecision::signbit)(val_ovf));
}
}
}
}
} // namespace local
auto main() -> int
{
{
using dec_float_backend_type = boost::multiprecision::cpp_dec_float<50>;
using dec_float_type = boost::multiprecision::number<dec_float_backend_type, boost::multiprecision::et_off>;
std::cout << "Testing type: " << typeid(dec_float_type).name() << std::endl;
static_cast<void>(local::test_edges<dec_float_type>());
static_cast<void>(local::test_edges_ovf_und<dec_float_type>());
static_cast<void>(local::test_edges_trig<dec_float_type>());
local::test_cpp_dec_float_rd_ovf_unf<dec_float_type>();
local::test_convert_and_back<double, dec_float_type>(0.0F);
local::test_frexp_edge<dec_float_type>();
}
{
using bin_float_backend_type = boost::multiprecision::cpp_bin_float<50>;
using bin_float_type = boost::multiprecision::number<bin_float_backend_type, boost::multiprecision::et_off>;
std::cout << "Testing type: " << typeid(bin_float_type).name() << std::endl;
static_cast<void>(local::test_edges<bin_float_type>());
// TBD: This seemingly trivial test fails for cpp_bin_float.
//static_cast<void>(local::test_edges_ovf_und<bin_float_type>());
static_cast<void>(local::test_edges_trig<bin_float_type>());
local::test_frexp_edge<bin_float_type>();
}
{
using double_float_type = boost::multiprecision::cpp_double_double;
std::cout << "Testing type: " << typeid(double_float_type).name() << std::endl;
static_cast<void>(local::test_edges<double_float_type>());
static_cast<void>(local::test_edges_ovf_und<double_float_type>());
static_cast<void>(local::test_edges_trig<double_float_type>());
local::test_frexp_edge<double_float_type>();
local::test_double_fp_string_gt_max();
}
{
using big_bin_float_backend_type = boost::multiprecision::cpp_bin_float<512>;
using big_dec_float_backend_type = boost::multiprecision::cpp_dec_float<512>;
using big_bin_float_type = boost::multiprecision::number<big_bin_float_backend_type, boost::multiprecision::et_off>;
using big_dec_float_type = boost::multiprecision::number<big_dec_float_backend_type, boost::multiprecision::et_off>;
static_cast<void>(local::test_convert_and_back_caller<big_bin_float_type>(0.25F));
static_cast<void>(local::test_convert_and_back_caller<big_dec_float_type>(0.50F));
}
return boost::report_errors();
}
template<typename FloatType> auto my_zero() noexcept -> FloatType& { using float_type = FloatType; static float_type val_zero { 0 }; return val_zero; }
template<typename FloatType> auto my_one () noexcept -> FloatType& { using float_type = FloatType; static float_type val_one { 1 }; return val_one; }
template<typename FloatType> auto my_inf () noexcept -> FloatType& { using float_type = FloatType; static float_type val_inf { std::numeric_limits<float_type>::infinity() }; return val_inf; }
template<typename FloatType> auto my_nan () noexcept -> FloatType& { using float_type = FloatType; static float_type val_nan { std::numeric_limits<float_type>::quiet_NaN() }; return val_nan; }
template<class FloatType>
static auto
overflow_expval_maker
{
[]()
{
std::stringstream strm { };
using float_type = FloatType;
strm << std::numeric_limits<float_type>::max_exponent10 + 4;
return strm.str();
}
};
template<class FloatType>
static auto
underflow_expval_maker
{
[]()
{
std::stringstream strm { };
using float_type = FloatType;
strm << std::numeric_limits<float_type>::min_exponent10 - 4;
return strm.str();
}
};
template<typename FloatType>
auto my_generator_overflow() noexcept -> FloatType
{
using float_type = FloatType;
const float_type flt_ovf("1.23E" + overflow_expval_maker<float_type>());
const float_type result(flt_ovf);
return result;
}
template<typename FloatType>
auto my_generator_underflow() noexcept -> FloatType
{
using float_type = FloatType;
float_type flt_und("1.23E" + underflow_expval_maker<float_type>());
const float_type result(flt_und);
return result;
}
#if (defined(__clang__) || defined(__GNUC__))
# pragma GCC diagnostic pop
#endif
|