Multiplying a float
/double
by 2 should be exact as it just increases exponent part.
What about adding two floating numbers of same value? Will it be exact?
(If it matters, hardware is standard x86-64).
Multiplying a float
/double
by 2 should be exact as it just increases exponent part.
What about adding two floating numbers of same value? Will it be exact?
(If it matters, hardware is standard x86-64).
When using IEEE-754 floating-point arithmetic or any reasonable floating-point arithmetic, the result of x o y, where “o” is any operation, is the result you would get by performing x o y using real-number arithmetic and then rounding the real number result to the nearest value representable in the floating-point format as direct by the rounding rule in use for the operation. (Round-to-nearest-ties-to-even is the most common rule and the most common default, but there are other rules such as round upward [toward +∞], round downward, and round toward zero.) (This rule for determining the floating-point result applies to elementary operations. For hard-to-calculate functions, such as pow
, correctly rounded results are not always provided.)
Therefore, the result that is obtained depends only on what the real-number result would be. In the calculation 2 • x, the real-number result would be 2x. In the calculation x + x, the real-number result would be 2x. Since these operations have the same real-number result, they will have the same floating-point result.
If the base of the floating-point format is two and we start with a number x representable in the format, then 2x is also representable unless it overflows the finite range of the format. If it does not overflow, the result is 2x, so the operation is exact.
If the floating-point format uses some other base, the result might not be exact. For example, with a three-digit decimal format, 7.89 is representable, but 2•7.89 = 15.78 is not representable, so the produced result, 15.8 will not be exact. However, it will be the same for 2•7.89 and 7.89+7.89. Even with bases that are powers of two, multiplying by two might not be exact. For example, with a three-digit octal format, 5.438 is representable, but 2•5.438 = 13.068 is not representable.