469,322 Members | 1,982 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,322 developers. It's quick & easy.

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


Nov 15 '05 #1
2 26013
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
Nov 15 '05 #2
On 2005-10-30, Steven Jones <ca**@swbell.net> 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;
}

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Andrew Straw | last post: by
10 posts views Thread by Vinny | last post: by
687 posts views Thread by cody | last post: by
21 posts views Thread by Allin Cottrell | last post: by
13 posts views Thread by Chris Stankevitz | last post: by
4 posts views Thread by jacob navia | last post: by
1 post views Thread by Shhnwz.a | last post: by
2 posts views Thread by Martin Manns | last post: by
39 posts views Thread by rembremading | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.