1 9 10 package org.nfunk.jep.function; 11 12 import java.util.*; 13 import org.nfunk.jep.*; 14 import org.nfunk.jep.type.*; 15 16 29 public class Comparative extends PostfixMathCommand 30 { 31 int id; 32 double tolerance; 33 public static final int LT = 0; 34 public static final int GT = 1; 35 public static final int LE = 2; 36 public static final int GE = 3; 37 public static final int NE = 4; 38 public static final int EQ = 5; 39 40 public Comparative(int id_in) 41 { 42 id = id_in; 43 numberOfParameters = 2; 44 tolerance = 1e-6; 45 } 46 47 public void run(Stack inStack) throws ParseException 48 { 49 checkStack(inStack); 51 Object param2 = inStack.pop(); 52 Object param1 = inStack.pop(); 53 boolean res=false; 54 switch(id) 55 { 56 case LT: res = lt(param1,param2); break; 57 case GT: res = gt(param1,param2); break; 58 case LE: res = le(param1,param2); break; 59 case GE: res = ge(param1,param2); break; 60 case NE: res = ne(param1,param2); break; 61 case EQ: res = eq(param1,param2); break; 62 } 63 if(res) inStack.push(new Double (1)); 64 else inStack.push(new Double (0)); 65 } 66 67 public boolean lt(Object param1,Object param2) throws ParseException 68 { 69 if ((param1 instanceof Complex) || (param2 instanceof Complex)) 70 throw new ParseException("< not defined for complex numbers"); 71 if ((param1 instanceof Number ) && (param2 instanceof Number )) 72 { 73 double x = ((Number )param1).doubleValue(); 74 double y = ((Number )param2).doubleValue(); 75 return (x<y); 76 } 77 throw new ParseException("< not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName()); 78 } 79 public boolean gt(Object param1,Object param2) throws ParseException 80 { 81 if ((param1 instanceof Complex) || (param2 instanceof Complex)) 82 throw new ParseException("> not defined for complex numbers"); 83 if ((param1 instanceof Number ) && (param2 instanceof Number )) 84 { 85 double x = ((Number )param1).doubleValue(); 86 double y = ((Number )param2).doubleValue(); 87 return (x>y); 88 } 89 throw new ParseException("> not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName()); 90 } 91 public boolean le(Object param1,Object param2) throws ParseException 92 { 93 if ((param1 instanceof Complex) || (param2 instanceof Complex)) 94 throw new ParseException("<= not defined for complex numbers"); 95 if ((param1 instanceof Number ) && (param2 instanceof Number )) 96 { 97 double x = ((Number )param1).doubleValue(); 98 double y = ((Number )param2).doubleValue(); 99 return (x<=y); 100 } 101 throw new ParseException("<= not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName()); 102 } 103 public boolean ge(Object param1,Object param2) throws ParseException 104 { 105 if ((param1 instanceof Complex) || (param2 instanceof Complex)) 106 throw new ParseException(">= not defined for complex numbers"); 107 if ((param1 instanceof Number ) && (param2 instanceof Number )) 108 { 109 double x = ((Number )param1).doubleValue(); 110 double y = ((Number )param2).doubleValue(); 111 return (x>=y); 112 } 113 throw new ParseException(">= not defined for object of type "+param1.getClass().getName()+" and "+param1.getClass().getName()); 114 } 115 116 public boolean eq(Object param1,Object param2) throws ParseException 117 { 118 if ((param1 instanceof Complex) && (param2 instanceof Complex)) 119 { 120 return ((Complex)param1).equals((Complex)param2,tolerance); 121 } 122 if ((param1 instanceof Complex) && (param2 instanceof Double )) 123 { 124 return ((Complex)param1).equals(new Complex((Number ) param2),tolerance); 125 } 126 if ((param2 instanceof Complex) && (param1 instanceof Double )) 127 { 128 return ((Complex)param2).equals(new Complex((Number ) param1),tolerance); 129 } 130 return param1.equals(param2); 131 } 132 133 public boolean ne(Object param1,Object param2) throws ParseException 134 { 135 if ((param1 instanceof Complex) && (param2 instanceof Complex)) 136 { 137 return !((Complex)param1).equals((Complex)param2,tolerance); 138 } 139 if ((param1 instanceof Complex) && (param2 instanceof Double )) 140 { 141 return !((Complex)param1).equals(new Complex((Number ) param2),tolerance); 142 } 143 if ((param2 instanceof Complex) && (param1 instanceof Double )) 144 { 145 return !((Complex)param2).equals(new Complex((Number ) param1),tolerance); 146 } 147 return !param1.equals(param2); 148 } 149 150 151 239 242 public double getTolerance() { 243 return tolerance; 244 } 245 246 250 public void setTolerance(double d) { 251 tolerance = d; 252 } 253 254 } 255 | Popular Tags |