Skip to content

Commit c4752c9

Browse files
committed
adding quad-double command line utility
1 parent 063d6be commit c4752c9

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

tools/cmd/quaddouble.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// quaddouble.cpp: components of a quad-double: cli to show the sign/scale/limb components of a quad-double floating-point
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 <universal/utility/bit_cast.hpp>
9+
#include <limits>
10+
#include <universal/number/qd/qd.hpp>
11+
#include <universal/common/number_traits_reports.hpp>
12+
13+
// ShowRepresentations prints the different output formats for the quad-double type
14+
template<typename Scalar>
15+
void ShowRepresentations(std::ostream& ostr, sw::universal::qd f) {
16+
using namespace sw::universal;
17+
auto defaultPrecision = ostr.precision(); // save stream state
18+
19+
constexpr int max_digits10 = std::numeric_limits<Scalar>::max_digits10; // floating-point attribute for printing scientific format
20+
21+
Scalar v(f); // convert to target cfloat
22+
ostr << "scientific : " << std::setprecision(max_digits10) << v << '\n';
23+
ostr << "triple form : " << to_triple(v) << '\n';
24+
ostr << "binary form : " << '\n' << to_binary(v, true) << '\n';
25+
ostr << "color coded : " << '\n' << color_print(v, true) << '\n';
26+
27+
ostr << std::setprecision(defaultPrecision);
28+
}
29+
30+
/*
31+
quad-double numbers are an unevaluated set of four doubles.
32+
Each double-precision segment has an epsilon of approximately 2^53.
33+
Combining four double-precision numbers gives as a precision of roughly 4 times 53 bits, or 212 bits.
34+
Therefore, the epsilon of a quad-double number is approximately 2^212/2 = 2^211
35+
2^211 = 3.2910091146424120843099383651147e+63 ~ 3.29100911e63
36+
*/
37+
38+
// receive a float and print the components of a long double representation
39+
int main(int argc, char** argv)
40+
try {
41+
using namespace sw::universal;
42+
using Scalar = qd;
43+
44+
if (argc != 2) {
45+
std::cerr << "quaddouble: components of a quad-double floating-point\n";
46+
std::cerr << "Show the sign/scale/limbs components of a quad-double.\n";
47+
std::cerr << "Usage: quaddouble fp_value_string\n";
48+
std::cerr << "Example: quaddouble 0.03124999\n";
49+
ShowRepresentations<Scalar>(std::cerr, 0.03124999);
50+
51+
std::cout << "Number Traits of quad-double\n";
52+
numberTraits<Scalar>(std::cout);
53+
54+
std::cout << "largest normal number\n";
55+
std::cout << to_binary(std::numeric_limits<Scalar>::max()) << '\n';
56+
std::cout << "smallest normal number\n";
57+
std::cout << to_binary(std::numeric_limits<Scalar>::min()) << '\n';
58+
std::cout << "smallest denormalized number\n";
59+
std::cout << to_binary(std::numeric_limits<Scalar>::denorm_min()) << '\n';
60+
61+
Scalar epsilon{ std::numeric_limits< Scalar >::epsilon() };
62+
std::cout << "epsilon : " << epsilon << '\n';
63+
std::cout << to_binary(epsilon) << '\n';
64+
std::cout.flush();
65+
return EXIT_SUCCESS; // signal successful completion for ctest
66+
}
67+
68+
qd q(argv[1]);
69+
ShowRepresentations<Scalar>(std::cout, q);
70+
71+
std::cout.flush();
72+
return EXIT_SUCCESS;
73+
}
74+
catch (const char* const msg) {
75+
std::cerr << msg << std::endl;
76+
return EXIT_FAILURE;
77+
}
78+
catch (...) {
79+
std::cerr << "Caught unknown exception" << std::endl;
80+
return EXIT_FAILURE;
81+
}

0 commit comments

Comments
 (0)