0
Debug.Log(temp3.y + " * " + "(1f - ((" + CurrentSide + " + 1) / (" + Sides + " + 1)))");
Debug.Log( temp3.y * (1f - ((CurrentSide + 1) / (Sides + 1))) );
Debug.Log(((CurrentSide + 1) / (Sides + 1))

My outputs are:

0.6 * (1f - ((0 + 1) / (1 + 1)))

0.6

0

why is (0+1) / (1+1) = 0

2 Answers2

2

It needs atleast one of the operand to be a floating point type (decimal, double or float).

Below is an example with an int and floating point types:

var intValue =  (0 + 1) / (1 + 1) // 1/2 = 0 (datatype is int)
var decimalValue = (0 + 1m) / (1 + 1)// 1/2 = 0.5 (datatype is decimal)
var floatValue = (0 + 1f) / (1 + 1)// 1/2 = 0.5 (datatype is float)
var doubleValue = (0 + 1d) / (1 + 1)// 1/2 = 0.5 (datatype is double)

in this case the numerator is a decimal, so the result of the operation is a decimal as well.

Look into integer and floating point divisions in C#.

Zee
  • 622
  • 4
  • 13
  • _"It needs atleast one of the operand to be a decimal."_ - no, it does not need specifically `decimal`, `float` or `double` will do just fine too. – Guru Stron Mar 12 '22 at 21:18
1

You have found an example of integer division.

(0+1)/(1+1) = 1/2 = 0 R 1. The / operator gives you the 0 part of the result, the % operator gives you the 1 of the result.

If you want a floating point number, at least one of the numbers must be a floating point number. Try

Debug.Log(((CurrentSide + 1.0) / (Sides + 1.0)
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222