[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Floating Point Exception

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 7
Thread images: 1

File: fpe.jpg (4KB, 367x31px) Image search: [Google]
fpe.jpg
4KB, 367x31px
Hey fa/g/s, im writing a meme in seeples and im encountering a floating point exception error when trying to run this :/ Compiles just fine, but i cant see the error

I might just be retarded, so im counting on the autism here to help me out

#include <math.h>
#include <vector>
#include <stdio.h>

typedef unsigned long ul;

const ul PRIMES = 2000000;

bool isPrime(int n)
{
if(n == 0) throw "Cannot calculate with 0";
if (n <= 1) return false;
int sqN = sqrt(n);
for (int i = 0; i <= sqN; i++) {
if (n % i == 0) return false;
} return true;
}

int main()
{
std::vector<ul> primes, fizz, buzz, fizzbuzz;
ul sumPrimes = 0, sumFizz = 0, sumBuzz = 0, sumFizzBuzz = 0;
ul numPrimes = 0, numFizz = 0, numBuzz = 0, numFizzBuzz = 0;
for (ul p = 1; p <= PRIMES; p++) {
if (isPrime(p)) primes.push_back(p);
} numPrimes = primes.size();
for (ul f = 0; f <= numPrimes; f++) {
if (primes[f] % 3 == 0) fizz.push_back(f);
} numFizz = fizz.size();
for (ul b = 0; b <= numPrimes; b++) {
if (primes[b] % 5 == 0) buzz.push_back(b);
} numBuzz = buzz.size();
for (ul fb = 0; fb <= numPrimes; fb++) {
if (primes[fb] % 15 == 0) fizzbuzz.push_back(fb);
} numFizzBuzz = fizzbuzz.size();
for (ul sp : primes) {
sumPrimes += primes[sp];
}
for (ul sf : fizz) {
sumFizz += fizz[sf];
}
for (ul sb : buzz) {
sumBuzz += buzz[sb];
}
for (ul sfb : fizzbuzz) {
sumFizzBuzz += fizzbuzz[sfb];
}
printf("\t\tPrimes upto <%lu>", PRIMES);
printf("Primes: %lu\nFizz: %lu\nBuzz: %lu\nFizzBuzz: %lu\n", numPrimes, numFizz, numBuzz, numFizzBuzz);
printf("Sum Primes: %lu\nSum Fizz: %lu\nSum Buzz: %lu\nSum FizzBuzz: %lu\n", sumPrimes, sumFizz, sumBuzz, sumFizzBuzz);
return 0;
}
>>
>implying /g/ knows how to code
>>
I think sqrt () wants a double or float. Try making ul that instead and just drop the decimal in your print
>>
Also if youre checking size in your for loops, dont do <= if you start with 0. Index range starts at 0, size count starts at 1. So just do < instead.

(A list with 3 uls has size 3 but indexes 0,1 and 2)
>>
`n % i == 0` mods zero at first iteration, which is not allowed. Do you even use a debugger?
>>
>>59751199
Yep this. Also it would work with 0 or lower, it just wouldn't do anything.

>>59750426
If you're debugging numerical errors, keep in mind 'floating point exceptions' are generally raised by divide-by-0 errors on integers (% operation is of course tied to the definition of division, or at the very least it also doesn't make sense to apply 0).

Floating point exceptions for floating point types don't generally get raised unless they're enabled or manually specified with <cfenv>.

>Do you even use a debugger?
Would have been the easiest and most appropriate thing here, >>59750426
get used to a debugging environment.
>>
OP here

>Do you even use a debugger?
I do, and i caught the division-by-zero in testing. Just forgot the definition of mod, kinda tired

>Also if youre checking size in your for loops, dont do <= if you start with 0. Index range starts at 0, size count starts at 1. So just do < instead.
Good point, i'll redo that when im abit more awake.

This is how its looking right now, but it still wont print anything... so im just confused desu

#include <math.h>
#include <vector>
#include <stdio.h>

typedef unsigned long ul;

const ul PRIMES = 6000000;

bool isPrime(int n)
{
if (n <= 1) return false;
int sqN = sqrt(n);
for (int i = 3; i <= sqN; i++) {
if (n % i == 0) return false;
} return true;
}

int main()
{
std::vector<ul> primes, fizz, buzz, fizzbuzz;
ul sumPrimes = 0, sumFizz = 0, sumBuzz = 0, sumFizzBuzz = 0;
ul numPrimes = 0, numFizz = 0, numBuzz = 0, numFizzBuzz = 0;

for (ul p = 0; p <= PRIMES; p++) if (isPrime(p)) primes.push_back(p);
numPrimes = primes.size();
for (ul f = 0; f <= numPrimes; f++) if (primes[f] % 3 == 0) fizz.push_back(f);
numFizz = fizz.size();
for (ul b = 0; b <= numPrimes; b++) if (primes[b] % 5 == 0) buzz.push_back(b);
numBuzz = buzz.size();
for (ul fb = 0; fb <= numPrimes; fb++) if (primes[fb] % 15 == 0) fizzbuzz.push_back(fb);
numFizzBuzz = fizzbuzz.size();

for (ul sp : primes) sumPrimes += primes[sp];
for (ul sf : fizz) sumFizz += fizz[sf];
for (ul sb : buzz) sumBuzz += buzz[sb];
for (ul sfb : fizzbuzz) sumFizzBuzz += fizzbuzz[sfb];

printf("\t\tPrimes upto <%lu>", PRIMES);
printf("Primes: %lu\nFizz: %lu\nBuzz: %lu\nFizzBuzz: %lu\n", numPrimes, numFizz, numBuzz, numFizzBuzz);
printf("Sum Primes: %lu\nSum Fizz: %lu\nSum Buzz: %lu\nSum FizzBuzz: %lu\n", sumPrimes, sumFizz, sumBuzz, sumFizzBuzz);
return 0;
}


Didn't really expect responses, so thanks!
Thread posts: 7
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.