]> www.ginac.de Git - cln.git/blob - src/rational/cl_RA.h
Replace unused macro with cl_unused.
[cln.git] / src / rational / cl_RA.h
1 // cl_RA internals
2
3 #ifndef _CL_RA_H
4 #define _CL_RA_H
5
6 #include "cln/number.h"
7 #include "cln/rational.h"
8 #include "base/cl_macros.h"
9 #include "cln/malloc.h"
10 #include "integer/cl_I.h"
11
12 namespace cln {
13
14 struct cl_heap_ratio : cl_heap {
15         cl_I numerator;
16         cl_I denominator;
17 };
18
19 inline cl_heap_ratio* TheRatio (cl_heap_ratio* p)
20         { return p; }
21 inline cl_heap_ratio* TheRatio (const cl_number& obj)
22         { return (cl_heap_ratio*)(obj.pointer); }
23
24 inline cl_heap_ratio* allocate_ratio (const cl_I& num, const cl_I& den)
25 {
26         cl_heap_ratio* p = (cl_heap_ratio*) malloc_hook(sizeof(cl_heap_ratio));
27         p->refcount = 1;
28         p->type = &cl_class_ratio;
29         p->numerator.pointer = num.pointer;     cl_inc_refcount(num);
30         p->denominator.pointer = den.pointer;   cl_inc_refcount(den);
31         return p;
32 }
33
34 // Private constructor.
35 // ptr should be the result of some allocate_ratio() call.
36 inline cl_RA::cl_RA (cl_heap_ratio* ptr)
37         : cl_R ((cl_private_thing) ptr) {}
38
39 // Both work, but the first definition results in less compiler-generated
40 // temporaries.
41 #if 1
42   #define Ratio  cl_heap_ratio*
43 #else
44   #define Ratio  cl_RA
45 #endif
46
47 // Type tests.
48 inline bool rationalp (const cl_RA& x)
49         { cl_unused x; return true; }
50 inline bool integerp (const cl_RA& x)
51 {
52         if (!x.pointer_p())
53                 return true;
54         else
55                 if (x.pointer_type() == &cl_class_bignum)
56                         return true;
57         return false;
58 }
59 inline bool ratiop (const cl_RA& x)
60 {
61         if (!x.pointer_p())
62                 return false;
63         else
64                 if (x.pointer_type() == &cl_class_bignum)
65                         return false;
66         return true;
67 }
68
69
70 // A ratio (cl_RT) is a rational number which is not an integer (cl_I).
71
72 // typedef
73 class cl_RT : public cl_RA {
74 public:
75 };
76
77 inline bool integerp (const cl_RT& x)
78         { cl_unused x; return false; }
79 inline bool ratiop (const cl_RT& x)
80         { cl_unused x; return true; }
81
82 // Access numerator and denominator.
83 inline const cl_I& numerator (const cl_RT& x)
84         { return TheRatio(x)->numerator; }
85 inline const cl_I& denominator (const cl_RT& x)
86         { return TheRatio(x)->denominator; }
87
88
89 // Sign test:
90
91 // (MINUSP x) == (< x 0)
92 inline bool minusp (const cl_RT& x)
93         { return minusp(numerator(x)); }
94 inline bool minusp (const cl_RA& x)
95 {
96         if (ratiop(x)) {
97                 DeclareType(cl_RT,x);
98                 return minusp(x);
99         } else {
100                 DeclareType(cl_I,x);
101                 return minusp(x);
102         }
103 }
104
105 // (ZEROP x) == (= x 0)
106 inline bool zerop (const cl_RT& x)
107         { cl_unused x; return false; }
108 inline bool zerop (const cl_RA& x)
109 {
110         return x.word == cl_combine(cl_FN_tag,0);
111 }
112
113 // (EQ x y) == (= x y), assuming y a fixnum
114 inline bool eq (const cl_RA& x, sint32 y)
115 {
116         return x.word == cl_combine(cl_FN_tag,y);
117 }
118
119 // Liefert zu den Integers a und b mit b>1 und ggT(a,b)=1 den Bruch a/b.
120 // I_I_to_RT(a,b)
121   extern const cl_RA I_I_to_RT (const cl_I& a, const cl_I& b);
122
123 // Liefert zu den Integers a und b mit b>0 und ggT(a,b)=1 den Bruch a/b
124 // (Ratio oder Integer).
125 // I_I_to_RA(a,b)
126   extern const cl_RA I_I_to_RA (const cl_I& a, const cl_I& b);
127
128 // Liefert zu den Integers a und b mit b>0 den Bruch a/b (Ratio oder Integer).
129 // I_posI_div_RA(a,b)
130   extern const cl_RA I_posI_div_RA (const cl_I& a, const cl_I& b);
131
132 // Liefert zu den Integers a und b den Bruch a/b (Ratio oder Integer).
133 // I_I_div_RA(a,b)
134   extern const cl_RA I_I_div_RA (const cl_I& a, const cl_I& b);
135
136 // Liefert den Zähler einer rationalen Zahl.
137 // numerator(r)
138 inline const cl_I numerator (const cl_RA& r)
139 {
140         if (integerp(r)) {
141                 DeclareType(cl_I,r);
142                 return r;
143         } else
144                 return TheRatio(r)->numerator;
145 }
146
147 // Liefert den Nenner einer rationalen Zahl.
148 // denominator(r)
149 inline const cl_I denominator (const cl_RA& r)
150 {
151         if (integerp(r))
152                 return 1;
153         else
154                 return TheRatio(r)->denominator;
155 }
156
157 // Liefert Zähler und Nenner einer rationalen Zahl.
158 // RA_numden_I_I(r, num=,den=);
159 // > r: rationale Zahl
160 // < num: (numerator r)
161 // < den: (denominator r)
162   #define RA_numden_I_I(r,num_zuweisung,den_zuweisung)  \
163     { if (integerp(r))                                                  \
164         { num_zuweisung *(const cl_I *)&r;                              \
165           den_zuweisung 1; /* Zähler = r, Nenner = 1 */                        \
166         }                                                               \
167         else                                                            \
168         { num_zuweisung TheRatio(r)->numerator;                         \
169           den_zuweisung TheRatio(r)->denominator;                       \
170         }                                                               \
171     }
172
173 }  // namespace cln
174
175 #endif /* _CL_RA_H */