1 | #ifndef NTL_RR__H |
---|
2 | #define NTL_RR__H |
---|
3 | |
---|
4 | #include <NTL/ZZ.h> |
---|
5 | #include <NTL/xdouble.h> |
---|
6 | #include <NTL/quad_float.h> |
---|
7 | |
---|
8 | NTL_OPEN_NNS |
---|
9 | |
---|
10 | |
---|
11 | class RR { |
---|
12 | |
---|
13 | public: |
---|
14 | |
---|
15 | ZZ x; |
---|
16 | long e; |
---|
17 | |
---|
18 | RR() { e = 0; } |
---|
19 | |
---|
20 | inline RR(INIT_VAL_TYPE, const ZZ& a); |
---|
21 | inline RR(INIT_VAL_TYPE, int a); |
---|
22 | inline RR(INIT_VAL_TYPE, long a); |
---|
23 | inline RR(INIT_VAL_TYPE, unsigned int a); |
---|
24 | inline RR(INIT_VAL_TYPE, unsigned long a); |
---|
25 | inline RR(INIT_VAL_TYPE, float a); |
---|
26 | inline RR(INIT_VAL_TYPE, double a); |
---|
27 | inline RR(INIT_VAL_TYPE, const xdouble& a); |
---|
28 | inline RR(INIT_VAL_TYPE, const quad_float& a); |
---|
29 | inline RR(INIT_VAL_TYPE, const char *a); // read from string |
---|
30 | inline RR(INIT_VAL_TYPE, const RR& a); |
---|
31 | |
---|
32 | |
---|
33 | inline RR& operator=(double a); |
---|
34 | |
---|
35 | RR(RR& z, INIT_TRANS_TYPE) : x(z.x, INIT_TRANS), e(z.e) { } |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | ~RR() { } |
---|
42 | |
---|
43 | const ZZ& mantissa() const { return x; } |
---|
44 | long exponent() const { return e; } |
---|
45 | |
---|
46 | static long prec; |
---|
47 | static void SetPrecision(long p); |
---|
48 | static long precision() { return prec; } |
---|
49 | |
---|
50 | static long oprec; |
---|
51 | static void SetOutputPrecision(long p); |
---|
52 | static long OutputPrecision() { return oprec; } |
---|
53 | |
---|
54 | #ifdef NTL_TRANSITION |
---|
55 | private: |
---|
56 | RR& operator=(const RR&); |
---|
57 | RR(const RR&); |
---|
58 | #endif |
---|
59 | |
---|
60 | }; |
---|
61 | |
---|
62 | |
---|
63 | long IsZero(const RR& a); |
---|
64 | long IsOne(const RR& a); |
---|
65 | long sign(const RR& a); |
---|
66 | void clear(RR& z); |
---|
67 | void set(RR& z); |
---|
68 | void swap(RR& a, RR& b); |
---|
69 | |
---|
70 | void add(RR& z, const RR& a, const RR& b); |
---|
71 | |
---|
72 | void add(RR& z, const RR& a, double b); |
---|
73 | inline void add(RR& z, double a, const RR& b) { add(z, b, a); } |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | void sub(RR& z, const RR& a, const RR& b); |
---|
78 | |
---|
79 | void sub(RR& z, const RR& a, double b); |
---|
80 | void sub(RR& z, double a, const RR& b); |
---|
81 | |
---|
82 | void negate(RR& z, const RR& a); |
---|
83 | |
---|
84 | void abs(RR& z, const RR& a); |
---|
85 | inline RR abs(const RR& a) |
---|
86 | { RR z; abs(z, a); NTL_OPT_RETURN(RR, z); } |
---|
87 | inline RR fabs(const RR& a) |
---|
88 | { RR z; abs(z, a); NTL_OPT_RETURN(RR, z); } |
---|
89 | |
---|
90 | void mul(RR& z, const RR& a, const RR& b); |
---|
91 | |
---|
92 | void mul(RR& z, const RR& a, double b); |
---|
93 | inline void mul(RR& z, double a, const RR& b) { mul(z, b, a); } |
---|
94 | |
---|
95 | void sqr(RR& z, const RR& a); |
---|
96 | inline RR sqr(const RR& a) |
---|
97 | { RR z; sqr(z, a); NTL_OPT_RETURN(RR, z); } |
---|
98 | |
---|
99 | void div(RR& z, const RR& a, const RR& b); |
---|
100 | |
---|
101 | void div(RR& z, const RR& a, double b); |
---|
102 | void div(RR& z, double a, const RR& b); |
---|
103 | |
---|
104 | void inv(RR& z, const RR& a); |
---|
105 | inline RR inv(const RR& a) |
---|
106 | { RR z; inv(z, a); NTL_OPT_RETURN(RR, z); } |
---|
107 | |
---|
108 | // operator notation: |
---|
109 | |
---|
110 | inline RR operator+(const RR& a, const RR& b) |
---|
111 | { RR x; add(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
112 | |
---|
113 | inline RR operator+(const RR& a, double b) |
---|
114 | { RR x; add(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
115 | |
---|
116 | inline RR operator+(double a, const RR& b) |
---|
117 | { RR x; add(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
118 | |
---|
119 | inline RR& operator+=(RR& x, const RR& b) |
---|
120 | { add(x, x, b); return x; } |
---|
121 | |
---|
122 | inline RR& operator+=(RR& x, double b) |
---|
123 | { add(x, x, b); return x; } |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | inline RR operator-(const RR& a, const RR& b) |
---|
128 | { RR x; sub(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
129 | |
---|
130 | inline RR operator-(const RR& a, double b) |
---|
131 | { RR x; sub(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
132 | |
---|
133 | inline RR operator-(double a, const RR& b) |
---|
134 | { RR x; sub(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
135 | |
---|
136 | inline RR& operator-=(RR& x, const RR& b) |
---|
137 | { sub(x, x, b); return x; } |
---|
138 | |
---|
139 | inline RR& operator-=(RR& x, double b) |
---|
140 | { sub(x, x, b); return x; } |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | inline RR operator*(const RR& a, const RR& b) |
---|
145 | { RR x; mul(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
146 | |
---|
147 | inline RR operator*(const RR& a, double b) |
---|
148 | { RR x; mul(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
149 | |
---|
150 | inline RR operator*(double a, const RR& b) |
---|
151 | { RR x; mul(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
152 | |
---|
153 | inline RR& operator*=(RR& x, const RR& b) |
---|
154 | { mul(x, x, b); return x; } |
---|
155 | |
---|
156 | inline RR& operator*=(RR& x, double b) |
---|
157 | { mul(x, x, b); return x; } |
---|
158 | |
---|
159 | |
---|
160 | inline RR operator/(const RR& a, const RR& b) |
---|
161 | { RR x; div(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
162 | |
---|
163 | inline RR operator/(const RR& a, double b) |
---|
164 | { RR x; div(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
165 | |
---|
166 | inline RR operator/(double a, const RR& b) |
---|
167 | { RR x; div(x, a, b); NTL_OPT_RETURN(RR, x); } |
---|
168 | |
---|
169 | inline RR& operator/=(RR& x, const RR& b) |
---|
170 | { div(x, x, b); return x; } |
---|
171 | |
---|
172 | inline RR& operator/=(RR& x, double b) |
---|
173 | { div(x, x, b); return x; } |
---|
174 | |
---|
175 | |
---|
176 | inline RR operator-(const RR& a) |
---|
177 | { RR x; negate(x, a); NTL_OPT_RETURN(RR, x); } |
---|
178 | |
---|
179 | |
---|
180 | inline RR& operator++(RR& x) { add(x, x, 1); return x; } |
---|
181 | inline void operator++(RR& x, int) { add(x, x, 1); } |
---|
182 | inline RR& operator--(RR& x) { sub(x, x, 1); return x; } |
---|
183 | inline void operator--(RR& x, int) { sub(x, x, 1); } |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | long compare(const RR& a, const RR& b); |
---|
188 | |
---|
189 | long compare(const RR& a, double b); |
---|
190 | inline long compare(double a, const RR& b) { return -compare(b, a); } |
---|
191 | |
---|
192 | |
---|
193 | long operator==(const RR& a, const RR& b); |
---|
194 | inline long operator!=(const RR& a, const RR& b) { return !(a == b); } |
---|
195 | inline long operator<=(const RR& a, const RR& b) { return compare(a, b) <= 0; } |
---|
196 | inline long operator>=(const RR& a, const RR& b) { return compare(a, b) >= 0; } |
---|
197 | inline long operator <(const RR& a, const RR& b) { return compare(a, b) < 0; } |
---|
198 | inline long operator >(const RR& a, const RR& b) { return compare(a, b) > 0; } |
---|
199 | |
---|
200 | long operator==(const RR& a, double b); |
---|
201 | inline long operator!=(const RR& a, double b) { return !(a == b); } |
---|
202 | inline long operator<=(const RR& a, double b) { return compare(a, b) <= 0; } |
---|
203 | inline long operator>=(const RR& a, double b) { return compare(a, b) >= 0; } |
---|
204 | inline long operator <(const RR& a, double b) { return compare(a, b) < 0; } |
---|
205 | inline long operator >(const RR& a, double b) { return compare(a, b) > 0; } |
---|
206 | |
---|
207 | inline long operator==(double a, const RR& b) { return (b == a); } |
---|
208 | inline long operator!=(double a, const RR& b) { return !(a == b); } |
---|
209 | inline long operator<=(double a, const RR& b) { return compare(a, b) <= 0; } |
---|
210 | inline long operator>=(double a, const RR& b) { return compare(a, b) >= 0; } |
---|
211 | inline long operator <(double a, const RR& b) { return compare(a, b) < 0; } |
---|
212 | inline long operator >(double a, const RR& b) { return compare(a, b) > 0; } |
---|
213 | |
---|
214 | void ceil(RR& z, const RR& a); |
---|
215 | inline RR ceil(const RR& a) |
---|
216 | { RR z; ceil(z, a); NTL_OPT_RETURN(RR, z); } |
---|
217 | |
---|
218 | void floor(RR& z, const RR& a); |
---|
219 | inline RR floor(const RR& a) |
---|
220 | { RR z; floor(z, a); NTL_OPT_RETURN(RR, z); } |
---|
221 | |
---|
222 | void trunc(RR& z, const RR& a); |
---|
223 | inline RR trunc(const RR& a) |
---|
224 | { RR z; trunc(z, a); NTL_OPT_RETURN(RR, z); } |
---|
225 | |
---|
226 | void round(RR& z, const RR& a); |
---|
227 | inline RR round(const RR& a) |
---|
228 | { RR z; round(z, a); NTL_OPT_RETURN(RR, z); } |
---|
229 | |
---|
230 | void RoundToPrecision(RR& z, const RR& a, long p); |
---|
231 | inline RR RoundToPrecision(const RR& a, long p) |
---|
232 | { RR z; RoundToPrecision(z, a, p); NTL_OPT_RETURN(RR, z); } |
---|
233 | |
---|
234 | |
---|
235 | void conv(RR& z, const ZZ& a); |
---|
236 | void conv(RR& z, long a); |
---|
237 | inline void conv(RR& z, int a) { conv(z, long(a)); } |
---|
238 | void conv(RR& z, unsigned long a); |
---|
239 | inline void conv(RR& z, unsigned int a) { conv(z, (unsigned long)(a)); } |
---|
240 | void conv(RR& z, const char *s); |
---|
241 | void conv(RR& z, double a); |
---|
242 | inline void conv(RR& z, float a) { conv(z, double(a)); } |
---|
243 | void conv(RR& z, const xdouble& a); |
---|
244 | void conv(RR& z, const quad_float& a); |
---|
245 | |
---|
246 | void conv(RR& z, const RR& a); |
---|
247 | |
---|
248 | |
---|
249 | |
---|
250 | inline RR::RR(INIT_VAL_TYPE, int a) { e = 0; conv(*this, a); } |
---|
251 | inline RR::RR(INIT_VAL_TYPE, long a) { e = 0; conv(*this, a); } |
---|
252 | inline RR::RR(INIT_VAL_TYPE, unsigned int a) { e = 0; conv(*this, a); } |
---|
253 | inline RR::RR(INIT_VAL_TYPE, unsigned long a) { e = 0; conv(*this, a); } |
---|
254 | inline RR::RR(INIT_VAL_TYPE, float a) { e = 0; conv(*this, a); } |
---|
255 | inline RR::RR(INIT_VAL_TYPE, double a) { e = 0; conv(*this, a); } |
---|
256 | |
---|
257 | inline RR::RR(INIT_VAL_TYPE, const RR& a) { e = 0; conv(*this, a); } |
---|
258 | inline RR::RR(INIT_VAL_TYPE, const ZZ& a) { e = 0; conv(*this, a); } |
---|
259 | inline RR::RR(INIT_VAL_TYPE, const xdouble& a) { e = 0; conv(*this, a); } |
---|
260 | inline RR::RR(INIT_VAL_TYPE, const quad_float& a) { e = 0; conv(*this, a); } |
---|
261 | inline RR::RR(INIT_VAL_TYPE, const char *a) { e = 0; conv(*this, a); } |
---|
262 | |
---|
263 | |
---|
264 | inline RR to_RR(int a) { return RR(INIT_VAL, a); } |
---|
265 | inline RR to_RR(long a) { return RR(INIT_VAL, a); } |
---|
266 | inline RR to_RR(unsigned int a) { return RR(INIT_VAL, a); } |
---|
267 | inline RR to_RR(unsigned long a) { return RR(INIT_VAL, a); } |
---|
268 | inline RR to_RR(float a) { return RR(INIT_VAL, a); } |
---|
269 | inline RR to_RR(double a) { return RR(INIT_VAL, a); } |
---|
270 | inline RR to_RR(const ZZ& a) { return RR(INIT_VAL, a); } |
---|
271 | inline RR to_RR(const RR& a) { return RR(INIT_VAL, a); } |
---|
272 | inline RR to_RR(const xdouble& a) { return RR(INIT_VAL, a); } |
---|
273 | inline RR to_RR(const quad_float& a) { return RR(INIT_VAL, a); } |
---|
274 | inline RR to_RR(const char *a) { return RR(INIT_VAL, a); } |
---|
275 | |
---|
276 | inline RR& RR::operator=(double a) { conv(*this, a); return *this; } |
---|
277 | |
---|
278 | void conv(ZZ& z, const RR& a); |
---|
279 | void conv(long& z, const RR& a); |
---|
280 | void conv(double& z, const RR& a); |
---|
281 | void conv(xdouble& z, const RR& a); |
---|
282 | void conv(quad_float& z, const RR& a); |
---|
283 | |
---|
284 | inline void conv(int& z, const RR& a) |
---|
285 | { long t; conv(t, a); z = int(t); } |
---|
286 | |
---|
287 | inline void conv(float& z, const RR& a) |
---|
288 | { double t; conv(t, a); z = float(t); } |
---|
289 | |
---|
290 | inline int to_int(const RR& a) { int z; conv(z, a); return z; } |
---|
291 | inline long to_long(const RR& a) { long z; conv(z, a); return z; } |
---|
292 | inline float to_float(const RR& a) { float z; conv(z, a); return z; } |
---|
293 | inline double to_double(const RR& a) { double z; conv(z, a); return z; } |
---|
294 | |
---|
295 | inline xdouble to_xdouble(const RR& a) |
---|
296 | { xdouble z; conv(z, a); return z; } |
---|
297 | inline quad_float to_quad_float(const RR& a) |
---|
298 | { quad_float z; conv(z, a); return z; } |
---|
299 | |
---|
300 | inline ZZ to_ZZ(const RR& a) |
---|
301 | { ZZ z; conv(z, a); NTL_OPT_RETURN(ZZ, z); } |
---|
302 | |
---|
303 | void CeilToZZ(ZZ& z, const RR& a); |
---|
304 | inline ZZ CeilToZZ(const RR& a) |
---|
305 | { ZZ z; CeilToZZ(z, a); NTL_OPT_RETURN(ZZ, z); } |
---|
306 | |
---|
307 | void TruncToZZ(ZZ& z, const RR& a); |
---|
308 | inline ZZ TruncToZZ(const RR& a) |
---|
309 | { ZZ z; TruncToZZ(z, a); NTL_OPT_RETURN(ZZ, z); } |
---|
310 | |
---|
311 | void RoundToZZ(ZZ& z, const RR& a); |
---|
312 | inline ZZ RoundToZZ(const RR& a) |
---|
313 | { ZZ z; RoundToZZ(z, a); NTL_OPT_RETURN(ZZ, z); } |
---|
314 | |
---|
315 | inline void FloorToZZ(ZZ& z, const RR& a) { conv(z, a); } |
---|
316 | inline ZZ FloorToZZ(const RR& a) |
---|
317 | { ZZ z; conv(z, a); NTL_OPT_RETURN(ZZ, z); } |
---|
318 | |
---|
319 | void MakeRR(RR& z, const ZZ& a, long e); |
---|
320 | inline RR MakeRR(const ZZ& a, long e) |
---|
321 | { RR z; MakeRR(z, a, e); NTL_OPT_RETURN(RR, z); } |
---|
322 | |
---|
323 | void random(RR& z); |
---|
324 | inline RR random_RR() |
---|
325 | { RR z; random(z); NTL_OPT_RETURN(RR, z); } |
---|
326 | |
---|
327 | |
---|
328 | void power(RR& z, const RR& a, long e); |
---|
329 | inline RR power(const RR& a, long e) |
---|
330 | { RR z; power(z, a, e); NTL_OPT_RETURN(RR, z); } |
---|
331 | |
---|
332 | void power2(RR& z, long e); |
---|
333 | |
---|
334 | inline RR power2_RR(long e) |
---|
335 | { RR z; power2(z, e); NTL_OPT_RETURN(RR, z); } |
---|
336 | |
---|
337 | NTL_SNS ostream& operator<<(NTL_SNS ostream& s, const RR& a); |
---|
338 | NTL_SNS istream& operator>>(NTL_SNS istream& s, RR& x); |
---|
339 | |
---|
340 | |
---|
341 | void SqrRoot(RR& x, const RR& a); |
---|
342 | inline RR SqrRoot(const RR& a) |
---|
343 | { RR z; SqrRoot(z, a); NTL_OPT_RETURN(RR, z); } |
---|
344 | inline RR sqrt(const RR& a) |
---|
345 | { RR z; SqrRoot(z, a); NTL_OPT_RETURN(RR, z); } |
---|
346 | |
---|
347 | void exp(RR& res, const RR& x); |
---|
348 | inline RR exp(const RR& a) |
---|
349 | { RR z; exp(z, a); NTL_OPT_RETURN(RR, z); } |
---|
350 | |
---|
351 | void log(RR& res, const RR& x); |
---|
352 | inline RR log(const RR& a) |
---|
353 | { RR z; log(z, a); NTL_OPT_RETURN(RR, z); } |
---|
354 | |
---|
355 | void log10(RR& res, const RR& x); |
---|
356 | inline RR log10(const RR& a) |
---|
357 | { RR z; log10(z, a); NTL_OPT_RETURN(RR, z); } |
---|
358 | |
---|
359 | void expm1(RR& res, const RR& x); |
---|
360 | inline RR expm1(const RR& a) |
---|
361 | { RR z; expm1(z, a); NTL_OPT_RETURN(RR, z); } |
---|
362 | |
---|
363 | void log1p(RR& res, const RR& x); |
---|
364 | inline RR log1p(const RR& a) |
---|
365 | { RR z; log1p(z, a); NTL_OPT_RETURN(RR, z); } |
---|
366 | |
---|
367 | void pow(RR& res, const RR& x, const RR& y); |
---|
368 | inline RR pow(const RR& x, const RR& y) |
---|
369 | { RR z; pow(z, x, y); NTL_OPT_RETURN(RR, z); } |
---|
370 | |
---|
371 | void ComputePi(RR& res); |
---|
372 | inline RR ComputePi_RR() |
---|
373 | { RR z; ComputePi(z); NTL_OPT_RETURN(RR, z); } |
---|
374 | |
---|
375 | void sin(RR& res, const RR& x); |
---|
376 | inline RR sin(const RR& a) |
---|
377 | { RR z; sin(z, a); NTL_OPT_RETURN(RR, z); } |
---|
378 | |
---|
379 | void cos(RR& res, const RR& x); |
---|
380 | inline RR cos(const RR& a) |
---|
381 | { RR z; cos(z, a); NTL_OPT_RETURN(RR, z); } |
---|
382 | |
---|
383 | |
---|
384 | |
---|
385 | |
---|
386 | NTL_CLOSE_NNS |
---|
387 | |
---|
388 | #endif |
---|