Scippy

SoPlex

Sequential object-oriented simPlex

rational.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file rational.cpp
17  * @brief Wrapper for GMP types.
18  */
19 
20 #include <cmath>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <iomanip>
25 #include <sstream>
26 #include <cstring>
27 #include <limits.h>
28 
29 
30 #include "soplex/rational.h"
31 #include "soplex/spxalloc.h"
32 #include "soplex/spxdefines.h"
33 
34 #ifdef SOPLEX_WITH_GMP
35 #include "gmp.h"
36 #endif
37 
38 /// use mpq_sgn instead of mpq_equal to compare rationals with zero
39 #define SOPLEX_PERFALT_1
40 
41 /// turn on checking rationals for zero in arithmetic plus and minus operators
42 #define SOPLEX_PERFALT_2a
43 
44 /// turn on checking rationals for zero, posone, negone in arithmetic multiplication and division operators
45 #define SOPLEX_PERFALT_2b
46 
47 /// turn on checking rationals for zero, posone, negone before conversion to double and long double
48 #define SOPLEX_PERFALT_3
49 
50 /// turn on checking for equality before assigning in operator=()
51 #define SOPLEX_PERFALT_4
52 
53 /// turn on checking whether assignment in assignment operators is redundant for 0
54 #define SOPLEX_PERFALT_5a
55 
56 /// turn on checking whether assignment in assignment operators is redundant for +1 and -1
57 #define SOPLEX_PERFALT_5b
58 
59 namespace soplex
60 {
61 
62 #ifdef SOPLEX_WITH_GMP
63 
64 /// rational zero
65 const Rational Rational::ZERO(0, true);
66 
67 /// rational plus one
68 const Rational Rational::POSONE(1, true);
69 
70 /// rational minus one
71 const Rational Rational::NEGONE(-1, true);
72 
73 /// list of unused Private objects; note that this cannot be used if SOPLEX_WITH_GMP is not defined, since then the
74 /// Private class has no member next() and prev()
75 /// should list memory be used?
76 #ifdef SOPLEX_NOLISTMEM
77 THREADLOCAL bool Rational::useListMem = false;
78 #else
79 THREADLOCAL bool Rational::useListMem = true;
80 #endif
81 
82 
83 
84 
85 /// list of unused Private objects
86 
87 #ifdef SOPLEX_WITH_GMP
88 THREADLOCAL IdList< Rational::Private > Rational::unusedPrivateList(0, 0, true);
89 #endif
90 
91 /// Defines the "Pimpl"-class Private
92 class Rational::Private
93 {
94 public:
95 
96  mpq_t privatevalue; ///< actual value of the Rational object
97  Private* theprev; ///< pointer to the previous element in the list
98  Private* thenext; ///< pointer to the next element in the list
99 
100  /// default constructor
101  Private()
102  : theprev(0)
103  , thenext(0)
104  {
105  mpq_init(privatevalue);
106  }
107 
108  /// copy constructor
109  Private(const Private& p)
110  : theprev(0)
111  , thenext(0)
112  {
113  // a newly constructed element is not in any list, even if the original element (p) is; hence we initialize
114  // theprev and thenext to zero
115  mpq_init(privatevalue);
116  mpq_set(this->privatevalue, p.privatevalue);
117  }
118 
119  /// constructor from long double
120  Private(const long double& r)
121  : theprev(0)
122  , thenext(0)
123  {
124  mpq_init(privatevalue);
125 
126  if(r == (long double)(1.0))
127  mpq_set(privatevalue, Rational::POSONE.dpointer->privatevalue);
128  else if(r == (long double)(-1.0))
129  mpq_set(privatevalue, Rational::NEGONE.dpointer->privatevalue);
130  else if(r == (long double)(0.0))
131  {
132  assert(mpq_equal(privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
133  }
134  else
135  mpq_set_d(privatevalue, double(r));
136  }
137 
138  /// constructor from double
139  Private(const double& r)
140  : theprev(0)
141  , thenext(0)
142  {
143  mpq_init(privatevalue);
144 
145  if(r == 1.0)
146  mpq_set(privatevalue, Rational::POSONE.dpointer->privatevalue);
147  else if(r == -1.0)
148  mpq_set(privatevalue, Rational::NEGONE.dpointer->privatevalue);
149  else if(r == 0.0)
150  {
151  assert(mpq_equal(privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
152  }
153  else
154  mpq_set_d(privatevalue, r);
155  }
156 
157  /// constructor from int
158  Private(const int& i)
159  : theprev(0)
160  , thenext(0)
161  {
162  mpq_init(privatevalue);
163 
164  if(i == 1)
165  mpq_set(privatevalue, Rational::POSONE.dpointer->privatevalue);
166  else if(i == -1)
167  mpq_set(privatevalue, Rational::NEGONE.dpointer->privatevalue);
168  else if(i == 0)
169  {
170  assert(mpq_equal(privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
171  }
172  else
173  mpq_set_si(privatevalue, i, 1);
174  }
175 
176  /// constructor from mpq_t
177  Private(const mpq_t& q)
178  : theprev(0)
179  , thenext(0)
180  {
181  mpq_init(privatevalue);
182  mpq_set(privatevalue, q);
183  }
184 
185  /// destructor
186  ~Private()
187  {
188  mpq_clear(privatevalue);
189  }
190 
191  /// assignment operator
192  Private& operator=(const Private& p)
193  {
194 #ifdef SOPLEX_PERFALT_4
195 
196  if(mpq_equal(this->privatevalue, p.privatevalue) != 0)
197  return *this;
198 
199 #endif
200 
201  // we only assign the value; the position in the list, i.e., theprev and thenext, must not be modified
202  mpq_set(this->privatevalue, p.privatevalue);
203  return *this;
204  }
205 
206  /// assignment operator from long double
207  Private& operator=(const long double& r)
208  {
209  // we only assign the value; the position in the list, i.e., theprev and thenext, must not be modified
210  if(r == (long double)(0.0))
211  {
212 #ifdef SOPLEX_PERFALT_5a
213 #ifdef SOPLEX_PERFALT_1
214 
215  if(mpq_sgn(privatevalue) != 0)
216 #else
217  if(mpq_equal(privatevalue, Rational::ZERO.dpointer->privatevalue) == 0)
218 #endif
219 #endif
220  mpq_set(privatevalue, Rational::ZERO.dpointer->privatevalue);
221  }
222  else if(r == (long double)(1.0))
223  {
224 #ifdef SOPLEX_PERFALT_5b
225 
226  if(mpq_equal(privatevalue, Rational::POSONE.dpointer->privatevalue) == 0)
227 #endif
228  mpq_set(privatevalue, Rational::POSONE.dpointer->privatevalue);
229  }
230  else if(r == (long double)(-1.0))
231  {
232 #ifdef SOPLEX_PERFALT_5b
233 
234  if(mpq_equal(privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0)
235 #endif
236  mpq_set(privatevalue, Rational::NEGONE.dpointer->privatevalue);
237  }
238  else
239  mpq_set_d(this->privatevalue, double(r));
240 
241  return *this;
242  }
243 
244  /// assignment operator from double
245  Private& operator=(const double& r)
246  {
247  // we only assign the value; the position in the list, i.e., theprev and thenext, must not be modified
248  if(r == 0.0)
249  {
250 #ifdef SOPLEX_PERFALT_5a
251 #ifdef SOPLEX_PERFALT_1
252 
253  if(mpq_sgn(privatevalue) != 0)
254 #else
255  if(mpq_equal(privatevalue, Rational::ZERO.dpointer->privatevalue) == 0)
256 #endif
257 #endif
258  mpq_set(privatevalue, Rational::ZERO.dpointer->privatevalue);
259  }
260  else if(r == 1.0)
261  {
262 #ifdef SOPLEX_PERFALT_5b
263 
264  if(mpq_equal(privatevalue, Rational::POSONE.dpointer->privatevalue) == 0)
265 #endif
266  mpq_set(privatevalue, Rational::POSONE.dpointer->privatevalue);
267  }
268  else if(r == -1.0)
269  {
270 #ifdef SOPLEX_PERFALT_5b
271 
272  if(mpq_equal(privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0)
273 #endif
274  mpq_set(privatevalue, Rational::NEGONE.dpointer->privatevalue);
275  }
276  else
277  mpq_set_d(privatevalue, r);
278 
279  return *this;
280  }
281 
282  /// assignment operator from int
283  Private& operator=(const int& i)
284  {
285  // we only assign the value; the position in the list, i.e., theprev and thenext, must not be modified
286  if(i == 0)
287  {
288 #ifdef SOPLEX_PERFALT_5a
289 #ifdef SOPLEX_PERFALT_1
290 
291  if(mpq_sgn(privatevalue) != 0)
292 #else
293  if(mpq_equal(privatevalue, Rational::ZERO.dpointer->privatevalue) == 0)
294 #endif
295 #endif
296  mpq_set(privatevalue, Rational::ZERO.dpointer->privatevalue);
297  }
298  else if(i == 1)
299  {
300 #ifdef SOPLEX_PERFALT_5b
301 
302  if(mpq_equal(privatevalue, Rational::POSONE.dpointer->privatevalue) == 0)
303 #endif
304  mpq_set(privatevalue, Rational::POSONE.dpointer->privatevalue);
305  }
306  else if(i == -1)
307  {
308 #ifdef SOPLEX_PERFALT_5b
309 
310  if(mpq_equal(privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0)
311 #endif
312  mpq_set(privatevalue, Rational::NEGONE.dpointer->privatevalue);
313  }
314  else
315  mpq_set_si(privatevalue, i, 1);
316 
317  return *this;
318  }
319 
320  /// assignment operator from mpq_t
321  Private& operator=(const mpq_t& q)
322  {
323 #ifdef SOPLEX_PERFALT_4
324 
325  if(mpq_equal(this->privatevalue, q) != 0)
326  return *this;
327 
328 #endif
329 
330  // we only assign the value; the position in the list, i.e., theprev and thenext, must not be modified
331  mpq_set(this->privatevalue, q);
332  return *this;
333  }
334 
335  /// previous Private element
336  Private*& prev()
337  {
338  return theprev;
339  }
340 
341  /// previous Private element
342  Private* const& prev() const
343  {
344  return theprev;
345  }
346 
347  /// next Private element
348  Private*& next()
349  {
350  return thenext;
351  }
352 
353  /// next Private element
354  Private* const& next() const
355  {
356  return thenext;
357  }
358 };
359 
360 
361 
362 /// special constructor only for initializing static rational variables; this is necessary since we need a constructor
363 /// for Rational::{ZERO, POSONE, NEGONE} that does not use these numbers
364 Rational::Rational(const int& i, const bool& dummy)
365 {
366  dpointer = 0;
368  new(dpointer) Private();
369  mpq_set_si(dpointer->privatevalue, i, 1);
370 
371  assert(dpointer != 0);
372 }
373 
374 
375 
376 /// default constructor
378 {
379  if(Rational::useListMem)
380  {
381  dpointer = unusedPrivateList.last();
382 
383  if(dpointer != 0)
384  {
385  assert(unusedPrivateList.first() != 0);
386  unusedPrivateList.remove(dpointer);
387  }
388  else
389  {
390  assert(unusedPrivateList.first() == 0);
392  new(dpointer) Private();
393  }
394  }
395  else
396  {
397  assert(unusedPrivateList.length() == 0);
398  dpointer = 0;
399  spx_alloc(dpointer);
400  new(dpointer) Private();
401  }
402 
403  assert(dpointer != 0);
404 }
405 
406 
407 
408 /// copy constructor
410 {
411  if(Rational::useListMem)
412  {
413  dpointer = unusedPrivateList.last();
414 
415  if(dpointer != 0)
416  {
417  assert(unusedPrivateList.first() != 0);
418  unusedPrivateList.remove(dpointer);
419  *dpointer = *(r.dpointer);
420  }
421  else
422  {
423  assert(unusedPrivateList.first() == 0);
425  new(dpointer) Private(*(r.dpointer));
426  }
427  }
428  else
429  {
430  assert(unusedPrivateList.length() == 0);
431  dpointer = 0;
432  spx_alloc(dpointer);
433  new(dpointer) Private(*(r.dpointer));
434  }
435 
436  assert(dpointer != 0);
437 }
438 
439 
440 
441 /// constructor from long double
442 Rational::Rational(const long double& r)
443 {
444  if(Rational::useListMem)
445  {
446  dpointer = unusedPrivateList.last();
447 
448  if(dpointer != 0)
449  {
450  assert(unusedPrivateList.first() != 0);
451  unusedPrivateList.remove(dpointer);
452  *dpointer = r;
453  }
454  else
455  {
456  assert(unusedPrivateList.first() == 0);
458  new(dpointer) Private(r);
459  }
460  }
461  else
462  {
463  assert(unusedPrivateList.length() == 0);
464  dpointer = 0;
465  spx_alloc(dpointer);
466  new(dpointer) Private(r);
467  }
468 
469  assert(dpointer != 0);
470 }
471 
472 
473 
474 /// constructor from double
475 Rational::Rational(const double& r)
476 {
477  if(Rational::useListMem)
478  {
479  dpointer = unusedPrivateList.last();
480 
481  if(dpointer != 0)
482  {
483  assert(unusedPrivateList.first() != 0);
484  unusedPrivateList.remove(dpointer);
485  *dpointer = r;
486  }
487  else
488  {
489  assert(unusedPrivateList.first() == 0);
491  new(dpointer) Private(r);
492  }
493  }
494  else
495  {
496  assert(unusedPrivateList.length() == 0);
497  dpointer = 0;
498  spx_alloc(dpointer);
499  new(dpointer) Private(r);
500  }
501 
502  assert(dpointer != 0);
503 }
504 
505 
506 
507 /// constructor from int
508 Rational::Rational(const int& i)
509 {
510  if(Rational::useListMem)
511  {
512  dpointer = unusedPrivateList.last();
513 
514  if(dpointer != 0)
515  {
516  assert(unusedPrivateList.first() != 0);
517  unusedPrivateList.remove(dpointer);
518  *dpointer = i;
519  }
520  else
521  {
522  assert(unusedPrivateList.first() == 0);
524  new(dpointer) Private(i);
525  }
526  }
527  else
528  {
529  assert(unusedPrivateList.length() == 0);
530  dpointer = 0;
531  spx_alloc(dpointer);
532  new(dpointer) Private(i);
533  }
534 
535  assert(dpointer != 0);
536 }
537 
538 
539 
540 /// constructor from mpq_t
541 Rational::Rational(const mpq_t& q)
542 {
543  if(Rational::useListMem)
544  {
545  dpointer = unusedPrivateList.last();
546 
547  if(dpointer != 0)
548  {
549  assert(unusedPrivateList.first() != 0);
550  unusedPrivateList.remove(dpointer);
551  *dpointer = q;
552  }
553  else
554  {
555  assert(unusedPrivateList.first() == 0);
557  new(dpointer) Private(q);
558  }
559  }
560  else
561  {
562  assert(unusedPrivateList.length() == 0);
563  dpointer = 0;
564  spx_alloc(dpointer);
565  new(dpointer) Private(q);
566  }
567 
568  assert(dpointer != 0);
569 }
570 
571 
572 
573 /// destructor
575 {
576  assert(Rational::useListMem || unusedPrivateList.length() == 0);
577 
578  if(!Rational::useListMem || this == &Rational::ZERO || this == &Rational::POSONE
579  || this == &Rational::NEGONE)
580  {
581  dpointer->~Private();
583  }
584  else
585  {
586  // for memory efficiency, we could free the Private object (or even more Private objects from the list of unused
587  // elements) if there are much more unused than used Private objects; this requires counting the used Private
588  // objects, though; we do not implement this currently, because we have not encountered memory problems, so far, and
589  // because freeing costs time
590  unusedPrivateList.append(dpointer);
591  }
592 }
593 
594 
595 
596 /// enables list memory
598 {
599  assert(Rational::useListMem || unusedPrivateList.length() == 0);
600  Rational::useListMem = true;
601 }
602 
603 
604 
605 /// frees the unused rational elements in the memory list
606 /// frees the unused rational elements in the memory list
607 /** this can be useful when you want to save memory or needed when working with a GMP memory manager like the one
608  * in EGlib that frees GMP memory before the destructor of the static memory list is called; in most cases this
609  * method is optional; note that this does not free the Rational elements that are currently in use
610  */
612 {
613  unusedPrivateList.clear(true);
614  assert(unusedPrivateList.length() == 0);
615 }
616 
617 
618 
619 /// disables list memory
621 {
623  Rational::useListMem = false;
624 }
625 
626 
627 
628 /// assignment operator
630 {
631  *(this->dpointer) = *(r.dpointer);
632  return *this;
633 }
634 
635 
636 
637 /// assignment operator from long double
638 Rational& Rational::operator=(const long double& r)
639 {
640  *(this->dpointer) = r;
641  return *this;
642 }
643 
644 
645 
646 /// assignment operator from double
647 Rational& Rational::operator=(const double& r)
648 {
649  *(this->dpointer) = r;
650  return *this;
651 }
652 
653 
654 
655 /// assignment operator from int
656 Rational& Rational::operator=(const int& i)
657 {
658  *(this->dpointer) = i;
659  return *this;
660 }
661 
662 
663 
664 /// assignment operator from mpq_t
665 Rational& Rational::operator=(const mpq_t& q)
666 {
667  *(this->dpointer) = q;
668  return *this;
669 }
670 
671 
672 
673 /// typecasts Rational to double (allows only explicit typecast)
674 Rational::operator double() const
675 {
676 #ifdef SOPLEX_PERFALT_3
677 #ifdef SOPLEX_PERFALT_1
678 
679  if(mpq_sgn(this->dpointer->privatevalue) == 0)
680 #else
681  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
682 #endif
683  return 0.0;
684  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
685  return 1.0;
686  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
687  return -1.0;
688 
689 #endif
690 
691  return mpq_get_d(this->dpointer->privatevalue);
692 }
693 
694 
695 
696 /// typecasts Rational to long double (allows only explicit typecast)
697 Rational::operator long double() const
698 {
699 #ifdef SOPLEX_PERFALT_3
700 #ifdef SOPLEX_PERFALT_1
701 
702  if(mpq_sgn(this->dpointer->privatevalue) == 0)
703 #else
704  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
705 #endif
706  return 0.0;
707  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
708  return 1.0;
709  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
710  return -1.0;
711 
712 #endif
713 
714  return (long double)mpq_get_d(this->dpointer->privatevalue);
715 }
716 
717 
718 
719 /// provides read-only access to underlying mpq_t
720 const mpq_t* Rational::getMpqPtr() const
721 {
722  return &(this->dpointer->privatevalue);
723 }
724 
725 
726 
727 /// provides read-only access to underlying mpq_t
728 const mpq_t& Rational::getMpqRef() const
729 {
730  return this->dpointer->privatevalue;
731 }
732 
733 
734 
735 /// provides write access to underlying mpq_t; use with care
736 mpq_t* Rational::getMpqPtr_w() const
737 {
738  return &(this->dpointer->privatevalue);
739 }
740 
741 
742 
743 /// provides write access to underlying mpq_t; use with care
744 mpq_t& Rational::getMpqRef_w() const
745 {
746  return this->dpointer->privatevalue;
747 }
748 
749 
750 
751 /// addition operator
752 Rational Rational::operator+(const Rational& r) const
753 {
754 #ifdef SOPLEX_PERFALT_2a
755 #ifdef SOPLEX_PERFALT_1
756 
757  if(mpq_sgn(r.dpointer->privatevalue) == 0)
758  return *this;
759  else if(mpq_sgn(this->dpointer->privatevalue) == 0)
760  return r;
761 
762 #else
763 
764  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
765  return *this;
766  else if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
767  return r;
768 
769 #endif
770 #endif
771 
772  Rational retval;
773  mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
774  return retval;
775 }
776 
777 
778 
779 /// addition assignment operator
781 {
782 #ifdef SOPLEX_PERFALT_2a
783 #ifdef SOPLEX_PERFALT_1
784 
785  if(mpq_sgn(r.dpointer->privatevalue) == 0)
786  return *this;
787  else if(mpq_sgn(this->dpointer->privatevalue) == 0)
788  return (*this = r);
789 
790 #else
791 
792  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
793  return *this;
794  else if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
795  return (*this = r);
796 
797 #endif
798 #endif
799 
800  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
801  return *this;
802 }
803 
804 
805 
806 /// addition operator for doubles
807 Rational Rational::operator+(const double& d) const
808 {
809  if(d == 0.0)
810  return *this;
811  else
812  {
813 #ifdef SOPLEX_PERFALT_2a
814 #ifdef SOPLEX_PERFALT_1
815 
816  if(mpq_sgn(this->dpointer->privatevalue) == 0)
817  return d;
818 
819 #else
820 
821  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
822  return d;
823 
824 #endif
825 #endif
826 
827  Rational retval(d);
828  mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
829  return retval;
830  }
831 }
832 
833 
834 
835 /// addition assignment operator for doubles
836 Rational& Rational::operator+=(const double& d)
837 {
838  if(d == 1.0)
839  return (*this += Rational::POSONE);
840  else if(d == -1.0)
841  return (*this += Rational::NEGONE);
842  else if(d != 0.0)
843  {
844 #ifdef SOPLEX_PERFALT_2a
845 #ifdef SOPLEX_PERFALT_1
846 
847  if(mpq_sgn(this->dpointer->privatevalue) == 0)
848  return (*this = d);
849 
850 #else
851 
852  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
853  return (*this = d);
854 
855 #endif
856 #endif
857 
858  Rational retval(d);
859  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
860  }
861 
862  return *this;
863 }
864 
865 
866 
867 /// addition operator for ints
868 Rational Rational::operator+(const int& d) const
869 {
870  if(d == 0)
871  return *this;
872  else
873  {
874 #ifdef SOPLEX_PERFALT_2a
875 #ifdef SOPLEX_PERFALT_1
876 
877  if(mpq_sgn(this->dpointer->privatevalue) == 0)
878  return d;
879 
880 #else
881 
882  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
883  return d;
884 
885 #endif
886 #endif
887 
888  Rational retval(d);
889  mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
890  return retval;
891  }
892 }
893 
894 
895 
896 /// addition assignment operator for ints
897 Rational& Rational::operator+=(const int& d)
898 {
899  if(d == 1)
900  return (*this += Rational::POSONE);
901  else if(d == -1)
902  return (*this += Rational::NEGONE);
903  else if(d != 0)
904  {
905 #ifdef SOPLEX_PERFALT_2a
906 #ifdef SOPLEX_PERFALT_1
907 
908  if(mpq_sgn(this->dpointer->privatevalue) == 0)
909  return (*this = d);
910 
911 #else
912 
913  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
914  return (*this = d);
915 
916 #endif
917 #endif
918 
919  Rational retval(d);
920  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
921  }
922 
923  return *this;
924 }
925 
926 
927 
928 /// subtraction operator
929 Rational Rational::operator-(const Rational& r) const
930 {
931 #ifdef SOPLEX_PERFALT_2a
932 #ifdef SOPLEX_PERFALT_1
933 
934  if(mpq_sgn(r.dpointer->privatevalue) == 0)
935  return *this;
936  else if(mpq_sgn(this->dpointer->privatevalue) == 0)
937  return -r;
938 
939 #else
940 
941  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
942  return *this;
943  else if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
944  return -r;
945 
946 #endif
947 #endif
948 
949  Rational retval;
950  mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
951  return retval;
952 }
953 
954 
955 
956 /// subtraction assignment operator
958 {
959 #ifdef SOPLEX_PERFALT_2a
960 #ifdef SOPLEX_PERFALT_1
961 
962  if(mpq_sgn(r.dpointer->privatevalue) == 0)
963  return *this;
964  else if(mpq_sgn(this->dpointer->privatevalue) == 0)
965  {
966  *this = r;
967  *this *= -1;
968  return *this;
969  }
970 
971 #else
972 
973  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
974  return *this;
975  else if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
976  {
977  *this = r;
978  *this *= -1;
979  return *this;
980  }
981 
982 #endif
983 #endif
984 
985  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
986  return *this;
987 }
988 
989 
990 
991 /// subtraction operator for doubles
992 Rational Rational::operator-(const double& d) const
993 {
994  if(d == 0.0)
995  return *this;
996  else
997  {
998 #ifdef SOPLEX_PERFALT_2a
999 #ifdef SOPLEX_PERFALT_1
1000 
1001  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1002  return -d;
1003 
1004 #else
1005 
1006  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1007  return -d;
1008 
1009 #endif
1010 #endif
1011 
1012  Rational retval(d);
1013  mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1014  return retval;
1015  }
1016 }
1017 
1018 
1019 
1020 /// subtraction assignment operator for doubles
1021 Rational& Rational::operator-=(const double& d)
1022 {
1023 
1024  if(d == 1.0)
1025  return (*this -= Rational::POSONE);
1026  else if(d == -1.0)
1027  return (*this -= Rational::NEGONE);
1028  else if(d != 0.0)
1029  {
1030 #ifdef SOPLEX_PERFALT_2a
1031 #ifdef SOPLEX_PERFALT_1
1032 
1033  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1034  return (*this = -d);
1035 
1036 #else
1037 
1038  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1039  return (*this = -d);
1040 
1041 #endif
1042 #endif
1043 
1044  Rational retval(d);
1045  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1046  }
1047 
1048  return *this;
1049 }
1050 
1051 
1052 
1053 /// subtraction operator for ints
1054 Rational Rational::operator-(const int& d) const
1055 {
1056  if(d == 0)
1057  return *this;
1058  else
1059  {
1060 #ifdef SOPLEX_PERFALT_2a
1061 #ifdef SOPLEX_PERFALT_1
1062 
1063  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1064  return -d;
1065 
1066 #else
1067 
1068  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1069  return -d;
1070 
1071 #endif
1072 #endif
1073 
1074  Rational retval(d);
1075  mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1076  return retval;
1077  }
1078 }
1079 
1080 
1081 
1082 /// subtraction assignment operator for ints
1083 Rational& Rational::operator-=(const int& d)
1084 {
1085 
1086  if(d == 1)
1087  return (*this -= Rational::POSONE);
1088  else if(d == -1)
1089  return (*this -= Rational::NEGONE);
1090  else if(d != 0)
1091  {
1092 #ifdef SOPLEX_PERFALT_2a
1093 #ifdef SOPLEX_PERFALT_1
1094 
1095  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1096  return (*this = -d);
1097 
1098 #else
1099 
1100  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1101  return (*this = -d);
1102 
1103 #endif
1104 #endif
1105 
1106  Rational retval(d);
1107  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1108  }
1109 
1110  return *this;
1111 }
1112 
1113 
1114 
1115 /// multiplication operator
1116 Rational Rational::operator*(const Rational& r) const
1117 {
1118 #ifdef SOPLEX_PERFALT_2b
1119 #ifdef SOPLEX_PERFALT_1
1120 
1121  if(mpq_sgn(r.dpointer->privatevalue) == 0)
1122  return Rational::ZERO;
1123  else if(mpq_sgn(this->dpointer->privatevalue) == 0)
1124  return Rational::ZERO;
1125 
1126 #else
1127 
1128  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1129  return Rational::ZERO;
1130  else if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1131  return Rational::ZERO;
1132 
1133 #endif
1134  else if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1135  return *this;
1136  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1137  return r;
1138  else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1139  return -*this;
1140  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1141  return -r;
1142 
1143 #endif
1144 
1145  Rational retval;
1146  mpq_mul(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1147  return retval;
1148 }
1149 
1150 
1151 
1152 /// multiplication assignment operator
1154 {
1155 #ifdef SOPLEX_PERFALT_2b
1156 #ifdef SOPLEX_PERFALT_1
1157 
1158  if(mpq_sgn(r.dpointer->privatevalue) == 0)
1159  return (*this = Rational::ZERO);
1160  else if(mpq_sgn(this->dpointer->privatevalue) == 0)
1161  return *this;
1162 
1163 #else
1164 
1165  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1166  return (*this = Rational::ZERO);
1167  else if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1168  return *this;
1169 
1170 #endif
1171  else if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1172  return *this;
1173  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1174  return (*this = r);
1175  else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1176  {
1177  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1178  return *this;
1179  }
1180  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1181  {
1182  mpq_neg(this->dpointer->privatevalue, r.dpointer->privatevalue);
1183  return *this;
1184  }
1185 
1186 #endif
1187 
1188  mpq_mul(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1189  return *this;
1190 }
1191 
1192 
1193 
1194 /// multiplication operator for doubles
1195 Rational Rational::operator*(const double& d) const
1196 {
1197  if(d == 0.0)
1198  return Rational::ZERO;
1199  else if(d == 1.0)
1200  return *this;
1201  else if(d == -1.0)
1202  {
1203  Rational retval;
1204  mpq_neg(retval.dpointer->privatevalue, this->dpointer->privatevalue);
1205  return retval;
1206  }
1207  else
1208  {
1209 #ifdef SOPLEX_PERFALT_2b
1210 #ifdef SOPLEX_PERFALT_1
1211 
1212  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1213  return Rational::ZERO;
1214 
1215 #else
1216 
1217  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1218  return Rational::ZERO;
1219 
1220 #endif
1221  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1222  return d;
1223  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1224  return -d;
1225 
1226 #endif
1227 
1228  Rational retval(d);
1229  mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, this->dpointer->privatevalue);
1230  return retval;
1231  }
1232 }
1233 
1234 
1235 
1236 /// multiplication assignment operator for doubles
1237 Rational& Rational::operator*=(const double& d)
1238 {
1239  if(d == 0.0)
1240  return (*this = Rational::ZERO);
1241  else if(d == 1.0)
1242  return *this;
1243  else if(d == -1.0)
1244  {
1245  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1246  return *this;
1247  }
1248  else
1249  {
1250 #ifdef SOPLEX_PERFALT_2b
1251 #ifdef SOPLEX_PERFALT_1
1252 
1253  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1254  return *this;
1255 
1256 #else
1257 
1258  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1259  return *this;
1260 
1261 #endif
1262  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1263  return (*this = d);
1264  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1265  {
1266  *this = d;
1267  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1268  return *this;
1269  }
1270 
1271 #endif
1272 
1273  Rational retval(d);
1274  mpq_mul(this->dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1275  return *this;
1276  }
1277 }
1278 
1279 
1280 
1281 /// multiplication operator for ints
1282 Rational Rational::operator*(const int& d) const
1283 {
1284  if(d == 0)
1285  return Rational::ZERO;
1286  else if(d == 1)
1287  return *this;
1288  else if(d == -1)
1289  {
1290  Rational retval;
1291  mpq_neg(retval.dpointer->privatevalue, this->dpointer->privatevalue);
1292  return retval;
1293  }
1294  else
1295  {
1296 #ifdef SOPLEX_PERFALT_2b
1297 #ifdef SOPLEX_PERFALT_1
1298 
1299  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1300  return Rational::ZERO;
1301 
1302 #else
1303 
1304  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1305  return Rational::ZERO;
1306 
1307 #endif
1308  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1309  return d;
1310  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1311  return -d;
1312 
1313 #endif
1314 
1315  Rational retval(d);
1316  mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, this->dpointer->privatevalue);
1317  return retval;
1318  }
1319 }
1320 
1321 
1322 
1323 /// multiplication assignment operator for ints
1324 Rational& Rational::operator*=(const int& d)
1325 {
1326  if(d == 0)
1327  return (*this = Rational::ZERO);
1328  else if(d == 1)
1329  return *this;
1330  else if(d == -1)
1331  {
1332  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1333  return *this;
1334  }
1335  else
1336  {
1337 #ifdef SOPLEX_PERFALT_2b
1338 #ifdef SOPLEX_PERFALT_1
1339 
1340  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1341  return *this;
1342 
1343 #else
1344 
1345  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1346  return *this;
1347 
1348 #endif
1349  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1350  return (*this = d);
1351  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1352  {
1353  *this = d;
1354  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1355  return *this;
1356  }
1357 
1358 #endif
1359 
1360  Rational retval(d);
1361  mpq_mul(this->dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1362  return *this;
1363  }
1364 }
1365 
1366 
1367 
1368 /// division operator
1369 Rational Rational::operator/(const Rational& r) const
1370 {
1371 #ifdef SOPLEX_PERFALT_2b
1372 #ifdef SOPLEX_PERFALT_1
1373 
1374  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1375  return Rational::ZERO;
1376 
1377 #else
1378 
1379  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1380  return Rational::ZERO;
1381 
1382 #endif
1383  else if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1384  return *this;
1385  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1386  {
1387  Rational retval(r);
1388  retval.invert();
1389  return retval;
1390  }
1391  else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1392  return -*this;
1393  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1394  {
1395  Rational retval(r);
1396  retval.invert();
1397  mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1398  return retval;
1399  }
1400 
1401 #endif
1402 
1403  Rational retval;
1404  mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1405  return retval;
1406 }
1407 
1408 
1409 
1410 /// division assignment operator
1412 {
1413 #ifdef SOPLEX_PERFALT_2b
1414 #ifdef SOPLEX_PERFALT_1
1415 
1416  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1417  return *this;
1418 
1419 #else
1420 
1421  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1422  return *this;
1423 
1424 #endif
1425  else if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1426  return *this;
1427  else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1428  {
1429  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1430  return *this;
1431  }
1432  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1433  {
1434  mpq_inv(this->dpointer->privatevalue, r.dpointer->privatevalue);
1435  return *this;
1436  }
1437  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1438  {
1439  mpq_inv(this->dpointer->privatevalue, r.dpointer->privatevalue);
1440  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1441  return *this;
1442  }
1443 
1444 #endif
1445 
1446  mpq_div(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1447  return *this;
1448 }
1449 
1450 
1451 
1452 /// division operator for doubles
1453 Rational Rational::operator/(const double& d) const
1454 {
1455  if(d == 1.0)
1456  return *this;
1457  else if(d == -1.0)
1458  return -(*this);
1459  else
1460  {
1461 #ifdef SOPLEX_PERFALT_2b
1462 #ifdef SOPLEX_PERFALT_1
1463 
1464  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1465  return Rational::ZERO;
1466 
1467 #else
1468 
1469  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1470  return Rational::ZERO;
1471 
1472 #endif
1473  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1474  {
1475  Rational retval(d);
1476  retval.invert();
1477  return retval;
1478  }
1479  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1480  {
1481  Rational retval(d);
1482  retval.invert();
1483  mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1484  return retval;
1485  }
1486 
1487 #endif
1488 
1489  Rational retval(d);
1490  mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1491  return retval;
1492  }
1493 }
1494 
1495 
1496 
1497 /// division assignment operator for doubles
1498 Rational& Rational::operator/=(const double& d)
1499 {
1500  if(d == 1.0)
1501  return *this;
1502  else if(d == -1.0)
1503  {
1504  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1505  return *this;
1506  }
1507  else
1508  {
1509 #ifdef SOPLEX_PERFALT_2b
1510 #ifdef SOPLEX_PERFALT_1
1511 
1512  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1513  return *this;
1514 
1515 #else
1516 
1517  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1518  return *this;
1519 
1520 #endif
1521  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1522  {
1523  *this = d;
1524  this->invert();
1525  return *this;
1526  }
1527  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1528  {
1529  *this = -d;
1530  this->invert();
1531  return *this;
1532  }
1533 
1534 #endif
1535 
1536  Rational retval(d);
1537  mpq_div(this->dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1538  return *this;
1539  }
1540 }
1541 
1542 
1543 
1544 /// division operator for ints
1545 Rational Rational::operator/(const int& d) const
1546 {
1547  if(d == 1)
1548  return *this;
1549  else if(d == -1)
1550  return -(*this);
1551  else
1552  {
1553 #ifdef SOPLEX_PERFALT_2b
1554 #ifdef SOPLEX_PERFALT_1
1555 
1556  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1557  return Rational::ZERO;
1558 
1559 #else
1560 
1561  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1562  return Rational::ZERO;
1563 
1564 #endif
1565  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1566  {
1567  Rational retval(d);
1568  retval.invert();
1569  return retval;
1570  }
1571  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1572  {
1573  Rational retval(d);
1574  retval.invert();
1575  mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1576  return retval;
1577  }
1578 
1579 #endif
1580 
1581  Rational retval(d);
1582  mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1583  return retval;
1584  }
1585 }
1586 
1587 
1588 
1589 /// division assignment operator for ints
1590 Rational& Rational::operator/=(const int& d)
1591 {
1592  if(d == 1)
1593  return *this;
1594  else if(d == -1)
1595  {
1596  mpq_neg(this->dpointer->privatevalue, this->dpointer->privatevalue);
1597  return *this;
1598  }
1599  else
1600  {
1601 #ifdef SOPLEX_PERFALT_2b
1602 #ifdef SOPLEX_PERFALT_1
1603 
1604  if(mpq_sgn(this->dpointer->privatevalue) == 0)
1605  return *this;
1606 
1607 #else
1608 
1609  if(mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1610  return *this;
1611 
1612 #endif
1613  else if(mpq_equal(this->dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1614  {
1615  *this = d;
1616  this->invert();
1617  return *this;
1618  }
1619  else if(mpq_equal(this->dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1620  {
1621  *this = -d;
1622  this->invert();
1623  return *this;
1624  }
1625 
1626 #endif
1627 
1628  Rational retval(d);
1629  mpq_div(this->dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1630  return *this;
1631  }
1632 }
1633 
1634 
1635 
1636 /// add product of two rationals
1637 Rational& Rational::addProduct(const Rational& r, const Rational& s)
1638 {
1639 #ifdef SOPLEX_PERFALT_2b
1640 
1641  if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1642  {
1643  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, s.dpointer->privatevalue);
1644  return *this;
1645  }
1646  else if(mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1647  {
1648  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1649  return *this;
1650  }
1651  else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1652  {
1653  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, s.dpointer->privatevalue);
1654  return *this;
1655  }
1656  else if(mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1657  {
1658  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1659  return *this;
1660  }
1661 
1662 #if 0 // currently, SoPlex calls this method only with nonzero r and s, hence we do not check this case
1663 #ifdef SOPLEX_PERFALT_1
1664  else if(mpq_sgn(r.dpointer->privatevalue) == 0)
1665  return *this;
1666  else if(mpq_sgn(s.dpointer->privatevalue) == 0)
1667  return *this;
1668 
1669 #else
1670  else if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1671  return *this;
1672  else if(mpq_equal(s.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1673  return *this;
1674 
1675 #endif
1676 #endif
1677 #endif
1678 
1679  Rational product(r);
1680  mpq_mul(product.dpointer->privatevalue, product.dpointer->privatevalue, s.dpointer->privatevalue);
1681  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, product.dpointer->privatevalue);
1682  return *this;
1683 }
1684 
1685 
1686 
1687 /// subtract product of two rationals
1688 Rational& Rational::subProduct(const Rational& r, const Rational& s)
1689 {
1690 #ifdef SOPLEX_PERFALT_2b
1691 
1692  if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1693  {
1694  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, s.dpointer->privatevalue);
1695  return *this;
1696  }
1697  else if(mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1698  {
1699  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1700  return *this;
1701  }
1702  else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1703  {
1704  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, s.dpointer->privatevalue);
1705  return *this;
1706  }
1707  else if(mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1708  {
1709  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1710  return *this;
1711  }
1712 
1713 #if 0 // currently, SoPlex calls this method only with nonzero r and s, hence we do not check this case
1714 #ifdef SOPLEX_PERFALT_1
1715 
1716  if(mpq_sgn(r.dpointer->privatevalue) == 0)
1717  return *this;
1718  else if(mpq_sgn(s.dpointer->privatevalue) == 0)
1719  return *this;
1720 
1721 #else
1722 
1723  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1724  return *this;
1725  else if(mpq_equal(s.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1726  return *this;
1727 
1728 #endif
1729 #endif
1730 #endif
1731 
1732  Rational product(r);
1733  mpq_mul(product.dpointer->privatevalue, product.dpointer->privatevalue, s.dpointer->privatevalue);
1734  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, product.dpointer->privatevalue);
1735  return *this;
1736 }
1737 
1738 
1739 
1740 /// add quotient of two rationals, r divided by s
1741 Rational& Rational::addQuotient(const Rational& r, const Rational& s)
1742 {
1743 #ifdef SOPLEX_PERFALT_2b
1744 #ifdef SOPLEX_PERFALT_1
1745 
1746  if(mpq_sgn(r.dpointer->privatevalue) == 0)
1747  return *this;
1748 
1749 #else
1750 
1751  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1752  return *this;
1753 
1754 #endif
1755  else if(mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1756  {
1757  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1758  return *this;
1759  }
1760  else if(mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1761  {
1762  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1763  return *this;
1764  }
1765 
1766 #endif
1767 
1768  Rational quotient(r);
1769  mpq_div(quotient.dpointer->privatevalue, quotient.dpointer->privatevalue, s.dpointer->privatevalue);
1770  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue,
1771  quotient.dpointer->privatevalue);
1772  return *this;
1773 }
1774 
1775 
1776 
1777 /// subtract quotient of two rationals, r divided by s
1778 Rational& Rational::subQuotient(const Rational& r, const Rational& s)
1779 {
1780 #ifdef SOPLEX_PERFALT_2b
1781 #ifdef SOPLEX_PERFALT_1
1782 
1783  if(mpq_sgn(r.dpointer->privatevalue) == 0)
1784  return *this;
1785 
1786 #else
1787 
1788  if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1789  return *this;
1790 
1791 #endif
1792  else if(mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1793  {
1794  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1795  return *this;
1796  }
1797  else if(mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1798  {
1799  mpq_add(this->dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1800  return *this;
1801  }
1802 
1803 #endif
1804 
1805  Rational quotient(r);
1806  mpq_div(quotient.dpointer->privatevalue, quotient.dpointer->privatevalue, s.dpointer->privatevalue);
1807  mpq_sub(this->dpointer->privatevalue, this->dpointer->privatevalue,
1808  quotient.dpointer->privatevalue);
1809  return *this;
1810 }
1811 
1812 
1813 
1814 /// inversion
1816 {
1817  mpq_inv(this->dpointer->privatevalue, this->dpointer->privatevalue);
1818  return *this;
1819 }
1820 
1821 
1822 
1823 /// round up to next power of two
1825 {
1826  mpz_t roundval;
1827 
1828  MSG_DEBUG(std::cout << "rounding " << rationalToString(this->dpointer->privatevalue) <<
1829  " to power of two" << "\n");
1830 
1831  mpz_init(roundval);
1832  mpz_cdiv_q(roundval, mpq_numref(this->dpointer->privatevalue),
1833  mpq_denref(this->dpointer->privatevalue));
1834  mpz_sub_ui(roundval, roundval, 1);
1835 
1836  MSG_DEBUG(std::cout << " --> " << mpz_get_str(0, 10, roundval) << "\n");
1837 
1838  size_t binlog = mpz_sizeinbase(roundval, 2);
1839 
1840  MSG_DEBUG(std::cout << " --> 2^" << binlog << "\n");
1841 
1842  mpz_ui_pow_ui(roundval, 2, binlog);
1843 
1844  MSG_DEBUG(std::cout << " --> " << mpz_get_str(0, 10, roundval) << "\n");
1845 
1846  mpq_set_z(this->dpointer->privatevalue, roundval);
1847  mpz_clear(roundval);
1848 
1849  MSG_DEBUG(std::cout << " --> " << rationalToString(this->dpointer->privatevalue) << "\n");
1850 
1851  return *this;
1852 }
1853 
1854 
1855 
1856 /// checks if d is the closest possible double
1857 bool Rational::isNextTo(const double& d)
1858 {
1859  // get intervall [a,b] of doubles that the Rational is in
1860  double x = mpq_get_d(this->dpointer->privatevalue);
1861  double a;
1862  double b;
1863 
1864  if(Rational(x) < *this)
1865  {
1866  a = x;
1867  b = (double)spxNextafter(a, infinity);
1868  }
1869  else
1870  {
1871  b = x;
1872  a = (double)spxNextafter(b, -infinity);
1873  }
1874 
1875  // check if d equals the closer end of the intervall
1876  bool result = (spxAbs(*this - a) < spxAbs(*this - b))
1877  ? (d == a)
1878  : (d == b);
1879 
1880  return result;
1881 }
1882 
1883 
1884 
1885 /// checks if d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles
1886 bool Rational::isAdjacentTo(const double& d) const
1887 {
1888  double x = mpq_get_d(this->dpointer->privatevalue);
1889  double a;
1890  double b;
1891  mpq_t tmp;
1892 
1893  mpq_init(tmp);
1894  mpq_set_d(tmp, x);
1895 
1896  int cmp = mpq_cmp(tmp, this->dpointer->privatevalue);
1897  mpq_clear(tmp);
1898 
1899  // the rounded value is smaller than the rational value
1900  if(cmp < 0)
1901  {
1902  a = x;
1903  b = (double)spxNextafter(a, infinity);
1904  }
1905  // the rounded value is larger than the rational value
1906  else if(cmp > 0)
1907  {
1908  b = x;
1909  a = (double)spxNextafter(b, -infinity);
1910  }
1911  // the rational value is representable in double precision
1912  else
1913  return (x == d);
1914 
1915  return ((a == d) || (b == d));
1916 }
1917 
1918 
1919 
1920 /// Size in specified base (bit size for base 2)
1921 int Rational::sizeInBase(const int base) const
1922 {
1923  return (int)mpz_sizeinbase(mpq_numref(this->dpointer->privatevalue), base)
1924  + (int)mpz_sizeinbase(mpq_denref(this->dpointer->privatevalue), base);
1925 }
1926 
1927 
1928 
1929 /// returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (INT_MAX if exact)
1930 int Rational::precision()
1931 {
1932  return INT_MAX;
1933 }
1934 
1935 
1936 
1937 #define MAX_STR_LEN 10000
1938 /// read Rational from string
1939 bool Rational::readString(const char* s)
1940 {
1941  assert(s != 0);
1942  assert(strlen(s) <= MAX_STR_LEN);
1943  Rational value;
1944  const char* pos;
1945 
1946  // if there is a slash or there is no dot and exponent (i.e. we
1947  // have an integer), we may simply call GMP's string reader
1948  if(strchr(s, '/') != 0 || strpbrk(s, ".eE") == 0)
1949  {
1950  pos = (*s == '+') ? s + 1 : s;
1951 
1952  if(mpq_set_str(value.dpointer->privatevalue, pos, 10) == 0)
1953  {
1954  mpq_canonicalize(value.dpointer->privatevalue);
1955  mpq_set(this->dpointer->privatevalue, value.dpointer->privatevalue);
1956  return true;
1957  }
1958  else
1959  return false;
1960  }
1961 
1962  // otherwise we analyze the string
1963 #ifndef NDEBUG
1964  bool has_exponent = false;
1965  bool has_dot = false;
1966 #endif
1967  bool has_digits = false;
1968  bool has_emptyexponent = false;
1969  long int exponent = 0;
1970  long int decshift = 0;
1971  mpz_t shiftpower;
1972  mpz_init(shiftpower);
1973  mpq_t shiftpowerRational;
1974  mpq_init(shiftpowerRational);
1975  char* t;
1976  char tmp[MAX_STR_LEN];
1977 
1978  pos = s;
1979 
1980  // 1. sign
1981  if((*pos == '+') || (*pos == '-'))
1982  pos++;
1983 
1984  // 2. Digits before the decimal dot
1985  while((*pos >= '0') && (*pos <= '9'))
1986  {
1987  has_digits = true;
1988  pos++;
1989  }
1990 
1991  // 3. Decimal dot
1992  if(*pos == '.')
1993  {
1994 #ifndef NDEBUG
1995  has_dot = true;
1996 #endif
1997  pos++;
1998 
1999  // 4. If there was a dot, possible digit behind it
2000  while((*pos >= '0') && (*pos <= '9'))
2001  {
2002  has_digits = true;
2003  pos++;
2004  }
2005  }
2006 
2007  // 5. Exponent
2008  if(tolower(*pos) == 'e')
2009  {
2010 #ifndef NDEBUG
2011  has_exponent = true;
2012 #endif
2013  has_emptyexponent = true;
2014  pos++;
2015 
2016  // 6. Exponent sign
2017  if((*pos == '+') || (*pos == '-'))
2018  pos++;
2019 
2020  // 7. Exponent digits
2021  while((*pos >= '0') && (*pos <= '9'))
2022  {
2023  has_emptyexponent = false;
2024  pos++;
2025  }
2026  }
2027 
2028  if(has_emptyexponent || !has_digits)
2029  return false;
2030 
2031  assert(has_exponent || has_dot);
2032 
2033  //read up to dot recording digits
2034  t = tmp;
2035  pos = s;
2036 
2037  if(*pos == '+')
2038  pos++;
2039 
2040  while(((*pos >= '0') && (*pos <= '9')) || *pos == '+' || *pos == '-')
2041  {
2042  *t++ = *pos;
2043  pos++;
2044  }
2045 
2046  //record digits after dot, recording positions
2047  decshift = 0;
2048 
2049  if(*pos == '.')
2050  {
2051  assert(has_dot);
2052  pos++;
2053 
2054  while((*pos >= '0') && (*pos <= '9'))
2055  {
2056  *t++ = *pos;
2057  decshift++;
2058  pos++;
2059  }
2060  }
2061 
2062  *t = '\0';
2063 
2064  if(mpq_set_str(value.dpointer->privatevalue, tmp, 10) != 0)
2065  return false;
2066 
2067  mpq_canonicalize(value.dpointer->privatevalue);
2068 
2069  //record exponent and update final result
2070  exponent = -decshift;
2071 
2072  if(tolower(*pos) == 'e')
2073  {
2074  pos++;
2075  assert(has_exponent);
2076 
2077  for(t = tmp; *pos != '\0'; pos++)
2078  *t++ = *pos;
2079 
2080  *t = '\0';
2081  exponent += atol(tmp);
2082  }
2083 
2084  if(exponent > 0)
2085  {
2086  mpz_ui_pow_ui(shiftpower, 10, exponent);
2087  mpq_set_z(shiftpowerRational, shiftpower);
2088  mpq_mul(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2089  }
2090  else if(exponent < 0)
2091  {
2092  mpz_ui_pow_ui(shiftpower, 10, -exponent);
2093  mpq_set_z(shiftpowerRational, shiftpower);
2094  mpq_div(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2095  }
2096 
2097  mpq_canonicalize(value.dpointer->privatevalue);
2098  mpq_set(this->dpointer->privatevalue, value.dpointer->privatevalue);
2099  mpz_clear(shiftpower);
2100  mpq_clear(shiftpowerRational);
2101  return true;
2102 
2103 }
2104 
2105 
2106 
2107 /// convert rational number to string
2108 std::string rationalToString(const Rational& r, const int precision)
2109 {
2110 
2111 #if defined(_WIN32) || defined(_WIN64) || defined(__APPLE__)
2112  std::stringstream sstream;
2113  sstream << r;
2114  return sstream.str();
2115 #else
2116 
2117  if(precision <= 0)
2118  {
2119  std::stringstream sstream;
2120  sstream << r;
2121  return sstream.str();
2122  }
2123  else
2124  {
2125  mpf_t tmpFloat;
2126  char tmpString[64];
2127  FILE* tmpStream;
2128 
2129  tmpStream = fmemopen(tmpString, 63, "w");
2130  mpf_init2(tmpFloat, 256);
2131  mpf_set_q(tmpFloat, r.dpointer->privatevalue);
2132  mpf_out_str(tmpStream, 10, precision, tmpFloat);
2133  mpf_clear(tmpFloat);
2134 
2135  fflush(tmpStream);
2136  std::string retString = std::string(tmpString);
2137  fclose(tmpStream);
2138  return retString;
2139  }
2140 
2141 #endif
2142 }
2143 
2144 
2145 
2146 /// read Rational from string
2147 bool readStringRational(const char* s, Rational& value)
2148 {
2149  assert(s != 0);
2150  assert(strlen(s) <= MAX_STR_LEN);
2151  const char* pos;
2152 
2153  // if there is a slash or there is no dot and exponent (i.e. we
2154  // have an integer), we may simply call GMP's string reader
2155  if(strchr(s, '/') != 0 || strpbrk(s, ".eE") == 0)
2156  {
2157  pos = (*s == '+') ? s + 1 : s;
2158 
2159  if(mpq_set_str(value.dpointer->privatevalue, pos, 10) == 0)
2160  {
2161  mpq_canonicalize(value.dpointer->privatevalue);
2162  return true;
2163  }
2164  else
2165  return false;
2166  }
2167 
2168  // otherwise we analyze the string
2169 #ifndef NDEBUG
2170  bool has_exponent = false;
2171  bool has_dot = false;
2172 #endif
2173  bool has_digits = false;
2174  bool has_emptyexponent = false;
2175  long int exponent = 0;
2176  long int decshift = 0;
2177  mpz_t shiftpower;
2178  mpz_init(shiftpower);
2179  mpq_t shiftpowerRational;
2180  mpq_init(shiftpowerRational);
2181  char* t;
2182  char tmp[MAX_STR_LEN];
2183 
2184  pos = s;
2185 
2186  // 1. sign
2187  if((*pos == '+') || (*pos == '-'))
2188  pos++;
2189 
2190  // 2. Digits before the decimal dot
2191  while((*pos >= '0') && (*pos <= '9'))
2192  {
2193  has_digits = true;
2194  pos++;
2195  }
2196 
2197  // 3. Decimal dot
2198  if(*pos == '.')
2199  {
2200 #ifndef NDEBUG
2201  has_dot = true;
2202 #endif
2203  pos++;
2204 
2205  // 4. If there was a dot, possible digit behind it
2206  while((*pos >= '0') && (*pos <= '9'))
2207  {
2208  has_digits = true;
2209  pos++;
2210  }
2211  }
2212 
2213  // 5. Exponent
2214  if(tolower(*pos) == 'e')
2215  {
2216 #ifndef NDEBUG
2217  has_exponent = true;
2218 #endif
2219  has_emptyexponent = true;
2220  pos++;
2221 
2222  // 6. Exponent sign
2223  if((*pos == '+') || (*pos == '-'))
2224  pos++;
2225 
2226  // 7. Exponent digits
2227  while((*pos >= '0') && (*pos <= '9'))
2228  {
2229  has_emptyexponent = false;
2230  pos++;
2231  }
2232  }
2233 
2234  if(has_emptyexponent || !has_digits)
2235  return false;
2236 
2237  assert(has_exponent || has_dot);
2238 
2239  // read up to dot recording digits
2240  t = tmp;
2241  pos = s;
2242 
2243  if(*pos == '+')
2244  pos++;
2245 
2246  while(((*pos >= '0') && (*pos <= '9')) || *pos == '+' || *pos == '-')
2247  {
2248  *t++ = *pos;
2249  pos++;
2250  }
2251 
2252  // record digits after dot, recording positions
2253  decshift = 0;
2254 
2255  if(*pos == '.')
2256  {
2257  assert(has_dot);
2258  pos++;
2259 
2260  while((*pos >= '0') && (*pos <= '9'))
2261  {
2262  *t++ = *pos;
2263  decshift++;
2264  pos++;
2265  }
2266  }
2267 
2268  *t = '\0';
2269 
2270  if(mpq_set_str(value.dpointer->privatevalue, tmp, 10) != 0)
2271  return false;
2272 
2273  mpq_canonicalize(value.dpointer->privatevalue);
2274 
2275  // record exponent and update final result
2276  exponent = -decshift;
2277 
2278  if(tolower(*pos) == 'e')
2279  {
2280  pos++;
2281  assert(has_exponent);
2282 
2283  for(t = tmp; *pos != '\0'; pos++)
2284  *t++ = *pos;
2285 
2286  *t = '\0';
2287  exponent += atol(tmp);
2288  }
2289 
2290  if(exponent > 0)
2291  {
2292  mpz_ui_pow_ui(shiftpower, 10, exponent);
2293  mpq_set_z(shiftpowerRational, shiftpower);
2294  mpq_mul(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2295  }
2296  else if(exponent < 0)
2297  {
2298  mpz_ui_pow_ui(shiftpower, 10, -exponent);
2299  mpq_set_z(shiftpowerRational, shiftpower);
2300  mpq_div(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2301  }
2302 
2303  mpq_canonicalize(value.dpointer->privatevalue);
2304  mpz_clear(shiftpower);
2305  mpq_clear(shiftpowerRational);
2306 
2307  return true;
2308 }
2309 
2310 
2311 
2312 /// print Rational
2313 std::ostream& operator<<(std::ostream& os, const Rational& r)
2314 {
2315  char* buffer;
2316  buffer = (char*) malloc(mpz_sizeinbase(mpq_numref(r.dpointer->privatevalue),
2317  10) + mpz_sizeinbase(mpq_denref(r.dpointer->privatevalue), 10) + 3);
2318  os << mpq_get_str(buffer, 10, r.dpointer->privatevalue);
2319  free(buffer);
2320  return os;
2321 }
2322 
2323 
2324 
2325 /// comparison operator returning a positive value if r > s, zero if r = s, and a negative value if r < s
2326 int compareRational(const Rational& r, const Rational& s)
2327 {
2328  return mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue);
2329 }
2330 
2331 
2332 
2333 /// equality operator
2334 bool operator==(const Rational& r, const Rational& s)
2335 {
2336  return (mpq_equal(r.dpointer->privatevalue, s.dpointer->privatevalue) != 0);
2337 }
2338 
2339 
2340 
2341 /// inequality operator
2342 bool operator!=(const Rational& r, const Rational& s)
2343 {
2344  return (mpq_equal(r.dpointer->privatevalue, s.dpointer->privatevalue) == 0);
2345 }
2346 
2347 
2348 
2349 /// less than operator
2350 bool operator<(const Rational& r, const Rational& s)
2351 {
2352  return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) < 0);
2353 }
2354 
2355 
2356 
2357 /// less than or equal to operator
2358 bool operator<=(const Rational& r, const Rational& s)
2359 {
2360  return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) <= 0);
2361 }
2362 
2363 
2364 
2365 /// greater than operator
2366 bool operator>(const Rational& r, const Rational& s)
2367 {
2368  return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) > 0);
2369 }
2370 
2371 
2372 
2373 /// greater than or equal to operator
2374 bool operator>=(const Rational& r, const Rational& s)
2375 {
2376  return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) >= 0);
2377 }
2378 
2379 
2380 
2381 /// equality operator for Rational and double
2382 bool operator==(const Rational& r, const double& s)
2383 {
2384  if(s == 0.0)
2385 #ifdef SOPLEX_PERFALT_1
2386  return (mpq_sgn(r.dpointer->privatevalue) == 0);
2387 
2388 #else
2389  return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2390 #endif
2391  else if(s == 1.0)
2392  return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2393  else if(s == -1.0)
2394  return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2395  else
2396  return (r == Rational(s));
2397 }
2398 
2399 
2400 
2401 /// inequality operator for Rational and double
2402 bool operator!=(const Rational& r, const double& s)
2403 {
2404  if(s == 0.0)
2405 #ifdef SOPLEX_PERFALT_1
2406  return (mpq_sgn(r.dpointer->privatevalue) != 0);
2407 
2408 #else
2409  return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2410 #endif
2411  else if(s == 1.0)
2412  return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2413  else if(s == -1.0)
2414  return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2415  else
2416  return (r != Rational(s));
2417 }
2418 
2419 
2420 
2421 /// less than operator for Rational and double
2422 bool operator<(const Rational& r, const double& s)
2423 {
2424  if(s == 0.0)
2425 #ifdef SOPLEX_PERFALT_1
2426  return (mpq_sgn(r.dpointer->privatevalue) == -1);
2427 
2428 #else
2429  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2430 #endif
2431  else if(s == 1.0)
2432  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2433  else if(s == -1.0)
2434  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2435  else
2436  return (r < Rational(s));
2437 }
2438 
2439 
2440 
2441 /// less than or equal to operator for Rational and double
2442 bool operator<=(const Rational& r, const double& s)
2443 {
2444  if(s == 0.0)
2445 #ifdef SOPLEX_PERFALT_1
2446  return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2447 
2448 #else
2449  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2450 #endif
2451  else if(s == 1.0)
2452  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2453  else if(s == -1.0)
2454  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2455  else
2456  return (r <= Rational(s));
2457 }
2458 
2459 
2460 
2461 /// greater than operator for Rational and double
2462 bool operator>(const Rational& r, const double& s)
2463 {
2464  if(s == 0.0)
2465 #ifdef SOPLEX_PERFALT_1
2466  return (mpq_sgn(r.dpointer->privatevalue) == 1);
2467 
2468 #else
2469  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2470 #endif
2471  else if(s == 1.0)
2472  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2473  else if(s == -1.0)
2474  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2475  else
2476  return (r > Rational(s));
2477 }
2478 
2479 
2480 
2481 /// greater than or equal to operator for Rational and double
2482 bool operator>=(const Rational& r, const double& s)
2483 {
2484  if(s == 0.0)
2485 #ifdef SOPLEX_PERFALT_1
2486  return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2487 
2488 #else
2489  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2490 #endif
2491  else if(s == 1.0)
2492  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2493  else if(s == -1.0)
2494  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2495  else
2496  return (r >= Rational(s));
2497 }
2498 
2499 
2500 
2501 /// equality operator for double and Rational
2502 bool operator==(const double& r, const Rational& s)
2503 {
2504  return (s == r);
2505 }
2506 
2507 
2508 
2509 /// inequality operator double and Rational
2510 bool operator!=(const double& r, const Rational& s)
2511 {
2512  return (s != r);
2513 }
2514 
2515 
2516 
2517 /// less than operator double and Rational
2518 bool operator<(const double& r, const Rational& s)
2519 {
2520  return (s > r);
2521 }
2522 
2523 
2524 
2525 /// less than or equal to operator double and Rational
2526 bool operator<=(const double& r, const Rational& s)
2527 {
2528  return (s >= r);
2529 }
2530 
2531 
2532 
2533 /// greater than operator double and Rational
2534 bool operator>(const double& r, const Rational& s)
2535 {
2536  return (s < r);
2537 }
2538 
2539 
2540 
2541 /// greater than or equal to operator double and Rational
2542 bool operator>=(const double& r, const Rational& s)
2543 {
2544  return (s <= r);
2545 }
2546 
2547 
2548 
2549 /// equality operator for Rational and long double
2550 bool operator==(const Rational& r, const long double& s)
2551 {
2552  if(s == 0.0)
2553 #ifdef SOPLEX_PERFALT_1
2554  return (mpq_sgn(r.dpointer->privatevalue) == 0);
2555 
2556 #else
2557  return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2558 #endif
2559  else if(s == 1.0)
2560  return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2561  else if(s == -1.0)
2562  return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2563  else
2564  return (r == Rational(s));
2565 }
2566 
2567 
2568 
2569 /// inequality operator for Rational and long double
2570 bool operator!=(const Rational& r, const long double& s)
2571 {
2572  if(s == 0.0)
2573 #ifdef SOPLEX_PERFALT_1
2574  return (mpq_sgn(r.dpointer->privatevalue) != 0);
2575 
2576 #else
2577  return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2578 #endif
2579  else if(s == 1.0)
2580  return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2581  else if(s == -1.0)
2582  return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2583  else
2584  return (r != Rational(s));
2585 }
2586 
2587 
2588 
2589 /// less than operator for Rational and long double
2590 bool operator<(const Rational& r, const long double& s)
2591 {
2592  if(s == 0.0)
2593 #ifdef SOPLEX_PERFALT_1
2594  return (mpq_sgn(r.dpointer->privatevalue) == -1);
2595 
2596 #else
2597  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2598 #endif
2599  else if(s == 1.0)
2600  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2601  else if(s == -1.0)
2602  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2603  else
2604  return (r < Rational(s));
2605 }
2606 
2607 
2608 
2609 /// less than or equal to operator for Rational and long double
2610 bool operator<=(const Rational& r, const long double& s)
2611 {
2612  if(s == 0.0)
2613 #ifdef SOPLEX_PERFALT_1
2614  return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2615 
2616 #else
2617  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2618 #endif
2619  else if(s == 1.0)
2620  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2621  else if(s == -1.0)
2622  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2623  else
2624  return (r <= Rational(s));
2625 }
2626 
2627 
2628 
2629 /// greater than operator for Rational and long double
2630 bool operator>(const Rational& r, const long double& s)
2631 {
2632  if(s == 0.0)
2633 #ifdef SOPLEX_PERFALT_1
2634  return (mpq_sgn(r.dpointer->privatevalue) == 1);
2635 
2636 #else
2637  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2638 #endif
2639  else if(s == 1.0)
2640  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2641  else if(s == -1.0)
2642  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2643  else
2644  return (r > Rational(s));
2645 }
2646 
2647 
2648 
2649 /// greater than or equal to operator for Rational and long double
2650 bool operator>=(const Rational& r, const long double& s)
2651 {
2652  if(s == 0.0)
2653 #ifdef SOPLEX_PERFALT_1
2654  return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2655 
2656 #else
2657  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2658 #endif
2659  else if(s == 1.0)
2660  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2661  else if(s == -1.0)
2662  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2663  else
2664  return (r >= Rational(s));
2665 }
2666 
2667 
2668 
2669 /// equality operator for long double and Rational
2670 bool operator==(const long double& r, const Rational& s)
2671 {
2672  return (s == r);
2673 }
2674 
2675 
2676 
2677 /// inequality operator for long double and Rational
2678 bool operator!=(const long double& r, const Rational& s)
2679 {
2680  return (s != r);
2681 }
2682 
2683 
2684 
2685 /// less than operator for long double and Rational
2686 bool operator<(const long double& r, const Rational& s)
2687 {
2688  return (s > r);
2689 }
2690 
2691 
2692 
2693 /// less than or equal to operator for long double and Rational
2694 bool operator<=(const long double& r, const Rational& s)
2695 {
2696  return (s >= r);
2697 }
2698 
2699 
2700 
2701 /// greater than operator for long double and Rational
2702 bool operator>(const long double& r, const Rational& s)
2703 {
2704  return (s < r);
2705 }
2706 
2707 
2708 
2709 /// greater than or equal to operator for long double and Rational
2710 bool operator>=(const long double& r, const Rational& s)
2711 {
2712  return (s <= r);
2713 }
2714 
2715 
2716 
2717 /// addition operator for double and Rational
2718 Rational operator+(const double& d, const Rational& r)
2719 {
2720  return (r + d);
2721 }
2722 
2723 
2724 
2725 /// subtraction operator for double and Rational
2726 Rational operator-(const double& d, const Rational& r)
2727 {
2728  Rational res(r);
2729  mpq_neg(res.dpointer->privatevalue, res.dpointer->privatevalue);
2730  res += d;
2731  return res;
2732 }
2733 
2734 
2735 
2736 /// multiplication operator for double and Rational
2737 Rational operator*(const double& d, const Rational& r)
2738 {
2739  if(d == 0.0)
2740  return Rational::ZERO;
2741  else if(d == 1.0)
2742  return r;
2743  else if(d == -1.0)
2744  return -r;
2745  else
2746  {
2747  Rational retval(d);
2748  mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2749  return retval;
2750  }
2751 }
2752 
2753 
2754 
2755 /// division operator for double and Rational
2756 Rational operator/(const double& d, const Rational& r)
2757 {
2758  if(d == 0.0)
2759  return Rational::ZERO;
2760  else if(d == 1.0)
2761  {
2762  Rational retval(r);
2763  retval.invert();
2764  return retval;
2765  }
2766  else if(d == -1.0)
2767  {
2768  Rational retval(r);
2769  retval.invert();
2770  mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
2771  return retval;
2772  }
2773  else
2774  {
2775  Rational retval(d);
2776  mpq_div(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2777  return retval;
2778  }
2779 }
2780 
2781 
2782 
2783 /// equality operator for Rational and int
2784 bool operator==(const Rational& r, const int& s)
2785 {
2786  if(s == 0)
2787 #ifdef SOPLEX_PERFALT_1
2788  return (mpq_sgn(r.dpointer->privatevalue) == 0);
2789 
2790 #else
2791  return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2792 #endif
2793  else if(s == 1)
2794  return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2795  else if(s == -1)
2796  return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2797  else
2798  return (r == Rational(s));
2799 }
2800 
2801 
2802 
2803 /// inequality operator for Rational and int
2804 bool operator!=(const Rational& r, const int& s)
2805 {
2806  if(s == 0)
2807 #ifdef SOPLEX_PERFALT_1
2808  return (mpq_sgn(r.dpointer->privatevalue) != 0);
2809 
2810 #else
2811  return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2812 #endif
2813  else if(s == 1)
2814  return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2815  else if(s == -1)
2816  return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2817  else
2818  return (r != Rational(s));
2819 }
2820 
2821 
2822 
2823 /// less than operator for Rational and int
2824 bool operator<(const Rational& r, const int& s)
2825 {
2826  if(s == 0)
2827 #ifdef SOPLEX_PERFALT_1
2828  return (mpq_sgn(r.dpointer->privatevalue) == -1);
2829 
2830 #else
2831  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2832 #endif
2833  else if(s == 1)
2834  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2835  else if(s == -1)
2836  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2837  else
2838  return (r < Rational(s));
2839 }
2840 
2841 
2842 
2843 /// less than or equal to operator for Rational and int
2844 bool operator<=(const Rational& r, const int& s)
2845 {
2846  if(s == 0)
2847 #ifdef SOPLEX_PERFALT_1
2848  return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2849 
2850 #else
2851  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2852 #endif
2853  else if(s == 1)
2854  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2855  else if(s == -1)
2856  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2857  else
2858  return (r <= Rational(s));
2859 }
2860 
2861 
2862 
2863 /// greater than operator for Rational and int
2864 bool operator>(const Rational& r, const int& s)
2865 {
2866  if(s == 0)
2867 #ifdef SOPLEX_PERFALT_1
2868  return (mpq_sgn(r.dpointer->privatevalue) == 1);
2869 
2870 #else
2871  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2872 #endif
2873  else if(s == 1)
2874  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2875  else if(s == -1)
2876  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2877  else
2878  return (r > Rational(s));
2879 }
2880 
2881 
2882 
2883 /// greater than or equal to operator for Rational and int
2884 bool operator>=(const Rational& r, const int& s)
2885 {
2886  if(s == 0)
2887 #ifdef SOPLEX_PERFALT_1
2888  return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2889 
2890 #else
2891  return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2892 #endif
2893  else if(s == 1)
2894  return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2895  else if(s == -1)
2896  return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2897  else
2898  return (r >= Rational(s));
2899 }
2900 
2901 
2902 
2903 /// equality operator for int and Rational
2904 bool operator==(const int& r, const Rational& s)
2905 {
2906  return (s == r);
2907 }
2908 
2909 
2910 
2911 /// inequality operator int and Rational
2912 bool operator!=(const int& r, const Rational& s)
2913 {
2914  return (s != r);
2915 }
2916 
2917 
2918 
2919 /// less than operator int and Rational
2920 bool operator<(const int& r, const Rational& s)
2921 {
2922  return (s > r);
2923 }
2924 
2925 
2926 
2927 /// less than or equal to operator int and Rational
2928 bool operator<=(const int& r, const Rational& s)
2929 {
2930  return (s >= r);
2931 }
2932 
2933 
2934 
2935 /// greater than operator int and Rational
2936 bool operator>(const int& r, const Rational& s)
2937 {
2938  return (s < r);
2939 }
2940 
2941 
2942 
2943 /// greater than or equal to operator int and Rational
2944 bool operator>=(const int& r, const Rational& s)
2945 {
2946  return (s <= r);
2947 }
2948 
2949 
2950 
2951 /// addition operator for int and Rational
2952 Rational operator+(const int& d, const Rational& r)
2953 {
2954  return (r + d);
2955 }
2956 
2957 
2958 
2959 /// subtraction operator for int and Rational
2960 Rational operator-(const int& d, const Rational& r)
2961 {
2962  Rational res(r);
2963  mpq_neg(res.dpointer->privatevalue, res.dpointer->privatevalue);
2964  res += d;
2965  return res;
2966 }
2967 
2968 
2969 
2970 /// multiplication operator for int and Rational
2971 Rational operator*(const int& d, const Rational& r)
2972 {
2973  if(d == 0)
2974  return Rational::ZERO;
2975  else if(d == 1)
2976  return r;
2977  else if(d == -1)
2978  return -r;
2979  else
2980  {
2981  Rational retval(d);
2982  mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2983  return retval;
2984  }
2985 }
2986 
2987 
2988 
2989 /// division operator for int and Rational
2990 Rational operator/(const int& d, const Rational& r)
2991 {
2992  if(d == 0)
2993  return Rational::ZERO;
2994  else if(d == 1)
2995  {
2996  Rational retval(r);
2997  retval.invert();
2998  return retval;
2999  }
3000  else if(d == -1)
3001  {
3002  Rational retval(r);
3003  retval.invert();
3004  mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
3005  return retval;
3006  }
3007  else
3008  {
3009  Rational retval(d);
3010  mpq_div(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
3011  return retval;
3012  }
3013 }
3014 
3015 
3016 
3017 /// Absolute.
3018 Rational spxAbs(const Rational& r)
3019 {
3020  Rational res;
3021  mpq_abs(res.dpointer->privatevalue, r.dpointer->privatevalue);
3022  return res;
3023 }
3024 
3025 
3026 
3027 /// Sign function; returns 1 if r > 0, 0 if r = 0, and -1 if r < 0.
3028 int sign(const Rational& r)
3029 {
3030  return mpq_sgn(r.dpointer->privatevalue);
3031 }
3032 
3033 
3034 
3035 /// Negation.
3036 Rational operator-(const Rational& r)
3037 {
3038  Rational res;
3039  mpq_neg(res.dpointer->privatevalue, r.dpointer->privatevalue);
3040  return res;
3041 }
3042 
3043 
3044 
3045 /// Total size of rational vector.
3046 int totalSizeRational(const Rational* vector, const int length, const int base)
3047 {
3048  assert(vector != 0);
3049  assert(length >= 0);
3050  assert(base >= 0);
3051 
3052  int size = 0;
3053 
3054  for(int i = 0; i < length; i++)
3055  size += vector[i].sizeInBase(base);
3056 
3057  return size;
3058 }
3059 
3060 
3061 
3062 /// Size of least common multiple of denominators in rational vector.
3063 int dlcmSizeRational(const Rational* vector, const int length, const int base)
3064 {
3065  assert(vector != 0);
3066  assert(length >= 0);
3067  assert(base >= 0);
3068 
3069  mpz_t lcm;
3070 
3071  mpz_init_set_ui(lcm, 1);
3072 
3073  for(int i = 0; i < length; i++)
3074  mpz_lcm(lcm, lcm, mpq_denref(vector[i].getMpqRef()));
3075 
3076  int size = (int)mpz_sizeinbase(lcm, base);
3077 
3078  mpz_clear(lcm);
3079 
3080  return size;
3081 }
3082 
3083 
3084 
3085 /// Size of largest denominator in rational vector.
3086 int dmaxSizeRational(const Rational* vector, const int length, const int base)
3087 {
3088  assert(vector != 0);
3089  assert(length >= 0);
3090  assert(base >= 0);
3091 
3092  size_t dmax = 0;
3093 
3094  for(int i = 0; i < length; i++)
3095  {
3096  size_t dsize = mpz_sizeinbase(mpq_denref(vector[i].getMpqRef()), base);
3097 
3098  if(dsize > dmax)
3099  dmax = dsize;
3100  }
3101 
3102  return (int)dmax;
3103 }
3104 
3105 
3106 
3107 #else
3108 
3109 
3110 
3111 /// Defines the "Pimpl"-class Private
3113 {
3114 
3115 public:
3116 
3117  /// value
3118  long double privatevalue;
3119 
3120  /// default constructor
3122  {
3123  privatevalue = 0;
3124  }
3125 
3126  /// copy constructor
3127  Private(const Private& p)
3128  {
3129  *this = p;
3130  }
3131 
3132  /// constructor from long double
3133  Private(const long double& r)
3134  {
3135  privatevalue = r;
3136  }
3137 
3138  /// constructor from double
3139  Private(const double& r)
3140  {
3141  privatevalue = r;
3142  }
3143 
3144  /// constructor from int
3145  Private(const int& i)
3146  {
3147  privatevalue = i;
3148  }
3149 
3150  /// assignment operator
3152  {
3153  this->privatevalue = p.privatevalue;
3154  return *this;
3155  }
3156 
3157  /// assignment operator from long double
3158  Private& operator=(const long double& r)
3159  {
3160  this->privatevalue = r;
3161  return *this;
3162  }
3163 
3164  /// assignment operator from double
3165  Private& operator=(const double& r)
3166  {
3167  this->privatevalue = (long double)(r);
3168  return *this;
3169  }
3170 
3171  /// assignment operator from int
3172  Private& operator=(const int& i)
3173  {
3174  this->privatevalue = (long double)(i);
3175  return *this;
3176  }
3177 };
3178 
3179 
3180 
3181 /// default constructor
3183 {
3184  dpointer = 0;
3186  dpointer = new(dpointer) Private();
3187 }
3188 
3189 
3190 
3191 /// copy constructor
3193 {
3194  dpointer = 0;
3196  dpointer = new(dpointer) Private(*(r.dpointer));
3197 }
3198 
3199 
3200 
3201 /// constructor from long double
3202 Rational::Rational(const long double& r)
3203 {
3204  dpointer = 0;
3206  dpointer = new(dpointer) Private(r);
3207 }
3208 
3209 
3210 
3211 /// constructor from double
3212 Rational::Rational(const double& r)
3213 {
3214  dpointer = 0;
3216  dpointer = new(dpointer) Private(r);
3217 }
3218 
3219 
3220 
3221 /// constructor from int
3222 Rational::Rational(const int& i)
3223 {
3224  dpointer = 0;
3226  dpointer = new(dpointer) Private(i);
3227 }
3228 
3229 
3230 
3231 /// destructor
3233 {
3234  spx_free(dpointer);
3235 }
3236 
3237 
3238 
3239 /// enables list memory
3241 {
3242  // because list memory is not used when SOPLEX_WITH_GMP is not defined, there is nothing to do here
3243 }
3244 
3245 
3246 
3247 /// frees the unused rational elements in the memory list
3248 /** this can be useful when you want to save memory or needed when working with a GMP memory manager like the one
3249  * in EGlib that frees GMP memory before the destructor of the static memory list is called; in most cases this
3250  * method is optional; note that this does not free the Rational elements that are currently in use
3251  */
3253 {
3254  // because list memory is not used when SOPLEX_WITH_GMP is not defined, there is nothing to do here
3255 }
3256 
3257 
3258 
3259 /// disables list memory
3261 {
3262  // because list memory is not used when SOPLEX_WITH_GMP is not defined, there is nothing to do here
3263 }
3264 
3265 
3266 
3267 /// assignment operator
3269 {
3270  *dpointer = *(r.dpointer);
3271  return *this;
3272 }
3273 
3274 
3275 
3276 /// assignment operator from long double
3277 Rational& Rational::operator=(const long double& r)
3278 {
3279  *dpointer = r;
3280  return *this;
3281 }
3282 
3283 
3284 
3285 /// assignment operator from double
3287 {
3288  *dpointer = r;
3289  return *this;
3290 }
3291 
3292 
3293 
3294 /// assignment operator from int
3296 {
3297  *dpointer = i;
3298  return *this;
3299 }
3300 
3301 
3302 
3303 /// typecasts Rational to double (allows only explicit typecast)
3304 Rational::operator double() const
3305 {
3306  return (double)this->dpointer->privatevalue;
3307 }
3308 
3309 
3310 
3311 /// typecasts Rational to long double (allows only explicit typecast)
3312 Rational::operator long double() const
3313 
3314 {
3315  return this->dpointer->privatevalue;
3316 }
3317 
3318 
3319 
3320 /// addition operator
3322 {
3323  Rational retval = *this;
3325  return retval;
3326 }
3327 
3328 
3329 
3330 /// addition assignment operator
3332 {
3334  return *this;
3335 }
3336 
3337 
3338 
3339 /// addition operator for doubles
3340 Rational Rational::operator+(const double& d) const
3341 {
3342  Rational retval = *this;
3343  retval.dpointer->privatevalue += d;
3344  return retval;
3345 }
3346 
3347 
3348 
3349 /// addition assignment operator for doubles
3351 {
3352  this->dpointer->privatevalue += d;
3353  return *this;
3354 }
3355 
3356 
3357 
3358 /// addition operator for ints
3359 Rational Rational::operator+(const int& d) const
3360 {
3361  Rational retval = *this;
3362  retval.dpointer->privatevalue += d;
3363  return retval;
3364 }
3365 
3366 
3367 
3368 /// addition assignment operator for ints
3370 {
3371  this->dpointer->privatevalue += d;
3372  return *this;
3373 }
3374 
3375 
3376 
3377 /// subtraction operator
3379 {
3380  Rational retval = *this;
3382  return retval;
3383 }
3384 
3385 
3386 
3387 /// subtraction assignment operator
3389 {
3391  return *this;
3392 }
3393 
3394 
3395 
3396 /// subtraction operator for doubles
3397 Rational Rational::operator-(const double& d) const
3398 {
3399  Rational retval = *this;
3400  retval.dpointer->privatevalue -= d;
3401  return retval;
3402 }
3403 
3404 
3405 
3406 /// subtraction assignment operator for doubles
3408 {
3409  this->dpointer->privatevalue -= d;
3410  return *this;
3411 }
3412 
3413 
3414 
3415 /// subtraction operator for ints
3416 Rational Rational::operator-(const int& d) const
3417 {
3418  Rational retval = *this;
3419  retval.dpointer->privatevalue -= d;
3420  return retval;
3421 }
3422 
3423 
3424 
3425 /// subtraction assignment operator for ints
3427 {
3428  this->dpointer->privatevalue -= d;
3429  return *this;
3430 }
3431 
3432 
3433 
3434 /// multiplication operator
3436 {
3437  Rational retval = *this;
3439  return retval;
3440 }
3441 
3442 
3443 
3444 /// multiplication assignment operator
3446 {
3448  return *this;
3449 }
3450 
3451 
3452 
3453 /// multiplication operator for doubles
3454 Rational Rational::operator*(const double& d) const
3455 {
3456  Rational retval = *this;
3457  retval.dpointer->privatevalue *= d;
3458  return retval;
3459 }
3460 
3461 
3462 
3463 /// multiplication assignment operator for doubles
3465 {
3466  this->dpointer->privatevalue *= d;
3467  return *this;
3468 }
3469 
3470 
3471 
3472 /// multiplication operator for ints
3473 Rational Rational::operator*(const int& d) const
3474 {
3475  Rational retval = *this;
3476  retval.dpointer->privatevalue *= d;
3477  return retval;
3478 }
3479 
3480 
3481 
3482 /// multiplication assignment operator for ints
3484 {
3485  this->dpointer->privatevalue *= d;
3486  return *this;
3487 }
3488 
3489 
3490 
3491 /// division operator
3493 {
3494  Rational retval = *this;
3496  return retval;
3497 }
3498 
3499 
3500 
3501 /// division assignment operator
3503 {
3505  return *this;
3506 }
3507 
3508 
3509 
3510 /// division operator for doubles
3511 Rational Rational::operator/(const double& d) const
3512 {
3513  Rational retval = *this;
3514  retval.dpointer->privatevalue /= d;
3515  return retval;
3516 }
3517 
3518 
3519 
3520 /// division assignment operator for doubles
3522 {
3523  this->dpointer->privatevalue /= d;
3524  return *this;
3525 }
3526 
3527 
3528 
3529 /// division operator for ints
3530 Rational Rational::operator/(const int& r) const
3531 {
3532  Rational retval = *this;
3533  retval.dpointer->privatevalue /= r;
3534  return retval;
3535 }
3536 
3537 
3538 
3539 /// division assignment operator for ints
3541 {
3542  this->dpointer->privatevalue /= r;
3543  return *this;
3544 }
3545 
3546 
3547 
3548 /// add product of two rationals
3550 {
3552  return *this;
3553 }
3554 
3555 
3556 
3557 /// subtract product of two rationals
3559 {
3561  return *this;
3562 }
3563 
3564 
3565 
3566 /// add quotient of two rationals, r divided by s
3568 {
3570  return *this;
3571 }
3572 
3573 
3574 
3575 /// subtract quotient of two rationals, r divided by s
3577 {
3579  return *this;
3580 }
3581 
3582 
3583 
3584 /// inversion
3586 {
3587  this->dpointer->privatevalue = 1.0 / this->dpointer->privatevalue;
3588  return *this;
3589 }
3590 
3591 
3592 
3593 /// round up to next power of two
3595 {
3596  ///@todo implement
3597  return *this;
3598 }
3599 
3600 
3601 
3602 /// checks if d is the closest possible double
3603 bool Rational::isNextTo(const double& d)
3604 {
3605  return (this->dpointer->privatevalue == d);
3606 }
3607 
3608 
3609 
3610 /// checks if d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles
3611 bool Rational::isAdjacentTo(const double& d) const
3612 {
3613  return (double(this->dpointer->privatevalue) == d);
3614 }
3615 
3616 
3617 
3618 /// Size in specified base (bit size for base 2)
3619 int Rational::sizeInBase(const int base) const
3620 {
3621  ///@todo this is only correct for base 2
3622  return precision();
3623 }
3624 
3625 
3626 
3627 /// returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (INT_MAX if exact)
3629 {
3630  return sizeof(long double);
3631 }
3632 
3633 
3634 
3635 /// read Rational from string
3636 bool Rational::readString(const char* s)
3637 {
3638  return (sscanf(s, "%Lf", &this->dpointer->privatevalue) == 1);
3639 }
3640 
3641 
3642 
3643 
3644 /// convert rational number to string
3645 std::string rationalToString(const Rational& r, const int precision)
3646 {
3647  std::stringstream sstream;
3648  sstream << std::setprecision(precision <= 0 ? 16 : precision) << r;
3649  return sstream.str();
3650 }
3651 
3652 
3653 
3654 /// read Rational from string
3655 bool readStringRational(const char* s, Rational& value)
3656 {
3657  return (sscanf(s, "%Lf", &value.dpointer->privatevalue) == 1);
3658 }
3659 
3660 
3661 
3662 /// print Rational
3663 std::ostream& operator<<(std::ostream& os, const Rational& r)
3664 {
3665  os << r.dpointer->privatevalue;
3666  return os;
3667 }
3668 
3669 
3670 
3671 /// comparison operator returning a positive value if r > s, zero if r = s, and a negative value if r < s
3672 int compareRational(const Rational& r, const Rational& s)
3673 {
3675  return 1;
3676  else if(r.dpointer->privatevalue < s.dpointer->privatevalue)
3677  return -1;
3678  else
3679  return 0;
3680 }
3681 
3682 
3683 
3684 /// equality operator
3685 bool operator==(const Rational& r, const Rational& s)
3686 {
3687  return (r.dpointer->privatevalue == s.dpointer->privatevalue);
3688 }
3689 
3690 
3691 
3692 /// inequality operator
3693 bool operator!=(const Rational& r, const Rational& s)
3694 {
3695  return (r.dpointer->privatevalue != s.dpointer->privatevalue);
3696 }
3697 
3698 
3699 
3700 /// less than operator
3701 bool operator<(const Rational& r, const Rational& s)
3702 {
3703  return (r.dpointer->privatevalue < s.dpointer->privatevalue);
3704 }
3705 
3706 
3707 
3708 /// less than or equal to operator
3709 bool operator<=(const Rational& r, const Rational& s)
3710 {
3711  return (r.dpointer->privatevalue <= s.dpointer->privatevalue);
3712 }
3713 
3714 
3715 
3716 /// greater than operator
3717 bool operator>(const Rational& r, const Rational& s)
3718 {
3719  return (r.dpointer->privatevalue > s.dpointer->privatevalue);
3720 }
3721 
3722 
3723 
3724 /// greater than or equal to operator
3725 bool operator>=(const Rational& r, const Rational& s)
3726 {
3727  return (r.dpointer->privatevalue >= s.dpointer->privatevalue);
3728 }
3729 
3730 
3731 
3732 /// equality operator for Rational and double
3733 bool operator==(const Rational& r, const double& s)
3734 {
3735  return (r.dpointer->privatevalue > s - DEFAULT_EPS_ZERO)
3736  && (r.dpointer->privatevalue < s + DEFAULT_EPS_ZERO);
3737 }
3738 
3739 
3740 
3741 /// inequality operator for Rational and double
3742 bool operator!=(const Rational& r, const double& s)
3743 {
3744  return (r.dpointer->privatevalue <= s - DEFAULT_EPS_ZERO)
3745  || (r.dpointer->privatevalue >= s + DEFAULT_EPS_ZERO);
3746 }
3747 
3748 
3749 
3750 /// less than operator for Rational and double
3751 bool operator<(const Rational& r, const double& s)
3752 {
3753  return (r.dpointer->privatevalue < s);
3754 }
3755 
3756 
3757 
3758 /// less than or equal to operator for Rational and double
3759 bool operator<=(const Rational& r, const double& s)
3760 {
3761  return (r.dpointer->privatevalue <= s);
3762 }
3763 
3764 
3765 
3766 /// greater than operator for Rational and double
3767 bool operator>(const Rational& r, const double& s)
3768 {
3769  return (r.dpointer->privatevalue > s);
3770 }
3771 
3772 
3773 
3774 /// greater than or equal to operator for Rational and double
3775 bool operator>=(const Rational& r, const double& s)
3776 {
3777  return (r.dpointer->privatevalue >= s);
3778 }
3779 
3780 
3781 
3782 /// equality operator for double and Rational
3783 bool operator==(const double& r, const Rational& s)
3784 {
3785  return (s.dpointer->privatevalue > r - DEFAULT_EPS_ZERO)
3786  && (s.dpointer->privatevalue < r + DEFAULT_EPS_ZERO);
3787 }
3788 
3789 
3790 
3791 /// inequality operator double and Rational
3792 bool operator!=(const double& r, const Rational& s)
3793 {
3794  return (s.dpointer->privatevalue <= r - DEFAULT_EPS_ZERO)
3795  || (s.dpointer->privatevalue >= r + DEFAULT_EPS_ZERO);
3796 }
3797 
3798 
3799 
3800 /// less than operator double and Rational
3801 bool operator<(const double& r, const Rational& s)
3802 {
3803  return (r < s.dpointer->privatevalue);
3804 }
3805 
3806 
3807 
3808 /// less than or equal to operator double and Rational
3809 bool operator<=(const double& r, const Rational& s)
3810 {
3811  return (r <= s.dpointer->privatevalue);
3812 }
3813 
3814 
3815 
3816 /// greater than operator double and Rational
3817 bool operator>(const double& r, const Rational& s)
3818 {
3819  return (r > s.dpointer->privatevalue);
3820 }
3821 
3822 
3823 
3824 /// greater than or equal to operator double and Rational
3825 bool operator>=(const double& r, const Rational& s)
3826 {
3827  return (r >= s.dpointer->privatevalue);
3828 }
3829 
3830 
3831 
3832 
3833 /// equality operator for Rational and long double
3834 bool operator==(const Rational& r, const long double& s)
3835 {
3836  return (r.dpointer->privatevalue > s - DEFAULT_EPS_ZERO)
3837  && (r.dpointer->privatevalue < s + DEFAULT_EPS_ZERO);
3838 }
3839 
3840 
3841 
3842 /// inequality operator for Rational and long double
3843 bool operator!=(const Rational& r, const long double& s)
3844 {
3845  return (r.dpointer->privatevalue <= s - DEFAULT_EPS_ZERO)
3846  || (r.dpointer->privatevalue >= s + DEFAULT_EPS_ZERO);
3847 }
3848 
3849 
3850 
3851 /// less than operator for Rational and long double
3852 bool operator<(const Rational& r, const long double& s)
3853 {
3854  return (r.dpointer->privatevalue < s);
3855 }
3856 
3857 
3858 
3859 /// less than or equal to operator for Rational and long double
3860 bool operator<=(const Rational& r, const long double& s)
3861 {
3862  return (r.dpointer->privatevalue <= s);
3863 }
3864 
3865 
3866 
3867 /// greater than operator for Rational and long double
3868 bool operator>(const Rational& r, const long double& s)
3869 {
3870  return (r.dpointer->privatevalue > s);
3871 }
3872 
3873 
3874 
3875 /// greater than or equal to operator for Rational and long double
3876 bool operator>=(const Rational& r, const long double& s)
3877 {
3878  return (r.dpointer->privatevalue >= s);
3879 }
3880 
3881 
3882 
3883 /// equality operator for long double and Rational
3884 bool operator==(const long double& r, const Rational& s)
3885 {
3886  return (s.dpointer->privatevalue > r - DEFAULT_EPS_ZERO)
3887  && (s.dpointer->privatevalue < r + DEFAULT_EPS_ZERO);
3888 }
3889 
3890 
3891 
3892 /// inequality operator long double and Rational
3893 bool operator!=(const long double& r, const Rational& s)
3894 {
3895  return (s.dpointer->privatevalue <= r - DEFAULT_EPS_ZERO)
3896  || (s.dpointer->privatevalue >= r + DEFAULT_EPS_ZERO);
3897 }
3898 
3899 
3900 
3901 /// less than operator long double and Rational
3902 bool operator<(const long double& r, const Rational& s)
3903 {
3904  return (r < s.dpointer->privatevalue);
3905 }
3906 
3907 
3908 
3909 /// less than or equal to operator long double and Rational
3910 bool operator<=(const long double& r, const Rational& s)
3911 {
3912  return (r <= s.dpointer->privatevalue);
3913 }
3914 
3915 
3916 
3917 /// greater than operator long double and Rational
3918 bool operator>(const long double& r, const Rational& s)
3919 {
3920  return (r > s.dpointer->privatevalue);
3921 }
3922 
3923 
3924 
3925 /// greater than or equal to operator long double and Rational
3926 bool operator>=(const long double& r, const Rational& s)
3927 {
3928  return (r >= s.dpointer->privatevalue);
3929 }
3930 
3931 
3932 
3933 /// equality operator for Rational and int
3934 bool operator==(const Rational& r, const int& s)
3935 {
3936  return r.dpointer->privatevalue == s;
3937 }
3938 
3939 
3940 
3941 /// inequality operator for Rational and int
3942 bool operator!=(const Rational& r, const int& s)
3943 {
3944  return r.dpointer->privatevalue != s;
3945 }
3946 
3947 
3948 
3949 /// less than operator for Rational and int
3950 bool operator<(const Rational& r, const int& s)
3951 {
3952  return r.dpointer->privatevalue < s;
3953 }
3954 
3955 
3956 
3957 /// less than or equal to operator for Rational and int
3958 bool operator<=(const Rational& r, const int& s)
3959 {
3960  return r.dpointer->privatevalue <= s;
3961 }
3962 
3963 
3964 
3965 /// greater than operator for Rational and int
3966 bool operator>(const Rational& r, const int& s)
3967 {
3968  return r.dpointer->privatevalue > s;
3969 }
3970 
3971 
3972 
3973 /// greater than or equal to operator for Rational and int
3974 bool operator>=(const Rational& r, const int& s)
3975 {
3976  return r.dpointer->privatevalue >= s;
3977 }
3978 
3979 
3980 
3981 /// equality operator for int and Rational
3982 bool operator==(const int& r, const Rational& s)
3983 {
3984  return r == s.dpointer->privatevalue;
3985 }
3986 
3987 
3988 
3989 /// inequality operator for int and Rational
3990 bool operator!=(const int& r, const Rational& s)
3991 {
3992  return r != s.dpointer->privatevalue;
3993 }
3994 
3995 
3996 
3997 /// less than operator for int and Rational
3998 bool operator<(const int& r, const Rational& s)
3999 {
4000  return r < s.dpointer->privatevalue;
4001 }
4002 
4003 
4004 
4005 /// less than or equal to operator for int and Rational
4006 bool operator<=(const int& r, const Rational& s)
4007 {
4008  return r <= s.dpointer->privatevalue;
4009 }
4010 
4011 
4012 
4013 /// greater than operator for int and Rational
4014 bool operator>(const int& r, const Rational& s)
4015 {
4016  return r > s.dpointer->privatevalue;
4017 }
4018 
4019 
4020 
4021 /// greater than or equal to operator for int and Rational
4022 bool operator>=(const int& r, const Rational& s)
4023 {
4024  return r >= s.dpointer->privatevalue;
4025 }
4026 
4027 
4028 
4029 /// addition operator for double and Rational
4030 Rational operator+(const double& d, const Rational& r)
4031 {
4032  Rational retval(d);
4034  return retval;
4035 }
4036 
4037 
4038 
4039 /// subtraction operator for double and Rational
4040 Rational operator-(const double& d, const Rational& r)
4041 {
4042  Rational retval(d);
4044  return retval;
4045 }
4046 
4047 
4048 
4049 /// multiplication operator for double and Rational
4050 Rational operator*(const double& d, const Rational& r)
4051 {
4052  Rational retval(d);
4054  return retval;
4055 }
4056 
4057 
4058 
4059 /// division operator for double and Rational
4060 Rational operator/(const double& d, const Rational& r)
4061 {
4062  Rational retval(d);
4064  return retval;
4065 }
4066 
4067 
4068 
4069 /// addition operator for int and Rational
4070 Rational operator+(const int& d, const Rational& r)
4071 {
4072  return (r + d);
4073 }
4074 
4075 
4076 
4077 /// subtraction operator for int and Rational
4078 Rational operator-(const int& d, const Rational& r)
4079 {
4080  return -(r - d);
4081 }
4082 
4083 
4084 
4085 /// multiplication operator for int and Rational
4086 Rational operator*(const int& d, const Rational& r)
4087 {
4088  return (r * d);
4089 }
4090 
4091 
4092 
4093 /// division operator for int and Rational
4094 Rational operator/(const int& d, const Rational& r)
4095 {
4096  return 1.0 / r;
4097 }
4098 
4099 
4100 
4101 /// Absolute.
4103 {
4104  Rational res = r;
4105 
4106  if(res.dpointer->privatevalue < 0)
4107  res.dpointer->privatevalue *= -1;
4108 
4109  return res;
4110 }
4111 
4112 
4113 
4114 /// Sign function; returns 1 if r > 0, 0 if r = 0, and -1 if r < 0.
4115 int sign(const Rational& r)
4116 {
4117  return (r.dpointer->privatevalue > 0) - (r.dpointer->privatevalue < 0);
4118 }
4119 
4120 
4121 
4122 /// Negation.
4124 {
4125  Rational res = r;
4126  res.dpointer->privatevalue *= -1;
4127  return res;
4128 }
4129 
4130 
4131 
4132 /// Total size of rational vector.
4133 int totalSizeRational(const Rational* vector, const int length, const int base)
4134 {
4135  assert(vector != 0);
4136  assert(length >= 0);
4137  assert(base >= 0);
4138 
4139  int size = 0;
4140 
4141  for(int i = 0; i < length; i++)
4142  size += vector[i].sizeInBase(base);
4143 
4144  return size;
4145 }
4146 
4147 
4148 
4149 /// Size of least common multiple of denominators in rational vector.
4150 int dlcmSizeRational(const Rational* vector, const int length, const int base)
4151 {
4152  assert(vector != 0);
4153  assert(length >= 0);
4154  assert(base >= 0);
4155 
4156  return 0;
4157 }
4158 
4159 
4160 
4161 /// Size of largest denominator in rational vector.
4162 int dmaxSizeRational(const Rational* vector, const int length, const int base)
4163 {
4164  assert(vector != 0);
4165  assert(length >= 0);
4166  assert(base >= 0);
4167 
4168  return 0;
4169 }
4170 
4171 
4172 
4173 #endif // SOPLEX_WITH_GMP
4174 } // namespace soplex
Rational & addProduct(const Rational &r, const Rational &s)
add product of two rationals
Definition: rational.cpp:3549
bool isAdjacentTo(const double &d) const
checks if d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles ...
Definition: rational.cpp:3611
bool isNextTo(const double &d)
checks if d is the closest number that can be represented by double
Definition: rational.cpp:3603
THREADLOCAL const Real infinity
Definition: spxdefines.cpp:26
friend Rational spxAbs(const Rational &r)
Absolute.
Definition: rational.cpp:4102
Memory allocation routines.
friend std::string rationalToString(const Rational &r, const int precision)
convert rational number to string
Definition: rational.cpp:3645
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
Definition: rational.cpp:4150
long double privatevalue
value
Definition: rational.cpp:3118
friend bool operator<(const Rational &r, const Rational &s)
less than operator
Definition: rational.cpp:3701
Private & operator=(const int &i)
assignment operator from int
Definition: rational.cpp:3172
Rational operator*(const Rational &r) const
multiplication operator
Definition: rational.cpp:3435
~Rational()
destructor
Definition: rational.cpp:3232
int sizeInBase(const int base=2) const
Size in specified base (bit size for base 2)
Definition: rational.cpp:3619
friend bool operator<=(const Rational &r, const Rational &s)
less than or equal to operator
Definition: rational.cpp:3709
Rational operator-(const Rational &r) const
subtraction operator
Definition: rational.cpp:3378
friend bool operator!=(const Rational &r, const Rational &s)
inequality operator
Definition: rational.cpp:3693
Rational & subProduct(const Rational &r, const Rational &s)
subtract product of two rationals
Definition: rational.cpp:3558
Rational & invert()
inversion
Definition: rational.cpp:3585
friend bool operator>=(const Rational &r, const Rational &s)
greater than or equal to operator
Definition: rational.cpp:3725
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
Definition: rational.cpp:4133
Wrapper for GMP type mpq_class.We wrap mpq_class so that we can replace it by a double type if GMP is...
Definition: rational.h:44
static void disableListMem()
disables list memory
Definition: rational.cpp:3260
Private & operator=(const long double &r)
assignment operator from long double
Definition: rational.cpp:3158
Private & operator=(const double &r)
assignment operator from double
Definition: rational.cpp:3165
Rational & operator/=(const Rational &r)
division assignment operator
Definition: rational.cpp:3502
Rational & operator-=(const Rational &r)
subtraction assignment operator
Definition: rational.cpp:3388
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:48
static void freeListMem()
frees the unused rational elements in the memory list
Definition: rational.cpp:3252
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
Definition: rational.cpp:4162
Private()
default constructor
Definition: rational.cpp:3121
#define MSG_DEBUG(x)
Definition: spxdefines.h:132
Rational & operator*=(const Rational &r)
multiplication assignment operator operator
Definition: rational.cpp:3445
Private & operator=(const Private &p)
assignment operator
Definition: rational.cpp:3151
Private(const double &r)
constructor from double
Definition: rational.cpp:3139
bool readString(const char *s)
read Rational from string
Definition: rational.cpp:3636
static int precision()
returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (IN...
Definition: rational.cpp:3628
Wrapper for GMP types.
#define DEFAULT_EPS_ZERO
default allowed additive zero: 1.0 + EPS_ZERO == 1.0
Definition: spxdefines.h:230
friend int sign(const Rational &r)
Sign function; returns 1 if r > 0, 0 if r = 0, and -1 if r < 0.
Definition: rational.cpp:4115
Defines the "Pimpl"-class Private.
Definition: rational.cpp:3112
friend std::ostream & operator<<(std::ostream &os, const Rational &q)
print Rational
Definition: rational.cpp:3663
Private(const int &i)
constructor from int
Definition: rational.cpp:3145
static void enableListMem()
enables list memory
Definition: rational.cpp:3240
Private(const long double &r)
constructor from long double
Definition: rational.cpp:3133
Debugging, floating point type and parameter definitions.
Private * dpointer
Definition: rational.h:47
friend bool operator==(const Rational &r, const Rational &s)
equality operator
Definition: rational.cpp:3685
friend int compareRational(const Rational &r, const Rational &s)
comparison operator returning a positive value if r > s, zero if r = s, and a negative value if r < s...
Definition: rational.cpp:3672
Rational operator+(const Rational &r) const
addition operator
Definition: rational.cpp:3321
Everything should be within this namespace.
Rational operator/(const Rational &r) const
division operator
Definition: rational.cpp:3492
Real spxNextafter(Real x, Real y)
Definition: spxdefines.h:338
friend bool readStringRational(const char *s, Rational &value)
read Rational from string
Definition: rational.cpp:3655
Rational & operator=(const Rational &)
assignment operator
Definition: rational.cpp:3268
Rational & subQuotient(const Rational &r, const Rational &s)
subtract quotient of two rationals, r divided by s
Definition: rational.cpp:3576
friend bool operator>(const Rational &r, const Rational &s)
greater than operator
Definition: rational.cpp:3717
Private(const Private &p)
copy constructor
Definition: rational.cpp:3127
Rational()
default constructor
Definition: rational.cpp:3182
#define THREADLOCAL
SOPLEX_DEBUG.
Definition: spxdefines.h:145
void spx_free(T &p)
Release memory.
Definition: spxalloc.h:110
Rational & addQuotient(const Rational &r, const Rational &s)
add quotient of two rationals, r divided by s
Definition: rational.cpp:3567
Rational & operator+=(const Rational &r)
addition assignment operator
Definition: rational.cpp:3331
Rational & powRound()
round up to next power of two
Definition: rational.cpp:3594