浮点异常C ++为什么和什么是? [英] Floating Point Exception C++ Why and what is it?

查看:303
本文介绍了浮点异常C ++为什么和什么是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Euler项目问题​​3创建一个程序,虽然这可能并不重要,结果,我目前试图让这个代码采取一个数字,并测试如果它是素数。现在,然后在我得到排除故障的功能,它给我的错误浮点异常输入数字后。这里是代码:

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:

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?

推荐答案

浮动点数是计算机通常表示不是整数的数字 - 基本上是具有小数点的数字。在C ++中,用 float 而不是 int 来声明它们。浮点异常是当您尝试使用浮点数执行不可能的操作(如除以零)时出现的错误。

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. 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 ++为什么和什么是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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