10
10
#include < typeinfo>
11
11
#include < universal/number/fixpnt/fixpnt.hpp>
12
12
13
- const char * msg = " \n \
14
- class sw::universal::fixpnt < 8, 4, 1, unsigned char>\n \
15
- 1.0625 \n \
16
- b0001.0001 \n \
17
- class sw::universal::fixpnt < 12, 4, 1, unsigned char>\n \
18
- 1.0625\n \
19
- b0000'0001.0001\n \
20
- class sw::universal::fixpnt < 16, 8, 1, unsigned char>\n \
21
- 1.06250000\n \
22
- b0000'0001.0001'0000\n \
23
- class sw::universal::fixpnt < 32, 16, 1, unsigned char>\n \
24
- 1.0625000000000000\n \
25
- b0000'0000'0000'0001.0001'0000'0000'0000\n \
26
- class sw::universal::fixpnt < 64, 32, 1, unsigned char>\n \
27
- 1.06250000000000000000000000000000\n \
28
- b0000'0000'0000'0000'0000'0000'0000'0001.0001'0000'0000'0000'0000'0000'0000'0000\n " ;
13
+ // ShowRepresentations prints the different output formats for the Scalar type
14
+ // TODO: guard with cfloat trait
15
+ template <typename Scalar>
16
+ void ShowRepresentations (std::ostream& ostr, Scalar value) {
17
+ using namespace sw ::universal;
18
+
19
+ auto defaultPrecision = ostr.precision (); // save stream state
20
+
21
+ constexpr int max_digits10 = std::numeric_limits<Scalar>::max_digits10; // floating-point attribute for printing scientific format
22
+
23
+ std::cout << ' \n ' << type_tag (Scalar ()) << ' \n ' ;
24
+ Scalar v (value); // convert to target fixpnt
25
+ ostr << " fixed form : " << std::setprecision (max_digits10) << v << ' \n ' ;
26
+ ostr << " triple form : " << to_triple (v) << ' \n ' ;
27
+ ostr << " binary form : " << to_binary (v, true ) << ' \n ' ;
28
+ ostr << " color coded : " << color_print (v) << ' \n ' ;
29
+
30
+ ostr << std::setprecision (defaultPrecision);
31
+ }
32
+
33
+ void Show (std::ostream& ostr, double d) {
34
+ using namespace sw ::universal;
29
35
30
- template <typename FixedPoint >
31
- void attributes (const FixedPoint & v) {
32
- constexpr int max_digits10 = std::numeric_limits<FixedPoint >::max_digits10;
33
- std::cout << " " << typeid (FixedPoint ).name () << ' \n ' << std::setprecision (max_digits10) << v << " \n " << to_binary (v, true ) << std::endl;
36
+ ShowRepresentations (ostr, fixpnt< 8 , 4 >(d));
37
+ ShowRepresentations (ostr, fixpnt<12 , 4 >(d));
38
+ ShowRepresentations (ostr, fixpnt<16 , 8 >(d));
39
+ ShowRepresentations (ostr, fixpnt<24 , 8 >(d));
40
+ ShowRepresentations (ostr, fixpnt<32 , 16 >(d));
41
+ ShowRepresentations (ostr, fixpnt<48 , 16 >(d));
42
+ ShowRepresentations (ostr, fixpnt<64 , 32 >(d));
34
43
}
35
44
36
45
// receive a float and print its components
@@ -43,16 +52,13 @@ try {
43
52
std::cerr << " Show the sign/scale/fraction components of a fixed-point value.\n " ;
44
53
std::cerr << " Usage: fixpnt float_value\n " ;
45
54
std::cerr << " Example: fixpnt 1.0625\n " ;
46
- std::cerr << msg << ' \n ' ;
55
+ Show ( std::cerr, 1.0625 ) ;
47
56
return EXIT_SUCCESS; // signal successful completion for ctest
48
57
}
49
58
double d = atof (argv[1 ]);
50
59
51
- attributes (fixpnt< 8 , 4 >(d));
52
- attributes (fixpnt<12 , 4 >(d));
53
- attributes (fixpnt<16 , 8 >(d));
54
- attributes (fixpnt<32 ,16 >(d));
55
- attributes (fixpnt<64 ,32 >(d));
60
+ std::cout << " fixpnt " << argv[1 ] << ' \n ' ;
61
+ Show (std::cout, d);
56
62
57
63
return EXIT_SUCCESS;
58
64
}
0 commit comments