1 package com.teamkonzept.lib.math; 2 3 public class MathFunction extends UnaryOperator{ 4 5 final static int LOG = 0; final static int ABS = 1; 7 final static int ACOS = 2; 8 final static int ASIN = 3; 9 final static int ATAN = 4; 10 final static int CEIL = 5; 11 final static int COSH = 6; 12 final static int COS = 7; 13 final static int COTAN = 8; 14 final static int EXP = 9; 15 final static int FAC = 10; 16 final static int FLOOR = 11; 17 final static int FPART = 12; 18 final static int LN = 13; 19 final static int ROUND = 14; 20 final static int SFAC = 15; 21 final static int SINH = 16; 22 final static int SIN = 17; 23 final static int SQRT = 18; 24 final static int TANH = 19; 25 final static int TAN = 20; 26 27 int type; 28 29 double extraOperand = 10.0; 31 32 public MathFunction(int type, int parenlevel, int position) 33 throws UnsupportedOperatorException{ 34 super(parenlevel, getPriority(type, position), position); 35 this.type = type; 36 } 37 public MathFunction(int type, int parenlevel, int position, 38 Double extraOperand){ 39 super(parenlevel, MATH_FUNCTION_PRIORITY, position); 40 this.type = type; 41 this.extraOperand = extraOperand.doubleValue(); 42 } 43 44 48 public Object evaluate(Object operand) throws BadOperandTypeException{ 49 if ( !(operand instanceof Double ) ) 50 throw new BadOperandTypeException(operand, "Double"); 51 double d = ((Double )operand).doubleValue(); 52 switch ( type ){ 53 case LOG: 54 return new Double (Math.log(d) / Math.log(extraOperand)); 55 case ABS: 56 return new Double (Math.abs(d)); 57 case ACOS: 58 return new Double (Math.acos(d)); 59 case ASIN: 60 return new Double (Math.asin(d)); 61 case ATAN: 62 return new Double (Math.atan(d)); 63 case CEIL: 64 return new Double (Math.ceil(d)); 65 case COSH: 66 return new Double ((Math.exp(d) + Math.exp(0.0 - d)) / 2.0); 67 case COS: 68 return new Double (Math.cos(d)); 69 case COTAN: 70 return new Double (1.0 / Math.tan(d)); 71 case EXP: 72 return new Double (Math.exp(d)); 73 case FAC: 74 if ( Math.ceil(d) != d ) 75 throw new BadOperandTypeException(operand, "Integer"); 76 return new Double (computeFaculty((int)d)); 77 case FLOOR: 78 return new Double (Math.floor(d)); 79 case FPART: 80 return new Double (d - Math.floor(d)); 81 case LN: 82 return new Double (Math.log(d)); 83 case ROUND: 84 return new Double (Math.round(d)); 85 case SFAC: 86 if ( Math.ceil(d) != d ) 87 throw new BadOperandTypeException(operand, "Integer"); 88 return new Double (computeSemiFaculty((int)d)); 89 case SINH: 90 return new Double ((Math.exp(d) - Math.exp(0.0 - d)) / 2.0); 91 case SIN: 92 return new Double (Math.sin(d)); 93 case SQRT: 94 return new Double (Math.sqrt(d)); 95 case TANH: 96 double d1 = Math.exp(d); 97 double d2 = Math.exp(0.0 - d); 98 return new Double ( (d1 - d2) / (d1 + d2) ); 99 case TAN: 100 return new Double (Math.tan(d)); 101 } 102 return null; 103 } 104 105 static int getPriority(int op, int position) 106 throws UnsupportedOperatorException{ 107 if ( op < 0 || op > 20 ) 108 throw new UnsupportedOperatorException(MathFunction.class, 109 op, position); 110 return MATH_FUNCTION_PRIORITY; 111 } 112 113 static long computeFaculty(int value){ 114 long res = 1; 115 for ( int i = 2; i <= value; i++ ) 116 res = res * i; 117 return res; 118 } 119 120 static long computeSemiFaculty(int value){ 121 long res = 1; 122 for ( int i = value; i > 1; i = i - 2 ) 123 res = res * i; 124 return res; 125 } 126 } 127 | Popular Tags |