You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Phil Steitz <ph...@gmail.com> on 2009/02/21 17:31:48 UTC
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
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
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