Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why am I getting floating point exception?

Reply
Thread Tools

Why am I getting floating point exception?

 
 
Steven Jones
Guest
Posts: n/a
 
      10-30-2005
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




 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      10-30-2005
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
 
Reply With Quote
 
 
 
 
Jordan Abel
Guest
Posts: n/a
 
      10-30-2005
On 2005-10-30, Steven Jones <(E-Mail Removed)> 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;
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments