Why am I getting floating point exception?

Discussion in 'C Programming' started by Steven Jones, Oct 30, 2005.

  1. Steven Jones

    Steven Jones Guest

    Im just learning C an do not understand why the following code fails.
    The while loop apperas to work fine until it bombs out with a
    floating point exception. Whats going on?

    Thanks


    /*
    * chapter 5, program 7
    * Calculate GCD of two numbers.
    */

    #include <stdio.h>

    main ()
    {
    int u,v, temp;

    temp = -1; // *** DEBUG
    printf("Please type two non-negative integers.\n");

    scanf("%d%d", &u,&v);

    printf("\n\nThe GCD of %d and %d is ", u,v);

    while ( u != 0)
    {
    printf ("\nu v temp ---> %d\t%d\t%d",u, v, temp); // *** DEBUG
    temp = u % v;
    u = v;
    v = temp;
    }

    // printf("%d\n", u);

    }


    **** OUTPUT:

    [sj@KUTI pic]$ a.out
    Please type two non-negative integers.
    150 35


    The GCD of 150 and 35 is
    u v temp ---> 150 35 -1
    u v temp ---> 35 10 10
    u v temp ---> 10 5 5
    Floating point exception
     
    Steven Jones, Oct 30, 2005
    #1
    1. Advertising

  2. Steven Jones

    pete Guest

    Steven Jones wrote:

    > while ( u != 0)


    while (v != 0)

    > {
    > printf ("\nu v temp ---> %d\t%d\t%d",u, v, temp); // *** DEBUG
    > temp = u % v;
    > u = v;
    > v = temp;
    > }


    --
    pete
     
    pete, Oct 30, 2005
    #2
    1. Advertising

  3. Steven Jones

    Jordan Abel Guest

    On 2005-10-30, Steven Jones <> wrote:
    > Im just learning C an do not understand why the following code fails.
    > The while loop apperas to work fine until it bombs out with a
    > floating point exception. Whats going on?


    Probable reasons for a floating point exception involving integers include:
    Trap representations of signed types [unlikely on modern systems]
    Overflow on signed types
    Division by zero.

    > while ( u != 0)
    > {
    > printf ("\nu v temp ---> %d\t%d\t%d",u, v, temp); // *** DEBUG
    > temp = u % v;

    If 'v' reaches zero, you will have a division by zero here.
    > u = v;
    > v = temp;
    > }
     
    Jordan Abel, Oct 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,669
    Ben Jones
    Mar 2, 2006
  2. Lionel B
    Replies:
    0
    Views:
    1,071
    Lionel B
    Sep 23, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,998
    Smokey Grindel
    Dec 2, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,348
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    259
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page