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