C++. Dividing 1 by any number gives 0

前端 未结 3 1321
不思量自难忘°
不思量自难忘° 2021-01-18 10:19

When I try to divide 1/60 or 1/(60*60) it gives 0. Even in debugger window. I am a bit confused what it could be, because 2/3 or 2.5/6 give results.

My code:

3条回答
  •  Happy的楠姐
    2021-01-18 10:51

    The line double k1 = 1/60 will be evaluated by the compiler as a compile-time constant. Since there is no '.0' on the end of the number, 1/60 will be evaluated via integer division, and hence be 0.

提交回复
热议问题