Mercurial > lasercutter
diff 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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/lang/Ratio.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,78 @@ 1.4 +/** 1.5 + * Copyright (c) Rich Hickey. All rights reserved. 1.6 + * The use and distribution terms for this software are covered by the 1.7 + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.8 + * which can be found in the file epl-v10.html at the root of this distribution. 1.9 + * By using this software in any fashion, you are agreeing to be bound by 1.10 + * the terms of this license. 1.11 + * You must not remove this notice, or any other, from this software. 1.12 + **/ 1.13 + 1.14 +/* rich Mar 31, 2008 */ 1.15 + 1.16 +package clojure.lang; 1.17 + 1.18 +import java.math.BigInteger; 1.19 +import java.math.BigDecimal; 1.20 +import java.math.MathContext; 1.21 + 1.22 +public class Ratio extends Number implements Comparable{ 1.23 +final public BigInteger numerator; 1.24 +final public BigInteger denominator; 1.25 + 1.26 +public Ratio(BigInteger numerator, BigInteger denominator){ 1.27 + this.numerator = numerator; 1.28 + this.denominator = denominator; 1.29 +} 1.30 + 1.31 +public boolean equals(Object arg0){ 1.32 + return arg0 != null 1.33 + && arg0 instanceof Ratio 1.34 + && ((Ratio) arg0).numerator.equals(numerator) 1.35 + && ((Ratio) arg0).denominator.equals(denominator); 1.36 +} 1.37 + 1.38 +public int hashCode(){ 1.39 + return numerator.hashCode() ^ denominator.hashCode(); 1.40 +} 1.41 + 1.42 +public String toString(){ 1.43 + return numerator.toString() + "/" + denominator.toString(); 1.44 +} 1.45 + 1.46 +public int intValue(){ 1.47 + return (int) doubleValue(); 1.48 +} 1.49 + 1.50 +public long longValue(){ 1.51 + return bigIntegerValue().longValue(); 1.52 +} 1.53 + 1.54 +public float floatValue(){ 1.55 + return (float)doubleValue(); 1.56 +} 1.57 + 1.58 +public double doubleValue(){ 1.59 + return decimalValue(MathContext.DECIMAL64).doubleValue(); 1.60 +} 1.61 + 1.62 +public BigDecimal decimalValue(){ 1.63 + return decimalValue(MathContext.UNLIMITED); 1.64 +} 1.65 + 1.66 +public BigDecimal decimalValue(MathContext mc){ 1.67 + BigDecimal numerator = new BigDecimal(this.numerator); 1.68 + BigDecimal denominator = new BigDecimal(this.denominator); 1.69 + 1.70 + return numerator.divide(denominator, mc); 1.71 +} 1.72 + 1.73 +public BigInteger bigIntegerValue(){ 1.74 + return numerator.divide(denominator); 1.75 +} 1.76 + 1.77 +public int compareTo(Object o){ 1.78 + Number other = (Number)o; 1.79 + return Numbers.compare(this, other); 1.80 +} 1.81 +}