The following example is from the page 14 of the book Discovering Modern C++, Peter Gottschling. The author states:
To illustrate this conversion behavior, let us look at the following example:
long l = 1234567890123;
long l2 = l + 1.0f - 1.0; // imprecise
long l3 = l + (1.0f - 1.0); // precise
This leads on the author's platform to:
l2 = 1234567954431;
l3 = 1234567890123;
My question is is that exactly what causes this imprecision? Is it due to left-associativity of addition and subtraction, so that l2
is calculated as (l + 1.0f) - 1.0
? If so, surely the value range 3.4E +/- 38 (7 digits)
of float
(see) covers the value 1234567890123
, so that to my knowledge narrowing shouldn't be an issue.