xref: /csrg-svn/usr.bin/f77/libF77/c_div.c (revision 47940)
1*47940Sbostic /*-
2*47940Sbostic  * Copyright (c) 1980 The Regents of the University of California.
3*47940Sbostic  * All rights reserved.
422827Skre  *
5*47940Sbostic  * %sccs.include.proprietary.c%
610443Sdlw  */
710443Sdlw 
8*47940Sbostic #ifndef lint
9*47940Sbostic static char sccsid[] = "@(#)c_div.c	5.2 (Berkeley) 04/12/91";
10*47940Sbostic #endif /* not lint */
11*47940Sbostic 
1210443Sdlw #include "complex"
1320187Slibs #include <stdio.h>
1420187Slibs #include <errno.h>
1510443Sdlw 
c_div(c,a,b)1610443Sdlw c_div(c, a, b)
1710443Sdlw complex *a, *b, *c;
1810443Sdlw {
1910443Sdlw double ratio, den;
2010443Sdlw double abr, abi;
2110443Sdlw 
2210443Sdlw if( (abr = b->real) < 0.)
2310443Sdlw 	abr = - abr;
2410443Sdlw if( (abi = b->imag) < 0.)
2510443Sdlw 	abi = - abi;
2610443Sdlw if( abr <= abi )
2710443Sdlw 	{
2820187Slibs 	if(abi == 0) {
2920187Slibs 		fprintf(stderr,"complex division by zero\n");
3020187Slibs 		f77_abort(EDOM);
3120187Slibs 	}
3210443Sdlw 	ratio = b->real / b->imag ;
3310443Sdlw 	den = b->imag * (1 + ratio*ratio);
3410443Sdlw 	c->real = (a->real*ratio + a->imag) / den;
3510443Sdlw 	c->imag = (a->imag*ratio - a->real) / den;
3610443Sdlw 	}
3710443Sdlw 
3810443Sdlw else
3910443Sdlw 	{
4010443Sdlw 	ratio = b->imag / b->real ;
4110443Sdlw 	den = b->real * (1 + ratio*ratio);
4210443Sdlw 	c->real = (a->real + a->imag*ratio) / den;
4310443Sdlw 	c->imag = (a->imag - a->real*ratio) / den;
4410443Sdlw 	}
4510443Sdlw 
4610443Sdlw }
47