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"
14 struct cl_heap_ratio : cl_heap {
19 inline cl_heap_ratio* TheRatio (cl_heap_ratio* p)
21 inline cl_heap_ratio* TheRatio (const cl_number& obj)
22 { return (cl_heap_ratio*)(obj.pointer); }
24 inline cl_heap_ratio* allocate_ratio (const cl_I& num, const cl_I& den)
26 cl_heap_ratio* p = (cl_heap_ratio*) malloc_hook(sizeof(cl_heap_ratio));
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);
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) {}
39 // Both work, but the first definition results in less compiler-generated
42 #define Ratio cl_heap_ratio*
48 inline bool rationalp (const cl_RA& x)
49 { unused x; return true; }
50 inline bool integerp (const cl_RA& x)
55 if (x.pointer_type() == &cl_class_bignum)
59 inline bool ratiop (const cl_RA& x)
64 if (x.pointer_type() == &cl_class_bignum)
70 // A ratio (cl_RT) is a rational number which is not an integer (cl_I).
73 class cl_RT : public cl_RA {
77 inline bool integerp (const cl_RT& x)
78 { unused x; return false; }
79 inline bool ratiop (const cl_RT& x)
80 { unused x; return true; }
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; }
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)
105 // (ZEROP x) == (= x 0)
106 inline bool zerop (const cl_RT& x)
107 { unused x; return false; }
108 inline bool zerop (const cl_RA& x)
110 return x.word == cl_combine(cl_FN_tag,0);
113 // (EQ x y) == (= x y), assuming y a fixnum
114 inline bool eq (const cl_RA& x, sint32 y)
116 return x.word == cl_combine(cl_FN_tag,y);
119 // Liefert zu den Integers a und b mit b>1 und ggT(a,b)=1 den Bruch a/b.
121 extern const cl_RA I_I_to_RT (const cl_I& a, const cl_I& b);
123 // Liefert zu den Integers a und b mit b>0 und ggT(a,b)=1 den Bruch a/b
124 // (Ratio oder Integer).
126 extern const cl_RA I_I_to_RA (const cl_I& a, const cl_I& b);
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);
132 // Liefert zu den Integers a und b den Bruch a/b (Ratio oder Integer).
134 extern const cl_RA I_I_div_RA (const cl_I& a, const cl_I& b);
136 // Liefert den Zähler einer rationalen Zahl.
138 inline const cl_I numerator (const cl_RA& r)
144 return TheRatio(r)->numerator;
147 // Liefert den Nenner einer rationalen Zahl.
149 inline const cl_I denominator (const cl_RA& r)
154 return TheRatio(r)->denominator;
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) \
164 { num_zuweisung *(const cl_I *)&r; \
165 den_zuweisung 1; /* Zähler = r, Nenner = 1 */ \
168 { num_zuweisung TheRatio(r)->numerator; \
169 den_zuweisung TheRatio(r)->denominator; \
175 #endif /* _CL_RA_H */