C++. Dividing 1 by any number gives 0

前端 未结 3 1220
不思量自难忘°
不思量自难忘° 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条回答
  •  无人及你
    2021-01-18 10:59

    The denominator has to be a decimal number too.

    double k1 = 1/60.0; //Should work
    

    Otherwise your program will essentially truncate all the decimals.

    LITTLE EXTRA: When your denominator is a variable, you have to cast it:

    double k2 = 1/(double)myDenom;
    

提交回复
热议问题