2003-10-21 11:43:22 +02:00
|
|
|
/*
|
|
|
|
csqrt.c
|
|
|
|
Contributed by Danny Smith
|
|
|
|
2003-10-20
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <complex.h>
|
|
|
|
|
|
|
|
double complex csqrt (double complex Z)
|
|
|
|
{
|
|
|
|
double complex Res;
|
|
|
|
double t;
|
|
|
|
double x = __real__ Z;
|
|
|
|
double y = __imag__ Z;
|
|
|
|
|
|
|
|
if (y == 0.0)
|
|
|
|
{
|
|
|
|
if (x < 0.0)
|
|
|
|
{
|
|
|
|
__real__ Res = 0.0;
|
|
|
|
__imag__ Res = sqrt (-x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__real__ Res = sqrt (x);
|
|
|
|
__imag__ Res = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (x == 0.0)
|
|
|
|
{
|
|
|
|
t = sqrt(0.5 * fabs (y));
|
2005-10-12 08:46:18 +02:00
|
|
|
__real__ Res = t;
|
|
|
|
__imag__ Res = y > 0 ? t : -t;
|
2003-10-21 11:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t = sqrt (2.0 * (_hypot (x, y) + fabs (x)));
|
2005-10-12 08:46:18 +02:00
|
|
|
double u = t / 2.0;
|
2003-10-21 11:43:22 +02:00
|
|
|
if ( x > 0.0)
|
|
|
|
{
|
2005-10-12 08:46:18 +02:00
|
|
|
__real__ Res = u;
|
2003-10-21 11:43:22 +02:00
|
|
|
__imag__ Res = y / t;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__real__ Res = fabs ( y / t);
|
2005-10-12 08:46:18 +02:00
|
|
|
__imag__ Res = y < 0.0 ? -u : u;
|
2003-10-21 11:43:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|