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