You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/02/21 14:36:09 UTC
svn commit: r746506 - in /commons/proper/math/trunk: pom.xml
src/java/org/apache/commons/math/util/MathUtils.java
src/site/xdoc/changes.xml
src/test/org/apache/commons/math/util/MathUtilsTest.java
Author: luc
Date: Sat Feb 21 13:36:08 2009
New Revision: 746506
URL: http://svn.apache.org/viewvc?rev=746506&view=rev
Log:
Added a MathUtils method to check equality given some error bounds
JIRA: MATH-247
Modified:
commons/proper/math/trunk/pom.xml
commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
commons/proper/math/trunk/src/site/xdoc/changes.xml
commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
Modified: commons/proper/math/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/pom.xml?rev=746506&r1=746505&r2=746506&view=diff
==============================================================================
--- commons/proper/math/trunk/pom.xml (original)
+++ commons/proper/math/trunk/pom.xml Sat Feb 21 13:36:08 2009
@@ -142,6 +142,9 @@
<name>Bob MacCallum</name>
</contributor>
<contributor>
+ <name>Benjamin McCann</name>
+ </contributor>
+ <contributor>
<name>Fredrik Norin</name>
</contributor>
<contributor>
Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java?rev=746506&r1=746505&r2=746506&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java Sat Feb 21 13:36:08 2009
@@ -369,6 +369,19 @@
}
/**
+ * Returns true iff both arguments are equal or within the range of allowed
+ * error (inclusive).
+ *
+ * @param x first value
+ * @param y second value
+ * @param eps the amount of absolute error to allow
+ * @return true if the values are equal or within range of each other
+ */
+ public static boolean equals(double x, double y, double eps) {
+ return x == y || (x < y && (x + eps) >= y) || (x > y && x <= (y + eps));
+ }
+
+ /**
* Returns true iff both arguments are null or have same dimensions
* and all their elements are {@link #equals(double,double) equals}
*
Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=746506&r1=746505&r2=746506&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sat Feb 21 13:36:08 2009
@@ -39,6 +39,9 @@
</properties>
<body>
<release version="2.0" date="TBD" description="TBD">
+ <action dev="luc" type="add" issue="MATH-247" due-to="Benjamin McCann">
+ Added a MathUtils method to check equality given some error bounds.
+ </action>
<action dev="psteitz" type="add" issue="MATH-114" due-to="John Gant">
Added PearsonsCorrelation class to compute correlation matrices, standard
errors and p-values for correlation coefficients.
Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java?rev=746506&r1=746505&r2=746506&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java Sat Feb 21 13:36:08 2009
@@ -335,6 +335,14 @@
}
}
+ public void testEqualsWithAllowedDelta() {
+ assertTrue(MathUtils.equals(153.0000, 153.0000, .0001));
+ assertTrue(MathUtils.equals(153.0000, 153.0001, .0001));
+ assertTrue(MathUtils.equals(152.9999, 153.0000, .0001));
+ assertFalse(MathUtils.equals(153.0000, 153.0001, .00001));
+ assertFalse(MathUtils.equals(152.9998, 153.0000, .0001));
+ }
+
public void testArrayEquals() {
assertFalse(MathUtils.equals(new double[] { 1d }, null));
assertFalse(MathUtils.equals(null, new double[] { 1d }));
Re: svn commit: r746506 - in /commons/proper/math/trunk: pom.xml
src/java/org/apache/commons/math/util/MathUtils.java src/site/xdoc/changes.xml
src/test/org/apache/commons/math/util/MathUtilsTest.java
Posted by Phil Steitz <ph...@gmail.com>.
Luc Maisonobe wrote:
> Phil Steitz a écrit :
>
>> luc@apache.org wrote: }
>>
>>>
>>> /**
>>> + * Returns true iff both arguments are equal or within the range
>>> of allowed
>>> + * error (inclusive).
>>> + * + * @param x first value
>>> + * @param y second value
>>> + * @param eps the amount of absolute error to allow
>>> + * @return true if the values are equal or within range of each
>>> other
>>> + */
>>> + public static boolean equals(double x, double y, double eps) {
>>> + return x == y || (x < y && (x + eps) >= y) || (x > y && x <= (y
>>> + eps));
>>> + }
>>>
>>>
>> Any reason not to just code this as Math.abs(x - y) <= eps ?
>>
>
> I think infinities are handled properly by the first x == y and would
> not be handled by the subtraction.
> However thinking further about it, there is another problem with the
> current implementation, it does not handle NaN the same way as equals
> with no tolerance.
>
> So what about:
>
> return equals(x, y) || Math.abs(x - y) <= eps
>
> Luc
>
>
+1 - and we should include javadoc and test cases for infinities and NaNs
Phil
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r746506 - in /commons/proper/math/trunk: pom.xml
src/java/org/apache/commons/math/util/MathUtils.java src/site/xdoc/changes.xml
src/test/org/apache/commons/math/util/MathUtilsTest.java
Posted by Luc Maisonobe <Lu...@free.fr>.
Phil Steitz a écrit :
> luc@apache.org wrote: }
>>
>> /**
>> + * Returns true iff both arguments are equal or within the range
>> of allowed
>> + * error (inclusive).
>> + * + * @param x first value
>> + * @param y second value
>> + * @param eps the amount of absolute error to allow
>> + * @return true if the values are equal or within range of each
>> other
>> + */
>> + public static boolean equals(double x, double y, double eps) {
>> + return x == y || (x < y && (x + eps) >= y) || (x > y && x <= (y
>> + eps));
>> + }
>>
> Any reason not to just code this as Math.abs(x - y) <= eps ?
I think infinities are handled properly by the first x == y and would
not be handled by the subtraction.
However thinking further about it, there is another problem with the
current implementation, it does not handle NaN the same way as equals
with no tolerance.
So what about:
return equals(x, y) || Math.abs(x - y) <= eps
Luc
>
> Phil
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r746506 - in /commons/proper/math/trunk: pom.xml
src/java/org/apache/commons/math/util/MathUtils.java src/site/xdoc/changes.xml
src/test/org/apache/commons/math/util/MathUtilsTest.java
Posted by Phil Steitz <ph...@gmail.com>.
luc@apache.org wrote: }
>
> /**
> + * Returns true iff both arguments are equal or within the range of allowed
> + * error (inclusive).
> + *
> + * @param x first value
> + * @param y second value
> + * @param eps the amount of absolute error to allow
> + * @return true if the values are equal or within range of each other
> + */
> + public static boolean equals(double x, double y, double eps) {
> + return x == y || (x < y && (x + eps) >= y) || (x > y && x <= (y + eps));
> + }
>
Any reason not to just code this as Math.abs(x - y) <= eps ?
Phil
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org