1 package com.icl.saxon.exslt; 2 import com.icl.saxon.expr.*; 3 import com.icl.saxon.Context; 4 import com.icl.saxon.Controller; 5 import com.icl.saxon.om.NodeInfo; 6 import com.icl.saxon.om.NodeEnumeration; 7 import java.util.Vector ; 8 9 13 14 public abstract class Math { 15 16 19 20 public static double max (NodeEnumeration nsv) throws XPathException { 21 double max = Double.NEGATIVE_INFINITY; 22 while (nsv.hasMoreElements()) { 23 double x = Value.stringToNumber(nsv.nextElement().getStringValue()); 24 if (Double.isNaN(x)) return x; 25 if (x>max) max = x; 26 } 27 return max; 28 } 29 30 31 34 35 public static double min (NodeEnumeration nsv) throws XPathException { 36 double min = Double.POSITIVE_INFINITY; 37 while (nsv.hasMoreElements()) { 38 double x = Value.stringToNumber(nsv.nextElement().getStringValue()); 39 if (Double.isNaN(x)) return x; 40 if (x<min) min = x; 41 } 42 return min; 43 } 44 45 46 49 50 public static NodeSetValue highest (Context c, NodeEnumeration nsv) throws XPathException { 51 double max = Double.NEGATIVE_INFINITY; 52 Vector highest = new Vector (); 53 while (nsv.hasMoreElements()) { 54 NodeInfo node = nsv.nextElement(); 55 double x = Value.stringToNumber(node.getStringValue()); 56 if (Double.isNaN(x)) return new EmptyNodeSet(); 57 if (x==max) { 58 highest.addElement(node); 59 } else if (x>max) { 60 max = x; 61 highest.removeAllElements(); 62 highest.addElement(node); 63 } 64 } 65 return new NodeSetExtent(highest, c.getController()); 66 } 67 68 69 70 73 74 public static NodeSetValue lowest (Context c, NodeEnumeration nsv) throws XPathException { 75 double min = Double.POSITIVE_INFINITY; 76 Vector lowest = new Vector (); 77 while (nsv.hasMoreElements()) { 78 NodeInfo node = nsv.nextElement(); 79 double x = Value.stringToNumber(node.getStringValue()); 80 if (Double.isNaN(x)) return new EmptyNodeSet(); 81 if (x==min) { 82 lowest.addElement(node); 83 } else if (x<min) { 84 min = x; 85 lowest.removeAllElements(); 86 lowest.addElement(node); 87 } 88 } 89 return new NodeSetExtent(lowest, c.getController()); 90 } 91 92 95 96 public static double abs (double x) throws XPathException { 97 98 return java.lang.Math.abs(x); 99 } 100 101 104 105 public static double sqrt (double x) throws XPathException { 106 107 108 return java.lang.Math.sqrt(x); 109 } 110 111 114 115 public static double power (double x, double y) throws XPathException { 116 117 return java.lang.Math.pow(x,y); 118 } 119 120 123 124 public static double constant (String name, double precision) throws XPathException { 125 127 String con=new String (); 128 129 if (name.equals("PI")) { 130 con="3.1415926535897932384626433832795028841971693993751"; 131 } else if (name.equals("E")) { 132 con="2.71828182845904523536028747135266249775724709369996"; 133 } else if (name.equals("SQRRT2")) { 134 con="1.41421356237309504880168872420969807856967187537694"; 135 } else if (name.equals("LN2")) { 136 con="0.69314718055994530941723212145817656807550013436025"; 137 } else if (name.equals("LN10")) { 138 con="2.302585092994046"; 139 } else if (name.equals("LOG2E")) { 140 con="1.4426950408889633"; 141 } else if (name.equals("SQRT1_2")) { 142 con="0.7071067811865476"; 143 } 144 int x = (int) precision; 145 String returnVal=con.substring(0,x+2); 146 double rV=new Double (returnVal).doubleValue(); 147 return rV; 148 } 149 150 153 154 public static double log (double x) throws XPathException { 155 156 return java.lang.Math.log(x); 157 } 158 159 162 163 public static double random () throws XPathException { 164 165 166 return java.lang.Math.random(); 167 } 168 169 172 173 public static double sin (double x) throws XPathException { 174 175 return java.lang.Math.sin(x); 176 } 177 178 181 182 public static double cos (double x) throws XPathException { 183 184 return java.lang.Math.cos(x); 185 } 186 187 190 191 public static double tan (double x) throws XPathException { 192 193 return java.lang.Math.tan(x); 194 } 195 196 199 200 public static double asin (double x) throws XPathException { 201 202 return java.lang.Math.asin(x); 203 } 204 205 208 209 public static double acos (double x) throws XPathException { 210 211 return java.lang.Math.acos(x); 212 } 213 214 217 218 public static double atan (double x) throws XPathException { 219 220 return java.lang.Math.atan(x); 221 } 222 223 226 227 public static double atan2 (double x, double y) throws XPathException { 228 229 return java.lang.Math.atan2(x,y); 230 } 231 232 235 236 public static double exp (double x) throws XPathException { 237 238 return java.lang.Math.exp(x); 239 } 240 241 } 242 243 244 245 246 247 | Popular Tags |