| Author |
C++ Floating point exception |
Zkunxen
Member
Posts: 47
Location:
Joined: 24.07.09 Rank: Elite |
|
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:
#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 Zkunxen on 19-11-09 01:54 |
|
| Author |
RE: C++ Floating point exception |
wolfmankurd
Member

Posts: 1519
Location: UK
Joined: 30.05.05 Rank: God |
|
|
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.
BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.

|
|
| Author |
RE: C++ Floating point exception |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
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.
|
|
| Author |
RE: C++ Floating point exception |
stealth-
Member

Posts: 995
Location: Eh?
Joined: 10.04.09 Rank: God |
|
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? 
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.stealth-x.com
Edited by stealth- on 19-11-09 06:12 |
|
| Author |
RE: C++ Floating point exception |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
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.org/crossroads/xrds3-1/ovp3-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.
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!
Edited by COM on 19-11-09 08:18 |
|
| Author |
RE: C++ Floating point exception |
korg
Admin from hell

Posts: 1704
Location: ENDING YOUR ONLINE EXPERIENCE!
Joined: 01.01.06 Rank: The Master |
|
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 scblockedript lying around.
EDIT: Just googled "c++ prime numbers 2000000" and found a shit load of info, Check it out on your own. 
I deal in pain, All life I drain, I dominate, I seal your fate.
Edited by korg on 19-11-09 07:25 |
|
| Author |
RE: C++ Floating point exception |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
|
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.
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth! |
|
| Author |
RE: C++ Floating point exception |
korg
Admin from hell

Posts: 1704
Location: ENDING YOUR ONLINE EXPERIENCE!
Joined: 01.01.06 Rank: The Master |
|
|
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.
I deal in pain, All life I drain, I dominate, I seal your fate.
|
|
| Author |
RE: C++ Floating point exception |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
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.
|
|
| Author |
RE: C++ Floating point exception |
wolfmankurd
Member

Posts: 1519
Location: UK
Joined: 30.05.05 Rank: God |
|
|
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++
BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.

Edited by wolfmankurd on 19-11-09 22:27 |
|
| Author |
RE: C++ Floating point exception |
stealth-
Member

Posts: 995
Location: Eh?
Joined: 10.04.09 Rank: God |
|
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 
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.stealth-x.com
Edited by stealth- on 20-11-09 02:22 |
|
| Author |
RE: C++ Floating point exception |
wolfmankurd
Member

Posts: 1519
Location: UK
Joined: 30.05.05 Rank: God |
|
stealth- wrote:
wolfmankurd wrote:
Also stealth it's C++
Close Enough
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 , 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.
BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.

|
|
| Author |
RE: C++ Floating point exception |
stealth-
Member

Posts: 995
Location: Eh?
Joined: 10.04.09 Rank: God |
|
Ah, yeah I see what you mean wolfman. Thanks for the explanation.
@ moshbat: I think you misread my question, but thanks anyways 
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.stealth-x.com |
|