C ++。用1除以任意数字得到0 [英] C++. Dividing 1 by any number gives 0

查看:159
本文介绍了C ++。用1除以任意数字得到0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试除以1/60或1 /(60 * 60)时,它的值为0。即使在调试器窗口中也是如此。我有点困惑,因为2/3或2.5 / 6会给出结果。

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.

我的代码:

int main()
{   
    double k1 = 1/60;
    cout << k1
        << endl;
    double k2 = 1/(60*60);
    cout << k2
        << endl;

    return 0;
}

感谢您的帮助。

推荐答案

由于两个操作数都是整数,因此编译器执行整数除法(不计算小数部分)。如果至少一个操作数是浮点类型(如您的其他示例),则另一个将被提升并执行浮点除法。

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

至少使一个浮点类型的操作数( double float );您可以这样做,例如:

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


  • 使其成为 double 文字 2 60 是整数, 60.0 甚至是 60。 double 60.f float

  • 使用强制转换( double(60)(double)60 )。

  • 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).

我个人更喜欢直接使用 double 字面量-并非对于中庸的编译器,cast可能会降低性能,但是对于仅使用正确类型的文字而言,它会感到错误和冗长。 (很明显,当两个操作数都是变量而不是文字时,您必须使用强制转换)

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


  • ,但我将其分配给 double

许多新手对此感到困惑,因为他们认为分配 1 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?

即使自变量,几种语言也会自动执行浮点除法都是整数(例如VB6,IIRC),因为对于新手来说更直观。这与C / C ++中的情况不同:当参数为整数时,除法是不可或缺的,因为在许多情况下,您根本不关心小数点,并且/或者出于性能方面的考虑,最好不要使用FPU(背景知识)在C和C ++中,您不用为不使用的东西付费)。

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").

显然,可以使用单独的运算符进行积分除法来解决该问题。 (VB再次使用 \ ),但是恕我直言,我们在C ++中有足够的运算符。 :)

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'角落:是的,这里实际上是初始化,而不是赋值,但我们正在谈论的是同一种误解。

  2. 源代码中包含一个文字值。 / li>
  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.

这篇关于C ++。用1除以任意数字得到0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆