Why am I getting floating point exception?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Jones

    Why am I getting floating point exception?

    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




  • pete

    #2
    Re: Why am I getting floating point exception?

    Steven Jones wrote:
    [color=blue]
    > while ( u != 0)[/color]

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

    --
    pete

    Comment

    • Jordan Abel

      #3
      Re: Why am I getting floating point exception?

      On 2005-10-30, Steven Jones <caja@swbell.ne t> wrote:[color=blue]
      > 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?[/color]

      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.
      [color=blue]
      > while ( u != 0)
      > {
      > printf ("\nu v temp ---> %d\t%d\t%d",u, v, temp); // *** DEBUG
      > temp = u % v;[/color]
      If 'v' reaches zero, you will have a division by zero here.[color=blue]
      > u = v;
      > v = temp;
      > }[/color]

      Comment

      Working...