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_cexpf_test.c
25 //
26 // complex exponent 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_cexpf(float complex _z)36 float complex sandbox_cexpf(float complex _z)
37 {
38 float r = expf( crealf(_z) );
39 float complex re = cosf( cimagf(_z) );
40 float complex im = sinf( cimagf(_z) );
41
42 return r*(re + _Complex_I*im);
43 }
44
main()45 int main() {
46 unsigned int n=32; // number of tests
47 unsigned int d=2; // number items per line
48
49 // data arrays
50 float complex z[n];
51 float complex test[n];
52
53 unsigned int i;
54 float complex err_max = 0.0f;
55 for (i=0; i<n; i++) {
56 // generate random complex number
57 z[i] = 4.0f*(2.0f*sandbox_randf() - 1.0f) +
58 4.0f*(2.0f*sandbox_randf() - 1.0f) * _Complex_I;
59
60 test[i] = cexpf(z[i]);
61 float complex expz_hat = sandbox_cexpf(z[i]);
62
63 float complex err = test[i] - expz_hat;
64
65 printf("%3u: z=%6.2f+j%6.2f, exp(z)=%6.2f+j%6.2f (%6.2f+j%6.2f) e=%12.4e\n",
66 i,
67 crealf(z[i]), cimagf(z[i]),
68 crealf(test[i]), cimagf(test[i]),
69 crealf(expz_hat), cimagf(expz_hat),
70 cabsf(err));
71
72 if ( cabsf(err) > cabsf(err_max) )
73 err_max = err;
74 }
75
76 printf("maximum error: %12.4e;\n", cabsf(err_max));
77
78 //
79 // print autotest lines
80 //
81
82 printf("\n");
83 printf(" float complex z[%u] = {\n ", n);
84 for (i=0; i<n; i++) {
85 printf("%14.6e+_Complex_I*%14.6e", crealf(z[i]), cimagf(z[i]));
86
87 if ( i == n-1)
88 printf(" ");
89 else if ( ((i+1)%d)==0 )
90 printf(",\n ");
91 else
92 printf(", ");
93 }
94 printf("};\n");
95
96 printf("\n");
97 printf(" float complex test[%u] = {\n ", n);
98 for (i=0; i<n; i++) {
99 printf("%14.6e+_Complex_I*%14.6e", crealf(test[i]), cimagf(test[i]));
100
101 if ( i == n-1)
102 printf(" ");
103 else if ( ((i+1)%d)==0 )
104 printf(",\n ");
105 else
106 printf(", ");
107 }
108 printf("};\n");
109
110 printf("done.\n");
111 return 0;
112 }
113
114