How to make the division of 2 ints produce a float instead of another int?

后端 未结 9 1191
天命终不由人
天命终不由人 2020-11-22 08:24

In another Bruce Eckels exercise in calculating velocity, v = s / t where s and t are integers. How do I make it so the division cranks out a float?

         


        
9条回答
  •  佛祖请我去吃肉
    2020-11-22 08:56

    Cast one of the integers/both of the integer to float to force the operation to be done with floating point Math. Otherwise integer Math is always preferred. So:

    1. v = (float)s / t;
    2. v = (float)s / (float)t;
    

提交回复
热议问题