01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26: package org.cougaar.util;
27:
28: /**
29: * Additional math functions.
30: **/
31: public class MoreMath {
32:
33: /**
34: * Returns the value ln[gamma(xx)] for xx > 0. Full accuracy is
35: * obtained for xx > 1. For 0 < xx < 1, the reflection formula (6.1.4)
36: * can be used first. Internal arithmetic will be done in double
37: * precision, a nicety that you can omit if five figure accuracy is
38: * good enough.
39: * Algorithm from "Recipes for Scientific Computing"
40: **/
41: public static double gammaLn(double xx) {
42: double x = xx - 1.0;
43: double tmp = x + 5.5;
44: tmp = (x + 0.5) * Math.log(tmp) - tmp;
45: double ser = 1.0;
46: ser += 76.18009173e0 / (x += 1.0);
47: ser -= 86.50532033e0 / (x += 1.0);
48: ser += 24.01409822e0 / (x += 1.0);
49: ser -= 1.231739516e0 / (x += 1.0);
50: ser += .120858003e-2 / (x += 1.0);
51: ser -= 0.536382e-5 / (x += 1.0);
52: return tmp + Math.log(2.50662827465e0 * ser);
53: }
54:
55: /**
56: * Compares two doubles for near equality. Equality comparisons of
57: * doubles is instrinsically difficult because of rounding. This
58: * methods checks the relative difference between two doubles and
59: * their values.
60: * @param a one of the values to compare
61: * @param b the other value to compare
62: * @return true if the difference is less than 1 part per billion
63: **/
64: public static boolean nearlyEquals(double a, double b) {
65: if (a == b)
66: return true;
67: return Math.abs(a - b) / (Math.abs(a) + Math.abs(b)) < .5e-9;
68: }
69:
70: /**
71: * Compares two doubles for near equality. Equality comparisons of
72: * doubles is instrinsically difficult because of rounding. This
73: * methods checks the relative difference between two doubles and
74: * their values.
75: * @param a one of the values to compare
76: * @param b the other value to compare
77: * @param tolerance the allowable difference
78: * @return true if the difference divided by the sum of the absolute
79: * values of the parameters is less than the tolerance
80: **/
81: public static boolean nearlyEquals(double a, double b,
82: double tolerance) {
83: if (a == b)
84: return true;
85: return Math.abs(a - b) / (Math.abs(a) + Math.abs(b)) < tolerance;
86: }
87: }
|