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