Use a decimal to mark a type as a float. 1.0
is always floating point. Hence 1.0/16
or 1/16.0
will always be floating point.
Numeric literals are treated as floats if used in a floating-point context. Remember that operators like *
and /
are left-associative. So in your first example, the code is equivalent to (float(...) * 1) / 16
. If you put the 1/16
in parentheses then it would be equal to 0
and that would be multiplied with the vector, e.g float2(...) * (1/16) == float2(0,0)
In your second example, the 1/16
is seen by itself, computed (as integers, because there's no float involved), and then the result of the expression is converted to float for the float2
construction. You need to cause either the 1
or the 16
to be a float in order for the /
operator to be in a float context and force the other value to be treated as a float.
f
suffix in HLSL. Anything with a decimal is a 32-bit single-precision float and then you can use theL
suffix to make it a 64-bit double. \$\endgroup\$