Thread: Prime #'s question. "Floating point exception" error.

  1. #1
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80

    Prime #'s question. "Floating point exception" error.

    Code:
    #include<stdio.h>
    #include<math.h>
    long long int testNum;
    long long int testAgainst;
    short int isNumPrime;
    
    int isPrime(long long int testNum, long long int testAgainst);
    
    int main()
    {
    //9223372036854775807 = largest int
    for(testNum=3;testNum<9223372036854775807;testNum++)
            {
            if(isPrime(testNum,testAgainst)==1)
                    {
                    printf("%lli\n",testNum);
                    }
            }
    return 0;
    }
    
    int isPrime(long long int testNum,long long int testAgainst)
    {
    int isNumPrime;
    for(isNumPrime=1;testAgainst!=sqrt(testNum);testAgainst++)
            {
            if(testNum%testAgainst==0)
                    {
                    isNumPrime=1;
                    } else {
                    isNumPrime=0;
                    break;
                    }
            }
    return isNumPrime;
    }
    I wrote a program to count from 3 to the highest long long int my compiler can handle. Only problem is, I'm not even sure if it works as I get a 'floating point exception' error on compile.

    I'm guessing it's for one of three reasons, but I can't point it out, maybe someone can help me there?

    Trap representations of signed types
    Overflow on signed types
    Division by zero

    If I divided by zero, I would be so happy.
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Forcing a compiler to divide by zero is not exactly a cause for celebration.

    The magic value (9223372036854775807) will be treated as type int by default. Change it to 9223372036854775807LL. Or, better yet, #include <limits.h> and replace that magic value with LLONG_MAX. That will work with any C compiler that supports long long types.

    A more fundamental problem in your code is that you are assuming a long long type can be converted to floating point without loss of information .... which is pretty unlikely for any compiler that supports an 8-byte double and 8-byte long integral types (i.e. a significant proportion of real-world compilers). sqrt() is for floating point, and is not guaranteed to work particularly accurately for integral types - particularly large integral values. Have a look at this post for a hint about an alternative that would work better....

    Unless you want to work with negative values, it might pay to use long long unsigned variables.

    Note that I haven't even bothered to check whether your test for primality is valid. But you will need to check that....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Thank you for taking a peek grumpy.
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<limits.h>
    
    long long int testNum;
    long long int testAgainst;
    short int isNumPrime;
    
    int isPrime(long long int testNum, long long int testAgainst);
    
    int main()
    {
    for(testNum=3;testNum<LLONG_MAX;testNum++)
            {
            if(isPrime(testNum,testAgainst)==1)
                    {
                    printf("%lli\n",testNum);
                    }
            }
    return 0;
    }
    int isNumPrime;
    for(isNumPrime=1;testAgainst!=testNum-1;testAgainst++)
            {
            if(testNum%testAgainst==0)
                    {
                    isNumPrime=1;
                    } else {
                    isNumPrime=0;
                    break;
                    }
            }
    return isNumPrime;
    }
    Changed it, but I'm still getting the same error.

    Edit: I'm not happy that I may have forced my compiler to divide by zero, rather I would be happy that I've possibly done something I've never done before.

    Edit again: I found the particular error, now I've got a new one. I'll reply on this thread if I can't get past it. The problem was I initialized int testAgainst, but I never set a value above zero, so I was, in fact, forcing my compiler to divide by zero after all lol.
    Last edited by andrew89; 12-28-2011 at 03:16 AM. Reason: darn color tags
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  4. #4
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Updated the source and now it hangs after printing two numbers.
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<limits.h>
    long long int testNum;
    long long int testAgainst=2;
    short int isNumPrime;
    
    int isPrime(long long int testNum, long long int testAgainst);
    
    int main()
    {
    //9223372036854775807 = largest int
    for(testNum=3;testNum<LLONG_MAX;testNum++)
    	{
    	if(isPrime(testNum,testAgainst)!=0)
    		{
    		printf("%lli\n",testNum);
    		}
    	}
    return 0;
    }
    
    int isPrime(long long int testNum,long long int testAgainst)
    {
    for(isNumPrime=1;testAgainst!=testNum-1;testAgainst++)
    	{
    	if(testNum%testAgainst!=0)
    		{
    		isNumPrime=0;
    		break;
    		}
    	}
    return isNumPrime;
    }
    Output:
    Code:
    andrew@home:~/Documents/c/isPrime$ ./test
    3
    4
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You have confused yourself by having arguments of isPrime() with the same name as global variables. So isPrime() does not affect the global variables.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Quote Originally Posted by grumpy View Post
    You have confused yourself by having arguments of isPrime() with the same name as global variables. So isPrime() does not affect the global variables.
    I'm confused by that. I moved the variables from the top and inside int main() and int isPrime() and it's still happening just as before. Or were you referring to something else?

    Code:
    //snip
    #include<limits.h>
    int isPrime(long long int testNum,long long int testAgainst);
    //snip
    Code:
    int main()
    {
    long long int testAgainst = 0;
    long long int testNum = 0;
    //snip
    Code:
    int isPrime()
    {
    short int isPrime=1;
    //snip
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  7. #7
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Started over, rewrote the majority of it using a dry-erase board. It works rather well now. Any thoughts, comments, suggestions, etc. are welcome and appreciated.
    Code:
    #include<stdio.h>
    #include<limits.h>
    
    int main()
    {
    short int yesno;
    long long int i,j;
    long long int x=3;
    long long int y=LLONG_MAX;
    
    FILE *fp;
    fp = fopen("output","w+");
    fprintf(fp,"1, 2, ");
    
    for(i=x;i<=y;i++)
    {
    yesno=0;
    for(j=2;j<=i/2;j+=2)
    	{
    	if(i%j==0)
    		{
    		yesno=1;
    		break;
    		}
    	}
    	if (yesno==0)
    		{
    		fprintf(fp,"%lld, ",i);
    		}
    	}
    fclose(fp);
    return 0;
    }
    Last edited by andrew89; 12-28-2011 at 10:34 AM. Reason: Forgot to use the updated code. Silly me.
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Looks like you're testing every number for all even factors. That's going to include 9 in the output of primes, and a whole lot more that only have odd factors.
    Also, you're testing every number for primeness, whereas you may as well only test odd numbers above 2 and skip every even number.
    Can you see what you did wrong there now?

    Why don't you only go up to the square root of the number being tested like you did before? Just don't use the srqt function. Instead change the loop condition to j*j<=i and it'll be a fair bit faster. You would have found that earlier if you read to the end of the thread that grumpy linked to.
    Last edited by iMalc; 12-28-2011 at 01:39 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    I did actually, I just didn't update this since. I've made a few improvements to the code, but yeah. One of them is only checking odd numbers, then I implemented the sqrt without sqrt. It's been running for about 20 minutes and has a 8mb text file :P
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  10. #10
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    In the above post I said it now takes me 20 minutes to make a file of 8 gigs. Not so. That was before I implemented the changes. Now it takes about 30 seconds

    My parents think I'm hacking... it's kind of embarrassing really. They see a terminal window with a bunch of numbers flying by and they automatically assume the worst. Anyway, here's the updated source so you can see what I've changed.
    Code:
    #include<stdio.h>
    #include<limits.h>
    
    int main()
    {
    short int yesno;
    long long int i,j;
    long long int x=3;
    long long int y=LLONG_MAX;
    
    FILE *fp;
    fp = fopen("output","w+");
    
    printf("1, 2, ");
    fprintf(fp,"1, 2, ");
    
    for(i=x;i<=y;i++)
    {
    yesno=0;
    for(j=2;j*j<=i/2;j+=2)
    	{
    	if(i%j==0)
    		{
    		yesno=1;
    		break;
    		}
    	}
    	if (yesno==0)
    		{
    		printf("%lld\n",i);
    		fprintf(fp,"%lld, ",i);
    		}
    	}
    fclose(fp);
    return 0;
    }
    Last edited by andrew89; 12-28-2011 at 02:01 PM.
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Should be j*j<=i not i/2. and you should start j at 3 not 2. Also, your variable names could use some thought!

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You didn't pay attention to the first part of my post.
    Check the contents of the file and you'll see that 9, 15, 21 etc are listed amoung the primes, because you still have it very wrong.
    Read my post again carefully, and if you still can't see the problem then ask for clarification.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Yes I did, I just copy/pasted from the backup source file lol. You guys are going to have to forgive me for that. Nano isn't exactly the friendliest of text editors when it comes to mouse usage and opening a file. It opened ~test.c instead of test.c and I didn't look :P

    Edit:

    ~ goes to your home directory, ctrl+tab (tries to) finish the command for you. Backups are prefixed with a ~ by default and it went downhill from there. Before I fixed the code I had been up for 36 hours, that's my only excuse there :P

    The variable names are what I'm used to in javascript.

    a, b, c, i, j, k, x, y, z are my usual variables for loops, less to type out but I suppose you're right, it could cause a bit of confusion. I'll see if I can't come up with a different set of variables.
    Last edited by andrew89; 12-29-2011 at 06:18 AM.
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  14. #14
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    I also said 8 gigs... meant mibs (how Ubuntu refers to megabytes)... As you all may or may not have been able to tell. I've been very tired lately.

    THIS is the source code I meant to post lol. I had to open up all of them in gedit and look them over (this is when backups begin to look like a bad idea lol).
    Code:
    #include<stdio.h>
    #include<limits.h>
    
    int main()
    {
    short int yesno;
    long long int i,j;
    long long int x=3;
    long long int y=LLONG_MAX;
    
    FILE *fp;
    fp = fopen("output","w+");
    
    printf("1, 2, ");
    fprintf(fp,"1, 2, ");
    
    for(i=x;i<=y;i+=2)
    {
    yesno=0;
    for(j=3;j*j<=i;j+=2)
    	{
    	if(i%j==0)
    		{
    		yesno=1;
    		break;
    		}
    	}
    	if (yesno==0)
    		{
    		printf("%lld, ",i);
    		fprintf(fp,"%lld, ",i);
    		}
    	}
    fclose(fp);
    return 0;
    }
    Of course I would like to know if I've got some error in this. I've looked over all of the ones from 1 to ~8000 before I got bored with it.

    Also, I am working on a way to limit the file size to ~512kb per file. I'm going to look up more information on reading file size or limiting output to files before I do anything else with it though... if I have any questions about that I'll start a new thread.
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  15. #15
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Here's some output in case anyone wants to test:
    Code:
    17613047
    17613049
    17613061
    17613091
    17613097
    17613137
    17613143
    17613151
    17613191
    17613199
    17613223
    17613227
    17613251
    17613259
    17613263
    17613269
    17613283
    17613293
    17613307
    17613311
    17613341
    17613371
    17613383
    17613389
    17613413
    17613451
    17613457
    17613473
    17613481
    17613493
    17613521
    17613523
    17613539
    17613553
    Last edited by andrew89; 12-29-2011 at 06:24 AM. Reason: All of those were on one line...
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-02-2007, 05:55 AM
  2. Replies: 7
    Last Post: 07-03-2007, 01:49 PM
  3. "Unlinked floating point" problem
    By sahil_m in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:21 PM
  4. "Unlimited" Length Floating Point Numbers
    By LuckY in forum C++ Programming
    Replies: 0
    Last Post: 03-22-2005, 07:49 PM
  5. "Floating Point : Overflow" ??
    By mellisa in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2003, 06:41 PM