1 3 package org.python.core; 4 5 8 public class ExtraMath { 9 public static double LOG10 = Math.log(10.0); 10 public static double EPSILON = Math.pow(2.0, -52.0); 11 public static double CLOSE = EPSILON * 2.0; 12 13 public static double log10(double v) { 14 return Math.log(v) / LOG10; 15 } 16 17 public static double hypot(double v, double w) { 18 v = Math.abs(v); 19 w = Math.abs(w); 20 if (v < w) { 21 double temp = v; 22 v = w; 23 w = temp; 24 } 25 if (v == 0.0) 26 return 0.0; 27 else { 28 double wv = w/v; 29 return v * Math.sqrt(1.0 + wv*wv); 30 } 31 } 32 33 36 public static boolean close(double v, double w, double tol) { 37 if (v == w) 38 { 39 return true; 40 } 41 double scaled = tol * (Math.abs(v) + Math.abs(w))/2.0; 42 return Math.abs(w - v) < scaled; 43 } 44 45 public static boolean close(double v, double w) 46 { 47 return close(v, w, CLOSE); 48 } 49 50 54 public static double closeFloor(double v) { 55 double floor = Math.floor(v); 56 return close(v, floor + 1.0) ? floor + 1.0 : floor; 57 } 58 } 59 | Popular Tags |