Commit | Line | Data |
---|---|---|
9381a6f3 | 1 | /* |
3337a9c2 RE |
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 | * | |
0ab3343e | 6 | * @(#)z_sqrt.c 5.2 %G% |
9381a6f3 DW |
7 | */ |
8 | ||
9 | #include "complex" | |
0ab3343e KM |
10 | #ifdef tahoe |
11 | #include <tahoemath/FP.h> | |
12 | #define cabs zabs | |
13 | #endif tahoe | |
9381a6f3 DW |
14 | |
15 | z_sqrt(r, z) | |
16 | dcomplex *r, *z; | |
17 | { | |
18 | double mag, sqrt(), cabs(); | |
19 | ||
20 | if( (mag = cabs(z->dreal, z->dimag)) == 0.) | |
21 | r->dreal = r->dimag = 0.; | |
22 | else if(z->dreal > 0) | |
23 | { | |
24 | r->dreal = sqrt(0.5 * (mag + z->dreal) ); | |
25 | r->dimag = z->dimag / r->dreal / 2; | |
26 | } | |
27 | else | |
28 | { | |
29 | r->dimag = sqrt(0.5 * (mag - z->dreal) ); | |
30 | if(z->dimag < 0) | |
0ab3343e | 31 | #ifndef tahoe |
9381a6f3 | 32 | r->dimag = - r->dimag; |
0ab3343e KM |
33 | #else tahoe |
34 | *((long int *)&r->dimag) ^= SIGN_BIT; | |
35 | #endif tahoe | |
9381a6f3 DW |
36 | r->dreal = z->dimag / r->dimag / 2; |
37 | } | |
38 | } |