PageRenderTime 29ms CodeModel.GetById 2ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/natives/NATFraction.java

http://ambienttalk.googlecode.com/
Java | 216 lines | 123 code | 27 blank | 66 comment | 15 complexity | da00045fd77eb6e75572ea3a30bbe9de MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATFraction.java created on 26-jul-2006 at 16:42:48
  4 * (c) Programming Technology Lab, 2006 - 2007
  5 * Authors: Tom Van Cutsem & Stijn Mostinckx
  6 * 
  7 * Permission is hereby granted, free of charge, to any person
  8 * obtaining a copy of this software and associated documentation
  9 * files (the "Software"), to deal in the Software without
 10 * restriction, including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense, and/or
 12 * sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following
 14 * conditions:
 15 *
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26 * OTHER DEALINGS IN THE SOFTWARE.
 27 */
 28package edu.vub.at.objects.natives;
 29
 30import edu.vub.at.exceptions.InterpreterException;
 31import edu.vub.at.exceptions.XIllegalArgument;
 32import edu.vub.at.exceptions.XTypeMismatch;
 33import edu.vub.at.objects.ATBoolean;
 34import edu.vub.at.objects.ATFraction;
 35import edu.vub.at.objects.ATNumber;
 36import edu.vub.at.objects.ATNumeric;
 37import edu.vub.at.objects.ATObject;
 38import edu.vub.at.objects.ATTable;
 39import edu.vub.at.objects.coercion.NativeTypeTags;
 40
 41/**
 42 * The native implementation of an AmbientTalk fraction.
 43 * A fraction is implemented by a Java double.
 44 * 
 45 * @author tvc
 46 */
 47public final class NATFraction extends NATNumeric implements ATFraction {
 48
 49	public static final NATFraction INFTY = new NATFraction(Double.POSITIVE_INFINITY);
 50	
 51	public final double javaValue;
 52	
 53	/**
 54	 * This method currently serves as a hook for fraction creation.
 55	 * Currently fraction objects are not reused, but this might change in the future.
 56	 */
 57	public static final NATFraction atValue(double javaFrc) {
 58		return new NATFraction(javaFrc);
 59	}
 60	
 61	private NATFraction(double javaFrc) {
 62		javaValue = javaFrc;
 63	}
 64	
 65	public boolean isNativeFraction() {
 66		return true;
 67	}
 68	
 69	public NATFraction asNativeFraction() {
 70		return this;
 71	}
 72	
 73	public int hashCode() { return (int) javaValue; }
 74    
 75    public ATBoolean base__opeql__opeql_(ATObject comparand) throws XTypeMismatch {
 76    	if (comparand.isNativeFraction()) {
 77    		return NATBoolean.atValue(javaValue == comparand.asNativeFraction().javaValue);
 78    	} else {
 79    		return NATBoolean._FALSE_;
 80    	}
 81    }
 82	
 83	public NATText meta_print() throws InterpreterException {
 84        return NATText.atValue(String.valueOf(javaValue));
 85	}
 86	
 87    public ATTable meta_typeTags() throws InterpreterException {
 88    	return NATTable.of(NativeTypeTags._FRACTION_, NativeTypeTags._ISOLATE_);
 89    }
 90	
 91	// contract with NATNumeric
 92	protected double getJavaValue() { return javaValue; }
 93	
 94	/* -------------------------------------
 95	 * - base-level interface to fractions -
 96	 * ------------------------------------- */
 97	
 98	// Fraction arithmetic operations
 99	
100	/**
101	 * FRC(n).inc() => FRC(n+1)
102	 */
103	public ATFraction base_inc() {
104		return NATFraction.atValue(javaValue+1);
105	}
106	
107	/**
108	 * FRC(n).dec() => FRC(n-1)
109	 */
110	public ATFraction base_dec() {
111		return NATFraction.atValue(javaValue-1);
112	}
113	
114	/**
115	 * FRC(n).abs() => FRC(abs(n))
116	 */
117	public ATFraction base_abs() {
118		return NATFraction.atValue(Math.abs(javaValue));
119	}
120	
121	/**
122	 * FRC(n).round() => NBR(round(n))
123	 */
124	public ATNumber base_round() {
125		return NATNumber.atValue(Math.round((float) javaValue));
126	}
127	
128	/**
129	 * FRC(n).floor() => NBR(floor(n))
130	 */
131	public ATNumber base_floor() {
132		return NATNumber.atValue(Math.round((float) Math.floor(javaValue)));
133	}
134	
135	/**
136	 * FRC(n).ceiling() => NBR(ceil(n))
137	 */
138	public ATNumber base_ceiling() {
139		return NATNumber.atValue(Math.round((float) Math.ceil(javaValue)));
140	}
141	
142    // Numeric arithmetic operations
143	
144	// addition +
145	public ATNumeric base__oppls_(ATNumeric other) throws InterpreterException {
146		return other.base_addFraction(this);
147	}
148	public ATNumeric base_addNumber(ATNumber other) throws InterpreterException {
149		return NATFraction.atValue(javaValue + other.asNativeNumber().javaValue);
150	}
151	public ATNumeric base_addFraction(ATFraction other) throws InterpreterException {
152		return NATFraction.atValue(javaValue + other.asNativeFraction().javaValue);
153	}
154	
155	// subtraction -
156	public ATNumeric base__opmns_(ATNumeric other) throws InterpreterException {
157		return other.base_subtractFraction(this);
158	}
159	public ATNumeric base_subtractNumber(ATNumber other) throws InterpreterException {
160		return NATFraction.atValue(other.asNativeNumber().javaValue - javaValue);
161	}
162	public ATNumeric base_subtractFraction(ATFraction other) throws InterpreterException {
163		return NATFraction.atValue(other.asNativeFraction().javaValue - javaValue);
164	}
165	
166	// multiplication *
167	public ATNumeric base__optms_(ATNumeric other) throws InterpreterException {
168		return other.base_timesFraction(this);
169	}
170	public ATNumeric base_timesNumber(ATNumber other) throws InterpreterException {
171		return NATFraction.atValue(other.asNativeNumber().javaValue * javaValue);
172	}
173	public ATNumeric base_timesFraction(ATFraction other) throws InterpreterException {
174		return NATFraction.atValue(other.asNativeFraction().javaValue * javaValue);
175	}
176	
177	// division /
178	public ATNumeric base__opdiv_(ATNumeric other) throws InterpreterException {
179		return other.base_divideFraction(this);
180	}
181	public ATNumeric base_divideNumber(ATNumber other) throws InterpreterException {
182		if (javaValue == 0)
183			throw new XIllegalArgument("Division by zero: " + other);
184		return NATFraction.atValue((other.asNativeNumber().javaValue * 1.0) / javaValue);
185	}
186	public ATNumeric base_divideFraction(ATFraction other) throws InterpreterException {
187		if (javaValue == 0)
188			throw new XIllegalArgument("Division by zero: " + other);
189		return NATFraction.atValue(other.asNativeFraction().javaValue / javaValue);
190	}
191	
192	// comparison: generalized equality <=>
193	public ATNumeric base__opltx__opeql__opgtx_(ATNumeric other) throws InterpreterException {
194		return other.base_gequalsFraction(this);
195	}
196	public ATNumeric base_gequalsNumber(ATNumber other) throws InterpreterException {
197		int n = other.asNativeNumber().javaValue;
198		if (n < javaValue) {
199			return NATNumber.MONE; // -1
200		} else if (n > javaValue) {
201			return NATNumber.ONE;  // +1
202		} else {
203			return NATNumber.ZERO; // 0
204		}
205	}
206	public ATNumeric base_gequalsFraction(ATFraction other) throws InterpreterException {
207		double n = other.asNativeFraction().javaValue;
208		if (n < javaValue) {
209			return NATNumber.MONE; // -1
210		} else if (n > javaValue) {
211			return NATNumber.ONE;  // +1
212		} else {
213			return NATNumber.ZERO; // 0
214		}
215	}
216}