APRONXX 0.9.15
/home/mine/apron/apronxx/apxx_scalar.hh
Go to the documentation of this file.
1/* -*- C++ -*-
2 * apxx_scalar.hh
3 *
4 * APRON Library / C++ class wrappers
5 *
6 * Copyright (C) Antoine Mine' 2007
7 *
8 */
9/* This file is part of the APRON Library, released under LGPL license
10 with an exception allowing the redistribution of statically linked
11 executables.
12
13 Please read the COPYING file packaged in the distribution.
14*/
15
16#ifndef __APXX_SCALAR_HH
17#define __APXX_SCALAR_HH
18
19#include <stdlib.h>
20#include <iostream>
21
22#include "ap_scalar.h"
23#include "gmpxx.h"
24
25
26namespace apron {
27
28
29
30struct infty {
31
32 int sgn;
33
35 infty(int sgn=1) : sgn(sgn) {};
36
37};
38
39
41struct frac {
43 long num;
44 unsigned long den;
45
46
49 frac(long num, unsigned long den=1) : num(num), den(den) {}
50
51};
52
53
55class bad_discriminant : public std::logic_error {
56public:
57 /*! \arg \c arg: Reason of failure.
58 */
59 bad_discriminant(const std::string &arg) : logic_error(arg) {}
60};
61
63/* ================================= */
64/* use_malloc */
65/* ================================= */
66
67
68//! Inherited by most wrappers to map new and delete to malloc and free.
69struct use_malloc {
70 void* operator new (size_t sz);
71 void* operator new[] (size_t sz);
72 void operator delete (void* p);
73 void operator delete[](void* p);
74};
75
76
77
78/* ================================= */
79/* scalar */
80/* ================================= */
81
82
85
86 * or a MPQ (multi-precision fraction).
87 * All operations are exact unless explicitly noted.
88 */
89class scalar : public use_malloc {
90
91protected:
92 ap_scalar_t c;
93
94public:
95
97 enum order {
98 LESS = -1,
99 EQUAL = 0,
101 };
102
103
104 /* constructors */
105 /* ============ */
106
109
111 scalar();
112
114 scalar(int x);
115
117 scalar(long x);
118
120 scalar(double x);
121
123 scalar(const frac& x);
124
125 //! Makes a double scalar equal to +oo or -oo.
126 scalar(infty x);
127
129 scalar(const mpq_class& x);
130
131 //! Makes a MPFR scalar from a MPFR number (copied).
132 scalar(mpfr_t x);
133
135 scalar(const scalar& x);
136
137 //@}
138
139
140 /* destructor */
141 /* ========== */
142
143 /** @name Destructor */
145
146 ~scalar();
147
148
150
151 /* assignments */
152 /* =========== */
153
154
155 //@{
156
158 scalar& operator= (int x);
159
160
162
164 scalar& operator= (double x);
165
166
168
171
172
173 scalar& operator= (const mpq_class& x);
174
176 scalar& operator= (mpfr_t x);
177
179 scalar& operator= (const scalar& x);
180
182 friend void swap(scalar& a, scalar &b);
183
186
187 /* access */
188 /* ====== */
189
190 /** @name Accesses */
192
195
196 */
197 ap_scalar_discr_t get_discr() const;
198
201
202 */
203 double& get_double();
204
207
208 */
209 const double& get_double() const;
210
213
215 mpq_class& get_mpq();
216
219 *\throw bad_discriminant when the scalar is not of MPQ type.
220 */
221 const mpq_class& get_mpq() const;
222
224
225
226 /* conversion */
227 /* ========== */
228
231
233
234 * \arg \c round: rounding direction, may be GMP_RNDN, GMP_RNDZ, GMP_RNDU, GMP_RNDD, or GMP_RND_MAX.
235 * \arg \c conv is set to either LESS, EQUAL, or GREATER, depending on whether the result is
236 * less than, equal to or greater than the value of *this.
237 * \note Currently round is ignored and the conversion is always exact.
238 */
239 mpq_class to_mpq(mp_rnd_t round, order& conv) const;
245
246 */
247 double to_double(mp_rnd_t round, order& conv) const;
248
251
255 void to_mpfr(mpfr_t x, mp_rnd_t round, order& conv) const;
258 operator mpq_class() const;
259
261 operator double() const;
262
264
265
266 /* print */
267 /* ===== */
268
271
273 friend std::ostream& operator<< (std::ostream& os, const scalar& s);
274
276 void print(FILE* stream=stdout) const;
277
279
280
281 /* tests */
282 /* ===== */
283
286
287 //! Returns LESS if -oo, GREATER if +oo, EQUAL if finite.
288 order is_infty() const;
289
291 order sgn () const;
292
294 friend order cmp(const scalar& a, const scalar& b);
295
296 friend bool operator>=(const scalar& a, const scalar& b);
297 friend bool operator<=(const scalar& a, const scalar& b);
298 friend bool operator> (const scalar& a, const scalar& b);
299 friend bool operator< (const scalar& a, const scalar& b);
300 friend bool operator==(const scalar& a, const scalar& b);
301 friend bool operator!=(const scalar& a, const scalar& b);
302
303 friend order cmp(const scalar& a, int b);
304 friend bool operator>=(const scalar& a, int b);
305 friend bool operator<=(const scalar& a, int b);
306 friend bool operator> (const scalar& a, int b);
307 friend bool operator< (const scalar& a, int b);
308 friend bool operator==(const scalar& a, int b);
309 friend bool operator!=(const scalar& a, int b);
310
311 friend order cmp(int a, const scalar& b);
312 friend bool operator>=(int a, const scalar& b);
313 friend bool operator<=(int a, const scalar& b);
314 friend bool operator> (int a, const scalar& b);
315 friend bool operator< (int a, const scalar& b);
316 friend bool operator==(int a, const scalar& b);
317 friend bool operator!=(int a, const scalar& b);
318
320
321
322 /* other operators */
323 /* =============== */
324
326
329 void neg();
330
331
333
335 void inv();
336
337 //! Returns the inverse of *this. Not exact for double type.
338 scalar operator~ () const;
339
341 long hash() const;
342
343 //@}
344
345
346 /* C API compatibility */
347 /* =================== */
351
352
353 const ap_scalar_t* get_ap_scalar_t() const;
354
356 ap_scalar_t* get_ap_scalar_t();
357
358 //@}
359
360};
361
362
364
365}
366
367#endif /* __APXX_SCALAR_HH */
bad_discriminant(const std::string &arg)
Definition apxx_scalar.hh:59
friend std::ostream & operator<<(std::ostream &os, const scalar &s)
Printing.
Definition apxx_scalar.hh:256
double to_double(mp_rnd_t round, order &conv) const
Returns a double containing the (possibly converted) value of the scalar.
Definition apxx_scalar.hh:226
scalar & operator=(int x)
Assigns a native integer to *this, setting its type to MPQ.
Definition apxx_scalar.hh:125
friend bool operator<=(const scalar &a, const scalar &b)
Definition apxx_scalar.hh:317
friend order cmp(const scalar &a, const scalar &b)
Returns LESS if a<b, GREATER if a>b, EQUAL if a=b (total order).
Definition apxx_scalar.hh:307
scalar()
Makes a double scalar equal to 0.0.
Definition apxx_scalar.hh:57
friend bool operator==(const scalar &a, const scalar &b)
Definition apxx_scalar.hh:332
void to_mpfr(mpfr_t x, mp_rnd_t round, order &conv) const
Copies the (possibly converted) value of the scalar into the MPFR.
Definition apxx_scalar.hh:234
void print(FILE *stream=stdout) const
Prints to a C stream.
Definition apxx_scalar.hh:287
double & get_double()
Returns a (modifiable) reference to the double contained in the scalar.
Definition apxx_scalar.hh:190
friend bool operator>=(const scalar &a, const scalar &b)
Definition apxx_scalar.hh:312
void neg()
Negates *this.
Definition apxx_scalar.hh:417
order is_infty() const
Returns LESS if -oo, GREATER if +oo, EQUAL if finite.
Definition apxx_scalar.hh:296
ap_scalar_discr_t get_discr() const
Whether the object wraps a double or a MPQ.
Definition apxx_scalar.hh:185
scalar operator-() const
Returns the opposite of *this.
Definition apxx_scalar.hh:422
friend bool operator>(const scalar &a, const scalar &b)
Definition apxx_scalar.hh:322
void inv()
Inverses *this. Not exact for double type.
Definition apxx_scalar.hh:429
order
Returned by ordering functions.
Definition apxx_scalar.hh:97
@ EQUAL
Means equal or null (depending on context).
Definition apxx_scalar.hh:99
@ LESS
Means less than, negative, or -oo (depending on context).
Definition apxx_scalar.hh:98
@ GREATER
Means greater than, positive, or +oo (depending on context).
Definition apxx_scalar.hh:100
order sgn() const
Returns LESS if negative, GREATER if positive, EQUAL if null.
Definition apxx_scalar.hh:301
const ap_scalar_t * get_ap_scalar_t() const
Returns a pointer to the internal APRON object stored in *this.
Definition apxx_scalar.hh:450
scalar operator~() const
Returns the inverse of *this. Not exact for double type.
Definition apxx_scalar.hh:434
mpq_class to_mpq(mp_rnd_t round, order &conv) const
Returns a new MPQ containing the (possibly converted) value of the scalar.
Definition apxx_scalar.hh:218
mpq_class & get_mpq()
Returns a (modifiable) reference to the MPQ contained in the scalar.
Definition apxx_scalar.hh:202
long hash() const
Returns a hash code.
Definition apxx_scalar.hh:441
~scalar()
Definition apxx_scalar.hh:114
friend void swap(scalar &a, scalar &b)
Swaps the contents (type and value) of two scalars.
Definition apxx_scalar.hh:176
friend bool operator<(const scalar &a, const scalar &b)
Definition apxx_scalar.hh:327
friend bool operator!=(const scalar &a, const scalar &b)
Definition apxx_scalar.hh:337
ap_scalar_t c
Structure managed by APRON.
Definition apxx_scalar.hh:92
Definition apxx_abstract0.hh:27
A fraction with native int coefficients, to simplify initialisations and assignments.
Definition apxx_scalar.hh:41
unsigned long den
Denominator.
Definition apxx_scalar.hh:44
long num
Numerator.
Definition apxx_scalar.hh:43
frac(long num, unsigned long den=1)
Definition apxx_scalar.hh:49
Either +oo or -oo, to simplify initialisations and assignments.
Definition apxx_scalar.hh:30
infty(int sgn=1)
Definition apxx_scalar.hh:35
int sgn
Sign: >0 for +oo, <0 for -oo.
Definition apxx_scalar.hh:32
Inherited by most wrappers to map new and delete to malloc and free.
Definition apxx_scalar.hh:69