forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex_h.c
106 lines (72 loc) · 2.42 KB
/
complex_h.c
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
/*
# complex.h
Defines:
- convenient typedefs like `_Complex` and `I`
- common operations over complex types: creal, cimag, cabs, etc.
The complex types themselves are a feature of the language and shall not be described here.
There is no direct printf way to print complex numbers:
http://stackoverflow.com/questions/4099433/c-complex-number-and-printf
All functions provided by this header are prefixed by `c`.
http://en.wikipedia.org/wiki/Tgmath.h#Complex_numbers
# complex vs _Complex
`complex` is a typedef to `_Complex` contained in this header.
The actual type keyword is `_Complex`: the language could not add
a `complex` keyword because it could conflict with older programs.
Adding `_Complex` however is fine because it is a reserved identifier.
Prefer using `complex`: in the future it may become an actual keyword.
It is easier to read and write.
# I vs _Complex_I
TODO
# _Imaginary
Defined in non normative section of C11.
TODO vs complex?
*/
#include "common.h"
int main() {
#if __STDC_VERSION__ >= 199901L
#ifndef __STDC_NO_COMPLEX__
puts("!__STDC_NO_COMPLEX__");
const double err = 10e-6;
/* Typedef to literal. There is not explicit literal in the language. */
assert(I == _Complex_I);
const double complex zd = 1.0 + 2.0*I;
const double complex zd2 = 1.0 + 1.0*I;
assert(sizeof(float complex ) <= sizeof(double complex));
assert(sizeof(double complex) <= sizeof(long double complex));
/* Real and imaginary parts. */
assert(creal(zd) == 1.0);
assert(cimag(zd) == 2.0);
/* Subtraction. */
assert(creal(zd - zd2) == 0.0);
assert(cimag(zd - zd2) == 1.0);
/* Multiplication. */
assert(creal(zd * zd) == -3.0);
assert(cimag(zd * zd) == 4.0);
/* Division. */
assert(creal(zd / zd) == 1.0);
assert(cimag(zd / zd) == 0.0);
/* Conjugation. */
assert(creal(conj(zd)) == 1.0);
assert(cimag(conj(zd)) == -2.0);
/* absolute value == norm == module */
assert(abs(cabs(3.0 + 4.0 * I) - 5.0) < err);
/*
# csqrt
Unlike sqrt, can return imaginary outputs and take imaginary inputs.
*/
{
assert(cabs(csqrt(-1.0) - I) < err);
}
/*
# cproj
TODO
*/
/*
# csin
TODO
*/
/*assert(cabs(csin(I) - ) < err);*/
#endif
#endif
return EXIT_SUCCESS;
}