PageRenderTime 47ms CodeModel.GetById 11ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at_build150307/src/edu/vub/at/objects/natives/NATFraction.java

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