Follow us on Twitter!
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill
Sunday, April 20, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 31
Guests Online: 29
Members Online: 2

Registered Members: 82847
Newest Member: Zanjux
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ Floating point exception


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-11-09 01:49
I'm trying to do problem 10 on project euler (sum of all primes below 2 million)
and my program outputs nothing more than "Floating point exception" when run, and I can't figure out why. here's the code:
Code

#include <vector>
#include <iostream>
using namespace std;

int l=1; //length of vector
vector<double> Primes(2000000,0.0);
int main(){
   int n=2;
   int i=2;
   Primes[0]=2.0; //prevents division by 0  v
   while(n<2000000){
      int cur=0; // current vector item
      bool isPrime=true;
      for(int iter=0;iter<l;iter++){
//commenting out the following line fixes the problem, but
         if(i%(int)Primes[iter]==0){//it need this line
            isPrime=false;
            break;
         }
      }
      if(isPrime){
         Primes[cur]=double(i);
         n=i;
         ++l;
      }
      ++i;
      ++cur;
   }
   double solution=0;
   for(int i=0;i<l;i++){
      cout << Primes[i] << endl;
      solution+=Primes[i];
   }
   return 0;
}




I'm sure this algorithm is messy, but it's better than what I was using before (Primes.push_back(i))
I based it loosely on the working program I made to find n primes.

Edited by on 19-11-09 01:54
Author

RE: C++ Floating point exception


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-11-09 02:22
MoshBat wrote:
Before we go any further, this looks to me like some homework.
For your sake, is this correct?


Project Euler is a bunch of challenge type things usually quiet interesting/challenging.
But I guess a teach could have set it/ used the project as the basis for the homework.


Author

RE: C++ Floating point exception

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 19-11-09 02:55
Why is your list of primes a double? By definition prime numbers are integers. With this you just have an ass load of extra typecasts that you wouldn't need if you had made an integer vector.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C++ Floating point exception

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 19-11-09 06:11
MoshBat wrote:
wolfmankurd wrote:
MoshBat wrote:
Before we go any further, this looks to me like some homework.
For your sake, is this correct?


Project Euler is a bunch of challenge type things usually quiet interesting/challenging.
But I guess a teach could have set it/ used the project as the basis for the homework.

I refuse to help people with homework.
But as it doesn't appear to be, I might give the thing a quick look.


Erm, why?
You think they should figure it out on their own, or is this just another way of rebelling against the establishment? Pfft

Edit: @OP, I'd help you, but unfortunately I don't know any C. Well I do, but I'm in the process of learning.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 19-11-09 06:12
http://www.stealth-x.com
Author

RE: C++ Floating point exception


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-11-09 06:58
OP, I'm going to be honest here, there's so much shit with this program that it hurts trying to figure out your intentions with it.

1. What ynori said, primes are by definition ints, stop with the casting.
2. No proper casting. For C++ please use the C++ type casting implemented to make casting clearer and safer, read here for more info: http://www.acm.or. . .vp3-1.html
3. No matter how much you increment something, if it's set to 0 at the start of a loop, it'll remain 0.
4. n is unnecessary.
5. Incrementation at wrong place.
6. Will calculate one more prime than is necessary (see 4).

I'm sure there's more in there but it hurt my eyes; fix these issues and your program will work.




Edited by on 19-11-09 08:18
Author

RE: C++ Floating point exception

korg
Admin from hell



Posts: 2798
Location: ENDING YOUR ONLINE EXPERIENCE!
Joined: 01.01.06
Rank:
God
Posted on 19-11-09 07:19
Too be honest I did this a while back and this very same project (or similar) has been around for years. I only got about 6 lines in on the original code and stopped. @OP if you need some help PM me, I still have my script lying around.

EDIT: Just googled "c++ prime numbers 2000000" and found a shit load of info, Check it out on your own. :angry:


i57.photobucket.com/albums/g215/korg1269/shodan13.jpg

I deal in pain, All life I drain, I dominate, I seal your fate.

Edited by korg on 19-11-09 07:25
O R
Author

RE: C++ Floating point exception


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-11-09 08:26
korg wrote:
Too be honest I did this a while back and this very same project (or similar) has been around for years.

That was kind of covered by the very first line of the OP:

I'm trying to do problem 10 on project euler

I'm sure googling the answer will work for him, but it'll really not be worth anything if he doesn't at least try to write the code himself.


Author

RE: C++ Floating point exception

korg
Admin from hell



Posts: 2798
Location: ENDING YOUR ONLINE EXPERIENCE!
Joined: 01.01.06
Rank:
God
Posted on 19-11-09 10:37
COM wrote:
I'm sure googling the answer will work for him, but it'll really not be worth anything if he doesn't at least try to write the code himself.


Exactly, but by finding examples on how people have done it can give him a better idea on how functions work so he can learn and hopefully optimize it from there.


i57.photobucket.com/albums/g215/korg1269/shodan13.jpg

I deal in pain, All life I drain, I dominate, I seal your fate.
O R
Author

RE: C++ Floating point exception

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 19-11-09 13:58
Modularization will make it easier. You seem to have a problem with over complicating your code. Try breaking it up and just make a function that determines if a number is prime. Then make a program that uses that function.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C++ Floating point exception


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-11-09 22:23
MoshBat wrote:
I refuse to help people with homework.
But as it doesn't appear to be, I might give the thing a quick look.


Makes sense. No point being set something if you don't intend to do it yourself.


Also stealth it's C++




Edited by on 19-11-09 22:27
Author

RE: C++ Floating point exception

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 20-11-09 02:22
wolfmankurd wrote:
MoshBat wrote:
I refuse to help people with homework.
But as it doesn't appear to be, I might give the thing a quick look.


Makes sense. No point being set something if you don't intend to do it yourself.


Also stealth it's C++


Close Enough Pfft

Out of curiosity, though, what would be the easiest way to tell the difference, other than knowing C enough to recognize it's not C?

edit: I think I might have found it. In C files you include stdio, but in this one it's including iostream.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 20-11-09 02:22
http://www.stealth-x.com
Author

RE: C++ Floating point exception


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-11-09 12:34
stealth- wrote:
wolfmankurd wrote:
Also stealth it's C++


Close Enough Pfft

Out of curiosity, though, what would be the easiest way to tell the difference, other than knowing C enough to recognize it's not C?

edit: I think I might have found it. In C files you include stdio, but in this one it's including iostream.


The title of the topic helps Grin, most C is valid in C++, stdio could be included.

I think you probably have trouble differentiating cause if you're starting out you might not have seen much c++ code. Have a look at it and you'll notice the small and large/obvious differences (between *GOOD*) C++ and C.


Author

RE: C++ Floating point exception

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 20-11-09 15:40
Ah, yeah I see what you mean wolfman. Thanks for the explanation.

@ moshbat: I think you misread my question, but thanks anyways Wink


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com