1 package net.sf.saxon.exslt; 2 import net.sf.saxon.expr.XPathContext; 3 import net.sf.saxon.om.Item; 4 import net.sf.saxon.om.SequenceIterator; 5 import net.sf.saxon.trans.DynamicError; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.value.EmptySequence; 8 import net.sf.saxon.value.SequenceExtent; 9 import net.sf.saxon.value.Value; 10 11 import java.util.ArrayList ; 12 13 17 18 public abstract class Math { 19 20 23 24 public static double max (SequenceIterator nsv) throws XPathException { 25 double max = Double.NEGATIVE_INFINITY; 26 try { 27 while (true) { 28 Item it = nsv.next(); 29 if (it == null) break; 30 double x = Value.stringToNumber(it.getStringValueCS()); 31 if (Double.isNaN(x)) return x; 32 if (x>max) max = x; 33 } 34 return max; 35 } catch (NumberFormatException err) { 36 return Double.NaN; 37 } 38 } 39 40 41 44 45 public static double min (SequenceIterator nsv) throws XPathException { 46 try { 47 double min = Double.POSITIVE_INFINITY; 48 while (true) { 49 Item it = nsv.next(); 50 if (it == null) break; 51 double x = Value.stringToNumber(it.getStringValueCS()); 52 if (Double.isNaN(x)) return x; 53 if (x<min) min = x; 54 } 55 return min; 56 } catch (NumberFormatException e) { 57 return Double.NaN; 58 } 59 } 60 61 62 66 67 public static Value highest (SequenceIterator nsv) throws XPathException { 68 try { 69 double max = Double.NEGATIVE_INFINITY; 70 ArrayList highest = new ArrayList (); 71 while (true) { 72 Item it = nsv.next(); 73 if (it == null) break; 74 double x = Value.stringToNumber(it.getStringValueCS()); 75 if (Double.isNaN(x)) return EmptySequence.getInstance(); 76 if (x==max) { 77 highest.add(it); 78 } else if (x>max) { 79 max = x; 80 highest.clear(); 81 highest.add(it); 82 } 83 } 84 return new SequenceExtent(highest); 85 } catch (NumberFormatException e) { 86 return EmptySequence.getInstance(); 87 } 88 } 89 90 91 92 96 97 public static Value lowest (SequenceIterator nsv) throws XPathException { 98 try { 99 double min = Double.POSITIVE_INFINITY; 100 ArrayList lowest = new ArrayList (); 101 while (true) { 102 Item it = nsv.next(); 103 if (it == null) break; 104 double x = Value.stringToNumber(it.getStringValueCS()); 105 if (Double.isNaN(x)) return EmptySequence.getInstance(); 106 if (x==min) { 107 lowest.add(it); 108 } else if (x<min) { 109 min = x; 110 lowest.clear(); 111 lowest.add(it); 112 } 113 } 114 return new SequenceExtent(lowest); 115 } catch (NumberFormatException e) { 116 return EmptySequence.getInstance(); 117 } 118 } 119 120 123 124 public static double abs (double x) { 125 return java.lang.Math.abs(x); 126 } 127 128 131 132 public static double sqrt (double x) { 133 return java.lang.Math.sqrt(x); 134 } 135 136 139 140 public static double power (double x, double y) { 141 return java.lang.Math.pow(x,y); 142 } 143 144 147 148 public static double constant (XPathContext context, String name, double precision) throws XPathException { 149 151 String con = ""; 152 153 if (name.equals("PI")) { 154 con="3.1415926535897932384626433832795028841971693993751"; 155 } else if (name.equals("E")) { 156 con="2.71828182845904523536028747135266249775724709369996"; 157 } else if (name.equals("SQRRT2")) { 158 con="1.41421356237309504880168872420969807856967187537694"; 159 } else if (name.equals("LN2")) { 160 con="0.69314718055994530941723212145817656807550013436025"; 161 } else if (name.equals("LN10")) { 162 con="2.302585092994046"; 163 } else if (name.equals("LOG2E")) { 164 con="1.4426950408889633"; 165 } else if (name.equals("SQRT1_2")) { 166 con="0.7071067811865476"; 167 } else { 168 DynamicError e = new DynamicError("Unknown math constant " + name); 169 e.setXPathContext(context); 170 throw e; 171 } 172 173 int x = (int) precision; 174 String returnVal=con.substring(0,x+2); 175 double rV=new Double (returnVal).doubleValue(); 176 return rV; 177 } 178 179 182 183 public static double log (double x) { 184 return java.lang.Math.log(x); 185 } 186 187 190 191 public static double random() { 192 return java.lang.Math.random(); 193 } 194 195 198 199 public static double sin (double x) { 200 return java.lang.Math.sin(x); 201 } 202 203 206 207 public static double cos (double x) { 208 return java.lang.Math.cos(x); 209 } 210 211 214 215 public static double tan (double x) { 216 return java.lang.Math.tan(x); 217 } 218 219 222 223 public static double asin (double x) { 224 return java.lang.Math.asin(x); 225 } 226 227 230 231 public static double acos (double x) { 232 return java.lang.Math.acos(x); 233 } 234 235 238 239 public static double atan (double x) { 240 return java.lang.Math.atan(x); 241 } 242 243 246 247 public static double atan2 (double x, double y) { 248 return java.lang.Math.atan2(x,y); 249 } 250 251 254 255 public static double exp (double x) { 256 return java.lang.Math.exp(x); 257 } 258 259 } 260 261 | Popular Tags |