C++. Dividing 1 by any number gives 0

前端 未结 3 1218
不思量自难忘°
不思量自难忘° 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: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.

    0 讨论(0)
  • 2021-01-18 10:53

    Since both your operands are integers, the compiler performs an integer division (that doesn't calculate the decimal part). If at least one of the operands is a floating point type (as in your other examples) the other one gets promoted and a floating point division is performed.

    The fix

    Make at least one of the operands of a floating point type (double or float); you can do this e.g.:

    • making it a double literal2 (60 is integer, 60.0 or even 60. is double, 60.f is float)
    • using a cast (double(60), (double)60).

    Personally I prefer using directly double literals - not that the cast has any performance penalty on halfway decent compilers, but it feels "wrong" and verbose in respect to just using a literal of the correct type. (Obviously when both the operands are variables and not literals you have to use the cast)

    Common objections

    • "but I'm assigning it to a double!"

      Many newbies are confused by this fact, since they think that assigning1 the result to a double should be some kind of hint to the compiler. In fact, it's not.

      The calculations/promotions done into the expression are completely independent from the type of the destination, which is just the last step. The subexpressions are evaluated for what they are, without regard to how the result will be used, so all the type promotions/operations depend only from the type of the operands.

    • why would one want integer division?

      Several languages automatically perform floating point division even when the arguments are both integers (e.g. VB6, IIRC), since it feels more intuitive for novices. That's not like that in C/C++: the division is integral when the arguments are integer, since in many cases you just don't care about the decimals, and/or it's preferable for performance reasons not to use the FPU (the background philosophy in C and C++ is "you don't pay for what you don't use").

      Obviously the problem could have been addressed using a separate operator for integral division (VB, again, uses \), but IMHO we have enough operators in C++ as it is. :)


    1. Nitpickers' corner: yes, here actually it's an initialization, not an assignment, but we are talking about the same kind of misconception.
    2. A "literal" is a value included in the source code.
    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题