Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a neutral element in IEEE754 with regards to addition

Consider the following code snippet

double id = ?;
double res;
long unsigned *res_u = (long unsigned*)&res;

long unsigned i;
for (i = 0; i < (long unsigned)-1; i++){
    double *d1 = (double*)&i;
    res = id + *d1;
    assert(*res_u == i);
}

My question: Is there a value for id, so that the assertion holds for all i? In other words for the mathematicians among us: is there double that is the neutral element for addition?

like image 656
niklasfi Avatar asked Apr 29 '13 20:04

niklasfi


1 Answers

-0. is paradoxically the floating-point value that serves as neutral for addition.

+0. nearly is, but -0. + (+0.) makes +0..

Apart from that, +inf + (-0.) makes +inf, -inf + (-0.) makes -inf, and NaN + (-0.) makes NaN.

like image 126
Pascal Cuoq Avatar answered Nov 03 '22 07:11

Pascal Cuoq