What operations can make floats leave a [0, 1] range?
Restricting this answer to IEEE754 if I may.
0
, 1
, and 2
can all be represented exactly as a float
. The arithmetic operators are required to return the best floating point value possible. Since x
and y
are neither greater than 1, their sum cannot be greater than 2 since then otherwise there would exist a better float
for the sum. Put another way, the sum of two float
s a little less than 1
cannot be greater than 2
.
The same applies to the product.
The third one requires a clamp since there are no guarantees that std::pow(x, y)
returns the best possible float
.