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