...
Functions That Should Not Be Called with Complex Values
atan2() | erf | fdim | fmin | ilogb | llround | logb | nextafter | rint | tgamma |
cbrt | erfc | floor | fmod | ldexp | log10 | lrint | nexttoward | round | trunc |
ceil | exp2 | fma | frexp | lgamma | log1p | lround | remainder | scalbn | |
copysign | expm1 | fmax | hypot | llrint | log2 | nearbyint | remquo | scalbln |
This noncompliant code example attempts to take the base-2 logarithm of a complex number, resulting in undefined behavior:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <complex.h>
#include <tgmath.h>
void func(void) {
double complex c = 2.0 + 4.0 * I;
double complex result = log2(c);
} |
...
If the clog2()
function is not available for your implementation as an extension, you can take the base-2 logarithm of a complex number using log()
instead of log2()
, as log()
can be used on complex arguments, as shown in this compliant solution:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <complex.h>
#include <tgmath.h>
void func(void) {
double complex c = 2.0 + 4.0 * I;
double complex result = log(c)/log(2);
} |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <complex.h>
#include <tgmath.h>
void func(void) {
double complex c = 2.0 + 4.0 * I;
double complex result = log2(creal(c));
} |
...