I love these ;-)
Ian Rogers (JIRA) wrote:
> Math.max(double, double) gives wrong answer when Math.max(-0.0d, 0.0d)
> ----------------------------------------------------------------------
>
> Key: HARMONY-6242
> URL: https://issues.apache.org/jira/browse/HARMONY-6242
> Project: Harmony
> Issue Type: Bug
> Components: Classlib
> Affects Versions: 5.0M10
> Environment: Any, my test environment is x64 Linux with MRP
> (http://mrp.codehaus.org/)
> Reporter: Ian Rogers
> Fix For: 5.0M11
>
>
> In the code:
>
> public static double max(double d1, double d2) {
> if (d1 > d2)
> return d1;
> if (d1 < d2)
> return d2;
> /* if either arg is NaN, return NaN */
> if (d1 != d2)
> return Double.NaN;
> /* max( +0.0,-0.0) == +0.0 */
> if (d1 == 0.0 && (d1 != -0.0d || d2 != -0.0d))
> return 0.0;
> return d1;
> }
>
> This test is never true:
>
> if (d1 == 0.0 && (d1 != -0.0d || d2 != -0.0d))
>
> as 0.0 == -0.0 and d2 must be 0.0 or -0.0. This means that in the case of two
> 0.0 parameters d1 is returned, which is the incorrect behaviour if d2 is 0.0
> and d1 -0.0.
>
> A simple test:
>
> System.out.println(Math.max(-0.0d, 0.0d));
>
> prints -0.0 with Harmony and 0.0 with a non-Harmony classlib.
>
So is the answer to replace the last test with:
if (Double.doubleToRawLongBits(d1) == Double.doubleToRawLongBits(-0.0d)) {
return d2;
}
return d1;
Regards,
Tim