summaryrefslogtreecommitdiff
path: root/2.3-1/src/c/elementaryFunctions/sqrt/zsqrts.c
blob: 3637ddd6c5161f5bd4d253942eb26ceeaa315050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2008-2008 - INRIA - Bruno JOFRET
 *  Copyright (C) Bruno Pincon
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */
#include <stdio.h>
#include <math.h>
#include "sqrt.h"
#include "lapack.h"
#include "abs.h"
#include "sign.h"
#include "pythag.h"

#ifdef _MSC_VER
#include <float.h>
#define isnan(x) _isnan((double)x)
#endif

#define _sign(a, b) b >=0 ? a : -a

doubleComplex	zsqrts(doubleComplex in) {
  double RMax =  getOverflowThreshold();
  double BRMin = 2 * getUnderflowThreshold();

  double RealIn = zreals(in);
  double ImgIn = zimags(in);

  double RealOut = 0;
  double ImgOut = 0;

  if(RealIn == 0)
    {/* pure imaginary case */
      if(dabss(ImgIn >= BRMin))
	RealOut = dsqrts(0.5 * dabss(ImgIn));
      else
	RealOut = dsqrts(dabss(ImgIn)) * dsqrts(0.5);

      ImgOut = _sign(1, ImgIn) * RealOut;
    }
  else if( dabss(RealIn) <= RMax && dabss(ImgIn) <= RMax)
    {/* standard case : a (not zero) and b are finite */
      double Temp = dsqrts(2 * (dabss(RealIn) + dpythags(RealIn, ImgIn)));
      /* overflow test */
      if(Temp > RMax)
	{/*     handle (spurious) overflow by scaling a and b */
	  double RealTemp = RealIn / 16;
	  double ImgTemp = ImgIn / 16;
	  Temp = dsqrts(2 * (dabss(RealIn) + dpythags(RealIn, ImgTemp)));
	  if(RealTemp >= 0)
	    {
	      RealOut	= 2 * Temp;
	      ImgOut	= 4 * ImgTemp / Temp;
	    }
	  else
	    {
	      RealOut	= 4 * dabss(ImgIn) / Temp;
	      ImgOut	= _sign(2, ImgIn) * Temp;
	    }
	}
      else if(RealIn >= 0) /* classic switch to get the stable formulas */
	{
	  RealOut	= 0.5 * Temp;
	  ImgOut	= ImgIn / Temp;
	}
      else
	{
	  RealOut	= dabss(ImgIn) / Temp;
	  ImgOut	= (_sign(0.5, ImgIn)) * Temp;
	}
    }
  else
    {
      /*
      //Here we treat the special cases where a and b are +- 00 or NaN.
      //The following is the treatment recommended by the C99 standard
      //with the simplification of returning NaN + i NaN if the
      //the real part or the imaginary part is NaN (C99 recommends
      //something more complicated)
      */

      if(isnan(RealIn) == 1 || isnan(ImgIn) == 1)
	{/* got NaN + i NaN */
	  RealOut	= RealIn + ImgIn;
	  ImgOut	= RealOut;
	}
      else if( dabss(ImgIn) > RMax)
	{/* case a +- i oo -> result must be +oo +- i oo  for all a (finite or not) */
	  RealOut	= dabss(ImgIn);
	  ImgOut	= ImgIn;
	}
      else if(RealIn < -RMax)
	{/* here a is -Inf and b is finite */
	  RealOut	= 0;
	  ImgOut	= _sign(1, ImgIn) * dabss(RealIn);
	}
      else
	{/* here a is +Inf and b is finite */
	  RealOut	= RealIn;
	  ImgOut	= 0;
	}
    }

  return DoubleComplex(RealOut, ImgOut);
}