xref: /csrg-svn/usr.bin/f77/libF77/z_div.c (revision 47940)
1*47940Sbostic /*-
2*47940Sbostic  * Copyright (c) 1980 The Regents of the University of California.
3*47940Sbostic  * All rights reserved.
422997Skre  *
5*47940Sbostic  * %sccs.include.proprietary.c%
610550Sdlw  */
710550Sdlw 
8*47940Sbostic #ifndef lint
9*47940Sbostic static char sccsid[] = "@(#)z_div.c	5.4 (Berkeley) 04/12/91";
10*47940Sbostic #endif /* not lint */
11*47940Sbostic 
1210550Sdlw #include "complex"
1320187Slibs #include <stdio.h>
1420187Slibs #include <errno.h>
1529971Smckusick #ifdef tahoe
1645967Sbostic #include <tahoe/math/FP.h>
1745967Sbostic #endif
1810550Sdlw 
z_div(c,a,b)1910550Sdlw z_div(c, a, b)
2010550Sdlw dcomplex *a, *b, *c;
2110550Sdlw {
2210550Sdlw double ratio, den;
2310550Sdlw double abr, abi;
2410550Sdlw 
2529971Smckusick #ifndef tahoe
2610550Sdlw if( (abr = b->dreal) < 0.)
2710550Sdlw 	abr = - abr;
2810550Sdlw if( (abi = b->dimag) < 0.)
2910550Sdlw 	abi = - abi;
3029971Smckusick #else tahoe
3129971Smckusick if( (abr = b->dreal) < 0.)
3229971Smckusick 	*((long int *)&abr) ^= SIGN_BIT;
3329971Smckusick if( (abi = b->dimag) < 0.)
3429971Smckusick 	*((long int *)&abi) ^= SIGN_BIT;
3529971Smckusick #endif tahoe
3610550Sdlw if( abr <= abi )
3710550Sdlw 	{
3820187Slibs 	if(abi == 0) {
3920187Slibs 		fprintf( stderr, "Double complex division by zero\n" );
4020187Slibs 		f77_abort(EDOM);
4120187Slibs 	}
4210550Sdlw 	ratio = b->dreal / b->dimag ;
4310550Sdlw 	den = b->dimag * (1 + ratio*ratio);
4410550Sdlw 	c->dreal = (a->dreal*ratio + a->dimag) / den;
4510550Sdlw 	c->dimag = (a->dimag*ratio - a->dreal) / den;
4610550Sdlw 	}
4710550Sdlw 
4810550Sdlw else
4910550Sdlw 	{
5010550Sdlw 	ratio = b->dimag / b->dreal ;
5110550Sdlw 	den = b->dreal * (1 + ratio*ratio);
5210550Sdlw 	c->dreal = (a->dreal + a->dimag*ratio) / den;
5310550Sdlw 	c->dimag = (a->dimag - a->dreal*ratio) / den;
5410550Sdlw 	}
5510550Sdlw 
5610550Sdlw }
57