Name: mf23781 Date: 12/09/97
The behaviour of abs(-0.0) = -0.0 leads to oddities such as
if (1.0 / abs(x) < 0.0) {
}
sometimes being true (when x = -0.0)
The behaviour of abs(-0.0) in JDK 1.1 still appears inconsistent:
The Java code is something like:
public static double abs(double a) {
return (a < 0) ? -a : a;
}
The behaviour also appears inconsistent in JDK 1.2:
public static double abs(double a) {
return (a < 0 || a == -0.0D) ? -a : a;
}
If a = +0.0, then a == -0.0D is true (both zeros are equal),
so the statement returns -0.0 for abs(+0.0)
Possible solution is:
return (a <= 0) ? 0.0 - a : a;
(because 0.0 - (-0.0) = 0.0, 0.0 - (0.0) = 0.0 )
======================================================================
- duplicates
-
JDK-4035455 Math.abs(-0.0f) and Math.abs(-0.0d) return -0.0f and -0.0d, respectively.
- Closed