|
| 1 | +// nextafter.cpp : exploration of the nextafter stdlib function to manipulate units in the last place |
| 2 | +// |
| 3 | +// Copyright (C) 2017 Stillwater Supercomputing, Inc. |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// |
| 6 | +// This file is part of the universal numbers project, which is released under an MIT Open Source license. |
| 7 | +#include <universal/utility/directives.hpp> |
| 8 | +#include <cmath> |
| 9 | +#include <concepts> |
| 10 | +#include <cfenv> |
| 11 | +#include <universal/verification/test_suite.hpp> |
| 12 | + |
| 13 | +// Regression testing guards: typically set by the cmake configuration, but MANUAL_TESTING is an override |
| 14 | +#define MANUAL_TESTING 0 |
| 15 | +// REGRESSION_LEVEL_OVERRIDE is set by the cmake file to drive a specific regression intensity |
| 16 | +// It is the responsibility of the regression test to organize the tests in a quartile progression. |
| 17 | +//#undef REGRESSION_LEVEL_OVERRIDE |
| 18 | +#ifndef REGRESSION_LEVEL_OVERRIDE |
| 19 | +#undef REGRESSION_LEVEL_1 |
| 20 | +#undef REGRESSION_LEVEL_2 |
| 21 | +#undef REGRESSION_LEVEL_3 |
| 22 | +#undef REGRESSION_LEVEL_4 |
| 23 | +#define REGRESSION_LEVEL_1 1 |
| 24 | +#define REGRESSION_LEVEL_2 1 |
| 25 | +#define REGRESSION_LEVEL_3 1 |
| 26 | +#define REGRESSION_LEVEL_4 1 |
| 27 | +#endif |
| 28 | + |
| 29 | +int main() |
| 30 | +try { |
| 31 | + using namespace sw::universal; |
| 32 | + |
| 33 | + std::string test_suite = "nextafter test"; |
| 34 | + std::string test_tag = "nextafter"; |
| 35 | + bool reportTestCases = false; |
| 36 | + int nrOfFailedTestCases = 0; |
| 37 | + |
| 38 | + ReportTestSuiteHeader(test_suite, reportTestCases); |
| 39 | + |
| 40 | + { |
| 41 | + float from = 0, to = std::nextafter(from, 1.f); |
| 42 | + std::cout << "The next representable float after " << std::setprecision(20) << from |
| 43 | + << " is " << to |
| 44 | + << std::hexfloat << " (" << to << ")\n" << std::defaultfloat; |
| 45 | + } |
| 46 | + |
| 47 | + { |
| 48 | + float from = 1, to = std::nextafter(from, 2.f); |
| 49 | + std::cout << "The next representable float after " << from << " is " << to |
| 50 | + << std::hexfloat << " (" << to << ")\n" << std::defaultfloat; |
| 51 | + } |
| 52 | + |
| 53 | + { |
| 54 | + double from = std::nextafter(0.1, 0), to = 0.1; |
| 55 | + std::cout << "The number 0.1 lies between two valid doubles:\n" |
| 56 | + << std::setprecision(56) << " " << from |
| 57 | + << std::hexfloat << " (" << from << ')' << std::defaultfloat |
| 58 | + << "\nand " << to << std::hexfloat << " (" << to << ")\n" |
| 59 | + << std::defaultfloat << std::setprecision(20); |
| 60 | + } |
| 61 | + |
| 62 | + { |
| 63 | + std::cout << "\nDifference between nextafter and nexttoward:\n"; |
| 64 | + float from = 0.0f; |
| 65 | + long double dir = std::nextafter(from, 1.0L); // first subnormal long double |
| 66 | + float x = std::nextafter(from, dir); // first converts dir to float, giving 0 |
| 67 | + std::cout << "With nextafter, next float after " << from << " is " << x << '\n'; |
| 68 | + x = std::nexttoward(from, dir); |
| 69 | + std::cout << "With nexttoward, next float after " << from << " is " << x << '\n'; |
| 70 | + } |
| 71 | + |
| 72 | + std::cout << "\nSpecial values:\n"; |
| 73 | + { |
| 74 | + // #pragma STDC FENV_ACCESS ON |
| 75 | + std::feclearexcept(FE_ALL_EXCEPT); |
| 76 | + double from4 = DBL_MAX, to4 = std::nextafter(from4, INFINITY); |
| 77 | + std::cout << "The next representable double after " << std::setprecision(6) |
| 78 | + << from4 << std::hexfloat << " (" << from4 << ')' |
| 79 | + << std::defaultfloat << " is " << to4 |
| 80 | + << std::hexfloat << " (" << to4 << ")\n" << std::defaultfloat; |
| 81 | + |
| 82 | + if (std::fetestexcept(FE_OVERFLOW)) |
| 83 | + std::cout << " raised FE_OVERFLOW\n"; |
| 84 | + if (std::fetestexcept(FE_INEXACT)) |
| 85 | + std::cout << " raised FE_INEXACT\n"; |
| 86 | + } // end FENV_ACCESS block |
| 87 | + |
| 88 | + { |
| 89 | + float from = 0.0, to = std::nextafter(from, -0.0); |
| 90 | + std::cout << "std::nextafter(+0.0, -0.0) gives " << std::fixed << to << '\n'; |
| 91 | + } |
| 92 | + |
| 93 | + auto precision_loss_demo = []<std::floating_point Fp>(const auto rem, const Fp start) |
| 94 | + { |
| 95 | + std::cout << rem; |
| 96 | + for (Fp from = start, to, delta; |
| 97 | + (delta = (to = std::nextafter(from, +INFINITY)) - from) < Fp(10.0); |
| 98 | + from *= Fp(10.0)) { |
| 99 | + std::cout << "nextafter(" << std::scientific << std::setprecision(0) << from |
| 100 | + << ", INF) gives " << std::fixed << std::setprecision(6) << to |
| 101 | + << "; delta = " << delta << '\n'; |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + precision_loss_demo("\nPrecision loss demo for float:\n", 10.0f); |
| 106 | + precision_loss_demo("\nPrecision loss demo for double:\n", 10.0e9); |
| 107 | + |
| 108 | +#if LONG_DOUBLE_SUPPORT |
| 109 | + precision_loss_demo("\nPrecision loss demo for long double:\n", 10.0e17L); |
| 110 | + |
| 111 | + constexpr long double denorm_min = std::numeric_limits<long double>::denorm_min(); |
| 112 | + std::cout << "smallest long double: " << to_binary(denorm_min) << " : " << denorm_min << '\n'; |
| 113 | +#endif |
| 114 | + |
| 115 | + |
| 116 | + auto ulp_progression = []<std::floating_point Fp>(const auto rem, const Fp start) |
| 117 | + { |
| 118 | + std::cout << rem; |
| 119 | + for (Fp from = start, to, delta; |
| 120 | + (delta = (to = std::nextafter(from, +INFINITY)) - from) < Fp(10.0); |
| 121 | + from *= Fp(10.0)) { |
| 122 | + std::cout << "ulp(" << std::scientific << std::setprecision(0) << from |
| 123 | + << ") gives " << to_binary(ulp(from)) << " : " |
| 124 | + << std::fixed << std::setprecision(6) << ulp(from) << '\n'; |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + ulp_progression("\nULP progression for float:\n", 10.0f); |
| 129 | + ulp_progression("\nULP progression for double:\n", 10.0e9); |
| 130 | + |
| 131 | + std::cout << '\n'; |
| 132 | + ReportTestSuiteResults(test_suite, nrOfFailedTestCases); |
| 133 | + return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 134 | +} |
| 135 | +catch (char const* msg) { |
| 136 | + std::cerr << msg << '\n'; |
| 137 | + return EXIT_FAILURE; |
| 138 | +} |
| 139 | +catch (const std::runtime_error& err) { |
| 140 | + std::cerr << "Uncaught runtime exception: " << err.what() << std::endl; |
| 141 | + return EXIT_FAILURE; |
| 142 | +} |
| 143 | +catch (...) { |
| 144 | + std::cerr << "Caught unknown exception" << '\n'; |
| 145 | + return EXIT_FAILURE; |
| 146 | +} |
0 commit comments