Why adding 0 to a variable?

Viewed 215

I am porting C source code to Delphi.

I find in that source a lot of occurrences of code similar to this (Here line 190):

if (x != 0) { *sinx += (real)(0); *cosx += (real)(0); }

We are in this context:

typedef double real;
real  x;
real* sinx;
real* cosx;

I wonder how adding 0 could be useful.

1 Answers

Thanks for all having commented my question. It pushed me on the right track:

Adding 0.0 to -0.0 gives the result 0.0. Adding 0.0 to anything else has no effect. To say it in other words, adding 0.0 to a value will not change the value unless the value was -0.0 in which case the result will be 0.0.

This article explain that in IEEE 754 binary floating-point numbers, zero is a signed quantity. You can have -0.0 and +0.0.

The C source code makes a lot of efforts to preserve precision in floating point operation and for that purpose must take care of negative zero.

I checked that MSVC and Delphi handle floating point values (double data type) exactly the same way and so I simply have to exactly translate the C-code to Delphi and it works the same in both languages.

Related