KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > ExtraMath


1 // Copyright (c) Corporation for National Research Initiatives
2

3 package org.python.core;
4
5 /**
6  * A static utility class with two additional math functions.
7  */

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     /**
34      * Are v and w "close" to each other? Uses a scaled tolerance.
35      */

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     /**
51      * Returns floor(v) except when v is very close to the next number,
52      * when it 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 }
59
Popular Tags