PageRenderTime 35ms CodeModel.GetById 23ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2-build060407/src/edu/vub/at/objects/natives/NATFraction.java

http://ambienttalk.googlecode.com/
Java | 210 lines | 118 code | 26 blank | 66 comment | 13 complexity | c58f5935ddfee0c9446d9c13530088e3 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.objects.ATBoolean;
 33import edu.vub.at.objects.ATFraction;
 34import edu.vub.at.objects.ATNumber;
 35import edu.vub.at.objects.ATNumeric;
 36import edu.vub.at.objects.ATObject;
 37import edu.vub.at.objects.ATTable;
 38import edu.vub.at.objects.coercion.NativeStripes;
 39
 40/**
 41 * The native implementation of an AmbientTalk fraction.
 42 * A fraction is implemented by a Java double.
 43 * 
 44 * @author tvc
 45 */
 46public final class NATFraction extends NATNumeric implements ATFraction {
 47
 48	public static final NATFraction INFTY = new NATFraction(Double.POSITIVE_INFINITY);
 49	
 50	public final double javaValue;
 51	
 52	/**
 53	 * This method currently serves as a hook for fraction creation.
 54	 * Currently fraction objects are not reused, but this might change in the future.
 55	 */
 56	public static final NATFraction atValue(double javaFrc) {
 57		return new NATFraction(javaFrc);
 58	}
 59	
 60	private NATFraction(double javaFrc) {
 61		javaValue = javaFrc;
 62	}
 63	
 64	public NATFraction asNativeFraction() {
 65		return this;
 66	}
 67    
 68    public ATBoolean base__opeql__opeql_(ATObject comparand) {
 69		return NATBoolean.atValue(this.equals(comparand));
 70    }
 71	
 72    public boolean equals(Object comparand) {
 73		return (comparand instanceof NATFraction) &&
 74		       (javaValue == ((NATFraction) comparand).javaValue);
 75    }
 76	
 77	public NATText meta_print() throws InterpreterException {
 78        return NATText.atValue(String.valueOf(javaValue));
 79	}
 80	
 81    public ATTable meta_getStripes() throws InterpreterException {
 82    	return NATTable.of(NativeStripes._FRACTION_);
 83    }
 84	
 85	// contract with NATNumeric
 86	protected double getJavaValue() { return javaValue; }
 87	
 88	/* -------------------------------------
 89	 * - base-level interface to fractions -
 90	 * ------------------------------------- */
 91	
 92	// Fraction arithmetic operations
 93	
 94	/**
 95	 * FRC(n).inc() => FRC(n+1)
 96	 */
 97	public ATNumeric base_inc() {
 98		return NATFraction.atValue(javaValue+1);
 99	}
100	
101	/**
102	 * FRC(n).dec() => FRC(n-1)
103	 */
104	public ATNumeric base_dec() {
105		return NATFraction.atValue(javaValue-1);
106	}
107	
108	/**
109	 * FRC(n).abs() => FRC(abs(n))
110	 */
111	public ATNumeric base_abs() {
112		return NATFraction.atValue(Math.abs(javaValue));
113	}
114	
115	/**
116	 * FRC(n).round() => NBR(round(n))
117	 */
118	public ATNumber base_round() {
119		return NATNumber.atValue(Math.round((float) javaValue));
120	}
121	
122	/**
123	 * FRC(n).floor() => NBR(floor(n))
124	 */
125	public ATNumber base_floor() {
126		return NATNumber.atValue(Math.round((float) Math.floor(javaValue)));
127	}
128	
129	/**
130	 * FRC(n).ceiling() => NBR(ceil(n))
131	 */
132	public ATNumber base_ceiling() {
133		return NATNumber.atValue(Math.round((float) Math.ceil(javaValue)));
134	}
135	
136    // Numeric arithmetic operations
137	
138	// addition +
139	public ATNumeric base__oppls_(ATNumeric other) throws InterpreterException {
140		return other.base_addFraction(this);
141	}
142	public ATNumeric base_addNumber(ATNumber other) throws InterpreterException {
143		return NATFraction.atValue(javaValue + other.asNativeNumber().javaValue);
144	}
145	public ATNumeric base_addFraction(ATFraction other) throws InterpreterException {
146		return NATFraction.atValue(javaValue + other.asNativeFraction().javaValue);
147	}
148	
149	// subtraction -
150	public ATNumeric base__opmns_(ATNumeric other) throws InterpreterException {
151		return other.base_subtractFraction(this);
152	}
153	public ATNumeric base_subtractNumber(ATNumber other) throws InterpreterException {
154		return NATFraction.atValue(other.asNativeNumber().javaValue - javaValue);
155	}
156	public ATNumeric base_subtractFraction(ATFraction other) throws InterpreterException {
157		return NATFraction.atValue(other.asNativeFraction().javaValue - javaValue);
158	}
159	
160	// multiplication *
161	public ATNumeric base__optms_(ATNumeric other) throws InterpreterException {
162		return other.base_timesFraction(this);
163	}
164	public ATNumeric base_timesNumber(ATNumber other) throws InterpreterException {
165		return NATFraction.atValue(other.asNativeNumber().javaValue * javaValue);
166	}
167	public ATNumeric base_timesFraction(ATFraction other) throws InterpreterException {
168		return NATFraction.atValue(other.asNativeFraction().javaValue * javaValue);
169	}
170	
171	// division /
172	public ATNumeric base__opdiv_(ATNumeric other) throws InterpreterException {
173		return other.base_divideFraction(this);
174	}
175	public ATNumeric base_divideNumber(ATNumber other) throws InterpreterException {
176		if (javaValue == 0)
177			throw new XIllegalArgument("Division by zero: " + other);
178		return NATFraction.atValue((other.asNativeNumber().javaValue * 1.0) / javaValue);
179	}
180	public ATNumeric base_divideFraction(ATFraction other) throws InterpreterException {
181		if (javaValue == 0)
182			throw new XIllegalArgument("Division by zero: " + other);
183		return NATFraction.atValue(other.asNativeFraction().javaValue / javaValue);
184	}
185	
186	// comparison: generalized equality <=>
187	public ATNumeric base__opltx__opeql__opgtx_(ATNumeric other) throws InterpreterException {
188		return other.base_gequalsFraction(this);
189	}
190	public ATNumeric base_gequalsNumber(ATNumber other) throws InterpreterException {
191		int n = other.asNativeNumber().javaValue;
192		if (n < javaValue) {
193			return NATNumber.MONE; // -1
194		} else if (n > javaValue) {
195			return NATNumber.ONE;  // +1
196		} else {
197			return NATNumber.ZERO; // 0
198		}
199	}
200	public ATNumeric base_gequalsFraction(ATFraction other) throws InterpreterException {
201		double n = other.asNativeFraction().javaValue;
202		if (n < javaValue) {
203			return NATNumber.MONE; // -1
204		} else if (n > javaValue) {
205			return NATNumber.ONE;  // +1
206		} else {
207			return NATNumber.ZERO; // 0
208		}
209	}
210}