1 // -*- C++ -*-
2 //==============================================================================================
3 //
4 //	This file is part of LiDIA --- a library for computational number theory
5 //
6 //	Copyright (c) 1994--2001 the LiDIA Group.  All rights reserved.
7 //
8 //	See http://www.informatik.tu-darmstadt.de/TI/LiDIA/
9 //
10 //----------------------------------------------------------------------------------------------
11 //
12 //	$Id$
13 //
14 //	Author	: Thomas Papanikolaou (TP)
15 //	Changes	: See CVS log
16 //
17 //==============================================================================================
18 
19 
20 #ifndef LIDIA_BIGRATIONAL_H_GUARD_
21 #define LIDIA_BIGRATIONAL_H_GUARD_
22 
23 
24 #ifndef LIDIA_BIGINT_H_GUARD_
25 # include	"LiDIA/bigint.h"
26 #endif
27 
28 
29 
30 #ifdef LIDIA_NAMESPACE
31 namespace LiDIA {
32 # define IN_NAMESPACE_LIDIA
33 #endif
34 
35 
36 
37 class bigrational
38 {
39 private:
40 
41 	//
42 	// the C++ type we use to represent a bigrational
43 	//
44 
45 	bigint num, den;
46 
47 
48 	void normalize();
49 
50 
51 
52 public:
53 
54 	//
55 	// c'tors and d'tor
56 	//
57 
58 	bigrational();
59 	bigrational(int n);
60 	bigrational(long n);
61 	bigrational(unsigned long n);
62 	bigrational(double d);
63 	bigrational(const bigint & n);
64 	bigrational(const bigint & n, const bigint & d);
65 	bigrational(const bigrational & a);
66 	~bigrational();
67 
68 
69 
70 #ifndef HEADBANGER
71 
72 	//
73 	// accessors
74 	//
75 
76 	const bigint & numerator() const;
77 	const bigint & denominator() const;
78 
79 
80 
81 	//
82 	// assigners
83 	//
84 
85 	void assign_zero();
86 	void assign_one();
87 
88 	void assign(int n);
89 	void assign(long n);
90 	void assign(unsigned long n);
91 	void assign(double d);
92 	void assign(const bigint & n);
93 	void assign(const bigint & n, const bigint & d);
94 	void assign(const bigrational & a);
95 
96 	void assign_numerator(const bigint & n);
97 	void assign_denominator(const bigint & d);
98 
99 	bigrational & operator = (int n);
100 	bigrational & operator = (long n);
101 	bigrational & operator = (unsigned long n);
102 	bigrational & operator = (double d);
103 	bigrational & operator = (const bigint & a);
104 	bigrational & operator = (const bigrational & a);
105 
106 
107 
108 	//
109 	// comparators
110 	//
111 
112 	int abs_compare(const bigrational & a) const;
113 	int abs_compare(const bigint & a) const;
114 	int abs_compare(unsigned long a) const;
115 	int compare(const bigrational & a) const;
116 	int compare(const bigint & a) const;
117 	int compare(const long a) const;
118 	int compare(const unsigned long a) const;
119 
120 	int sign() const;
121 
122 	bool is_positive() const;
123 	bool is_negative() const;
124 	bool is_zero() const;
125 	bool is_gt_zero() const;
126 	bool is_ge_zero() const;
127 	bool is_lt_zero() const;
128 	bool is_le_zero() const;
129 	bool is_one() const;
130 
131 
132 
133 	//
134 	// converters
135 	//
136 
137 	bool intify(int & i) const;
138 	bool longify(long & l) const;
139 
140 
141 
142 	//
143 	// modifiers
144 	//
145 
146 	void absolute_value();
147 	void negate();
148 	void invert();
149 
150 	void inc();
151 	void dec();
152 
153 	void multiply_by_denominator();
154 
155 	void multiply_by_2();
156 	void divide_by_2();
157 
158 	void swap(bigrational & a);
159 
160 
161 
162 	//
163 	// procedural arithmetic
164 	//
165 
166 	friend void add(bigrational & c, const bigrational & a, const bigrational & b);
167 	friend void add(bigrational & c, const bigrational & a, const bigint & b);
168 	friend void add(bigrational & c, const bigrational & a, long b);
169 	friend void add(bigrational & c, const bigrational & a, unsigned long b);
170 	friend void subtract(bigrational & c, const bigrational & a, const bigrational & b);
171 	friend void subtract(bigrational & c, const bigrational & a, const bigint & b);
172 	friend void subtract(bigrational & c, const bigrational & a, long b);
173 	friend void subtract(bigrational & c, const bigrational & a, unsigned long b);
174 
175 	friend void multiply(bigrational & c, const bigrational & a, const bigrational & b);
176 	friend void multiply(bigrational & c, const bigrational & a, const bigint & b);
177 	friend void multiply(bigrational & c, const bigrational & a, long b);
178 	friend void multiply(bigrational & c, const bigrational & a, unsigned long b);
179 	friend void square(bigrational & a, const bigrational & b);
180 	friend void square(bigrational & a, const bigint & b);
181 
182 	friend void divide(bigrational & c, const bigrational & a, const bigrational & b);
183 	friend void divide(bigrational & c, const bigrational & a, const bigint & b);
184 	friend void divide(bigrational & c, const bigrational & a, long b);
185 	friend void divide(bigrational & c, const bigrational & a, unsigned long b);
186 
187 	friend void shift_left(bigrational & c, const bigrational & a, long ui);
188 	friend void shift_right(bigrational & c, const bigrational & a, long ui);
189 
190 
191 #endif
192 
193 	//
194 	// input / output
195 	//
196 
197 	friend std::istream & operator >> (std::istream & in, bigrational & a);
198 	friend std::ostream & operator << (std::ostream & out, const bigrational & a);
199 
200 	friend int string_to_bigrational(char *s, char *t, bigrational & a);
201 	friend int bigrational_to_string(const bigrational & a, char *s, char *t);
202 
203 #ifdef C_STDIO
204 	//
205 	// using fread/fwrite
206 	//
207 
208 	void read_from_file(FILE * fp);
209 	void write_to_file(FILE * fp);
210 
211 	//
212 	// using fscanf/fprintf
213 	//
214 
215 	void scan_from_file(FILE * fp);
216 	void print_to_file(FILE * fp);
217 #endif	// C_STDIO
218 
219 	// for use in elliptic curve code
220 	static bigint characteristic();
221 
222 };
223 
224 void add(bigrational & c, const bigrational & a, const bigrational & b);
225 void add(bigrational & c, const bigrational & a, const bigint & b);
226 void add(bigrational & c, const bigrational & a, long b);
227 void add(bigrational & c, const bigrational & a, unsigned long b);
228 void subtract(bigrational & c, const bigrational & a, const bigrational & b);
229 void subtract(bigrational & c, const bigrational & a, const bigint & b);
230 void subtract(bigrational & c, const bigrational & a, long b);
231 void subtract(bigrational & c, const bigrational & a, unsigned long b);
232 
233 void multiply(bigrational & c, const bigrational & a, const bigrational & b);
234 void multiply(bigrational & c, const bigrational & a, const bigint & b);
235 void multiply(bigrational & c, const bigrational & a, long b);
236 void multiply(bigrational & c, const bigrational & a, unsigned long b);
237 void square(bigrational & a, const bigrational & b);
238 void square(bigrational & a, const bigint & b);
239 
240 void divide(bigrational & c, const bigrational & a, const bigrational & b);
241 void divide(bigrational & c, const bigrational & a, const bigint & b);
242 void divide(bigrational & c, const bigrational & a, long b);
243 void divide(bigrational & c, const bigrational & a, unsigned long b);
244 
245 void shift_left(bigrational & c, const bigrational & a, long ui);
246 void shift_right(bigrational & c, const bigrational & a, long ui);
247 
248 //
249 // input / output
250 //
251 
252 std::istream & operator >> (std::istream & in, bigrational & a);
253 std::ostream & operator << (std::ostream & out, const bigrational & a);
254 
255 int string_to_bigrational(char *s, char *t, bigrational & a);
256 int bigrational_to_string(const bigrational & a, char *s, char *t);
257 
258 
259 //
260 // c'tors and d'tor
261 //
262 
263 inline
bigrational()264 bigrational::bigrational ()
265 	: num(0UL),
266 	  den(1UL)
267 {
268 	// nothing to do
269 }
270 
271 
272 
273 inline
bigrational(int n)274 bigrational::bigrational (int n)
275 	: num(n),
276 	  den(1UL)
277 {
278 	// nothing to do
279 }
280 
281 
282 
283 inline
bigrational(long n)284 bigrational::bigrational (long n)
285 	: num(n),
286 	  den(1UL)
287 {
288 	// nothing to do
289 }
290 
291 
292 
293 inline
bigrational(unsigned long n)294 bigrational::bigrational (unsigned long n)
295 	: num(n),
296 	  den(1UL)
297 {
298 	// nothing to do
299 }
300 
301 
302 
303 inline
bigrational(double d)304 bigrational::bigrational (double d)
305 {
306 	assign(d);
307 }
308 
309 
310 
311 inline
bigrational(const bigint & n)312 bigrational::bigrational (const bigint & n)
313 	: num(n),
314 	  den(1UL)
315 {
316 	// nothing to do
317 }
318 
319 
320 
321 inline
bigrational(const bigint & n,const bigint & d)322 bigrational::bigrational (const bigint & n, const bigint & d)
323 	: num(n),
324 	  den(d)
325 {
326 	if (d.is_zero()) {
327 		lidia_error_handler("bigrational", "constructor(n, d)::division by zero.");
328 		// actually call assign_zero(), but this is declared inline later
329 		num.assign_zero();
330 		den.assign_zero();
331 	}
332 	else {
333 		normalize();
334 	}
335 }
336 
337 
338 
339 inline
bigrational(const bigrational & a)340 bigrational::bigrational (const bigrational & a)
341 	: num(a.num),
342 	  den(a.den)
343 {
344 	// nothing to do
345 }
346 
347 
348 
349 inline
~bigrational()350 bigrational::~bigrational()
351 {
352 }
353 
354 
355 
356 //
357 // accessors
358 //
359 
360 inline const bigint &
numerator()361 bigrational::numerator () const
362 {
363 	return num;
364 }
365 
366 
367 
368 inline const bigint &
denominator()369 bigrational::denominator () const
370 {
371 	return den;
372 }
373 
374 
375 
376 inline const bigint &
numerator(const bigrational & a)377 numerator (const bigrational & a)
378 {
379 	return a.numerator();
380 }
381 
382 
383 
384 inline const bigint &
denominator(const bigrational & a)385 denominator (const bigrational & a)
386 {
387 	return a.denominator();
388 }
389 
390 
391 
392 //
393 // assigners
394 //
395 
396 inline void
assign_zero()397 bigrational::assign_zero ()
398 {
399 	num.assign_zero();
400 	den.assign_one();
401 }
402 
403 
404 
405 inline void
assign_one()406 bigrational::assign_one ()
407 {
408 	num.assign_one();
409 	den.assign_one();
410 }
411 
412 
413 
414 inline void
assign(int n)415 bigrational::assign(int n)
416 {
417 	num.assign(n);
418 	den.assign_one();
419 }
420 
421 
422 
423 inline void
assign(long n)424 bigrational::assign(long n)
425 {
426 	num.assign(n);
427 	den.assign_one();
428 }
429 
430 
431 
432 inline void
assign(unsigned long n)433 bigrational::assign(unsigned long n)
434 {
435 	num.assign(n);
436 	den.assign_one();
437 }
438 
439 
440 
441 inline void
assign(const bigint & n)442 bigrational::assign(const bigint & n)
443 {
444 	num.assign(n);
445 	den.assign_one();
446 }
447 
448 
449 
450 inline void
assign(const bigint & n,const bigint & d)451 bigrational::assign (const bigint & n, const bigint & d)
452 {
453 	if (d.is_zero()) {
454 		lidia_error_handler("bigrational", "assign(n, d)::division by zero.");
455 		assign_zero();
456 		return;
457 	}
458 	num.assign(n);
459 	den.assign(d);
460 	normalize();
461 }
462 
463 
464 
465 inline void
assign(const bigrational & a)466 bigrational::assign (const bigrational & a)
467 {
468 	if (&a != this) {
469 		num.assign(a.num);
470 		den.assign(a.den);
471 	}
472 }
473 
474 
475 
476 inline void
assign_numerator(const bigint & n)477 bigrational::assign_numerator (const bigint & n)
478 {
479 	if (&n != &num) {
480 		num.assign(n);
481 	}
482 }
483 
484 
485 
486 inline void
assign_denominator(const bigint & d)487 bigrational::assign_denominator (const bigint & d)
488 {
489 	if (d.is_zero()) {
490 		lidia_error_handler("bigrational", "assign(n, d)::division by zero.");
491 		return;
492 	}
493 	den.assign(d);
494 	if (den.is_negative()) {
495 		num.negate();
496 		den.negate();
497 	}
498 }
499 
500 
501 
502 inline bigrational &
503 bigrational::operator = (int n)
504 {
505 	assign(n);
506 	return *this;
507 }
508 
509 
510 
511 inline bigrational &
512 bigrational::operator = (long n)
513 {
514 	assign(n);
515 	return *this;
516 }
517 
518 
519 
520 inline bigrational &
521 bigrational::operator = (unsigned long n)
522 {
523 	assign(n);
524 	return *this;
525 }
526 
527 
528 
529 inline bigrational &
530 bigrational::operator = (double d)
531 {
532 	assign(d);
533 	return *this;
534 }
535 
536 
537 
538 inline bigrational &
539 bigrational::operator = (const bigint & n)
540 {
541 	assign(n);
542 	return *this;
543 }
544 
545 
546 
547 inline bigrational &
548 bigrational::operator = (const bigrational & a)
549 {
550 	assign(a);
551 	return *this;
552 }
553 
554 
555 
556 //
557 // comparators
558 //
559 
560 inline int
abs_compare(const bigrational & a)561 bigrational::abs_compare (const bigrational & a) const
562 {
563 	if (&a == this) {
564 		return 0;
565 	}
566 
567 	bigint r1, r2;
568 
569 	multiply(r1, num, a.den);
570 	multiply(r2, den, a.num);
571 	return r1.abs_compare(r2);
572 }
573 
574 
575 
576 inline int
abs_compare(const bigint & a)577 bigrational::abs_compare (const bigint & a) const
578 {
579 	if (den.is_one()) {
580 		return num.compare(a);
581 	}
582 
583 	bigint r;
584 
585 	multiply(r, den, a);
586 	return num.abs_compare(r);
587 }
588 
589 
590 
591 inline int
abs_compare(unsigned long a)592 bigrational::abs_compare (unsigned long a) const
593 {
594 	if (den.is_one()) {
595 		return num.compare(a);
596 	}
597 
598 	bigint r;
599 
600 	multiply(r, den, a);
601 	return num.abs_compare(r);
602 }
603 
604 
605 
606 inline int
abs_compare(const bigrational & a,const bigrational & b)607 abs_compare (const bigrational & a, const bigrational & b)
608 {
609 	return a.abs_compare(b);
610 }
611 
612 
613 
614 inline int
abs_compare(const bigrational & a,const bigint & b)615 abs_compare (const bigrational & a, const bigint & b)
616 {
617 	return a.abs_compare(b);
618 }
619 
620 
621 
622 inline int
abs_compare(const bigrational & a,const unsigned long b)623 abs_compare (const bigrational & a, const unsigned long b)
624 {
625 	return a.abs_compare(b);
626 }
627 
628 
629 
630 inline int
compare(const bigrational & a)631 bigrational::compare (const bigrational & a) const
632 {
633 	if (&a == this) {
634 		return 0;
635 	}
636 
637 	bigint r1, r2;
638 
639 	multiply(r1, num, a.den);
640 	multiply(r2, den, a.num);
641 	return r1.compare(r2);
642 }
643 
644 
645 
646 inline int
compare(const bigint & a)647 bigrational::compare (const bigint & a) const
648 {
649 	if (den.is_one()) {
650 		return num.compare(a);
651 	}
652 
653 	bigint r;
654 
655 	multiply(r, den, a);
656 	return num.compare(r);
657 }
658 
659 
660 
661 inline int
compare(unsigned long a)662 bigrational::compare (unsigned long a) const
663 {
664 	if (den.is_one()) {
665 		return num.compare(a);
666 	}
667 
668 	bigint r;
669 
670 	multiply(r, den, a);
671 	return num.compare(r);
672 }
673 
674 
675 
676 inline int
compare(long a)677 bigrational::compare (long a) const
678 {
679 	if (den.is_one()) {
680 		return num.compare(a);
681 	}
682 
683 	bigint r;
684 
685 	multiply(r, den, a);
686 	return num.compare(r);
687 }
688 
689 
690 
691 inline int
compare(const bigrational & a,const bigrational & b)692 compare (const bigrational & a, const bigrational & b)
693 {
694 	return a.compare(b);
695 }
696 
697 
698 
699 inline int
compare(const bigrational & a,const bigint & b)700 compare (const bigrational & a, const bigint & b)
701 {
702 	return a.compare(b);
703 }
704 
705 
706 
707 inline int
compare(const bigrational & a,unsigned long b)708 compare (const bigrational & a, unsigned long b)
709 {
710 	return a.compare(b);
711 }
712 
713 
714 
715 inline int
compare(const bigrational & a,long b)716 compare (const bigrational & a, long b)
717 {
718 	return a.compare(b);
719 }
720 
721 
722 
723 inline bool
724 operator == (const bigrational & a, const bigrational & b)
725 {
726 	return (a.compare(b) == 0);
727 }
728 
729 
730 
731 inline bool
732 operator == (const bigrational & a, const bigint & b)
733 {
734 	return (a.compare(b) == 0);
735 }
736 
737 
738 
739 inline bool
740 operator == (const bigrational & a, unsigned long b)
741 {
742 	return (a.compare(b) == 0);
743 }
744 
745 
746 
747 inline bool
748 operator == (const bigrational & a, long b)
749 {
750 	return (a.compare(b) == 0);
751 }
752 
753 
754 
755 inline bool
756 operator == (const bigrational & a, int b)
757 {
758 	return (a.compare(static_cast<long>(b)) == 0);
759 }
760 
761 
762 
763 inline bool
764 operator == (const bigint & a, const bigrational & b)
765 {
766 	return (b.compare(a) == 0);
767 }
768 
769 
770 
771 inline bool
772 operator == (const unsigned long a, const bigrational & b)
773 {
774 	return (b.compare(a) == 0);
775 }
776 
777 
778 
779 inline bool
780 operator == (const long a, const bigrational & b)
781 {
782 	return (b.compare(a) == 0);
783 }
784 
785 
786 
787 inline bool
788 operator == (const int a, const bigrational & b)
789 {
790 	return (b.compare(static_cast<long>(a)) == 0);
791 }
792 
793 
794 
795 inline bool
796 operator != (const bigrational & a, const bigrational & b)
797 {
798 	return (a.compare(b) != 0);
799 }
800 
801 
802 
803 inline bool
804 operator != (const bigrational & a, const bigint & b)
805 {
806 	return (a.compare(b) != 0);
807 }
808 
809 
810 
811 inline bool
812 operator != (const bigrational & a, unsigned long b)
813 {
814 	return (a.compare(b) != 0);
815 }
816 
817 
818 
819 inline bool
820 operator != (const bigrational & a, long b)
821 {
822 	return (a.compare(b) != 0);
823 }
824 
825 
826 
827 inline bool
828 operator != (const bigrational & a, int b)
829 {
830 	return (a.compare(static_cast<long>(b)) != 0);
831 }
832 
833 
834 
835 inline bool
836 operator != (const bigint & a, const bigrational & b)
837 {
838 	return (b.compare(a) != 0);
839 }
840 
841 
842 
843 inline bool
844 operator != (long a, const bigrational & b)
845 {
846 	return (b.compare(a) != 0);
847 }
848 
849 
850 
851 inline bool
852 operator != (unsigned long a, const bigrational & b)
853 {
854 	return (b.compare(a) != 0);
855 }
856 
857 
858 
859 inline bool
860 operator != (int a, const bigrational & b)
861 {
862 	return (b.compare(static_cast<long>(a)) != 0);
863 }
864 
865 
866 
867 inline bool
868 operator > (const bigrational & a, const bigrational & b)
869 {
870 	return (a.compare(b) > 0);
871 }
872 
873 
874 
875 inline bool
876 operator > (const bigrational & a, const bigint & b)
877 {
878 	return (a.compare(b) > 0);
879 }
880 
881 
882 
883 inline bool
884 operator > (const bigrational & a, long b)
885 {
886 	return (a.compare(b) > 0);
887 }
888 
889 
890 
891 inline bool
892 operator > (const bigrational & a, unsigned long b)
893 {
894 	return (a.compare(b) > 0);
895 }
896 
897 
898 
899 inline bool
900 operator > (const bigrational & a, int b)
901 {
902 	return (a.compare(static_cast<long>(b)) > 0);
903 }
904 
905 
906 
907 inline bool
908 operator > (const bigint & a, const bigrational & b)
909 {
910 	return (b.compare(a) < 0);
911 }
912 
913 
914 
915 inline bool
916 operator > (long a, const bigrational & b)
917 {
918 	return (b.compare(a) < 0);
919 }
920 
921 
922 
923 inline bool
924 operator > (unsigned long a, const bigrational & b)
925 {
926 	return (b.compare(a) < 0);
927 }
928 
929 
930 
931 inline bool
932 operator > (int a, const bigrational & b)
933 {
934 	return (b.compare(static_cast<long>(a)) < 0);
935 }
936 
937 
938 
939 inline bool
940 operator >= (const bigrational & a, const bigrational & b)
941 {
942 	return (a.compare(b) >= 0);
943 }
944 
945 
946 
947 inline bool
948 operator >= (const bigrational & a, const bigint & b)
949 {
950 	return (a.compare(b) >= 0);
951 }
952 
953 
954 
955 inline bool
956 operator >= (const bigrational & a, long b)
957 {
958 	return (a.compare(b) >= 0);
959 }
960 
961 
962 
963 inline bool
964 operator >= (const bigrational & a, unsigned long b)
965 {
966 	return (a.compare(b) >= 0);
967 }
968 
969 
970 
971 inline bool
972 operator >= (const bigrational & a, int b)
973 {
974 	return (a.compare(static_cast<long>(b)) >= 0);
975 }
976 
977 
978 
979 inline bool
980 operator >= (const bigint & a, const bigrational & b)
981 {
982 	return (b.compare(a) <= 0);
983 }
984 
985 
986 
987 inline bool
988 operator >= (long a, const bigrational & b)
989 {
990 	return (b.compare(a) <= 0);
991 }
992 
993 
994 
995 inline bool
996 operator >= (unsigned long a, const bigrational & b)
997 {
998 	return (b.compare(a) <= 0);
999 }
1000 
1001 
1002 
1003 inline bool
1004 operator >= (int a, const bigrational & b)
1005 {
1006 	return (b.compare(static_cast<long>(a)) <= 0);
1007 }
1008 
1009 
1010 
1011 inline bool
1012 operator < (const bigrational & a, const bigrational & b)
1013 {
1014 	return (a.compare(b) < 0);
1015 }
1016 
1017 
1018 
1019 inline bool
1020 operator < (const bigrational & a, const bigint & b)
1021 {
1022 	return (a.compare(b) < 0);
1023 }
1024 
1025 
1026 
1027 inline bool
1028 operator < (const bigrational & a, long b)
1029 {
1030 	return (a.compare(b) < 0);
1031 }
1032 
1033 
1034 
1035 inline bool
1036 operator < (const bigrational & a, unsigned long b)
1037 {
1038 	return (a.compare(b) < 0);
1039 }
1040 
1041 
1042 
1043 inline bool
1044 operator < (const bigrational & a, int b)
1045 {
1046 	return (a.compare(static_cast<long>(b)) < 0);
1047 }
1048 
1049 
1050 
1051 inline bool
1052 operator < (const bigint & a, const bigrational & b)
1053 {
1054 	return (b.compare(a) > 0);
1055 }
1056 
1057 
1058 
1059 inline bool
1060 operator < (long a, const bigrational & b)
1061 {
1062 	return (b.compare(a) > 0);
1063 }
1064 
1065 
1066 
1067 inline bool
1068 operator < (unsigned long a, const bigrational & b)
1069 {
1070 	return (b.compare(a) > 0);
1071 }
1072 
1073 
1074 
1075 inline bool
1076 operator < (int a, const bigrational & b)
1077 {
1078 	return (b.compare(static_cast<long>(a)) > 0);
1079 }
1080 
1081 
1082 
1083 inline bool
1084 operator <= (const bigrational & a, const bigrational & b)
1085 {
1086 	return (a.compare(b) <= 0);
1087 }
1088 
1089 
1090 
1091 inline bool
1092 operator <= (const bigrational & a, const bigint & b)
1093 {
1094 	return (a.compare(b) <= 0);
1095 }
1096 
1097 
1098 
1099 inline bool
1100 operator <= (const bigrational & a, long b)
1101 {
1102 	return (a.compare(b) <= 0);
1103 }
1104 
1105 
1106 
1107 inline bool
1108 operator <= (const bigrational & a, unsigned long b)
1109 {
1110 	return (a.compare(b) <= 0);
1111 }
1112 
1113 
1114 
1115 inline bool
1116 operator <= (const bigrational & a, int b)
1117 {
1118 	return (a.compare(static_cast<long>(b)) <= 0);
1119 }
1120 
1121 
1122 
1123 inline bool
1124 operator <= (const bigint & a, const bigrational & b)
1125 {
1126 	return (b.compare(a) >= 0);
1127 }
1128 
1129 
1130 
1131 inline bool
1132 operator <= (long a, const bigrational & b)
1133 {
1134 	return (b.compare(a) >= 0);
1135 }
1136 
1137 
1138 
1139 inline bool
1140 operator <= (unsigned long a, const bigrational & b)
1141 {
1142 	return (b.compare(a) >= 0);
1143 }
1144 
1145 
1146 
1147 inline bool
1148 operator <= (int a, const bigrational & b)
1149 {
1150 	return (b.compare(static_cast<long>(a)) >= 0);
1151 }
1152 
1153 
1154 
1155 inline int
sign()1156 bigrational::sign () const
1157 {
1158 	return num.sign();
1159 }
1160 
1161 
1162 
1163 inline bool
is_positive()1164 bigrational::is_positive () const
1165 {
1166 	return (num.sign() > 0);
1167 }
1168 
1169 
1170 
1171 inline bool
is_negative()1172 bigrational::is_negative () const
1173 {
1174 	return (num.sign() < 0);
1175 }
1176 
1177 
1178 
1179 inline bool
is_zero()1180 bigrational::is_zero () const
1181 {
1182 	return (num.sign() == 0);
1183 }
1184 
1185 
1186 
1187 inline bool
is_gt_zero()1188 bigrational::is_gt_zero () const
1189 {
1190 	return (num.sign() > 0);
1191 }
1192 
1193 
1194 
1195 inline bool
is_ge_zero()1196 bigrational::is_ge_zero () const
1197 {
1198 	return (num.sign() >= 0);
1199 }
1200 
1201 
1202 
1203 inline bool
is_lt_zero()1204 bigrational::is_lt_zero () const
1205 {
1206 	return (num.sign() < 0);
1207 }
1208 
1209 
1210 
1211 inline bool
is_le_zero()1212 bigrational::is_le_zero () const
1213 {
1214 	return (num.sign() <= 0);
1215 }
1216 
1217 
1218 
1219 inline bool
is_one()1220 bigrational::is_one () const
1221 {
1222 	return (num.is_one() && den.is_one());
1223 }
1224 
1225 
1226 
1227 //
1228 // converters
1229 //
1230 
1231 inline bool
intify(int & i)1232 bigrational::intify (int & i) const
1233 {
1234 	bigint I;
1235 
1236 	divide(I, num, den);
1237 	return I.intify(i);
1238 }
1239 
1240 
1241 
1242 inline bool
longify(long & i)1243 bigrational::longify (long & i) const
1244 {
1245 	bigint I;
1246 
1247 	divide(I, num, den);
1248 	return I.longify(i);
1249 }
1250 
1251 
1252 
1253 double dbl (const bigrational & a);
1254 
1255 
1256 
1257 //
1258 // predicates
1259 //
1260 
1261 inline bool
is_bigint(const bigrational & a)1262 is_bigint (const bigrational & a)
1263 {
1264 	return a.denominator().is_one();
1265 }
1266 
1267 
1268 
1269 //
1270 // modifiers
1271 //
1272 
1273 inline void
absolute_value()1274 bigrational::absolute_value ()
1275 {
1276 	num.absolute_value();
1277 }
1278 
1279 
1280 
1281 inline void
negate()1282 bigrational::negate ()
1283 {
1284 	num.negate();
1285 }
1286 
1287 
1288 
1289 inline void
multiply_by_denominator()1290 bigrational::multiply_by_denominator ()
1291 {
1292 	den.assign_one();
1293 }
1294 
1295 
1296 
1297 inline void
inc()1298 bigrational::inc ()
1299 {
1300 	add(num, num, den);
1301 }
1302 
1303 
1304 
1305 inline void
dec()1306 bigrational::dec ()
1307 {
1308 	subtract(num, num, den);
1309 }
1310 
1311 
1312 
1313 inline void
multiply_by_2()1314 bigrational::multiply_by_2 ()
1315 {
1316 	if (den.is_even()) {
1317 		den.divide_by_2();
1318 	}
1319 	else {
1320 		num.multiply_by_2();
1321 	}
1322 }
1323 
1324 
1325 
1326 inline void
divide_by_2()1327 bigrational::divide_by_2 ()
1328 {
1329 	if (num.is_even()) {
1330 		num.divide_by_2();
1331 	}
1332 	else {
1333 		den.multiply_by_2();
1334 	}
1335 }
1336 
1337 
1338 
1339 inline void
swap(bigrational & a)1340 bigrational::swap (bigrational & a)
1341 {
1342 	num.swap(a.num);
1343 	den.swap(a.den);
1344 }
1345 
1346 
1347 
1348 inline void
swap(bigrational & a,bigrational & b)1349 swap (bigrational & a, bigrational & b)
1350 {
1351 	a.swap(b);
1352 }
1353 
1354 
1355 
1356 //
1357 // procedural arithmetic
1358 //
1359 
1360 void add(bigrational & c, const bigrational & a, const bigrational & b);
1361 void subtract(bigrational & c, const bigrational & a, const bigrational & b);
1362 void multiply(bigrational & c, const bigrational & a, const bigrational & b);
1363 void divide(bigrational & c, const bigrational & a, const bigrational & b);
1364 
1365 void shift_left(bigrational & c, const bigrational & a, long ui);
1366 void shift_right(bigrational & c, const bigrational & a, long ui);
1367 
1368 
1369 
1370 inline void
negate(bigrational & a,const bigrational & b)1371 negate (bigrational & a, const bigrational & b)
1372 {
1373 	a.assign(b);
1374 	a.negate();
1375 }
1376 
1377 
1378 
1379 inline void
add(bigrational & c,const bigrational & a,const bigint & b)1380 add (bigrational & c, const bigrational & a, const bigint & b)
1381 {
1382 	bigint tmp;
1383 
1384 	multiply(tmp, a.den, b);
1385 	add(c.num, a.num, tmp);
1386 	c.den.assign(a.den);
1387 }
1388 
1389 
1390 
1391 inline void
add(bigrational & c,const bigrational & a,long b)1392 add (bigrational & c, const bigrational & a, long b)
1393 {
1394 	bigint tmp;
1395 
1396 	multiply(tmp, a.den, b);
1397 	add(c.num, a.num, tmp);
1398 	c.den.assign(a.den);
1399 }
1400 
1401 
1402 
1403 inline void
add(bigrational & c,const bigrational & a,unsigned long b)1404 add (bigrational & c, const bigrational & a, unsigned long b)
1405 {
1406 	bigint tmp;
1407 
1408 	multiply(tmp, a.den, b);
1409 	add(c.num, a.num, tmp);
1410 	c.den.assign(a.den);
1411 }
1412 
1413 
1414 
1415 inline void
add(bigrational & c,const bigrational & a,int b)1416 add (bigrational & c, const bigrational & a, int b)
1417 {
1418 	add(c, a, static_cast<long>(b));
1419 }
1420 
1421 
1422 
1423 inline void
subtract(bigrational & c,const bigrational & a,const bigint & b)1424 subtract (bigrational & c, const bigrational & a, const bigint & b)
1425 {
1426 	bigint tmp;
1427 
1428 	multiply(tmp, a.den, b);
1429 	subtract(c.num, a.num, tmp);
1430 	c.den.assign(a.den);
1431 }
1432 
1433 
1434 
1435 inline void
subtract(bigrational & c,const bigrational & a,long b)1436 subtract (bigrational & c, const bigrational & a, long b)
1437 {
1438 	bigint tmp;
1439 
1440 	multiply(tmp, a.den, b);
1441 	subtract(c.num, a.num, tmp);
1442 	c.den.assign(a.den);
1443 }
1444 
1445 
1446 
1447 inline void
subtract(bigrational & c,const bigrational & a,unsigned long b)1448 subtract (bigrational & c, const bigrational & a, unsigned long b)
1449 {
1450 	bigint tmp;
1451 
1452 	multiply(tmp, a.den, b);
1453 	subtract(c.num, a.num, tmp);
1454 	c.den.assign(a.den);
1455 }
1456 
1457 
1458 
1459 inline void
subtract(bigrational & c,const bigrational & a,int b)1460 subtract (bigrational & c, const bigrational & a, int b)
1461 {
1462 	subtract(c, a, static_cast<long>(b));
1463 }
1464 
1465 
1466 
1467 inline void
multiply(bigrational & c,const bigrational & a,const bigint & b)1468 multiply (bigrational & c, const bigrational & a, const bigint & b)
1469 {
1470 	multiply(c.num, a.num, b);
1471 	c.den.assign(a.den);
1472 	c.normalize();
1473 }
1474 
1475 
1476 
1477 inline void
multiply(bigrational & c,const bigrational & a,long b)1478 multiply (bigrational & c, const bigrational & a, long b)
1479 {
1480 	multiply(c.num, a.num, b);
1481 	c.den.assign(a.den);
1482 	c.normalize();
1483 }
1484 
1485 
1486 
1487 inline void
multiply(bigrational & c,const bigrational & a,unsigned long b)1488 multiply (bigrational & c, const bigrational & a, unsigned long b)
1489 {
1490 	multiply(c.num, a.num, b);
1491 	c.den.assign(a.den);
1492 	c.normalize();
1493 }
1494 
1495 
1496 
1497 inline void
multiply(bigrational & c,const bigrational & a,int b)1498 multiply (bigrational & c, const bigrational & a, int b)
1499 {
1500 	multiply(c, a, static_cast<long>(b));
1501 }
1502 
1503 
1504 
1505 inline void
square(bigrational & c,const bigrational & a)1506 square (bigrational & c, const bigrational & a)
1507 {
1508 	square(c.num, a.num);
1509 	square(c.den, a.den);
1510 }
1511 
1512 
1513 
1514 inline void
square(bigrational & c,const bigint & a)1515 square (bigrational & c, const bigint & a)
1516 {
1517 	square(c.num, a);
1518 	c.den.assign_one();
1519 }
1520 
1521 
1522 
1523 inline void
invert(bigrational & c,const bigrational & a)1524 invert (bigrational & c, const bigrational & a)
1525 {
1526 	c.assign(a);
1527 	c.invert();
1528 }
1529 
1530 
1531 
1532 inline void
invert(bigrational & c,const bigint & a)1533 invert (bigrational & c, const bigint & a)
1534 {
1535 	c.assign(1UL, a);
1536 }
1537 
1538 
1539 
1540 inline void
divide(bigrational & c,const bigrational & a,const bigint & b)1541 divide (bigrational & c, const bigrational & a, const bigint & b)
1542 {
1543 	c.num.assign(a.num);
1544 	multiply(c.den, a.den, b);
1545 	c.normalize();
1546 }
1547 
1548 
1549 
1550 inline void
divide(bigrational & c,const bigrational & a,long b)1551 divide (bigrational & c, const bigrational & a, long b)
1552 {
1553 	c.num.assign(a.num);
1554 	multiply(c.den, a.den, b);
1555 	c.normalize();
1556 }
1557 
1558 
1559 
1560 inline void
divide(bigrational & c,const bigrational & a,unsigned long b)1561 divide (bigrational & c, const bigrational & a, unsigned long b)
1562 {
1563 	c.num.assign(a.num);
1564 	multiply(c.den, a.den, b);
1565 	c.normalize();
1566 }
1567 
1568 
1569 
1570 inline void
divide(bigrational & c,const bigrational & a,int b)1571 divide (bigrational & c, const bigrational & a, int b)
1572 {
1573 	divide(c, a, static_cast<long>(b));
1574 }
1575 
1576 
1577 
1578 void power(bigrational & c, const bigrational & a, const bigint & b);
1579 void power(bigrational & c, const bigrational & a, long b);
1580 
1581 bigint round(const bigrational & a);
1582 bigint floor(const bigrational & a);
1583 bigint ceiling(const bigrational & a);
1584 bigint truncate(const bigrational & a);
1585 
1586 bool square_root(bigrational & root, const bigrational & s);
1587 bool cube_root(bigrational & root, const bigrational & s);
1588 
1589 
1590 
1591 //
1592 // functions
1593 //
1594 
1595 inline bigrational
abs(const bigrational & a)1596 abs (const bigrational & a)
1597 {
1598 	bigrational c(a);
1599 
1600 	c.absolute_value();
1601 	return c;
1602 }
1603 
1604 
1605 
1606 inline bigrational
inverse(const bigrational & a)1607 inverse (const bigrational & a)
1608 {
1609 	bigrational c(a);
1610 
1611 	c.invert();
1612 	return c;
1613 }
1614 
1615 
1616 
1617 //
1618 // operators
1619 //
1620 
1621 inline bigrational
1622 operator - (const bigrational & a)
1623 {
1624 	bigrational c(a);
1625 
1626 	c.negate();
1627 	return c;
1628 }
1629 
1630 
1631 
1632 inline bigrational
1633 operator + (const bigrational & a, const bigrational & b)
1634 {
1635 	bigrational c;
1636 
1637 	add(c, a, b);
1638 	return c;
1639 }
1640 
1641 
1642 
1643 inline bigrational
1644 operator + (const bigrational & a, int b)
1645 {
1646 	bigrational c;
1647 
1648 	add(c, a, static_cast<long>(b));
1649 	return c;
1650 }
1651 
1652 
1653 
1654 inline bigrational
1655 operator + (const bigrational & a, long b)
1656 {
1657 	bigrational c;
1658 
1659 	add(c, a, b);
1660 	return c;
1661 }
1662 
1663 
1664 
1665 inline bigrational
1666 operator + (const bigrational & a, const unsigned long b)
1667 {
1668 	bigrational c;
1669 
1670 	add(c, a, b);
1671 	return c;
1672 }
1673 
1674 
1675 
1676 inline bigrational
1677 operator + (const bigrational & a, const bigint & b)
1678 {
1679 	bigrational c;
1680 
1681 	add(c, a, b);
1682 	return c;
1683 }
1684 
1685 
1686 
1687 inline bigrational
1688 operator + (int a, const bigrational & b)
1689 {
1690 	bigrational c;
1691 
1692 	add(c, b, static_cast<long>(a));
1693 	return c;
1694 }
1695 
1696 
1697 
1698 inline bigrational
1699 operator + (long a, const bigrational & b)
1700 {
1701 	bigrational c;
1702 
1703 	add(c, b, a);
1704 	return c;
1705 }
1706 
1707 
1708 
1709 inline bigrational
1710 operator + (unsigned long a, const bigrational & b)
1711 {
1712 	bigrational c;
1713 
1714 	add(c, b, a);
1715 	return c;
1716 }
1717 
1718 
1719 
1720 inline bigrational
1721 operator + (const bigint & a, const bigrational & b)
1722 {
1723 	bigrational c;
1724 
1725 	add(c, b, a);
1726 	return c;
1727 }
1728 
1729 
1730 
1731 inline bigrational
1732 operator - (const bigrational & a, const bigrational & b)
1733 {
1734 	bigrational c;
1735 
1736 	subtract(c, a, b);
1737 	return c;
1738 }
1739 
1740 
1741 
1742 inline bigrational
1743 operator - (const bigrational & a, int b)
1744 {
1745 	bigrational c;
1746 
1747 	subtract(c, a, static_cast<long>(b));
1748 	return c;
1749 }
1750 
1751 
1752 
1753 inline bigrational
1754 operator - (const bigrational & a, long b)
1755 {
1756 	bigrational c;
1757 
1758 	subtract(c, a, b);
1759 	return c;
1760 }
1761 
1762 
1763 
1764 inline bigrational
1765 operator - (const bigrational & a, unsigned long b)
1766 {
1767 	bigrational c;
1768 
1769 	subtract(c, a, b);
1770 	return c;
1771 }
1772 
1773 
1774 
1775 inline bigrational
1776 operator - (const bigrational & a, const bigint & b)
1777 {
1778 	bigrational c;
1779 
1780 	subtract(c, a, b);
1781 	return c;
1782 }
1783 
1784 
1785 
1786 inline bigrational
1787 operator - (int a, const bigrational & b)
1788 {
1789 	bigrational c;
1790 
1791 	subtract(c, b, static_cast<long>(a));
1792 	c.negate();
1793 	return c;
1794 }
1795 
1796 
1797 
1798 inline bigrational
1799 operator - (long a, const bigrational & b)
1800 {
1801 	bigrational c;
1802 
1803 	subtract(c, b, a);
1804 	c.negate();
1805 	return c;
1806 }
1807 
1808 
1809 
1810 inline bigrational
1811 operator - (unsigned long a, const bigrational & b)
1812 {
1813 	bigrational c;
1814 
1815 	subtract(c, b, a);
1816 	c.negate();
1817 	return c;
1818 }
1819 
1820 
1821 
1822 inline bigrational
1823 operator - (const bigint & a, const bigrational & b)
1824 {
1825 	bigrational c;
1826 
1827 	subtract(c, b, a);
1828 	c.negate();
1829 	return c;
1830 }
1831 
1832 
1833 
1834 inline bigrational
1835 operator * (const bigrational & a, const bigrational & b)
1836 {
1837 	bigrational c;
1838 
1839 	multiply(c, a, b);
1840 	return c;
1841 }
1842 
1843 
1844 
1845 inline bigrational
1846 operator * (const bigrational & a, int b)
1847 {
1848 	bigrational c;
1849 
1850 	multiply(c, a, static_cast<long>(b));
1851 	return c;
1852 }
1853 
1854 
1855 
1856 inline bigrational
1857 operator * (const bigrational & a, long b)
1858 {
1859 	bigrational c;
1860 
1861 	multiply(c, a, b);
1862 	return c;
1863 }
1864 
1865 
1866 
1867 inline bigrational
1868 operator * (const bigrational & a, unsigned long b)
1869 {
1870 	bigrational c;
1871 
1872 	multiply(c, a, b);
1873 	return c;
1874 }
1875 
1876 
1877 
1878 inline bigrational
1879 operator * (const bigrational & a, const bigint & b)
1880 {
1881 	bigrational c;
1882 
1883 	multiply(c, a, b);
1884 	return c;
1885 }
1886 
1887 
1888 
1889 inline bigrational
1890 operator * (int a, const bigrational & b)
1891 {
1892 	bigrational c;
1893 
1894 	multiply(c, b, static_cast<long>(a));
1895 	return c;
1896 }
1897 
1898 
1899 
1900 inline bigrational
1901 operator * (long a, const bigrational & b)
1902 {
1903 	bigrational c;
1904 
1905 	multiply(c, b, a);
1906 	return c;
1907 }
1908 
1909 
1910 
1911 inline bigrational
1912 operator * (unsigned long a, const bigrational & b)
1913 {
1914 	bigrational c;
1915 
1916 	multiply(c, b, a);
1917 	return c;
1918 }
1919 
1920 
1921 
1922 inline bigrational
1923 operator * (const bigint & a, const bigrational & b)
1924 {
1925 	bigrational c;
1926 
1927 	multiply(c, b, a);
1928 	return c;
1929 }
1930 
1931 
1932 
1933 inline bigrational
1934 operator / (const bigrational & a, const bigrational & b)
1935 {
1936 	bigrational c;
1937 
1938 	divide(c, a, b);
1939 	return c;
1940 }
1941 
1942 
1943 
1944 inline bigrational
1945 operator / (const bigrational & a, int b)
1946 {
1947 	bigrational c;
1948 
1949 	divide(c, a, static_cast<long>(b));
1950 	return c;
1951 }
1952 
1953 
1954 
1955 inline bigrational
1956 operator / (const bigrational & a, long b)
1957 {
1958 	bigrational c;
1959 
1960 	divide(c, a, b);
1961 	return c;
1962 }
1963 
1964 
1965 
1966 inline bigrational
1967 operator / (const bigrational & a, unsigned long b)
1968 {
1969 	bigrational c;
1970 
1971 	divide(c, a, b);
1972 	return c;
1973 }
1974 
1975 
1976 
1977 inline bigrational
1978 operator / (const bigrational & a, const bigint & b)
1979 {
1980 	bigrational c;
1981 
1982 	divide(c, a, b);
1983 	return c;
1984 }
1985 
1986 
1987 
1988 inline bigrational
1989 operator / (int a, const bigrational & b)
1990 {
1991 	bigrational c;
1992 
1993         if(a == 0) {
1994           if(b.is_zero()) {
1995             lidia_error_handler("bigrational", "operator/()::division by zero.");
1996           }
1997           c.assign_zero();
1998         }
1999         else {
2000           divide(c, b, static_cast<long>(a));
2001           c.invert();
2002         }
2003 	return c;
2004 }
2005 
2006 
2007 
2008 inline bigrational
2009 operator / (long a, const bigrational & b)
2010 {
2011 	bigrational c;
2012 
2013         if(a == 0) {
2014           if(b.is_zero()) {
2015             lidia_error_handler("bigrational", "operator/()::division by zero.");
2016           }
2017           c.assign_zero();
2018         }
2019         else {
2020           divide(c, b, a);
2021           c.invert();
2022         }
2023 	return c;
2024 }
2025 
2026 
2027 
2028 inline bigrational
2029 operator / (unsigned long a, const bigrational & b)
2030 {
2031 	bigrational c;
2032 
2033         if(a == 0) {
2034           if(b.is_zero()) {
2035             lidia_error_handler("bigrational", "operator/()::division by zero.");
2036           }
2037           c.assign_zero();
2038         }
2039         else {
2040           divide(c, b, a);
2041           c.invert();
2042         }
2043 	return c;
2044 }
2045 
2046 
2047 
2048 inline bigrational
2049 operator / (const bigint & a, const bigrational & b)
2050 {
2051 	bigrational c;
2052 
2053         if(a == 0) {
2054           if(b.is_zero()) {
2055             lidia_error_handler("bigrational", "operator/()::division by zero.");
2056           }
2057           c.assign_zero();
2058         }
2059         else {
2060           divide(c, b, a);
2061           c.invert();
2062         }
2063 	return c;
2064 }
2065 
2066 
2067 
2068 inline bigrational
2069 operator << (const bigrational & a, unsigned long ui)
2070 {
2071 	bigrational c;
2072 
2073 	shift_left(c, a, ui);
2074 	return c;
2075 }
2076 
2077 
2078 
2079 inline bigrational
2080 operator >> (const bigrational & a, unsigned long ui)
2081 {
2082 	bigrational c;
2083 
2084 	shift_right(c, a, ui);
2085 	return c;
2086 }
2087 
2088 
2089 
2090 inline bigrational &
2091 operator += (bigrational & a, const bigrational & b)
2092 {
2093 	add(a, a, b);
2094 	return a;
2095 }
2096 
2097 
2098 
2099 inline bigrational &
2100 operator += (bigrational & a, int b)
2101 {
2102 	add(a, a, static_cast<long>(b));
2103 	return a;
2104 }
2105 
2106 
2107 
2108 inline bigrational &
2109 operator += (bigrational & a, long b)
2110 {
2111 	add(a, a, b);
2112 	return a;
2113 }
2114 
2115 
2116 
2117 inline bigrational &
2118 operator += (bigrational & a, unsigned long b)
2119 {
2120 	add(a, a, b);
2121 	return a;
2122 }
2123 
2124 
2125 
2126 inline bigrational &
2127 operator += (bigrational & a, const bigint & b)
2128 {
2129 	add(a, a, b);
2130 	return a;
2131 }
2132 
2133 
2134 
2135 inline bigrational &
2136 operator -= (bigrational & a, const bigrational & b)
2137 {
2138 	subtract(a, a, b);
2139 	return a;
2140 }
2141 
2142 
2143 
2144 inline bigrational &
2145 operator -= (bigrational & a, int b)
2146 {
2147 	subtract(a, a, static_cast<long>(b));
2148 	return a;
2149 }
2150 
2151 
2152 
2153 inline bigrational &
2154 operator -= (bigrational & a, long b)
2155 {
2156 	subtract(a, a, b);
2157 	return a;
2158 }
2159 
2160 
2161 
2162 inline bigrational &
2163 operator -= (bigrational & a, unsigned long b)
2164 {
2165 	subtract(a, a, b);
2166 	return a;
2167 }
2168 
2169 
2170 
2171 inline bigrational &
2172 operator -= (bigrational & a, const bigint & b)
2173 {
2174 	subtract(a, a, b);
2175 	return a;
2176 }
2177 
2178 
2179 
2180 inline bigrational &
2181 operator *= (bigrational & a, const bigrational & b)
2182 {
2183 	multiply(a, a, b);
2184 	return a;
2185 }
2186 
2187 
2188 
2189 inline bigrational &
2190 operator *= (bigrational & a, int b)
2191 {
2192 	multiply(a, a, static_cast<long>(b));
2193 	return a;
2194 }
2195 
2196 
2197 
2198 inline bigrational &
2199 operator *= (bigrational & a, long b)
2200 {
2201 	multiply(a, a, b);
2202 	return a;
2203 }
2204 
2205 
2206 
2207 inline bigrational &
2208 operator *= (bigrational & a, unsigned long b)
2209 {
2210 	multiply(a, a, b);
2211 	return a;
2212 }
2213 
2214 
2215 
2216 inline bigrational &
2217 operator *= (bigrational & a, const bigint & b)
2218 {
2219 	multiply(a, a, b);
2220 	return a;
2221 }
2222 
2223 
2224 
2225 inline bigrational &
2226 operator /= (bigrational & a, const bigrational & b)
2227 {
2228 	divide(a, a, b);
2229 	return a;
2230 }
2231 
2232 
2233 
2234 inline bigrational &
2235 operator /= (bigrational & a, int b)
2236 {
2237 	divide(a, a, static_cast<long>(b));
2238 	return a;
2239 }
2240 
2241 
2242 
2243 inline bigrational &
2244 operator /= (bigrational & a, long b)
2245 {
2246 	divide(a, a, b);
2247 	return a;
2248 }
2249 
2250 
2251 
2252 inline bigrational &
2253 operator /= (bigrational & a, unsigned long b)
2254 {
2255 	divide(a, a, b);
2256 	return a;
2257 }
2258 
2259 
2260 
2261 inline bigrational &
2262 operator /= (bigrational & a, const bigint & b)
2263 {
2264 	divide(a, a, b);
2265 	return a;
2266 }
2267 
2268 
2269 
2270 inline bigrational &
2271 operator <<= (bigrational & a, unsigned long ui)
2272 {
2273 	shift_left(a, a, ui);
2274 	return a;
2275 }
2276 
2277 
2278 
2279 inline bigrational &
2280 operator >>= (bigrational & a, unsigned long ui)
2281 {
2282 	shift_right(a, a, ui);
2283 	return a;
2284 }
2285 
2286 
2287 
2288 inline bigrational &
2289 operator ++ (bigrational & a)
2290 {
2291 	a.inc();
2292 	return a;
2293 }
2294 
2295 
2296 
2297 inline bigrational
2298 operator ++ (bigrational & a, int)
2299 {
2300 	bigrational c(a);
2301 
2302 	a.inc();
2303 	return c;
2304 }
2305 
2306 
2307 
2308 inline bigrational &
2309 operator -- (bigrational & a)
2310 {
2311 	a.dec();
2312 	return a;
2313 }
2314 
2315 
2316 
2317 inline bigrational
2318 operator -- (bigrational & a, int)
2319 {
2320 	bigrational c(a);
2321 
2322 	a.dec();
2323 	return c;
2324 }
2325 
2326 
2327 
2328 inline bool
2329 operator ! (const bigrational & a)
2330 {
2331 	return a.is_zero();
2332 }
2333 
2334 
2335 
2336 //
2337 // procedural modifiers
2338 //
2339 
2340 inline void
multiply_by_two(bigrational & c,const bigrational & a)2341 multiply_by_two (bigrational & c, const bigrational & a)
2342 {
2343 	c.assign(a);
2344 	c.multiply_by_2();
2345 }
2346 
2347 
2348 
2349 inline void
divide_by_two(bigrational & c,const bigrational & a)2350 divide_by_two (bigrational & c, const bigrational & a)
2351 {
2352 	c.assign(a);
2353 	c.divide_by_2();
2354 }
2355 
2356 
2357 
2358 inline void
inc(bigrational & c)2359 inc (bigrational & c)
2360 {
2361 	c.inc();
2362 }
2363 
2364 
2365 
2366 inline void
dec(bigrational & c)2367 dec (bigrational & c)
2368 {
2369 	c.dec();
2370 }
2371 
2372 
2373 
2374 //
2375 // I/O and converters
2376 //
2377 
2378 std::istream & operator >> (std::istream & in, bigrational & a);
2379 std::ostream & operator << (std::ostream & out, const bigrational & a);
2380 
2381 int string_to_bigrational(char *s, char *t, bigrational & a);
2382 int bigrational_to_string(const bigrational & a, char *s, char *t);
2383 
2384 
2385 
2386 //
2387 // some properties of Q
2388 //
2389 
2390 inline bigint
characteristic()2391 bigrational::characteristic ()
2392 {
2393 	return 0UL;
2394 }
2395 
2396 
2397 
2398 #ifdef LIDIA_NAMESPACE
2399 }	// end of namespace LiDIA
2400 # undef IN_NAMESPACE_LIDIA
2401 #endif
2402 
2403 
2404 
2405 #define LIDIA_CLASS_BIGRATIONAL
2406 
2407 #include	"LiDIA/specialization/bigrational.special"
2408 
2409 
2410 
2411 #endif	// LIDIA_BIGRATIONAL_H_GUARD_
2412