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
|
// Copyright John Maddock 2006.
// Copyright Paul A. Bristow 2007
// 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)
#include <pch.hpp>
#include <cmath>
#include <math.h>
#include <boost/limits.hpp>
#include <boost/math/concepts/real_concept.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <iomanip>
#ifdef _MSC_VER
#pragma warning(disable: 4127 4146) // conditional expression is constant
#endif
const char* method_name(const boost::math::detail::native_tag&)
{
return "Native";
}
const char* method_name(const boost::math::detail::generic_tag<true>&)
{
return "Generic (with numeric limits)";
}
const char* method_name(const boost::math::detail::generic_tag<false>&)
{
return "Generic (without numeric limits)";
}
const char* method_name(const boost::math::detail::ieee_tag&)
{
return "IEEE std";
}
const char* method_name(const boost::math::detail::ieee_copy_all_bits_tag&)
{
return "IEEE std, copy all bits";
}
const char* method_name(const boost::math::detail::ieee_copy_leading_bits_tag&)
{
return "IEEE std, copy leading bits";
}
template <class T>
void test_classify(T t, const char* type)
{
std::cout << "Testing type " << type << std::endl;
typedef typename boost::math::detail::fp_traits<T>::type traits;
typedef typename traits::method method;
std::cout << "Evaluation method = " << method_name(method()) << std::endl;
t = 2;
T u = 2;
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_NORMAL);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_NORMAL);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), true);
if(std::numeric_limits<T>::is_specialized)
{
t = (std::numeric_limits<T>::max)();
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_NORMAL);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_NORMAL);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), true);
t = (std::numeric_limits<T>::min)();
if(t != 0)
{
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_NORMAL);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), true);
if(!std::numeric_limits<T>::is_integer)
{
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_NORMAL);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
}
}
}
if(std::numeric_limits<T>::has_denorm)
{
t = (std::numeric_limits<T>::min)();
t /= 2;
if(t != 0)
{
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_SUBNORMAL);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_SUBNORMAL);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
}
t = std::numeric_limits<T>::denorm_min();
if((t != 0) && (t < (std::numeric_limits<T>::min)()))
{
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_SUBNORMAL);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_SUBNORMAL);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
}
}
else
{
std::cout << "Denormalised forms not tested" << std::endl;
}
t = 0;
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_ZERO);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_ZERO);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
t /= -u; // create minus zero if it exists
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_ZERO);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_ZERO);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
// infinity:
if(std::numeric_limits<T>::has_infinity)
{
// At least one std::numeric_limits<T>::infinity)() returns zero
// (Compaq true64 cxx), hence the check.
t = (std::numeric_limits<T>::infinity)();
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_INFINITE);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_INFINITE);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
#if !defined(BOOST_BORLANDC) && !(defined(__DECCXX) && !defined(_IEEE_FP))
// divide by zero on Borland triggers a C++ exception :-(
// divide by zero on Compaq CXX triggers a C style signal :-(
t = 2;
u = 0;
t /= u;
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_INFINITE);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_INFINITE);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
t = -2;
t /= u;
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_INFINITE);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_INFINITE);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
#else
std::cout << "Infinities from divide by zero not tested" << std::endl;
#endif
}
else
{
std::cout << "Infinity not tested" << std::endl;
}
#ifndef BOOST_BORLANDC
// NaN's:
// Note that Borland throws an exception if we even try to obtain a Nan
// by calling std::numeric_limits<T>::quiet_NaN() !!!!!!!
if(std::numeric_limits<T>::has_quiet_NaN)
{
t = std::numeric_limits<T>::quiet_NaN();
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_NAN);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_NAN);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
}
else
{
std::cout << "Quiet NaN's not tested" << std::endl;
}
if(std::numeric_limits<T>::has_signaling_NaN)
{
t = std::numeric_limits<T>::signaling_NaN();
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(t), (int)FP_NAN);
BOOST_CHECK_EQUAL((::boost::math::fpclassify)(-t), (int)FP_NAN);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isfinite)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isinf)(-t), false);
BOOST_CHECK_EQUAL((::boost::math::isnan)(t), true);
BOOST_CHECK_EQUAL((::boost::math::isnan)(-t), true);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(t), false);
BOOST_CHECK_EQUAL((::boost::math::isnormal)(-t), false);
}
else
{
std::cout << "Signaling NaN's not tested" << std::endl;
}
#endif
}
BOOST_AUTO_TEST_CASE( test_main )
{
BOOST_MATH_CONTROL_FP;
// start by printing some information:
#ifdef isnan
std::cout << "Platform has isnan macro." << std::endl;
#endif
#ifdef fpclassify
std::cout << "Platform has fpclassify macro." << std::endl;
#endif
#ifdef BOOST_HAS_FPCLASSIFY
std::cout << "Platform has FP_NORMAL macro." << std::endl;
#endif
std::cout << "FP_ZERO: " << (int)FP_ZERO << std::endl;
std::cout << "FP_NORMAL: " << (int)FP_NORMAL << std::endl;
std::cout << "FP_INFINITE: " << (int)FP_INFINITE << std::endl;
std::cout << "FP_NAN: " << (int)FP_NAN << std::endl;
std::cout << "FP_SUBNORMAL: " << (int)FP_SUBNORMAL << std::endl;
// then run the tests:
test_classify(float(0), "float");
test_classify(double(0), "double");
// long double support for fpclassify is considered "core" so we always test it
// even when long double support is turned off via BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
test_classify((long double)(0), "long double");
test_classify((boost::math::concepts::real_concept)(0), "real_concept");
// We should test with integer types as well:
test_classify(int(0), "int");
test_classify(unsigned(0), "unsigned");
}
/*
Autorun "i:\Boost-sandbox\math_toolkit\libs\math\test\MSVC80\debug\test_classify.exe"
Running 1 test case...
FP_ZERO: 0
FP_NORMAL: 1
FP_INFINITE: 2
FP_NAN: 3
FP_SUBNORMAL: 4
Testing type float
Testing type double
Testing type long double
Testing type real_concept
Denormalised forms not tested
Infinity not tested
Quiet NaN's not tested
Signaling NaN's not tested
Test suite "Test Program" passed with:
79 assertions out of 79 passed
1 test case out of 1 passed
Test case "test_main_caller( argc, argv )" passed with:
79 assertions out of 79 passed
*/
|