Skip to main content
precedence -> associative
Source Link
Nathan Reed
  • 33.7k
  • 3
  • 93
  • 116

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 / haveare left-precedenceassociative. So in your first example, the code is equivalent to (float(...) * 1) / 16. If you put the 1/16 in parenthesisparentheses 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.

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 / have left-precedence. So in your first example, the code is equivalent to (float(...) * 1) / 16. If you put the 1/16 in parenthesis 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.

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.

Source Link
Sean Middleditch
  • 42k
  • 4
  • 91
  • 133

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 / have left-precedence. So in your first example, the code is equivalent to (float(...) * 1) / 16. If you put the 1/16 in parenthesis 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.