/interpreter/tags/at2-build190607/src/edu/vub/at/objects/natives/NATNumeric.java
Java | 148 lines | 58 code | 16 blank | 74 comment | 0 complexity | 20606c3601cfb40fb56529a9ad601544 MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * NATNumeric.java created on 18-aug-2006 at 11:06:11 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.XTypeMismatch; 32import edu.vub.at.objects.ATBoolean; 33import edu.vub.at.objects.ATFraction; 34import edu.vub.at.objects.ATNumeric; 35import edu.vub.at.objects.ATObject; 36import edu.vub.at.objects.ATTable; 37import edu.vub.at.objects.coercion.NativeTypeTags; 38import edu.vub.at.objects.natives.grammar.AGExpression; 39 40/** 41 * A common superclass of both numbers and fractions to factor out common base-level behaviour. 42 * 43 * @author tvc 44 */ 45public abstract class NATNumeric extends AGExpression implements ATNumeric { 46 47 /** 48 * Template method that should return the value of the underlying number or fraction as a double 49 */ 50 protected abstract double getJavaValue(); 51 52 public NATNumeric asNativeNumeric() throws XTypeMismatch { 53 return this; 54 } 55 56 public ATTable meta_getTypeTags() throws InterpreterException { 57 return NATTable.of(NativeTypeTags._NUMERIC_); 58 } 59 60 // numbers and fractions are singletons 61 public ATObject meta_clone() throws InterpreterException { 62 return this; 63 } 64 65 // trigonometric functions 66 67 /** 68 * NUM(n).cos() => FRC(Math.cos(n)) 69 */ 70 public ATFraction base_cos() { 71 return NATFraction.atValue(Math.cos(getJavaValue())); 72 } 73 74 /** 75 * NUM(n).sin() => FRC(Math.sin(n)) 76 */ 77 public ATFraction base_sin() { 78 return NATFraction.atValue(Math.sin(getJavaValue())); 79 } 80 81 /** 82 * NUM(n).tan() => FRC(Math.tan(n)) 83 */ 84 public ATFraction base_tan() { 85 return NATFraction.atValue(Math.tan(getJavaValue())); 86 } 87 88 /** 89 * NUM(n).log() => FRC(log(e,n)) 90 */ 91 public ATFraction base_log() { 92 return NATFraction.atValue(Math.log(getJavaValue())); 93 } 94 95 /** 96 * NUM(n).sqrt() => FRC(Math.sqrt(n)) 97 */ 98 public ATFraction base_sqrt() { 99 return NATFraction.atValue(Math.sqrt(getJavaValue())); 100 } 101 102 /** 103 * NUM(n).expt(NUM(e)) => FRC(Math.pow(n,e)) 104 */ 105 public ATFraction base_expt(ATNumeric pow) throws InterpreterException { 106 return NATFraction.atValue(Math.pow(getJavaValue(), pow.asNativeNumeric().getJavaValue())); 107 } 108 109 // Comparable 'mixin' based on <=> 110 111 /** 112 * a < b iff (a <=> b) == -1 113 */ 114 public ATBoolean base__opltx_(ATNumeric other) throws InterpreterException { 115 return NATBoolean.atValue(this.base__opltx__opeql__opgtx_(other).equals(NATNumber.MONE)); 116 } 117 /** 118 * a > b iff (a <=> b) == +1 119 */ 120 public ATBoolean base__opgtx_(ATNumeric other) throws InterpreterException { 121 return NATBoolean.atValue(this.base__opltx__opeql__opgtx_(other).equals(NATNumber.ONE)); 122 } 123 /** 124 * a <= b iff (a <=> b) != +1 125 */ 126 public ATBoolean base__opltx__opeql_(ATNumeric other) throws InterpreterException { 127 return NATBoolean.atValue(! this.base__opltx__opeql__opgtx_(other).equals(NATNumber.ONE)); 128 } 129 /** 130 * a >= b iff (a <=> b) != -1 131 */ 132 public ATBoolean base__opgtx__opeql_(ATNumeric other) throws InterpreterException { 133 return NATBoolean.atValue(! this.base__opltx__opeql__opgtx_(other).equals(NATNumber.MONE)); 134 } 135 /** 136 * a = b iff (a <=> b) == 0 137 */ 138 public ATBoolean base__opeql_(ATNumeric other) throws InterpreterException { 139 return NATBoolean.atValue(this.base__opltx__opeql__opgtx_(other).equals(NATNumber.ZERO)); 140 } 141 /** 142 * a != b iff (a <=> b) != 0 143 */ 144 public ATBoolean base__opnot__opeql_(ATNumeric other) throws InterpreterException { 145 return NATBoolean.atValue(! this.base__opltx__opeql__opgtx_(other).equals(NATNumber.ZERO)); 146 } 147 148}