48

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?

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
samuraiseoul
  • 2,888
  • 9
  • 45
  • 65
  • 9
    Something is wrong with your for loop. – Lazer Nov 21 '10 at 07:24
  • 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.. – TopDeveloper Nov 21 '10 at 07:38

5 Answers5

50

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.

Crashworks
  • 40,496
  • 12
  • 101
  • 170
  • 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? – samuraiseoul Nov 21 '10 at 07:29
  • If you look carefully at your loop, you'll see there is a way that its body can get run once with i == 0. – Crashworks Nov 21 '10 at 07:30
  • 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? – samuraiseoul Nov 21 '10 at 07:35
  • 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. – Pete Nov 21 '10 at 07:37
  • 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! =) – Pete Nov 21 '10 at 07:41
  • 30
    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. – R.. GitHub STOP HELPING ICE Dec 09 '16 at 02:47
  • @R, I asked a related question here, https://stackoverflow.com/questions/52612895/should-divide-by-zero-raise-an-exception 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 set – SmacL Oct 04 '18 at 09:01
  • @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.) – ZachB Sep 01 '21 at 06:10
43
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.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
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.

Pete
  • 10,651
  • 9
  • 52
  • 74
  • so the for loop automatically decrements i the first time through? – samuraiseoul Nov 21 '10 at 07:31
  • 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? – Pete Nov 21 '10 at 07:36
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.

Algoman
  • 1,905
  • 16
  • 16
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.

Manjunatha S M
  • 180
  • 1
  • 5