JavaScript is an implementation of ECMAScript, and the ECMAScript specification says IEEE 754 arithmetic is used, with the IEEE-754 “double precision” (binary64) format. Clause 5.2.5 says “… When applied to Numbers, the operators refer to the relevant operations within IEEE 754-2019…”
In IEEE 754, and in any reasonable floating-point system, the result of an operation is the exact mathematical result rounded according to whichever rounding rule is selected (such as round-to-nearest-ties-to-even, round toward zero, round upward, or round downward). IEEE 754-2019 4.3 says:
… Except where stated otherwise, every operation shall be performed as if it first produced an intermediate result correct to infinite precision and with unbounded range, and then rounded that result according to one of the attributes in this clause…
Since x
+x
and 2•x
have the same mathematical result, the floating point operations x+x
and 2*x
must produce the same computed result. Both of them would have the same mathematical result with the same rounding rule applied, so the computed result must be the same.
The above covers cases where x
is a number, including +∞ and −∞. If x
is a NaN
, x+x
and 2*x
also produce a NaN
, so the result is again the same. (Note that, in this case, x+x == 2*x
would evaluate as false because a NaN is not equal to anything, not even itself. Nonetheless, the two operations produce the same result; the program behavior would be the same if 2*x
were used in place of x+x
or vice-versa.)