From 77e1e9e2f1cddea196df5f392b12fbe8ff2196fd Mon Sep 17 00:00:00 2001 From: kiran Date: Thu, 23 Apr 2009 17:45:19 +0000 Subject: [PATCH] Added a static class to house useful math methods. All this has at the moment are methods for comparing doubles and floats, but I suggest that the bulk of our little math methods should be added here to avoid filling up Utils.java with so much random stuff. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@505 348d0f76-0448-11de-a6fe-93d51630548a --- .../broadinstitute/sting/utils/MathUtils.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 java/src/org/broadinstitute/sting/utils/MathUtils.java diff --git a/java/src/org/broadinstitute/sting/utils/MathUtils.java b/java/src/org/broadinstitute/sting/utils/MathUtils.java new file mode 100755 index 000000000..c76bbc3b2 --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/MathUtils.java @@ -0,0 +1,59 @@ +package org.broadinstitute.sting.utils; + +/** + * MathUtils is a static class (no instantiation allowed!) with some useful math methods. + * + * @author Kiran Garimella + */ +public class MathUtils { + /** Private constructor. No instantiating this class! */ + private MathUtils() {} + + /** + * Compares double values for equality (within 1e-6), or inequality. + * + * @param a the first double value + * @param b the second double value + * @return -1 if a is greater than b, 0 if a is equal to be within 1e-6, 1 if b is greater than a. + */ + public static byte compareDoubles(double a, double b) { return compareDoubles(a, b, 1e-6); } + + /** + * Compares double values for equality (within epsilon), or inequality. + * + * @param a the first double value + * @param b the second double value + * @param epsilon the precision within which two double values will be considered equal + * @return -1 if a is greater than b, 0 if a is equal to be within epsilon, 1 if b is greater than a. + */ + public static byte compareDoubles(double a, double b, double epsilon) + { + if (Math.abs(a - b) < epsilon) { return 0; } + if (a > b) { return -1; } + return 1; + } + + /** + * Compares float values for equality (within 1e-6), or inequality. + * + * @param a the first float value + * @param b the second float value + * @return -1 if a is greater than b, 0 if a is equal to be within 1e-6, 1 if b is greater than a. + */ + public static byte compareFloats(float a, float b) { return compareFloats(a, b, 1e-6f); } + + /** + * Compares float values for equality (within epsilon), or inequality. + * + * @param a the first float value + * @param b the second float value + * @param epsilon the precision within which two float values will be considered equal + * @return -1 if a is greater than b, 0 if a is equal to be within epsilon, 1 if b is greater than a. + */ + public static byte compareFloats(float a, float b, float epsilon) + { + if (Math.abs(a - b) < epsilon) { return 0; } + if (a > b) { return -1; } + return 1; + } +}