]> www.ginac.de Git - cln.git/blob - src/real/format-output/cl_fmt_oldroman.cc
Finalize CLN 1.3.7 release.
[cln.git] / src / real / format-output / cl_fmt_oldroman.cc
1 // format_old_roman().
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "real/format-output/cl_format.h"
8
9
10 // Implementation.
11
12 #include <sstream>
13 #include "cln/integer.h"
14 #include "cln/integer_io.h"
15 #include "cln/exception.h"
16
17 namespace cln {
18
19 void format_old_roman (std::ostream& stream, const cl_I& arg)
20 {
21         if (!(0 < arg && arg < 5000)) {
22                 std::ostringstream buf;
23                 fprint(buf, "format_old_roman: argument should be in the range 1 - 4999, not ");
24                 fprint(buf, arg);
25                 fprint(buf, "\n");
26                 throw runtime_exception(buf.str());
27         }
28         var uintL value = cl_I_to_UL(arg);
29         struct roman { char symbol; uintL value; };
30         static const roman scale[7] = {
31                 { 'I',    1 },
32                 { 'V',    5 },
33                 { 'X',   10 },
34                 { 'L',   50 },
35                 { 'C',  100 },
36                 { 'D',  500 },
37                 { 'M', 1000 },
38         };
39         for (int i = 6; value > 0 /* && i >= 0 */ ; i--) {
40                 var const roman * p = &scale[i];
41                 var uintL multiplicity = floor(value,p->value);
42                 value = value % p->value;
43                 while (multiplicity > 0) {
44                         fprintchar(stream,p->symbol);
45                         multiplicity--;
46                 }
47         }
48 }
49
50 }  // namespace cln