Coverage details for edu.uci.ics.jung.utils.NumericalPrecision

LineHitsSource
1 /*
2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
3 * of California
4 * All rights reserved.
5 *
6 * This software is open-source under the BSD license; see either
7 * "license.txt" or
8 * http://jung.sourceforge.net/license.txt for a description.
9 */
10 package edu.uci.ics.jung.utils;
11  
12 import java.io.PrintStream;
13 /**
14  * Implements additional mathematical functions to within numerical
15  * precision tolerances, and determines the parameters of the
16  * floating point representation.
17  */
180public final class NumericalPrecision {
19     /**
20      * Typical meaningful precision for numerical calculations.
21      */
2215    static private double defaultNumericalPrecision = 0;
23     /**
24      * Typical meaningful small number for numerical calculations.
25      */
2615    static private double smallNumber = 0;
27     /**
28      * Radix used by floating-point numbers.
29      */
3015    static private int radix = 0;
31     /**
32      * Largest positive value which, when added to 1.0, yields 0.
33      */
3415    static private double machinePrecision = 0;
35     /**
36      * Largest positive value which, when subtracted to 1.0, yields 0.
37      */
3815    static private double negativeMachinePrecision = 0;
39     /**
40      * Smallest number different from zero.
41      */
4215    static private double smallestNumber = 0;
43     /**
44      * Largest possible number
45      */
4615    static private double largestNumber = 0;
47     /**
48      * Largest argument for the exponential
49      */
5015    static private double largestExponentialArgument = 0;
51     /**
52      * Values used to compute human readable scales.
53      */
5415    private static final double scales[] = { 1.25, 2, 2.5, 4, 5, 7.5, 8, 10 };
5515    private static final double semiIntegerScales[] =
56         { 2, 2.5, 4, 5, 7.5, 8, 10 };
5715    private static final double integerScales[] = { 2, 4, 5, 8, 10 };
58  
59     private static void computeLargestNumber() {
600        double floatingRadix = getRadix();
610        double fullMantissaNumber =
62             1.0d - floatingRadix * getNegativeMachinePrecision();
630        while (!Double.isInfinite(fullMantissaNumber)) {
640            largestNumber = fullMantissaNumber;
650            fullMantissaNumber *= floatingRadix;
66         }
670    }
68     private static void computeMachinePrecision() {
6914        double floatingRadix = getRadix();
7014        double inverseRadix = 1.0d / floatingRadix;
7114        machinePrecision = 1.0d;
7214        double tmp = 1.0d + machinePrecision;
73756        while (tmp - 1.0d != 0.0d) {
74742            machinePrecision *= inverseRadix;
75742            tmp = 1.0d + machinePrecision;
76         }
7714    }
78     private static void computeNegativeMachinePrecision() {
790        double floatingRadix = getRadix();
800        double inverseRadix = 1.0d / floatingRadix;
810        negativeMachinePrecision = 1.0d;
820        double tmp = 1.0d - negativeMachinePrecision;
830        while (tmp - 1.0d != 0.0d) {
840            negativeMachinePrecision *= inverseRadix;
850            tmp = 1.0d - negativeMachinePrecision;
86         }
870    }
88     private static void computeRadix() {
8914        double a = 1.0d;
90         double tmp1, tmp2;
91         do {
9214            a += a;
9314            tmp1 = a + 1.0d;
9414            tmp2 = tmp1 - a;
9514        } while (tmp2 - 1.0d != 0.0d);
9614        double b = 1.0d;
9728        while (radix == 0) {
9814            b += b;
9914            tmp1 = a + b;
10014            radix = (int) (tmp1 - a);
101         }
10214    }
103     private static void computeSmallestNumber() {
1040        double floatingRadix = getRadix();
1050        double inverseRadix = 1.0d / floatingRadix;
1060        double fullMantissaNumber =
107             1.0d - floatingRadix * getNegativeMachinePrecision();
1080        while (fullMantissaNumber != 0.0d) {
1090            smallestNumber = fullMantissaNumber;
1100            fullMantissaNumber *= inverseRadix;
111         }
1120    }
113     public static double defaultNumericalPrecision() {
11434        if (defaultNumericalPrecision == 0)
11514            defaultNumericalPrecision = Math.sqrt(getMachinePrecision());
11634        return defaultNumericalPrecision;
117     }
118     /**
119      * @return boolean true if the difference between a and b is
120      * less than the default numerical precision
121      * @param a double
122      * @param b double
123      */
124     public static boolean equal(double a, double b) {
1250        return equal(a, b, defaultNumericalPrecision());
126     }
127     /**
128      * @return boolean true if the relative difference between a and b
129      * is less than precision
130      * @param a double
131      * @param b double
132      * @param precision double
133      */
134     public static boolean equal(double a, double b, double precision) {
135285        double norm = Math.max(Math.abs(a), Math.abs(b));
136285        return norm < precision || Math.abs(a - b) < precision * norm;
137     }
138     public static double getLargestExponentialArgument() {
1390        if (largestExponentialArgument == 0)
1400            largestExponentialArgument = Math.log(getLargestNumber());
1410        return largestExponentialArgument;
142     }
143     /**
144      * (c) Copyrights Didier BESSET, 1999, all rights reserved.
145      */
146     public static double getLargestNumber() {
1470        if (largestNumber == 0)
1480            computeLargestNumber();
1490        return largestNumber;
150     }
151     public static double getMachinePrecision() {
15214        if (machinePrecision == 0)
15314            computeMachinePrecision();
15414        return machinePrecision;
155     }
156     public static double getNegativeMachinePrecision() {
1570        if (negativeMachinePrecision == 0)
1580            computeNegativeMachinePrecision();
1590        return negativeMachinePrecision;
160     }
161     public static int getRadix() {
16214        if (radix == 0)
16314            computeRadix();
16414        return radix;
165     }
166     public static double getSmallestNumber() {
1670        if (smallestNumber == 0)
1680            computeSmallestNumber();
1690        return smallestNumber;
170     }
171     public static void printParameters(PrintStream printStream) {
1720        printStream.println("Floating-point machine parameters");
1730        printStream.println("---------------------------------");
1740        printStream.println(" ");
1750        printStream.println("radix = " + getRadix());
1760        printStream.println("Machine precision = " + getMachinePrecision());
1770        printStream.println(
178             "Negative machine precision = " + getNegativeMachinePrecision());
1790        printStream.println("Smallest number = " + getSmallestNumber());
1800        printStream.println("Largest number = " + getLargestNumber());
1810        return;
182     }
183     public static void reset() {
1840        defaultNumericalPrecision = 0;
1850        smallNumber = 0;
1860        radix = 0;
1870        machinePrecision = 0;
1880        negativeMachinePrecision = 0;
1890        smallestNumber = 0;
1900        largestNumber = 0;
1910    }
192     /**
193      * This method returns the specified value rounded to
194      * the nearest integer multiple of the specified scale.
195      *
196      * @param value number to be rounded
197      * @param scale defining the rounding scale
198      * @return rounded value
199      */
200     public static double roundTo(double value, double scale) {
2010        return Math.round(value / scale) * scale;
202     }
203     /**
204      * Round the specified value upward to the next scale value.
205      * @param value the value to be rounded.
206      * @param integerValued a flag specified whether integer scale are used, otherwise double scale is used.
207      * @return a number rounded upward to the next scale value.
208      */
209     public static double roundToScale(double value, boolean integerValued) {
210         double[] scaleValues;
2112        int orderOfMagnitude =
212             (int) Math.floor(Math.log(value) / Math.log(10.0));
2132        if (integerValued) {
2140            orderOfMagnitude = Math.max(1, orderOfMagnitude);
2150            if (orderOfMagnitude == 1)
2160                scaleValues = integerScales;
2170            else if (orderOfMagnitude == 2)
2180                scaleValues = semiIntegerScales;
219             else
2200                scaleValues = scales;
221         } else
2222            scaleValues = scales;
2232        double exponent = Math.pow(10.0, orderOfMagnitude);
2242        double rValue = value / exponent;
22513        for (int n = 0; n < scaleValues.length; n++) {
22613            if (rValue <= scaleValues[n])
2272                return scaleValues[n] * exponent;
228         }
2290        return exponent; // Should never reach here
230     }
231  
232     /**
233      * Returns the smallest number that the system can handle.
234      *
235      * @return double The smallest number the machine can handle.
236      */
237     public static double smallNumber() {
2380        if (smallNumber == 0)
2390            smallNumber = Math.sqrt(getSmallestNumber());
2400        return smallNumber;
241     }
242 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.