Thread: Floating Point Exception error

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    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.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    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

    checksum.c: In function ‘main’:
    checksum.c:20:44: error: invalid operands to binary % (have ‘double’ and ‘int’)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Oh yeah that's a much better way. I can throw out the math library now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating Point Exception
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 08-12-2011, 06:48 AM
  2. Floating point exception?
    By JM1082 in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2011, 08:13 PM
  3. floating point exception?
    By snowball in forum C Programming
    Replies: 2
    Last Post: 03-04-2011, 10:45 AM
  4. floating point exception
    By megastar in forum C Programming
    Replies: 6
    Last Post: 07-09-2007, 04:22 AM
  5. floating point exception? what causes these?
    By salvelinus in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2002, 12:12 PM