# Floating Point Exception error

Printable View

• 08-27-2011
Mentallic
Floating Point Exception error
In my code
Code:

```#include <stdio.h> #include <math.h> #define MAX 10000000000LL int main(int argc, char * argv[]) {     int i=0;     long long barcode=0;     int digits[10];         printf("Please enter product code :\n");     scanf("%lld", &barcode);         for (i=10; i>0; i--) {         digits[i] = (barcode/(10^i)) % 10;         printf("digits[%lld] = %d\n", i, digits[i]);     }     return 0; }```
I keep getting "floating point exception". What's happening exactly?
• 08-27-2011
Salem
Perhaps you're mistaking that ^ means "raise to power of".
It doesn't - it is bitwise exclusive or.

Some "10^i" is going to be zero I would guess, and that leads to a big black hole of division by zero.
• 08-27-2011
Mentallic
Oh I just assumed since ^ stands for the exponent in pretty much everywhere else... then maybe it would be here too. I'm so naive ;)

A quick little google search finds that pow(x) is what I'm looking for. Thanks Salem.
• 08-27-2011
Mentallic
Ok it looks like I'm not going to get away with it that easily,

I've replaced 10^i with pow (10, i) but I'm still out of luck, I keep getting the error message

Quote:

checksum.c: In function ‘main’:
checksum.c:20:44: error: invalid operands to binary % (have ‘double’ and ‘int’)
• 08-27-2011
Salem
The more usual way (and more accurate) is

digits[i] = (barcode/divisor) % 10;
divisor /= 10;

Where divisor starts of at say 10000000000 and progressively reduces by a power of 10 each time around the loop.
• 08-27-2011
Mentallic
Oh yeah that's a much better way. I can throw out the math library now :)