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