Repository navigation
Expand file tree
/
Copy pathexample_special_values.cpp
More file actions
63 lines (53 loc) · 1.85 KB
/
Copy pathexample_special_values.cpp
File metadata and controls
63 lines (53 loc) · 1.85 KB
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
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string_view>
#include <fltx.h>
using namespace bl;
using namespace bl::literals;
const char* class_name(int cls)
{
switch (cls)
{
case FP_INFINITE: return "infinite";
case FP_NAN: return "nan";
case FP_NORMAL: return "normal";
case FP_SUBNORMAL: return "subnormal";
case FP_ZERO: return "zero";
default: return "unknown";
}
}
template<class T>
void describe(std::string_view name, T value)
{
std::cout
<< std::boolalpha
<< name << ":\n"
<< " class = " << class_name(bl::fpclassify(value)) << "\n"
<< " finite = " << bl::isfinite(value) << "\n"
<< " infinite = " << bl::isinf(value) << "\n"
<< " nan = " << bl::isnan(value) << "\n"
<< " signbit = " << bl::signbit(value) << "\n\n";
}
int main()
{
using T = fqd;
describe("positive zero", T{ 0.0 });
describe("negative zero", T{ -0.0 });
describe("denorm_min", std::numeric_limits<T>::denorm_min());
describe("infinity", std::numeric_limits<T>::infinity());
describe("quiet_NaN", std::numeric_limits<T>::quiet_NaN());
const T value = bl::parse<T>("-12.75");
int exponent{};
const T significand = bl::frexp(value, &exponent);
const T rebuilt = bl::ldexp(significand, exponent);
T integer_part{};
const T fraction = bl::modf(value, &integer_part);
std::cout
<< std::setprecision(std::numeric_limits<T>::max_digits10)
<< "frexp(-12.75): significand = " << significand << ", exponent = " << exponent << "\n"
<< "ldexp(significand, exponent): " << rebuilt << "\n"
<< "modf(-12.75): integer = " << integer_part << ", fraction = " << fraction << "\n"
<< "nextafter(0, 1): " << bl::nextafter(T{ 0 }, T{ 1 }) << "\n\n";
}