File: BigRational.java

package info (click to toggle)
libpj-java 0.0~20150107%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,396 kB
  • sloc: java: 99,543; ansic: 987; sh: 153; xml: 26; makefile: 10; sed: 4
file content (728 lines) | stat: -rw-r--r-- 19,283 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
//******************************************************************************
//
// File:    BigRational.java
// Package: edu.rit.numeric
// Unit:    Class edu.rit.numeric.BigRational
//
// This Java source file is copyright (C) 2009 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************

package edu.rit.numeric;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * Class BigRational provides an arbitrary precision rational number. An
 * arbitrary precision rational number is the ratio of two arbitrary precision
 * integers (type java.math.BigInteger). Operations are provided for exact
 * arithmetic with rational numbers.
 * <P>
 * A rational number is said to be <B>normalized</B> if
 * GCD(numerator,denominator) = 1. The methods below do <B><I>not</I></B>
 * automatically normalize the rational number. Thus, the numerator and
 * denominator tend to get larger and larger as operations are performed on the
 * rational number. To reduce the numerator and denominator to lowest terms
 * again, call the <TT>normalize()</TT> method. It is up to you to decide
 * whether to normalize the rational number after each operation, or after a
 * series of operations.
 * <P>
 * Class BigRational provides the <TT>equals()</TT> and <TT>hashCode()</TT>
 * methods, and BigRational objects can be used as keys in hashed data
 * structures. However, BigRational objects are mutable. If a BigRational object
 * is used as a hash key, be sure not to change its value.
 * <P>
 * Class BigRational is not multiple thread safe.
 *
 * @author  Alan Kaminsky
 * @version 31-Dec-2009
 */
public class BigRational
	extends Number
	implements Comparable<BigRational>
	{

// Hidden data members.

	private BigInteger numer;
	private BigInteger denom;

// Exported constructors.

	/**
	 * Construct a new rational number. Its value is 0.
	 */
	public BigRational()
		{
		numer = BigInteger.ZERO;
		denom = BigInteger.ONE;
		}

	/**
	 * Construct a new rational number. Its value is <TT>x</TT>.
	 *
	 * @param  x  Long integer.
	 */
	public BigRational
		(long x)
		{
		assign (x);
		}

	/**
	 * Construct a new rational number. Its value is <TT>n/d</TT>.
	 *
	 * @param  n  Numerator.
	 * @param  d  Denominator.
	 *
	 * @exception  ArithmeticException
	 *     (unchecked exception) Thrown if <TT>d</TT> is 0.
	 */
	public BigRational
		(long n,
		 long d)
		{
		assign (n, d);
		}

	/**
	 * Construct a new rational number. Its value is <TT>x</TT>.
	 *
	 * @param  x  Big integer.
	 */
	public BigRational
		(BigInteger x)
		{
		assign (x);
		}

	/**
	 * Construct a new rational number. Its value is <TT>n/d</TT>.
	 *
	 * @param  n  Numerator.
	 * @param  d  Denominator.
	 *
	 * @exception  ArithmeticException
	 *     (unchecked exception) Thrown if <TT>d</TT> is 0.
	 */
	public BigRational
		(BigInteger n,
		 BigInteger d)
		{
		assign (n, d);
		}

	/**
	 * Construct a new rational number. Its value is <TT>x</TT>.
	 *
	 * @param  x  Rational number.
	 */
	public BigRational
		(BigRational x)
		{
		assign (x);
		}

	/**
	 * Construct a new rational number. Its value comes from the string
	 * <TT>s</TT>. The string must obey this syntax: optional minus sign
	 * (<TT>-</TT>), numerator (decimal integer), slash (<TT>/</TT>),
	 * denominator (decimal integer). The slash-and-denominator is optional. If
	 * present, the denominator must be greater than 0. There is no whitespace
	 * within the string.
	 *
	 * @param  s  String.
	 *
	 * @exception  NumberFormatException
	 *     (unchecked exception) Thrown if <TT>s</TT> cannot be parsed into a
	 *     rational number.
	 */
	public BigRational
		(String s)
		{
		assign (s);
		}

// Exported operations.

	/**
	 * Returns this rational number's numerator.
	 *
	 * @return  Numerator.
	 */
	public BigInteger numerator()
		{
		return numer;
		}

	/**
	 * Returns this rational number's denominator.
	 *
	 * @return  Denominator.
	 */
	public BigInteger denominator()
		{
		return denom;
		}

	/**
	 * Set this rational number to the given number.
	 *
	 * @param  x  Long integer.
	 *
	 * @return  This rational number, with its value set to <TT>x</TT>.
	 */
	public BigRational assign
		(long x)
		{
		this.numer = BigInteger.valueOf (x);
		this.denom = BigInteger.ONE;
		return this;
		}

	/**
	 * Set this rational number to the given fraction.
	 *
	 * @param  n  Numerator.
	 * @param  d  Denominator.
	 *
	 * @return  This rational number, with its value set to <TT>n/d</TT>.
	 *
	 * @exception  ArithmeticException
	 *     (unchecked exception) Thrown if <TT>d</TT> is 0.
	 */
	public BigRational assign
		(long n,
		 long d)
		{
		if (d == 0)
			{
			throw new ArithmeticException
				("BigRational.assign(): Zero denominator");
			}
		this.numer = BigInteger.valueOf (n);
		this.denom = BigInteger.valueOf (d);
		return this;
		}

	/**
	 * Set this rational number to the given number.
	 *
	 * @param  x  Big integer.
	 *
	 * @return  This rational number, with its value set to <TT>x</TT>.
	 */
	public BigRational assign
		(BigInteger x)
		{
		this.numer = x;
		this.denom = BigInteger.ONE;
		return this;
		}

	/**
	 * Set this rational number to the given fraction.
	 *
	 * @param  n  Numerator.
	 * @param  d  Denominator.
	 *
	 * @return  This rational number, with its value set to <TT>n/d</TT>.
	 *
	 * @exception  ArithmeticException
	 *     (unchecked exception) Thrown if <TT>d</TT> is 0.
	 */
	public BigRational assign
		(BigInteger n,
		 BigInteger d)
		{
		if (d.equals (BigInteger.ZERO))
			{
			throw new ArithmeticException
				("BigRational.assign(): Zero denominator");
			}
		this.numer = n;
		this.denom = d;
		return this;
		}

	/**
	 * Set this rational number to the given number.
	 *
	 * @param  x  Rational number.
	 *
	 * @return  This rational number, with its value set to <TT>x</TT>.
	 */
	public BigRational assign
		(BigRational x)
		{
		this.numer = x.numer;
		this.denom = x.denom;
		return this;
		}

	/**
	 * Set this rational number to the value parsed from the given string. The
	 * string must obey this syntax: optional minus sign (<TT>-</TT>), numerator
	 * (decimal integer), slash (<TT>/</TT>), denominator (decimal integer). The
	 * slash-and-denominator is optional. If present, the denominator must be
	 * greater than 0. There is no whitespace within the string.
	 *
	 * @param  s  String.
	 *
	 * @return  This rational number, with its value set to <TT>s</TT>.
	 *
	 * @exception  NumberFormatException
	 *     (unchecked exception) Thrown if <TT>s</TT> cannot be parsed into a
	 *     rational number.
	 */
	public BigRational assign
		(String s)
		{
		BigInteger numer, denom;
		int iSlash = s.indexOf ('/');
		if (iSlash == -1)
			{
			// No denominator.
			numer = new BigInteger (s);
			denom = BigInteger.ONE;
			}
		else if (iSlash+1 < s.length())
			{
			// Slash and denominator.
			numer = new BigInteger (s.substring (0, iSlash));
			denom = new BigInteger (s.substring (iSlash+1));
			if (denom.compareTo (BigInteger.ZERO) <= 0)
				{
				throw new NumberFormatException
					("BigRational.assign(): Negative denominator not allowed");
				}
			}
		else
			{
			// Slash but no denominator.
			throw new NumberFormatException
				("BigRational.assign(): Missing denominator after /");
			}
		this.numer = numer;
		this.denom = denom;
		return this;
		}

	/**
	 * Set this rational number to the integer part of itself.
	 *
	 * @return  This rational number, with its value set to <TT>int(this)</TT>.
	 */
	public BigRational intPart()
		{
		numer = numer.divide (denom);
		denom = BigInteger.ONE;
		return this;
		}

	/**
	 * Set this rational number to the fractional part of itself.
	 *
	 * @return  This rational number, with its value set to <TT>frac(this)</TT>.
	 */
	public BigRational fracPart()
		{
		numer = numer.remainder (denom);
		return this;
		}

	/**
	 * Set this rational number to the sum of itself and the given number.
	 *
	 * @param  x  Rational number.
	 *
	 * @return  This rational number, with its value set to <TT>this+x</TT>.
	 */
	public BigRational add
		(BigRational x)
		{
		this.numer =
			this.numer.multiply(x.denom).add
				(x.numer.multiply (this.denom));
		this.denom = this.denom.multiply (x.denom);
		return this;
		}

	/**
	 * Set this rational number to the difference of itself and the given
	 * number.
	 *
	 * @param  x  Rational number.
	 *
	 * @return  This rational number, with its value set to <TT>this-x</TT>.
	 */
	public BigRational sub
		(BigRational x)
		{
		this.numer =
			this.numer.multiply(x.denom).subtract
				(x.numer.multiply (this.denom));
		this.denom = this.denom.multiply (x.denom);
		return this;
		}

	/**
	 * Set this rational number to the product of itself and the given number.
	 *
	 * @param  x  Rational number.
	 *
	 * @return  This rational number, with its value set to <TT>this*x</TT>.
	 */
	public BigRational mul
		(BigRational x)
		{
		this.numer = this.numer.multiply (x.numer);
		this.denom = this.denom.multiply (x.denom);
		return this;
		}

	/**
	 * Set this rational number to the quotient of itself and the given number.
	 *
	 * @param  x  Rational number.
	 *
	 * @return  This rational number, with its value set to <TT>this/x</TT>.
	 *
	 * @exception  ArithmeticException
	 *     (unchecked exception) Thrown if <TT>x</TT> is 0.
	 */
	public BigRational div
		(BigRational x)
		{
		if (x.numer.equals (BigInteger.ZERO))
			{
			throw new ArithmeticException
				("BigRational.div(): Divide by zero");
			}
		this.numer = this.numer.multiply (x.denom);
		this.denom = this.denom.multiply (x.numer);
		return this;
		}

	/**
	 * Set this rational number to the remainder when divided by the given
	 * number.
	 *
	 * @param  x  Rational number.
	 *
	 * @return  This rational number, with its value set to
	 *          <TT>frac(this/x)*x</TT>.
	 *
	 * @exception  ArithmeticException
	 *     (unchecked exception) Thrown if <TT>x</TT> is 0.
	 */
	public BigRational rem
		(BigRational x)
		{
		return div (x) .fracPart() .mul (x);
		}

	/**
	 * Increment this rational number.
	 *
	 * @return  This rational number, with its value set to <TT>this+1</TT>.
	 */
	public BigRational incr()
		{
		this.numer = this.numer.add (this.denom);
		return this;
		}

	/**
	 * Decrement this rational number.
	 *
	 * @return  This rational number, with its value set to <TT>this-1</TT>.
	 */
	public BigRational decr()
		{
		this.numer = this.numer.subtract (this.denom);
		return this;
		}

	/**
	 * Set this rational number to the absolute value of itself.
	 *
	 * @return  This rational number, with its value set to <TT>abs(this)</TT>.
	 */
	public BigRational abs()
		{
		this.numer = this.numer.abs();
		this.denom = this.denom.abs();
		return this;
		}

	/**
	 * Set this rational number to the negative of itself.
	 *
	 * @return  This rational number, with its value set to <TT>-this</TT>.
	 */
	public BigRational negate()
		{
		this.numer = this.numer.negate();
		return this;
		}

	/**
	 * Set this rational number to the reciprocal of itself.
	 *
	 * @return  This rational number, with its value set to <TT>1/this</TT>.
	 *
	 * @exception  ArithmeticException
	 *     (unchecked exception) Thrown if this rational number is 0.
	 */
	public BigRational recip()
		{
		if (this.numer.equals (BigInteger.ZERO))
			{
			throw new ArithmeticException
				("BigRational.recip(): Divide by zero");
			}
		BigInteger tmp = this.numer;
		this.numer = this.denom;
		this.denom = tmp;
		return this;
		}

	/**
	 * Set this rational number to the smaller of itself and the given number.
	 *
	 * @param  x  Rational number.
	 *
	 * @return  This rational number, with its value set to
	 *          <TT>min(this,x)</TT>.
	 */
	public BigRational min
		(BigRational x)
		{
		if (this.compareTo (x) > 0) this.assign (x);
		return this;
		}

	/**
	 * Set this rational number to the larger of itself and the given number.
	 *
	 * @param  x  Rational number.
	 *
	 * @return  This rational number, with its value set to
	 *          <TT>max(this,x)</TT>.
	 */
	public BigRational max
		(BigRational x)
		{
		if (this.compareTo (x) < 0) this.assign (x);
		return this;
		}

	/**
	 * Normalize this rational number. Afterwards, the denominator is greater
	 * than or equal to 1, and the denominator does not divide the numerator; in
	 * other words, GCD(numerator,denominator) = 1.
	 *
	 * @return  This rational number, normalized.
	 */
	public BigRational normalize()
		{
		int sign = numer.signum() * denom.signum();
		numer = numer.abs();
		denom = denom.abs();
		BigInteger gcd = numer.gcd (denom);
		numer = numer.divide (gcd);
		denom = denom.divide (gcd);
		if (sign < 0) numer = numer.negate();
		return this;
		}

	/**
	 * Converts this rational number to an integer.
	 *
	 * @return  Integer value of this rational number.
	 */
	public int intValue()
		{
		BigDecimal n = new BigDecimal (numer);
		BigDecimal d = new BigDecimal (denom);
		return n.divide (d, 1, RoundingMode.HALF_UP) .intValue();
		}

	/**
	 * Converts this rational number to a long integer.
	 *
	 * @return  Long integer value of this rational number.
	 */
	public long longValue()
		{
		BigDecimal n = new BigDecimal (numer);
		BigDecimal d = new BigDecimal (denom);
		return n.divide (d, 1, RoundingMode.HALF_UP) .longValue();
		}

	/**
	 * Converts this rational number to a single precision floating point
	 * number.
	 *
	 * @return  Single precision floating point value of this rational number.
	 */
	public float floatValue()
		{
		BigDecimal n = new BigDecimal (numer);
		BigDecimal d = new BigDecimal (denom);
		return n.divide (d, MathContext.DECIMAL32) .floatValue();
		}

	/**
	 * Converts this rational number to a double precision floating point
	 * number.
	 *
	 * @return  Double precision floating point value of this rational number.
	 */
	public double doubleValue()
		{
		BigDecimal n = new BigDecimal (numer);
		BigDecimal d = new BigDecimal (denom);
		return n.divide (d, MathContext.DECIMAL64) .doubleValue();
		}

	/**
	 * Compare this rational number to the given rational number.
	 *
	 * @param  x  Rational number to compare.
	 *
	 * @return  A number less than, equal to, or greater than 0 if this rational
	 *          number is less than, equal to, or greater than <TT>x</TT>,
	 *          respectively.
	 */
	public int compareTo
		(BigRational x)
		{
		BigInteger diff =
			this.numer.multiply(x.denom).subtract
				(x.numer.multiply (this.denom));
		return diff.compareTo (BigInteger.ZERO);
		}

	/**
	 * Determine if this rational number equals the given object.
	 * <P>
	 * <I>Note:</I> Class BigRational provides the <TT>equals()</TT> and
	 * <TT>hashCode()</TT> methods, and BigRational objects can be used as keys
	 * in hashed data structures. However, BigRational objects are mutable. If a
	 * BigRational object is used as a hash key, be sure not to change its
	 * value.
	 * <P>
	 * <I>Note:</I> Two rational numbers are equal only if their numerators and
	 * denominators are equal. Thus, it is possible for <TT>equals()</TT> to
	 * return false even if the two rational numbers have the same numerical
	 * value. To ensure that <TT>equals()</TT> will return true if the two
	 * rational numbers have the same numerical value, normalize the two
	 * rational numbers first.
	 *
	 * @param  obj  Object to compare.
	 *
	 * @return  True if this rational number equals <TT>obj</TT>, false
	 *          otherwise.
	 */
	public boolean equals
		(Object obj)
		{
		return
			(obj instanceof BigRational) &&
			(((BigRational) obj).numer.equals (this.numer)) &&
			(((BigRational) obj).denom.equals (this.denom));
		}

	/**
	 * Returns a hash code for this rational number.
	 * <P>
	 * <I>Note:</I> Class BigRational provides the <TT>equals()</TT> and
	 * <TT>hashCode()</TT> methods, and BigRational objects can be used as keys
	 * in hashed data structures. However, BigRational objects are mutable. If a
	 * BigRational object is used as a hash key, be sure not to change its
	 * value.
	 * <P>
	 * <I>Note:</I> Two rational numbers have the same hash code only if their
	 * numerators and denominators are equal. Thus, it is possible for
	 * <TT>hashCode()</TT> to return different values even if the two rational
	 * numbers have the same numerical value. To ensure that <TT>hashCode()</TT>
	 * will return the same value if the two rational numbers have the same
	 * numerical value, normalize the two rational numbers first.
	 *
	 * @return  Hash code.
	 */
	public int hashCode()
		{
		return numer.hashCode()*31 + denom.hashCode();
		}

	/**
	 * Returns a string version of this rational number. If this rational
	 * number's denominator is 1, the string is in the form
	 * <TT>"&lt;numer&gt;"</TT>, otherwise the string is in the form
	 * <TT>"&lt;numer&gt;/&lt;denom&gt;"</TT>. The negative sign, if any, is
	 * attached to the numerator.
	 * <P>
	 * <I>Note:</I> The <TT>toString()</TT> method yields the numerator and
	 * denominator as they are, without normalizing this rational number.
	 */
	public String toString()
		{
		String ns = this.numer.toString();
		String ds = this.denom.toString();
		if (ns.charAt(0) == '-' && ds.charAt(0) == '-')
			{
			ns = ns.substring (1);
			ds = ds.substring (1);
			}
		else if (ns.charAt(0) != '-' && ds.charAt(0) == '-')
			{
			ns = "-" + ns;
			ds = ds.substring (1);
			}
		if (ds.equals ("1"))
			{
			return ns;
			}
		else
			{
			return ns + "/" + ds;
			}
		}

	}