蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 <complex.h> 复数四则运算、模长幅角、复开方幂次与欧拉/三角恒等式

C本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。explore-standard-library-complex-h.c
explore-standard-library-complex-h.c66 行
#include <stdio.h>
#include <complex.h>
#include <math.h>
#include <stdbool.h>

static bool near(double a, double b, double tol) {
    return fabs(a - b) <= tol;
}

static bool cnear(double complex a, double complex b, double tol) {
    return cabs(a - b) <= tol;
}

int main(void) {
    double complex z1 = 3.0 + 4.0 * I;
    double complex z2 = 1.0 - 2.0 * I;
    double complex sum = z1 + z2;
    double complex prod = z1 * z2;
    double complex cj = conj(z1);
    double complex pj = cproj(z1);
    bool c1_pass = (
        near(creal(sum), 4.0, 1e-12) && near(cimag(sum), 2.0, 1e-12) &&
        near(creal(prod), 11.0, 1e-12) && near(cimag(prod), -2.0, 1e-12) &&
        near(creal(cj), 3.0, 1e-12) && near(cimag(cj), -4.0, 1e-12) &&
        cnear(pj, z1, 1e-12)
    );

    double mod = cabs(z1);
    double phase = carg(z1);
    double complex sq_neg1 = csqrt(-1.0 + 0.0 * I);
    double complex i_sq = cpow(1.0 * I, 2.0);
    bool c2_pass = (
        near(mod, 5.0, 1e-12) &&
        near(phase, atan2(4.0, 3.0), 1e-12) &&
        cnear(sq_neg1, 0.0 + 1.0 * I, 1e-12) &&
        cnear(i_sq, -1.0 + 0.0 * I, 1e-12)
    );

    double complex ez1 = cexp(z1);
    double complex euler = exp(3.0) * (cos(4.0) + I * sin(4.0));
    double complex w = 1.0 + 0.5 * I;
    double complex roundtrip = clog(cexp(w));
    double complex s = csin(w), c = ccos(w), t = ctan(w);
    bool c3_pass = (
        cnear(ez1, euler, 1e-10) &&
        cnear(roundtrip, w, 1e-12) &&
        cnear(s * s + c * c, 1.0 + 0.0 * I, 1e-12) &&
        cnear(t, s / c, 1e-12)
    );

    printf(
        "{"
        "\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
        "\"cases\":["
        "{\"name\":\"complex_arithmetic_and_parts\",\"expected\":\"sum=4.0+2.0i,prod=11.0-2.0i\",\"actual\":\"sum=%.1f+%.1fi,prod=%.1f%.1fi\",\"passed\":%s},"
        "{\"name\":\"modulus_phase_and_roots\",\"expected\":\"mod=5.00,i2=-1.00\",\"actual\":\"mod=%.2f,i2=%.2f\",\"passed\":%s},"
        "{\"name\":\"cexp_clog_and_trig_identities\",\"expected\":1,\"actual\":%d,\"passed\":%s}"
        "]"
        "}\n",
        creal(sum), cimag(sum), creal(prod), cimag(prod), c1_pass ? "true" : "false",
        mod, creal(i_sq), c2_pass ? "true" : "false",
        c3_pass ? 1 : 0, c3_pass ? "true" : "false"
    );
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。

入口文件:projects/c-standard-library-complex-h-lab/explore-standard-library-complex-h.c

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-standard-library-complex-h.c,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3241.zip

下载课程关联工程包:C

课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习