Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is - (- x) = x correct in floating point arithmetic?

Suppose x is a floating point number, I want to know whether - ( - x) equals to x in general?

Let us ignore corner cases such as x = max_floating_point_number / min_floating_number.

like image 266
zell Avatar asked Dec 11 '22 02:12

zell


1 Answers

In general, yes; negation of a floating-point value induces no error or loss of precision. (It just involves flipping a bit.)

The one exception to this is NaN. -(-(NaN)) is NaN, but NaN does not compare equal to any value, even another NaN. That's firmly in the realm of "corner cases", though.

like image 105
Sneftel Avatar answered Jan 28 '23 15:01

Sneftel