Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-19 08:08:32

0001 // Public rational number operations.
0002 
0003 #ifndef _CL_RATIONAL_H
0004 #define _CL_RATIONAL_H
0005 
0006 #include "cln/number.h"
0007 #include "cln/rational_class.h"
0008 #include "cln/integer_class.h"
0009 #include "cln/exception.h"
0010 
0011 namespace cln {
0012 
0013 CL_DEFINE_AS_CONVERSION(cl_RA)
0014 
0015 
0016 // numerator(r) liefert den Zähler der rationalen Zahl r.
0017 extern const cl_I numerator (const cl_RA& r);
0018 
0019 // denominator(r) liefert den Nenner (> 0) der rationalen Zahl r.
0020 extern const cl_I denominator (const cl_RA& r);
0021 
0022 
0023 // Liefert (- r), wo r eine rationale Zahl ist.
0024 extern const cl_RA operator- (const cl_RA& r);
0025 
0026 // (+ r s), wo r und s rationale Zahlen sind.
0027 extern const cl_RA operator+ (const cl_RA& r, const cl_RA& s);
0028 // Dem C++-Compiler muß man auch das Folgende sagen:
0029 inline const cl_RA operator+ (const int x, const cl_RA& y)
0030     { return cl_I(x) + y; }
0031 inline const cl_RA operator+ (const unsigned int x, const cl_RA& y)
0032     { return cl_I(x) + y; }
0033 inline const cl_RA operator+ (const long x, const cl_RA& y)
0034     { return cl_I(x) + y; }
0035 inline const cl_RA operator+ (const unsigned long x, const cl_RA& y)
0036     { return cl_I(x) + y; }
0037 inline const cl_RA operator+ (const long long x, const cl_RA& y)
0038     { return cl_I(x) + y; }
0039 inline const cl_RA operator+ (const unsigned long long x, const cl_RA& y)
0040     { return cl_I(x) + y; }
0041 inline const cl_RA operator+ (const cl_RA& x, const int y)
0042     { return x + cl_I(y); }
0043 inline const cl_RA operator+ (const cl_RA& x, const unsigned int y)
0044     { return x + cl_I(y); }
0045 inline const cl_RA operator+ (const cl_RA& x, const long y)
0046     { return x + cl_I(y); }
0047 inline const cl_RA operator+ (const cl_RA& x, const unsigned long y)
0048     { return x + cl_I(y); }
0049 inline const cl_RA operator+ (const cl_RA& x, const long long y)
0050     { return x + cl_I(y); }
0051 inline const cl_RA operator+ (const cl_RA& x, const unsigned long long y)
0052     { return x + cl_I(y); }
0053 
0054 // (- r s), wo r und s rationale Zahlen sind.
0055 extern const cl_RA operator- (const cl_RA& r, const cl_RA& s);
0056 // Dem C++-Compiler muß man auch das Folgende sagen:
0057 inline const cl_RA operator- (const int x, const cl_RA& y)
0058     { return cl_I(x) - y; }
0059 inline const cl_RA operator- (const unsigned int x, const cl_RA& y)
0060     { return cl_I(x) - y; }
0061 inline const cl_RA operator- (const long x, const cl_RA& y)
0062     { return cl_I(x) - y; }
0063 inline const cl_RA operator- (const unsigned long x, const cl_RA& y)
0064     { return cl_I(x) - y; }
0065 inline const cl_RA operator- (const long long x, const cl_RA& y)
0066     { return cl_I(x) - y; }
0067 inline const cl_RA operator- (const unsigned long long x, const cl_RA& y)
0068     { return cl_I(x) - y; }
0069 inline const cl_RA operator- (const cl_RA& x, const int y)
0070     { return x - cl_I(y); }
0071 inline const cl_RA operator- (const cl_RA& x, const unsigned int y)
0072     { return x - cl_I(y); }
0073 inline const cl_RA operator- (const cl_RA& x, const long y)
0074     { return x - cl_I(y); }
0075 inline const cl_RA operator- (const cl_RA& x, const unsigned long y)
0076     { return x - cl_I(y); }
0077 inline const cl_RA operator- (const cl_RA& x, const long long y)
0078     { return x - cl_I(y); }
0079 inline const cl_RA operator- (const cl_RA& x, const unsigned long long y)
0080     { return x - cl_I(y); }
0081 
0082 // (1+ r), wo r eine rationale Zahl ist.
0083 extern const cl_RA plus1 (const cl_RA& r);
0084 
0085 // (1- r), wo r eine rationale Zahl ist.
0086 extern const cl_RA minus1 (const cl_RA& r);
0087 
0088 // (abs r), wo r eine rationale Zahl ist.
0089 extern const cl_RA abs (const cl_RA& r);
0090 
0091 // equal(r,s) vergleicht zwei rationale Zahlen r und s auf Gleichheit.
0092 extern bool equal (const cl_RA& r, const cl_RA& s);
0093 // equal_hashcode(r) liefert einen equal-invarianten Hashcode für r.
0094 extern uint32 equal_hashcode (const cl_RA& r);
0095 
0096 // compare(r,s) vergleicht zwei rationale Zahlen r und s.
0097 // Ergebnis: 0 falls r=s, +1 falls r>s, -1 falls r<s.
0098 extern cl_signean compare (const cl_RA& r, const cl_RA& s);
0099 
0100 inline bool operator== (const cl_RA& x, const cl_RA& y)
0101     { return equal(x,y); }
0102 inline bool operator!= (const cl_RA& x, const cl_RA& y)
0103     { return !equal(x,y); }
0104 inline bool operator<= (const cl_RA& x, const cl_RA& y)
0105     { return compare(x,y)<=0; }
0106 inline bool operator< (const cl_RA& x, const cl_RA& y)
0107     { return compare(x,y)<0; }
0108 inline bool operator>= (const cl_RA& x, const cl_RA& y)
0109     { return compare(x,y)>=0; }
0110 inline bool operator> (const cl_RA& x, const cl_RA& y)
0111     { return compare(x,y)>0; }
0112 
0113 // minusp(x) == (< x 0)
0114 extern bool minusp (const cl_RA& x);
0115 
0116 // zerop(x) stellt fest, ob eine rationale Zahl = 0 ist.
0117 extern bool zerop (const cl_RA& x);
0118 
0119 // plusp(x) == (> x 0)
0120 extern bool plusp (const cl_RA& x);
0121 
0122 // Kehrwert (/ r), wo r eine rationale Zahl ist.
0123 extern const cl_RA recip (const cl_RA& r);
0124 
0125 // Liefert (* r s), wo r und s rationale Zahlen sind.
0126 extern const cl_RA operator* (const cl_RA& r, const cl_RA& s);
0127 // Dem C++-Compiler muß man auch das Folgende sagen:
0128 inline const cl_RA operator* (const int x, const cl_RA& y)
0129     { return cl_I(x) * y; }
0130 inline const cl_RA operator* (const unsigned int x, const cl_RA& y)
0131     { return cl_I(x) * y; }
0132 inline const cl_RA operator* (const long x, const cl_RA& y)
0133     { return cl_I(x) * y; }
0134 inline const cl_RA operator* (const unsigned long x, const cl_RA& y)
0135     { return cl_I(x) * y; }
0136 inline const cl_RA operator* (const long long x, const cl_RA& y)
0137     { return cl_I(x) * y; }
0138 inline const cl_RA operator* (const unsigned long long x, const cl_RA& y)
0139     { return cl_I(x) * y; }
0140 inline const cl_RA operator* (const cl_RA& x, const int y)
0141     { return x * cl_I(y); }
0142 inline const cl_RA operator* (const cl_RA& x, const unsigned int y)
0143     { return x * cl_I(y); }
0144 inline const cl_RA operator* (const cl_RA& x, const long y)
0145     { return x * cl_I(y); }
0146 inline const cl_RA operator* (const cl_RA& x, const unsigned long y)
0147     { return x * cl_I(y); }
0148 inline const cl_RA operator* (const cl_RA& x, const long long y)
0149     { return x * cl_I(y); }
0150 inline const cl_RA operator* (const cl_RA& x, const unsigned long long y)
0151     { return x * cl_I(y); }
0152 
0153 // Quadrat (* r r), wo r eine rationale Zahl ist.
0154 extern const cl_RA square (const cl_RA& r);
0155 
0156 // Liefert (/ r s), wo r und s rationale Zahlen sind.
0157 extern const cl_RA operator/ (const cl_RA& r, const cl_RA& s);
0158 // Dem C++-Compiler muß man auch das Folgende sagen:
0159 inline const cl_RA operator/ (const int x, const cl_RA& y)
0160     { return cl_I(x) / y; }
0161 inline const cl_RA operator/ (const unsigned int x, const cl_RA& y)
0162     { return cl_I(x) / y; }
0163 inline const cl_RA operator/ (const long x, const cl_RA& y)
0164     { return cl_I(x) / y; }
0165 inline const cl_RA operator/ (const unsigned long x, const cl_RA& y)
0166     { return cl_I(x) / y; }
0167 inline const cl_RA operator/ (const long long x, const cl_RA& y)
0168     { return cl_I(x) / y; }
0169 inline const cl_RA operator/ (const unsigned long long x, const cl_RA& y)
0170     { return cl_I(x) / y; }
0171 inline const cl_RA operator/ (const cl_RA& x, const int y)
0172     { return x / cl_I(y); }
0173 inline const cl_RA operator/ (const cl_RA& x, const unsigned int y)
0174     { return x / cl_I(y); }
0175 inline const cl_RA operator/ (const cl_RA& x, const long y)
0176     { return x / cl_I(y); }
0177 inline const cl_RA operator/ (const cl_RA& x, const unsigned long y)
0178     { return x / cl_I(y); }
0179 inline const cl_RA operator/ (const cl_RA& x, const long long y)
0180     { return x / cl_I(y); }
0181 inline const cl_RA operator/ (const cl_RA& x, const unsigned long long y)
0182     { return x / cl_I(y); }
0183 
0184 // Return type for rounding operators.
0185 // x / y  --> (q,r) with x = y*q+r.
0186 struct cl_RA_div_t {
0187     cl_I quotient;
0188     cl_RA remainder;
0189 // Constructor.
0190     cl_RA_div_t () {}
0191     cl_RA_div_t (const cl_I& q, const cl_RA& r) : quotient(q), remainder(r) {}
0192 };
0193 
0194 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
0195 // (q,r) := (floor x)
0196 // floor2(x)
0197 // > x: rationale Zahl
0198 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
0199   extern const cl_RA_div_t floor2 (const cl_RA& x);
0200   extern const cl_I floor1 (const cl_RA& x);
0201 
0202 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
0203 // (q,r) := (ceiling x)
0204 // ceiling2(x)
0205 // > x: rationale Zahl
0206 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
0207   extern const cl_RA_div_t ceiling2 (const cl_RA& x);
0208   extern const cl_I ceiling1 (const cl_RA& x);
0209 
0210 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
0211 // (q,r) := (truncate x)
0212 // truncate2(x)
0213 // > x: rationale Zahl
0214 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
0215   extern const cl_RA_div_t truncate2 (const cl_RA& x);
0216   extern const cl_I truncate1 (const cl_RA& x);
0217 
0218 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
0219 // (q,r) := (round x)
0220 // round2(x)
0221 // > x: rationale Zahl
0222 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
0223   extern const cl_RA_div_t round2 (const cl_RA& x);
0224   extern const cl_I round1 (const cl_RA& x);
0225 
0226 // floor2(x,y) liefert (floor x y).
0227 extern const cl_RA_div_t floor2 (const cl_RA& x, const cl_RA& y);
0228 extern const cl_I floor1 (const cl_RA& x, const cl_RA& y);
0229 
0230 // ceiling2(x,y) liefert (ceiling x y).
0231 extern const cl_RA_div_t ceiling2 (const cl_RA& x, const cl_RA& y);
0232 extern const cl_I ceiling1 (const cl_RA& x, const cl_RA& y);
0233 
0234 // truncate2(x,y) liefert (truncate x y).
0235 extern const cl_RA_div_t truncate2 (const cl_RA& x, const cl_RA& y);
0236 extern const cl_I truncate1 (const cl_RA& x, const cl_RA& y);
0237 
0238 // round2(x,y) liefert (round x y).
0239 extern const cl_RA_div_t round2 (const cl_RA& x, const cl_RA& y);
0240 extern const cl_I round1 (const cl_RA& x, const cl_RA& y);
0241 
0242 // max(x,y) liefert (max x y), wo x und y rationale Zahlen sind.
0243 extern const cl_RA max (const cl_RA& x, const cl_RA& y);
0244 
0245 // min(x,y) liefert (min x y), wo x und y rationale Zahlen sind.
0246 extern const cl_RA min (const cl_RA& x, const cl_RA& y);
0247 
0248 // signum(x) liefert (signum x), wo x eine rationale Zahl ist.
0249 extern const cl_RA signum (const cl_RA& x);
0250 
0251 // (expt x y), wo x eine rationale Zahl und y ein Integer >0 ist.
0252 extern const cl_RA expt_pos (const cl_RA& x, uintL y);
0253 extern const cl_RA expt_pos (const cl_RA& x, const cl_I& y);
0254 
0255 // (expt x y), wo x eine rationale Zahl und y ein Integer ist.
0256 extern const cl_RA expt (const cl_RA& x, sintL y);
0257 extern const cl_RA expt (const cl_RA& x, const cl_I& y);
0258 
0259 // Stellt fest, ob eine rationale Zahl >=0 das Quadrat einer rationalen Zahl
0260 // ist.
0261 // sqrtp(x,&w)
0262 // > x: eine rationale Zahl >=0
0263 // < w: rationale Zahl (sqrt x) falls x Quadratzahl
0264 // < ergebnis: true      ..................., false sonst
0265   extern bool sqrtp (const cl_RA& x, cl_RA* w);
0266 
0267 // Stellt fest, ob eine rationale Zahl >=0 die n-te Potenz einer rationalen Zahl
0268 // ist.
0269 // rootp(x,n,&w)
0270 // > x: eine rationale Zahl >=0
0271 // > n: ein Integer >0
0272 // < w: exakte n-te Wurzel (expt x (/ n)) falls x eine n-te Potenz
0273 // < ergebnis: true                       ........................, false sonst
0274   extern bool rootp (const cl_RA& x, uintL n, cl_RA* w);
0275   extern bool rootp (const cl_RA& x, const cl_I& n, cl_RA* w);
0276 
0277 // Liefert zu Integers a>0, b>1 den Logarithmus log(a,b),
0278 // falls er eine rationale Zahl ist.
0279 // logp(a,b,&l)
0280 // > a: ein Integer >0
0281 // > b: ein Integer >1
0282 // < l: log(a,b)       falls er eine exakte rationale Zahl ist
0283 // < ergebnis: true    ......................................., false sonst
0284   extern bool logp (const cl_I& a, const cl_I& b, cl_RA* l);
0285 
0286 // Liefert zu rationalen Zahlen a>0, b>0 den Logarithmus log(a,b),
0287 // falls er eine rationale Zahl ist.
0288 // logp(a,b,&l)
0289 // > a: eine rationale Zahl >0
0290 // > b: eine rationale Zahl >0, /=1
0291 // < l: log(a,b)       falls er eine exakte rationale Zahl ist
0292 // < ergebnis: true    ......................................., false sonst
0293   extern bool logp (const cl_RA& a, const cl_RA& b, cl_RA* l);
0294 
0295 // Konversion zu einem C "float".
0296 extern float float_approx (const cl_RA& x);
0297 
0298 // Konversion zu einem C "double".
0299 extern double double_approx (const cl_RA& x);
0300 
0301 
0302 // This could be optimized to use in-place operations.
0303 inline cl_RA& operator+= (cl_RA& x, const cl_RA& y) { return x = x + y; }
0304 inline cl_RA& operator+= (cl_RA& x, const int y) { return x = x + y; }
0305 inline cl_RA& operator+= (cl_RA& x, const unsigned int y) { return x = x + y; }
0306 inline cl_RA& operator+= (cl_RA& x, const long y) { return x = x + y; }
0307 inline cl_RA& operator+= (cl_RA& x, const unsigned long y) { return x = x + y; }
0308 inline cl_RA& operator+= (cl_RA& x, const long long y) { return x = x + y; }
0309 inline cl_RA& operator+= (cl_RA& x, const unsigned long long y) { return x = x + y; }
0310 inline cl_RA& operator++ /* prefix */ (cl_RA& x) { return x = plus1(x); }
0311 inline void operator++ /* postfix */ (cl_RA& x, int dummy) { (void)dummy; x = plus1(x); }
0312 inline cl_RA& operator-= (cl_RA& x, const cl_RA& y) { return x = x - y; }
0313 inline cl_RA& operator-= (cl_RA& x, const int y) { return x = x - y; }
0314 inline cl_RA& operator-= (cl_RA& x, const unsigned int y) { return x = x - y; }
0315 inline cl_RA& operator-= (cl_RA& x, const long y) { return x = x - y; }
0316 inline cl_RA& operator-= (cl_RA& x, const unsigned long y) { return x = x - y; }
0317 inline cl_RA& operator-= (cl_RA& x, const long long y) { return x = x - y; }
0318 inline cl_RA& operator-= (cl_RA& x, const unsigned long long y) { return x = x - y; }
0319 inline cl_RA& operator-- /* prefix */ (cl_RA& x) { return x = minus1(x); }
0320 inline void operator-- /* postfix */ (cl_RA& x, int dummy) { (void)dummy; x = minus1(x); }
0321 inline cl_RA& operator*= (cl_RA& x, const cl_RA& y) { return x = x * y; }
0322 inline cl_RA& operator/= (cl_RA& x, const cl_RA& y) { return x = x / y; }
0323 
0324 
0325 // Runtime typing support.
0326 extern cl_class cl_class_ratio;
0327 
0328 
0329 // Debugging support.
0330 #ifdef CL_DEBUG
0331 extern int cl_RA_debug_module;
0332 CL_FORCE_LINK(cl_RA_debug_dummy, cl_RA_debug_module)
0333 #endif
0334 
0335 }  // namespace cln
0336 
0337 #endif /* _CL_RATIONAL_H */