prestige

Rational

Apr 3rd, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import java.math.BigInteger;
  2.  
  3. public class Rational implements Comparable<Rational> {
  4. BigInteger num;
  5. BigInteger den;
  6.  
  7. public static final Rational ZERO = new Rational(0);
  8. public static final Rational ONE = new Rational(1);
  9. public static final Rational TWO = new Rational(2);
  10. public static final Rational POSITIVE_INFINITY = new Rational(1, 0);
  11. public static final Rational NEGATIVE_INFINITY = new Rational(-1, 0);
  12.  
  13. public Rational(long num) {
  14. this.num = BigInteger.valueOf(num);
  15. this.den = BigInteger.ONE;
  16. }
  17.  
  18. public Rational(long num, long den) {
  19. this(BigInteger.valueOf(num), BigInteger.valueOf(den));
  20. }
  21.  
  22. public Rational(BigInteger num, BigInteger den) {
  23. this.num = num;
  24. this.den = den;
  25. reduce();
  26. }
  27.  
  28. void reduce() {
  29. if (!den.abs().equals(BigInteger.ONE)) {
  30. BigInteger gcd = num.gcd(den);
  31. if (gcd.signum() != 0 && !gcd.equals(BigInteger.ONE)) {
  32. num = num.divide(gcd);
  33. den = den.divide(gcd);
  34. }
  35. }
  36. if (den.signum() < 0 || den.signum() == 0 && num.signum() < 0) {
  37. num = num.negate();
  38. den = den.negate();
  39. }
  40. }
  41.  
  42. public Rational add(Rational r) {
  43. return new Rational(num.multiply(r.den).add(r.num.multiply(den)), den.multiply(r.den));
  44. }
  45.  
  46. public Rational sub(Rational r) {
  47. return new Rational(num.multiply(r.den).subtract(r.num.multiply(den)), den.multiply(r.den));
  48. }
  49.  
  50. public Rational mul(Rational r) {
  51. return new Rational(num.multiply(r.num), den.multiply(r.den));
  52. }
  53.  
  54. public Rational div(Rational r) {
  55. return new Rational(num.multiply(r.den), den.multiply(r.num));
  56. }
  57.  
  58. public Rational negate() {
  59. return new Rational(num.negate(), den);
  60. }
  61.  
  62. public Rational inverse() {
  63. return new Rational(den, num);
  64. }
  65.  
  66. public Rational abs() {
  67. return new Rational(num.abs(), den);
  68. }
  69.  
  70. public int signum() {
  71. return num.signum();
  72. }
  73.  
  74. public double doubleValue() {
  75. return num.doubleValue() / den.doubleValue();
  76. }
  77.  
  78. public double longValue() {
  79. return num.longValue() / den.longValue();
  80. }
  81.  
  82. public int compareTo(Rational other) {
  83. return (num.multiply(other.den).compareTo(other.num.multiply(den)));
  84. }
  85.  
  86. public boolean equals(Object obj) {
  87. return num.equals(((Rational) obj).num) && den.equals(((Rational) obj).den);
  88. }
  89.  
  90. public int hashCode() {
  91. return num.hashCode() * 31 + den.hashCode();
  92. }
  93.  
  94. public String toString() {
  95. return num + "/" + den;
  96. }
  97.  
  98. // Usage example
  99. public static void main(String[] args) {
  100. Rational a = new Rational(1, 3);
  101. Rational b = new Rational(1, 6);
  102. Rational c = new Rational(1, 2);
  103. System.out.println(true == c.equals(a.add(b)));
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment