commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gilles (JIRA)" <j...@apache.org>
Subject [jira] Commented: (MATH-471) MathUtils.equals(double, double) does not work properly for floats
Date Thu, 13 Jan 2011 11:07:45 GMT

    [ https://issues.apache.org/jira/browse/MATH-471?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12981216#action_12981216
] 

Gilles commented on MATH-471:
-----------------------------

{quote}
MathUtils.equals(double, double) does not work properly for floats.

There is no equals(float,float) so float parameters are automatically promoted to double.
However, that is not necessarily appropriate, given that the ULP for a double is much smaller
than the ULP for a float.
{quote}

This is not a bug, but expected behaviour. But I certainly agree that it does not hurt to
mention the conversion issue for the unwary.

However, shouldn't there be emphasis, in the user guide, that CM is a "double" precision library?
A quick poll of the code gives the following numbers:
Occurrences of the string "float ": *41* (roughly half of which were introduced with this
patch)
Occurrences of the string "double ": *4061*

Also, I'm curious as to what use case you were having that requires comparing {{float}} numbers.

Finally if we want to help users avoid such pitfalls, I think that we should consider refactoring
{{MathUtils}} so that similar methods that should behave differently for different types are
in _different_ classes (exactly as with classes {{Float}} and {{Double}}). Thus, we should
create {{MathUtilsDouble}} (or assume that the current {{MathUtils}} is for {{double}} utilities)
and {{MathUtilsFloat}}.



> MathUtils.equals(double, double) does not work properly for floats
> ------------------------------------------------------------------
>
>                 Key: MATH-471
>                 URL: https://issues.apache.org/jira/browse/MATH-471
>             Project: Commons Math
>          Issue Type: Bug
>            Reporter: Sebb
>         Attachments: Math471.patch
>
>
> MathUtils.equals(double, double) does not work properly for floats.
> There is no equals(float,float) so float parameters are automatically promoted to double.
However, that is not necessarily appropriate, given that the ULP for a double is much smaller
than the ULP for a float.
> So for example:
> {code}
> double oneDouble = 1.0d;
> assertTrue(MathUtils.equals(oneDouble, Double.longBitsToDouble(1 + Double.doubleToLongBits(oneDouble))));
// OK
> float oneFloat = 1.0f;
> assertTrue(MathUtils.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat))));
// FAILS
> float  f1 = 333.33334f;
> double d1 = 333.33334d;
> assertTrue(MathUtils.equals(d1, f1)); // FAILS
> {code}
> I think the equals() methods need to be duplicated with the appropriate changes for floats
to avoid any problems with the promotion of floats.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message