1 | #include <stdint.h>
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <cmath>
|
---|
4 | #include <iostream>
|
---|
5 | #include <boost/multiprecision/cpp_bin_float.hpp>
|
---|
6 |
|
---|
7 | typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<128, boost::multiprecision::backends::digit_base_2> > boost_quadfloat_t;
|
---|
8 |
|
---|
9 | float my_convert_to_float(const boost_quadfloat_t& x)
|
---|
10 | {
|
---|
11 | if (isnormal(x)) {
|
---|
12 | if (x.backend().exponent() < -127+1) {
|
---|
13 | // subnormal or zero
|
---|
14 | if (x.backend().exponent() < -127 - 23) {
|
---|
15 | return x.backend().sign() ? -0.0f : 0.0f; // underflow
|
---|
16 | }
|
---|
17 | float adj = x.backend().sign() ? -FLT_MIN : FLT_MIN;
|
---|
18 | boost_quadfloat_t tmp = x + adj;
|
---|
19 | return tmp.convert_to<float>() - adj;
|
---|
20 | }
|
---|
21 | }
|
---|
22 | return x.convert_to<float>();
|
---|
23 | }
|
---|
24 |
|
---|
25 | int main(int , char** )
|
---|
26 | {
|
---|
27 | boost_quadfloat_t b = ldexp(boost_quadfloat_t(0x8000017f), -31 - 127);
|
---|
28 | float f0 = b.convert_to<float>();
|
---|
29 | float f1 = std::nexttoward(f0, f0 < b ? FLT_MAX : -FLT_MAX);
|
---|
30 |
|
---|
31 | std::cout
|
---|
32 | << std::setprecision(11)
|
---|
33 | << b << " = b\n"
|
---|
34 | << f0 << " = f0 = b.convert_to<float>()\n"
|
---|
35 | << f1 << " = f1 = std::nexttoward (f0, ...)\n"
|
---|
36 | << abs(f0 - b) << " = abs(f0 - b)\n"
|
---|
37 | << abs(f1 - b) << " = abs(f1 - b)\n"
|
---|
38 | << ((abs(f1 - b) < abs(f0 - b)) ? "f0 is not the closest representable float!!!\n" : "o.k.\n")
|
---|
39 | ;
|
---|
40 |
|
---|
41 | f0 = my_convert_to_float(b);
|
---|
42 | f1 = std::nexttoward (f0, f0 < b ? FLT_MAX : -FLT_MAX);
|
---|
43 | std::cout
|
---|
44 | << f0 << " = f0 = my_convert_to_float(b)\n"
|
---|
45 | << f1 << " = f1 = std::nexttoward (f0, ...)\n"
|
---|
46 | << abs(f0 - b) << " = abs(f0 - b)\n"
|
---|
47 | << abs(f1 - b) << " = abs(f1 - b)\n"
|
---|
48 | << ((abs(f1 - b) < abs(f0 - b)) ? "f0 is not the closest representable float!!!\n" : "o.k.\n")
|
---|
49 | ;
|
---|
50 |
|
---|
51 | return 0;
|
---|
52 | }
|
---|