annotate src/clojure/lang/Ratio.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 /**
rlm@10 2 * Copyright (c) Rich Hickey. All rights reserved.
rlm@10 3 * The use and distribution terms for this software are covered by the
rlm@10 4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 5 * which can be found in the file epl-v10.html at the root of this distribution.
rlm@10 6 * By using this software in any fashion, you are agreeing to be bound by
rlm@10 7 * the terms of this license.
rlm@10 8 * You must not remove this notice, or any other, from this software.
rlm@10 9 **/
rlm@10 10
rlm@10 11 /* rich Mar 31, 2008 */
rlm@10 12
rlm@10 13 package clojure.lang;
rlm@10 14
rlm@10 15 import java.math.BigInteger;
rlm@10 16 import java.math.BigDecimal;
rlm@10 17 import java.math.MathContext;
rlm@10 18
rlm@10 19 public class Ratio extends Number implements Comparable{
rlm@10 20 final public BigInteger numerator;
rlm@10 21 final public BigInteger denominator;
rlm@10 22
rlm@10 23 public Ratio(BigInteger numerator, BigInteger denominator){
rlm@10 24 this.numerator = numerator;
rlm@10 25 this.denominator = denominator;
rlm@10 26 }
rlm@10 27
rlm@10 28 public boolean equals(Object arg0){
rlm@10 29 return arg0 != null
rlm@10 30 && arg0 instanceof Ratio
rlm@10 31 && ((Ratio) arg0).numerator.equals(numerator)
rlm@10 32 && ((Ratio) arg0).denominator.equals(denominator);
rlm@10 33 }
rlm@10 34
rlm@10 35 public int hashCode(){
rlm@10 36 return numerator.hashCode() ^ denominator.hashCode();
rlm@10 37 }
rlm@10 38
rlm@10 39 public String toString(){
rlm@10 40 return numerator.toString() + "/" + denominator.toString();
rlm@10 41 }
rlm@10 42
rlm@10 43 public int intValue(){
rlm@10 44 return (int) doubleValue();
rlm@10 45 }
rlm@10 46
rlm@10 47 public long longValue(){
rlm@10 48 return bigIntegerValue().longValue();
rlm@10 49 }
rlm@10 50
rlm@10 51 public float floatValue(){
rlm@10 52 return (float)doubleValue();
rlm@10 53 }
rlm@10 54
rlm@10 55 public double doubleValue(){
rlm@10 56 return decimalValue(MathContext.DECIMAL64).doubleValue();
rlm@10 57 }
rlm@10 58
rlm@10 59 public BigDecimal decimalValue(){
rlm@10 60 return decimalValue(MathContext.UNLIMITED);
rlm@10 61 }
rlm@10 62
rlm@10 63 public BigDecimal decimalValue(MathContext mc){
rlm@10 64 BigDecimal numerator = new BigDecimal(this.numerator);
rlm@10 65 BigDecimal denominator = new BigDecimal(this.denominator);
rlm@10 66
rlm@10 67 return numerator.divide(denominator, mc);
rlm@10 68 }
rlm@10 69
rlm@10 70 public BigInteger bigIntegerValue(){
rlm@10 71 return numerator.divide(denominator);
rlm@10 72 }
rlm@10 73
rlm@10 74 public int compareTo(Object o){
rlm@10 75 Number other = (Number)o;
rlm@10 76 return Numbers.compare(this, other);
rlm@10 77 }
rlm@10 78 }