c++floating-pointeigen3

negative zero in identity


I need to know if there is any detrimental impact on computations with this code

MatrixXd A = -Matrix<double,20,20>::Identity();

which creates the identity matrix with negative ones. Printing out the above matrix yields -0 for the off-diagonal elements which from a mathematical point of view makes no sense. I'm asking this because the matrix needs to be passed to optimisation solver.


Solution

  • Floating point -0.0 and 0.0 have the same mathematical value. The -0.0 in the matrix should pose no problem for an optimisation solver. Consider -0.0 does come up in other subtle cases such as rounding of very small negative intermediate numbers toward 0.0 (e.g. DBL_TRUE_MIN/-3.0) and are not likely a special case for the optimizer.

    Some operations use the sign of a zero to form distinctive results. I could see your optimisation solver forming a -infinity when a +infinity would otherwise occur.

    Adding 0.0 to a floating-point value, which might be -0.0, will normally drop the negative sign in the sum yet not affect the mathematical value.


    I think of -0.0 as: