Re:One does not simply calculate the absolute value
by andi999 on Sunday August 22, 2021 @04:33PM
(#28269330)
Couldn't you also implement max as return a>b? a:b;
(changing greater equal to just greater). Would the problem then still persist?
Re:One does not simply calculate the absolute value
by kmm on Sunday August 22, 2021 @04:46PM
(#28269452)
Positive and negative zero cannot be distinguished by normal equality checks, meaning +0.0 == -0.0. Therefore, for consistency's sake +0.0 > -0.0 ought to be false, which would yield a return value of -0.0 for max(+0.0, -(+0.0))
Re:One does not simply calculate the absolute value
by gvx on Sunday August 22, 2021 @04:50PM
(#28269477)
That wouldn't solve it: max(0.0, -0.0) would be false ? 0.0 : -0.0
The problem is that 0.0 == -0.0. As long as you don't take care of signed zero, you cannot guarantee that abs(x) has no negative sign.
Looking at the actual implementation[0], doubleToRawLongBits is used again here to take care of this edge case. So it would work, but a lot slower than the version given in the article.
0: https://github.com/openjdk/jdk/blob/36e2ddad4d2ef3ce27475af6...