Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fiddling with clog2 stuff

...

This noncompliant code example attempts to take the base-2 logarithm of a complex number, resulting in undefined behavior:

Code Block
bgColor#ffcccc
langc
#include <complex.h>
#include <tgmath.h>
 
void func(void) {
  double complex c = 2.0 + 4.0 * I;
  double complex result = log2(c);
}

Compliant Solution (

...

Complex Number)

This compliant solution ensures that the logarithm is applied only to the real part of the complex numberIf the clog2() function is not available for your implementation, 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
bgColor#ccccff
langc
#include <complex.h>
#include <tgmath.h>
  
void func(void) {
  double complex c = 2.0 + 4.0 * I;
  double complex result = log2log(creal(c)/log(2);
}

Compliant Solution (

...

Real Number)

If the programmer was intending to take the logarithm of the complex number this compliant solution uses log() instead of log2(), as log() can be used on complex argumentsThis compliant solution can be used if the programmers intent is to only take the base-2 logarithm of the real part of the complex number:

Code Block
bgColor#ccccff
langc
#include <complex.h>
#include <tgmath.h>
  
void func(void) {
  double complex c = 2.0 + 4.0 * I;
  double complex result = loglog2(creal(c)/log(2);
}

Noncompliant Code Example

...