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

5

51

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?

Cristiano answered 21/11, 2010 at 7:21 Comment(2)
Something is wrong with your for loop.Extrude
the floating point exception has many reasons but depending on your code, I do agree with Pete and I think c= input%i; is the cause of the problem and I hope my answer helps you..Lxx
K
53

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.

Kep answered 21/11, 2010 at 7:26 Comment(8)
Okay, well let me make sure I understand my own code before I try to fix it. The for lop will only execute if i > 0 right? Then the only time it will divide later is c= input%i So it should never divide by 0?Cristiano
If you look carefully at your loop, you'll see there is a way that its body can get run once with i == 0.Kep
if i == 1? So a for loop's increment/decrement happens at the end of the loop even though you write it at the top?Cristiano
Yes, thats the entire reason for a for loop. The loop runs, then it does something (in your case i--) then it runs the loop again until the condition at the top is no longer true.Roger
lol, yeah, it wasn't working correctly after I fixed the floating point issue but I'm sure you can work it out. If not, just post back! =)Roger
This answer is just wrong. The name "floating point exception" is a historical misnomer. Floating point division by zero is well-defined (per Annex F/IEEE754) and does not produce any signal. In OP's code, it's the way integer division by zero, which is undefined behavior, manifests on the particular implementation OP is using.Barrack
@R, I asked a related question here, #52613395 My reading of the wikipedia entry for IEEE754 is that divide by zero does produce a signal and it is clearly listed as such in the VS2015 exceptions list. However, as per your comment above, the exception never gets raised, even with /fp:except setRondelet
@R..GitHubSTOPHELPINGICE fp division by zero is specified in IEEE 754-2008 section 7.3 to signal although it has an exact infinite result. (Annex F compliance is technically optional as well.)Taciturn
W
45
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.

Waxy answered 21/11, 2010 at 11:4 Comment(2)
Yes, but that's not the reason for the floating point exceptionInvoluted
@Involuted Indirectly, it is. The misplacement of the semicolons allowed i to be 0 (which the condition i>0 would not have alowed).Advertent
R
14

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 (i>0; i--;){
c= input%i;

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

Roger answered 21/11, 2010 at 7:25 Comment(2)
so the for loop automatically decrements i the first time through?Cristiano
I think you need to make some use of breakpoints and try to think through the solution a bit more. You dont want us to just give you an answer that will work, do you?Roger
J
9

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.

Jaconet answered 13/9, 2018 at 21:23 Comment(0)
F
2

Problem is in the for loop in the code snippet:
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.

Does it work like that? lets see.

Look at the for() loop syntax:

**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.

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

Condition check : i -- : post decrement.

              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.

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.

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.

Florineflorio answered 9/3, 2018 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.