views:

125

answers:

1

I came across this code in some existing codebase:

double rad = ComputeCurviness();
double off = Math.Abs(rad);
if (rad < 0) off = -off;

It seems to be basically just making off equal to rad. The variables are used interchangeably later in the code. Is there any reason to leave this code in?

+6  A: 

If rad is -0.0, off will be +0.0. You'd have to inspect the code to see whether this would actually make a difference. The two are equivalent when it comes to calculations and comparisons, but -0.0 is negative, which you can detect if you try hard enough.

brone
Who was such a downer that even *zero* had to be expressable as a negative?
Anthony Pegram
+1 Ack, I forgot to check +/- 0 when thinking about outliers. Nice catch. I posted sample code to differentiate between +/- 0 in response to another question: http://stackoverflow.com/questions/1043816/how-to-get-a-0-result-in-floating-point-calculations-and-distinguish-it-from-0/1043909#1043909
Brian
@Anthony: IEEE 754
Brian