1 // print_rational().
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "cln/rational_io.h"
8
9
10 // Implementation.
11
12 #include "cln/output.h"
13 #include "cln/integer_io.h"
14 #include "cln/rational.h"
15 #include "rational/cl_RA.h"
16
17 namespace cln {
18
print_rational(std::ostream & stream,const cl_print_rational_flags & flags,const cl_RA & z)19 void print_rational (std::ostream& stream, const cl_print_rational_flags& flags, const cl_RA& z)
20 {
21 var unsigned int base = flags.rational_base;
22 if (flags.rational_readably)
23 // Radix-Specifier ausgeben:
24 switch (base) {
25 case 2:
26 fprintchar(stream,'#');
27 fprintchar(stream,'b');
28 break;
29 case 8:
30 fprintchar(stream,'#');
31 fprintchar(stream,'o');
32 break;
33 case 16:
34 fprintchar(stream,'#');
35 fprintchar(stream,'x');
36 break;
37 case 10:
38 if (integerp(z)) {
39 DeclareType(cl_I,z);
40 // Basis 10 bei Integers durch
41 // nachgestellten Punkt kennzeichnen:
42 print_integer(stream,base,z);
43 fprintchar(stream,'.');
44 return;
45 }
46 // fallthrough
47 default:
48 // Basis in #nR-Schreibweise ausgeben:
49 fprintchar(stream,'#');
50 print_integer(stream,10,base);
51 fprintchar(stream,'r');
52 break;
53 }
54 if (integerp(z)) {
55 DeclareType(cl_I,z);
56 // Integer in Basis base ausgeben:
57 print_integer(stream,base,z);
58 } else {
59 DeclareType(cl_RT,z);
60 // Ratio in Basis base ausgeben; Zähler / Nenner
61 print_integer(stream,base,numerator(z));
62 fprintchar(stream,'/');
63 print_integer(stream,base,denominator(z));
64 }
65 }
66
67 } // namespace cln
68