01: // Copyright (c) Corporation for National Research Initiatives
02:
03: package org.python.core;
04:
05: /**
06: * A static utility class with two additional math functions.
07: */
08: public class ExtraMath {
09: public static double LOG10 = Math.log(10.0);
10:
11: public static double EPSILON = Math.pow(2.0, -52.0);
12:
13: public static double CLOSE = EPSILON * 2.0;
14:
15: public static double log10(double v) {
16: return Math.log(v) / LOG10;
17: }
18:
19: public static double hypot(double v, double w) {
20: v = Math.abs(v);
21: w = Math.abs(w);
22: if (v < w) {
23: double temp = v;
24: v = w;
25: w = temp;
26: }
27: if (v == 0.0) {
28: return 0.0;
29: } else {
30: double wv = w / v;
31: return v * Math.sqrt(1.0 + wv * wv);
32: }
33: }
34:
35: /**
36: * Are v and w "close" to each other? Uses a scaled tolerance.
37: */
38: public static boolean close(double v, double w, double tol) {
39: if (v == w) {
40: return true;
41: }
42: double scaled = tol * (Math.abs(v) + Math.abs(w)) / 2.0;
43: return Math.abs(w - v) < scaled;
44: }
45:
46: public static boolean close(double v, double w) {
47: return close(v, w, CLOSE);
48: }
49:
50: /**
51: * Returns floor(v) except when v is very close to the next number, when it
52: * returns ceil(v);
53: */
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: }
|