1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)c_sqrt.c 5.3 01/15/91 7 */ 8 9 #include "complex" 10 #ifdef tahoe 11 #include <tahoe/math/FP.h> 12 #endif 13 14 c_sqrt(r, z) 15 complex *r, *z; 16 { 17 double mag, sqrt(), cabs(); 18 19 if( (mag = cabs(z->real, z->imag)) == 0.) 20 r->real = r->imag = 0.; 21 else if(z->real > 0) 22 { 23 r->real = sqrt(0.5 * (mag + z->real) ); 24 r->imag = z->imag / r->real / 2; 25 } 26 else 27 { 28 r->imag = sqrt(0.5 * (mag - z->real) ); 29 if(z->imag < 0) 30 #ifndef tahoe 31 r->imag = - r->imag; 32 #else tahoe 33 *(unsigned long*)&(r->imag) ^= SIGN_BIT; 34 #endif tahoe 35 r->real = z->imag / r->imag /2; 36 } 37 } 38