浮点异常 C++ 为什么以及它是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4236853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:54:36  来源:igfitidea点击:

Floating Point Exception C++ Why and what is it?

c++floating-point

提问by samuraiseoul

I'm building a program for the Euler projects question 3, and while that might not really matter as a result I'm current trying to make this code take a number and test if it is prime or not. Now then before I get to troubleshoot the function it gives me the error "floating point exception" right after inputting the number. Here's the code:

我正在为 Euler 项目问题 3 构建一个程序,虽然这可能并不重要,但我目前正在尝试使此代码取一个数字并测试它是否为质数。现在,在我对该函数进行故障排除之前,它在输入数字后立即给我错误“浮点异常”。这是代码:

int main()
{
    int input;
    cout << "Enter number: " << endl;
    cin>> input;
    int i = input/2;
    int c;
    for (i>0; i--;) {
        c= input%i;
        if (c==0 || i == 1)
            cout << "not prime" << endl;
        else
            cout << "prime" << endl;
    }
    return 0;
}

so essentially why is it giving me a floating point exception and what does that even mean?

所以基本上为什么它会给我一个浮点异常,这甚至意味着什么?

回答by Crashworks

A "floating point number" is how computers usually represent numbers that are not integers -- basically, a number with a decimal point. In C++ you declare them with floatinstead of int. A floating point exception is an error that occurs when you try to do something impossible with a floating point number, such as divide by zero.

浮点数”是计算机通常表示不是整数的数字的方式——基本上,带有小数点的数字。在 C++ 中,你用float而不是int. 浮点异常是当您尝试用浮点数做不可能的事情时发生的错误,例如除以零。

回答by fredoverflow

for (i>0; i--;)

is probably wrong and should be

可能是错误的,应该是

for (; i>0; i--)

instead. Note where I put the semicolons. The condition goes in the middle, not at the start.

反而。注意我把分号放在哪里。条件发生在中间,而不是在开始时。

回答by Pete

Lots of reasons for a floating point exception. Looking at your code your for loop seems to be a bit "incorrect". Looks like a possible division by zero.

浮点异常的原因很多。查看您的代码,您的 for 循环似乎有点“不正确”。看起来可能被零除。

for (i>0; i--;){
c= input%i;

Thats division by zero at some point since you are decrementing i.

那是在某个时候除以零,因为您正在递减 i。

回答by Algoman

Since this page is the number 1 result for the google search "c++ floating point exception", I want to add another thing that can cause such a problem: use of undefined variables.

由于这个页面是谷歌搜索“c++浮点异常”的第一个结果,我想添加另一个可能导致此类问题的东西:未定义变量的使用。

回答by Manjunatha S M

Problem is in the for loop in the code snippet:
for (i > 0; i--;)

问题出在代码片段的 for 循环中:
for (i > 0; i--;)

Here, your intention seems to be entering the loop if (i > 0)and decrement the value of i by one after the completion of for loop.

在这里,您的意图似乎是进入循环 if (i > 0)并在for loop 完成后将i 的值减一。

Does it work like that? lets see.

它是这样工作的吗?让我们来看看。

Look at the for() loop syntax:

查看 for() 循环语法:

**for ( initialization; condition check; increment/decrement ) {  
    statements;  
}**

Initialization gets executed only once in the beginning of the loop. Pay close attention to ";" in your code snippet and map it with for loop syntax.

初始化仅在循环开始时执行一次。密切注意“;” 在您的代码片段中并使用 for 循环语法映射它。

Initialization : i > 0 : Gets executed only once. Doesn't have any impact in your code.

初始化 : i > 0 : 只执行一次。对您的代码没有任何影响。

Condition check : i -- : post decrement.

条件检查 : i -- : 递减。

              Here, i is used for condition check and then it is decremented. 
              Decremented value will be used in statements within for loop. 
              This condition check is working as increment/decrement too in your code. 

Lets stop here and see floating point exception.

让我们停下来看看浮点异常。

what is it? One easy example is Divide by 0. Same is happening with your code.

它是什么?一个简单的例子是除以 0。你的代码也是如此。

When i reaches 1 in condition check, condition check validates to be true.
Because of post decrement i will be 0 when it enters for loop.

当我在条件检查中达到 1 时,条件检查验证为真。
由于后递减,当它进入 for 循环时,我将是 0。

Modulo operation at line #9 results in divide by zero operation.  

With this background you should be able to fix the problem in for loop.

有了这个背景,您应该能够解决 for 循环中的问题。