1 16 19 package org.apache.xalan.lib; 20 21 import org.apache.xpath.NodeSet; 22 23 import org.w3c.dom.Node ; 24 import org.w3c.dom.NodeList ; 25 26 40 public class ExsltMath extends ExsltBase 41 { 42 private static String PI = "3.1415926535897932384626433832795028841971693993751"; 44 private static String E = "2.71828182845904523536028747135266249775724709369996"; 45 private static String SQRRT2 = "1.41421356237309504880168872420969807856967187537694"; 46 private static String LN2 = "0.69314718055994530941723212145817656807550013436025"; 47 private static String LN10 = "2.302585092994046"; 48 private static String LOG2E = "1.4426950408889633"; 49 private static String SQRT1_2 = "0.7071067811865476"; 50 51 67 public static double max (NodeList nl) 68 { 69 if (nl == null || nl.getLength() == 0) 70 return Double.NaN; 71 72 double m = - Double.MAX_VALUE; 73 for (int i = 0; i < nl.getLength(); i++) 74 { 75 Node n = nl.item(i); 76 double d = toNumber(n); 77 if (Double.isNaN(d)) 78 return Double.NaN; 79 else if (d > m) 80 m = d; 81 } 82 83 return m; 84 } 85 86 102 public static double min (NodeList nl) 103 { 104 if (nl == null || nl.getLength() == 0) 105 return Double.NaN; 106 107 double m = Double.MAX_VALUE; 108 for (int i = 0; i < nl.getLength(); i++) 109 { 110 Node n = nl.item(i); 111 double d = toNumber(n); 112 if (Double.isNaN(d)) 113 return Double.NaN; 114 else if (d < m) 115 m = d; 116 } 117 118 return m; 119 } 120 121 138 public static NodeList highest (NodeList nl) 139 { 140 double maxValue = max(nl); 141 142 NodeSet highNodes = new NodeSet(); 143 highNodes.setShouldCacheNodes(true); 144 145 if (Double.isNaN(maxValue)) 146 return highNodes; 148 for (int i = 0; i < nl.getLength(); i++) 149 { 150 Node n = nl.item(i); 151 double d = toNumber(n); 152 if (d == maxValue) 153 highNodes.addElement(n); 154 } 155 return highNodes; 156 } 157 158 175 public static NodeList lowest (NodeList nl) 176 { 177 double minValue = min(nl); 178 179 NodeSet lowNodes = new NodeSet(); 180 lowNodes.setShouldCacheNodes(true); 181 182 if (Double.isNaN(minValue)) 183 return lowNodes; 185 for (int i = 0; i < nl.getLength(); i++) 186 { 187 Node n = nl.item(i); 188 double d = toNumber(n); 189 if (d == minValue) 190 lowNodes.addElement(n); 191 } 192 return lowNodes; 193 } 194 195 201 public static double abs(double num) 202 { 203 return Math.abs(num); 204 } 205 206 212 public static double acos(double num) 213 { 214 return Math.acos(num); 215 } 216 217 223 public static double asin(double num) 224 { 225 return Math.asin(num); 226 } 227 228 234 public static double atan(double num) 235 { 236 return Math.atan(num); 237 } 238 239 246 public static double atan2(double num1, double num2) 247 { 248 return Math.atan2(num1, num2); 249 } 250 251 257 public static double cos(double num) 258 { 259 return Math.cos(num); 260 } 261 262 268 public static double exp(double num) 269 { 270 return Math.exp(num); 271 } 272 273 279 public static double log(double num) 280 { 281 return Math.log(num); 282 } 283 284 291 public static double power(double num1, double num2) 292 { 293 return Math.pow(num1, num2); 294 } 295 296 301 public static double random() 302 { 303 return Math.random(); 304 } 305 306 312 public static double sin(double num) 313 { 314 return Math.sin(num); 315 } 316 317 323 public static double sqrt(double num) 324 { 325 return Math.sqrt(num); 326 } 327 328 334 public static double tan(double num) 335 { 336 return Math.tan(num); 337 } 338 339 355 public static double constant(String name, double precision) 356 { 357 String value = null; 358 if (name.equals("PI")) 359 value = PI; 360 else if (name.equals("E")) 361 value = E; 362 else if (name.equals("SQRRT2")) 363 value = SQRRT2; 364 else if (name.equals("LN2")) 365 value = LN2; 366 else if (name.equals("LN10")) 367 value = LN10; 368 else if (name.equals("LOG2E")) 369 value = LOG2E; 370 else if (name.equals("SQRT1_2")) 371 value = SQRT1_2; 372 373 if (value != null) 374 { 375 int bits = new Double (precision).intValue(); 376 377 if (bits <= value.length()) 378 value = value.substring(0, bits); 379 380 return new Double (value).doubleValue(); 381 } 382 else 383 return Double.NaN; 384 385 } 386 387 } 388 | Popular Tags |