1 /*
2 * Copyright (c) 2007 - 2015 Joseph Gaeddert
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 //
24 // math_catanf_test.c
25 //
26 // complex arc-tangent test
27 //
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <complex.h>
32 #include <math.h>
33
34 #define sandbox_randf() ((float) rand() / (float) RAND_MAX)
35
sandbox_catanf(float complex _z)36 float complex sandbox_catanf(float complex _z)
37 {
38 return 0.5f*_Complex_I*clogf( (1.0f-_Complex_I*_z)/(1.0f+_Complex_I*_z) );
39 }
40
main()41 int main() {
42 unsigned int n=32; // number of tests
43 unsigned int d=2; // number items per line
44
45 // data arrays
46 float complex z[n];
47 float complex test[n];
48
49 float complex err_max = 0.0f;
50 unsigned int i;
51 for (i=0; i<n; i++) {
52 // generate random complex number
53 z[i] = 2.0f*(2.0f*sandbox_randf() - 1.0f) +
54 2.0f*(2.0f*sandbox_randf() - 1.0f) * _Complex_I;
55
56 test[i] = catanf(z[i]);
57 float complex atanz_hat = sandbox_catanf(z[i]);
58
59 float complex err = test[i] - atanz_hat;
60
61 printf("%3u: z=%6.2f+j%6.2f, atan(z)=%6.2f+j%6.2f (%6.2f+j%6.2f) e=%12.4e\n",
62 i,
63 crealf(z[i]), cimagf(z[i]),
64 crealf(test[i]), cimagf(test[i]),
65 crealf(atanz_hat), cimagf(atanz_hat),
66 cabsf(err));
67
68 if ( cabsf(err) > cabsf(err_max) )
69 err_max = err;
70 }
71
72 printf("maximum error: %12.4e;\n", cabsf(err_max));
73
74 //
75 // print autotest lines
76 //
77
78 printf("\n");
79 printf(" float complex z[%u] = {\n ", n);
80 for (i=0; i<n; i++) {
81 printf("%12.4e+_Complex_I*%12.4e", crealf(z[i]), cimagf(z[i]));
82
83 if ( i == n-1)
84 printf(" ");
85 else if ( ((i+1)%d)==0 )
86 printf(",\n ");
87 else
88 printf(", ");
89 }
90 printf("};\n");
91
92 printf("\n");
93 printf(" float complex test[%u] = {\n ", n);
94 for (i=0; i<n; i++) {
95 printf("%12.4e+_Complex_I*%12.4e", crealf(test[i]), cimagf(test[i]));
96
97 if ( i == n-1)
98 printf(" ");
99 else if ( ((i+1)%d)==0 )
100 printf(",\n ");
101 else
102 printf(", ");
103 }
104 printf("};\n");
105
106 printf("done.\n");
107 return 0;
108 }
109
110