summaryrefslogtreecommitdiff
path: root/2.3-1/src/c/elementaryFunctions/acosd/cacoss.c
blob: 9742031370eb84514e823e45f7c5e1cdb2fbce52 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2007-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
 *
 */

/*
 * This fonction is a translation of fortran wacos write by Bruno Pincon <Bruno.Pincon@iecn.u-nancy.fr>
 *     REFERENCE
 *        This is a Fortran-77 translation of an algorithm by
 *        T.E. Hull, T. F. Fairgrieve and P.T.P. Tang which
 *        appears in their article :
 *          "Implementing the Complex Arcsine and Arccosine
 *           Functions Using Exception Handling", ACM, TOMS,
 *           Vol 23, No. 3, Sept 1997, p. 299-335
 */

#include "acos.h"
#include "atan.h"
#include "log.h"
#include "log1p.h"
#include "sqrt.h"
#include "abs.h"
#include "lapack.h"
#include "min.h"
#include "max.h"

#define localSign(x) (x>0 ? 1.0f : -1.0f)

floatComplex		cacoss(floatComplex z) {
	static float sfltPi		= 3.1415926535897932384626433f;
	static float sfltPi_2		= 1.5707963267948966192313216f;
	static float sfltLn2		= 0.6931471805599453094172321f;
	static float sfltAcross	= 1.5f;
	static float sfltBcross	= 0.6417f;

	float fltLsup = ssqrts((float) getOverflowThreshold())/8.0f;
	float fltLinf = 4.0f * ssqrts((float) getUnderflowThreshold());
	float fltEpsm = ssqrts((float) getRelativeMachinePrecision());

	float fltAbsReal	= sabss(creals(z));
	float fltAbsImg		= sabss(cimags(z));
	float fltSignReal	= localSign(creals(z));
	float fltSignImg	= localSign(cimags(z));

	float fltR = 0, fltS = 0, fltA = 0, fltB = 0;

	float fltTemp = 0;

	float _pfltReal = 0;
	float _pfltImg = 0;

	if( min(fltAbsReal, fltAbsImg) > fltLinf && max(fltAbsReal, fltAbsImg) <= fltLsup)
	  {/* we are in the safe region */
		fltR = ssqrts( (fltAbsReal + 1 )*(fltAbsReal + 1 ) + fltAbsImg*fltAbsImg);
		fltS = ssqrts( (fltAbsReal - 1 )*(fltAbsReal - 1 ) + fltAbsImg*fltAbsImg);
		fltA = 0.5f * ( fltR + fltS );
		fltB = fltAbsReal / fltA;


		/* compute the real part */
		if(fltB <= sfltBcross)
			_pfltReal = sacoss(fltB);
		else if( fltAbsReal <= 1)
			_pfltReal = satans(ssqrts(0.5f * (fltA + fltAbsReal) * (fltAbsImg*fltAbsImg / (fltR + (fltAbsReal + 1)) + (fltS + (1 - fltAbsReal)))) / fltAbsReal);
		else
			_pfltReal = satans((fltAbsImg * ssqrts(0.5f * ((fltA + fltAbsReal) / (fltR + (fltAbsReal + 1)) + (fltA + fltAbsReal) / (fltS + (fltAbsReal - 1))))) / fltAbsReal);

		/* compute the imaginary part */
		if(fltA <= sfltAcross)
		{
			float fltImg1 = 0;

			if(fltAbsReal < 1)
			  /* Am1 = 0.5d0*((y**2)/(R+(x+1.d0))+(y**2)/(S+(1.d0-x))) */
				fltImg1 = 0.5f * (fltAbsImg*fltAbsImg / (fltR + (fltAbsReal + 1)) + fltAbsImg*fltAbsImg / (fltS + (1 - fltAbsReal)));
			else
			  /* Am1 = 0.5d0*((y**2)/(R+(x+1.d0))+(S+(x-1.d0))) */
				fltImg1 = 0.5f * (fltAbsImg*fltAbsImg / (fltR + (fltAbsReal + 1)) + (fltS + (fltAbsReal - 1)));
			/* ai = logp1(Am1 + sqrt(Am1*(A+1.d0))) */
			fltTemp = fltImg1 + ssqrts(fltImg1 *( fltA + 1));
			_pfltImg = slog1ps(fltTemp);
		}
		else
		  /* ai = log(A + sqrt(A**2 - 1.d0)) */
			_pfltImg = slogs(fltA + ssqrts(fltA*fltA - 1));
	}
	else
	  {/* evaluation in the special regions ... */
		if(fltAbsImg <= fltEpsm * sabss(fltAbsReal - 1))
		{
			if(fltAbsReal < 1)
			{
				_pfltReal	= sacoss(fltAbsReal);
				_pfltImg	= fltAbsImg / ssqrts((1 + fltAbsReal) * (1 - fltAbsReal));
			}
			else
			{
				_pfltReal = 0;
				if(fltAbsReal <= fltLsup)
				{
					fltTemp		= (fltAbsReal - 1) + ssqrts((fltAbsReal - 1) * (fltAbsReal + 1));
					_pfltImg	= slog1ps(fltTemp);
				}
				else
					_pfltImg	= sfltLn2 + slogs(fltAbsReal);
			}
		}
		else if(fltAbsImg < fltLinf)
		{
			_pfltReal	= ssqrts(fltAbsImg);
			_pfltImg	= _pfltReal;
		}
		else if((fltEpsm * fltAbsImg - 1 >= fltAbsReal))
		{
			_pfltReal	= sfltPi_2;
			_pfltImg	= sfltLn2 + slogs(fltAbsImg);
		}
		else if(fltAbsReal > 1)
		{
			_pfltReal	= satans(fltAbsImg / fltAbsReal);
			fltTemp		= (fltAbsReal / fltAbsImg)*(fltAbsReal / fltAbsImg);
			_pfltImg	= sfltLn2 + slogs(fltAbsImg) + 0.5f * slog1ps(fltTemp);
		}
		else
		{
			float fltTemp2 = ssqrts(1 + fltAbsImg*fltAbsImg);
			_pfltReal	= sfltPi_2;
			fltTemp		= 2 * fltAbsImg * (fltAbsImg + fltTemp2);
			_pfltImg	= 0.5f * slog1ps(fltTemp);
		}
	}
	if(fltSignReal < 0)
		_pfltReal = sfltPi - _pfltReal;

	if(fltAbsImg != 0 || fltSignReal < 0)
		_pfltImg = - fltSignImg * _pfltImg;

	return FloatComplex(_pfltReal, _pfltImg);
}