You are not logged in.

#1 2008-09-25 20:25:54

zandaa
Member
From: The Netherlands
Registered: 2008-07-14
Posts: 33
Website

[solved] my C code gives floating point errors.

I've tried to create a simple function to determine if a number is prime or not.. whenever I compile and try to run I get a floating point exception and can't quite figure out why, I'm very new to C and compiling manually using gcc so any help is appreciated.

here's my code: http://pastebin.com/m2f19dbac

Last edited by zandaa (2008-10-01 06:36:35)


With my army of penguins, I shall overthrow governments and free those who have been waiting for liberty.

Offline

#2 2008-09-25 20:39:41

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [solved] my C code gives floating point errors.

Your problem is in isprime, you should be initializing i to be 1 instead of 0.  Otherwise, you're dividing by zero, which isn't cool.

Offline

#3 2008-09-25 20:43:50

zandaa
Member
From: The Netherlands
Registered: 2008-07-14
Posts: 33
Website

Re: [solved] my C code gives floating point errors.

I found the problem with initializing i=0 instead of i=1, but it gives me numbers that aren't even prime numbers, so where am I going wrong with this? what's wrong with my logic?


With my army of penguins, I shall overthrow governments and free those who have been waiting for liberty.

Offline

#4 2008-09-25 21:04:02

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [solved] my C code gives floating point errors.

you should probably uncomment that first 'break' statement.

Offline

#5 2008-09-25 21:14:13

zandaa
Member
From: The Netherlands
Registered: 2008-07-14
Posts: 33
Website

Re: [solved] my C code gives floating point errors.

I figured something out and yet have no idea why it worked, I have turned around my whole if statements into something like:

    if ( (x%i == 0 && i != 1) && (x%i == 0 && i != x) ) {
        result = 0;
        break;
    } else if (x%i == 0 && i == x) {
        result = 1;
        //printf("%d\n", x);
        break;
    }

With my army of penguins, I shall overthrow governments and free those who have been waiting for liberty.

Offline

#6 2008-09-25 21:23:53

ghostHack
Member
From: Bristol UK
Registered: 2008-02-29
Posts: 261

Re: [solved] my C code gives floating point errors.

If you start the for loop counting from 2 there is no need to keep tesing for i = 1, theres no need for i to be 1 anyway.  You could also replace the break statements with 'return result' since the only thing you do after the for loop is to return result anyway.

EDIT: scratch what I wrote above, theres a better way.

A number is prime if it can only divide by itself and 1, if you change the isprime routine to

int isprime(int x) {

    int i, result;
    result = 1;

    for (i=2; i < x; i++) {
        if ( x % i == 0)
    {
        result = 0;
        return result;
        }
    }
    return result;
}

We initialise result to be 1 and then loop i between 2 and x-1, if x % i is ever zero then we know that x is not prime so we set result to 0 and return, if x % i is never 0 then result is still 1 and x must be prime so we can return result.

Last edited by ghostHack (2008-09-25 21:32:23)

Offline

#7 2008-09-25 21:33:56

carlocci
Member
From: Padova - Italy
Registered: 2008-02-12
Posts: 368

Re: [solved] my C code gives floating point errors.

I'm quite useless at programming:

for (i=0; i <= x; i++) {
        if (x%i == 0 && i != 1) {
                result = 0;
                //break;
        }
        if (i == x) {
                result = 1;
                //printf("%d\n", x);
                break;
        }
}

This loop always ends with result=1 because the last loop has i=x and therefore it changes the result value to 1.
There are also many checks you could avoid with a "smart" for cycle:

for (i=2; i <= x; i++) {
        if (x%i == 0) {
                return 0;
        }
}
return 1

I was able to remove the "i != 1" part by having the for loop starting from 2.
I removed all the if (i == x) clause because that only happens at the end of the for cycle.
Also you could be smart and eliminate some values from the loop: for example you could avoid checking all the numbers higher than n/2 or than sqrt(n), but that's more related to the mathematics behind than to the programming
Hope this helps


edit: beaten

Last edited by carlocci (2008-09-25 21:36:04)

Offline

#8 2008-09-26 10:03:57

Adams
Member
From: Poland
Registered: 2007-02-12
Posts: 14

Re: [solved] my C code gives floating point errors.

Hmm, I see you're trying to find all primes between 2 and 10000. You could use Erastotenes sieve. Divide only by values that you know that are prime. A little example: Checking if 169 is prime: 169%2 != 0, 169%3 != 0, 169%5 != 0, 169%7 != 0, 169%11 != 0, 169%13  == 0, so 169 is not a prime. The idea, only to check numbers lower then or equal sqrt(x) applies here to.


"The bureaucracy is expanding to meet the needs of an expanding bureaucracy"

Offline

Board footer

Powered by FluxBB