简体   繁体   English

当向下舍入的值不为 0 时,为什么这个浮点除法会产生零?

[英]Why does this floating-point division produce zero, when the value rounded down isn't 0?

I am practice with C++ and I see some problem:我正在练习 C++,我发现了一些问题:

#include <iostream>
#include <string>
using namespace std;

int main() {
    int a = 2;
    {
        cout << a;
        cout << "\n";
        float a = a / 2;
        cout << "a= a/2 = ";
        cout << a;

    }
    cout << "\n";
    a = 2;
    {
        cout << a;
        cout << "\n";
        float b = a / 2;
        cout << "b= a/2 = ";
        cout << b;

    }
}

This return:这个回报:

2 
a= a/2 = 0 
2 
b= a/2 = 1

I want to know why a = a/2 = 0?我想知道为什么 a = a/2 = 0?

Thank you谢谢

This is a subtle error.这是一个微妙的错误。 Look at this code:看看这段代码:

int a = 2;
{
    float a = a / 2;
}

Outside of the curly braces, the name a refers to int a , the integer declared up top.在大括号之外,名称a指的是int a ,integer 在顶部声明。 But inside the curly braces, once you reach the line in which float a is declared, the name a refers to float a inside the braces rather than int a outside the braces.但是在大括号内,一旦到达声明float a的行,名称a指的是大括号内的float a而不是大括号外的int a

This is a problem because the line这是一个问题,因为该行

float a = a / 2;

means "create a new variable named a of type float . Oh, and it needs an initial value. That's okay! Give it the value of float a , divided by two."意思是“创建一个名为afloat类型的新变量。哦,它需要一个初始值。没关系!给它float a的值,除以二。” See the problem here?看到这里的问题了吗? The variable a is being initialized in terms of itself, so when a / 2 is computed a has not been initialized and the results are undefined.变量a正在根据自身进行初始化,因此当计算a a / 2时 a 尚未初始化且结果未定义。

To fix this, simply give float a a new name.要解决这个问题,只需给float a一个新名称。

Because you are actually using declared, but never initialized variable when you stated float a = a/2 .因为当您声明float a = a/2时,您实际上是在使用已声明但从未初始化的变量。 My computer prints 4.49985e-039 but that could be any number.我的电脑打印4.49985e-039但这可以是任何数字。
You are confusing yourself because you have two int & float variable with same name.您感到困惑,因为您有两个具有相同名称的intfloat变量。 Better to choose your name of the variable carefully or you have to track your code to see which is indicating which.最好仔细选择您的变量名称,否则您必须跟踪您的代码以查看哪个指示哪个。
I'll comment on the each line which variable a has been used.我将对使用了变量a的每一行进行评论。

#include <iostream>
#include <string>
using namespace std;

int main() {
  int a = 2;  // int a declared in main scope used;
  {
    cout << a;  // int a declared in main scope; since there is no a declared in local scope.
    cout << "\n";
    float a = a/2; // block scope variable a used without initialization to initialize itself. UB. 
    cout << "a= a/2 = ";  // block scope variable used
    cout << a;  // block scope variable used
  }
  cout << "\n";   a = 2;  // int a declared in main scope; Since it's block scope is within main scope only.
  {
    cout << a;  // int a declared in main scope; since no a has been declared in local scope
    cout << "\n";
    float b = a/2; // int a declared in main scope; since no a has been declared in local scope
    cout << "b= a/2 = "; // int a declared in main scope; since no a has been declared in local scope
    cout << b;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM