c code for the roots of x to the power of 3 plus 1?

來源: 2023-02-12 13:26:57 [舊帖] [給我悄悄話] 本文已被閱讀:

#include <complex.h>
#include <math.h>
#include <stdio.h>

int main() {
    double complex root1, root2, root3;
    root1 = cbrt(-1) * cexp(I * M_PI / 3);
    root2 = cbrt(-1) * cexp(I * (2 * M_PI / 3));
    root3 = cbrt(-1) * cexp(I * (4 * M_PI / 3));
    printf("root 1: %.2f + %.2fi\n", creal(root1), cimag(root1));
    printf("root 2: %.2f + %.2fi\n", creal(root2), cimag(root2));
    printf("root 3: %.2f + %.2fi\n", creal(root3), cimag(root3));
    return 0;
}