简体   繁体   English

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

[英]Floating Point Exception C++ Why and what is it?

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.我正在为 Euler 项目问题 3 构建一个程序,虽然这可能并不重要,但我目前正在尝试让这段代码接受一个数字并测试它是否为素数。 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:这是代码:

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?所以本质上为什么它给我一个浮点异常,这甚至意味着什么?

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 float instead of int .在 C++ 中,您使用float而不是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.浮点异常是当您尝试用浮点数做一些不可能的事情时发生的错误,例如除以零。

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.条件发生在中间,而不是开始。

Lots of reasons for a floating point exception.浮点异常的原因有很多。 Looking at your code your for loop seems to be a bit "incorrect".查看您的代码,您的 for 循环似乎有点“不正确”。 Looks like a possible division by zero.看起来可能被零除。

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

Thats division by zero at some point since you are decrementing i.那是在某个时候被零除,因为你正在递减 i。

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.由于此页面是 google 搜索“c++ 浮点异常”的第 1 个结果,因此我想添加可能导致此类问题的另一件事:使用未定义的变量。

Problem is in the for loop in the code snippet:问题出在代码片段中的 for 循环中:
for (i > 0; i--;)对于 (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.初始化:i > 0:只执行一次。 Doesn't have any impact in your code.对您的代码没有任何影响。

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.当条件检查中 i 达到 1 时,条件检查验证为真。
Because of post decrement i will be 0 when it enters for loop.由于后递减,当它进入 for 循环时 i 将为 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 循环中的问题。

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

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