C语言函数大全--c开头的函数之复数篇

总览

函数声明 函数功能
double cabs (double complex z); 计算复数 z 的绝对值(double)
float cabsf (float complex z); 计算复数 z 的绝对值(float)
long double cabsl (long double complex z); 计算复数 z 的绝对值(long double)
double creal (double complex z); 计算复数z的实部(double)
float crealf (float complex z); 计算复数z的实部(float)
long double creall (long double complex z); 计算复数z的实部(long double)
double cimag (double complex z); 计算复数z的虚部(double)
float cimagf (float complex z); 计算复数z的虚部(float)
long double cimagl (long double complex z); 计算复数z的虚部(long double)
double carg (double complex z); 计算复数z的相位角 (double)
float cargf (float complex z); 计算复数z的相位角(float)
long double cargl (long double complex z); 计算复数z的相位角(long double)
double complex cacos (double complex z); 计算复数z的反余弦 (double complex)
float complex cacosf (float complex z); 计算复数z的反余弦(float complex)
long double complex cacosl (long double complex z); 计算复数z的反余弦(long double complex)
double complex cacosh (double complex z); 计算复数z的反双曲余弦(double complex)
float complex cacoshf (float complex z); 计算复数z的反双曲余弦(float complex)
long double complex cacoshl (long double complex z); 计算复数z的反双曲余弦(long double complex)
double complex casin (double complex z); 计算复数z的反正弦(double complex)
float complex casinf (float complex z); 计算复数z的反正弦(float complex)
long double complex casinl (long double complex z); 计算复数z的反正弦(long double complex)
double complex casinh (double complex z); 计算复数z的反双曲正弦(double complex)
float complex casinhf (float complex z); 计算复数z的反双曲正弦(float complex)
long double complex casinhl (long double complex z); 计算复数z的反双曲正弦(long double complex)
double complex catan (double complex z); 计算复数z的反正切(double complex)
float complex catanf (float complex z); 计算复数z的反正切(float complex)
long double complex catanl (long double complex z); 计算复数z的反正切(long double complex)
double complex catanh (double complex z); 计算复数z的反双曲正切(double complex)
float complex catanhf (float complex z); 计算复数z的反双曲正切(float complex)
long double complex catanhl (long double complex z); 计算复数z的反双曲正切(long double complex)
double complex ccos (double complex z); 计算复数z的余弦(double complex)
float complex ccosf (float complex z); 计算复数z的余弦(float complex)
long double complex ccosl (long double complex z); 计算复数z的余弦(long double complex)
double complex ccosh (double complex z); 计算复数z的双曲余弦(double complex)
float complex ccoshf (float complex z); 计算复数z的双曲余弦(float complex)
long double complex ccoshl (long double complex z); 计算复数z的双曲余弦(long double complex)
double complex csin (double complex z); 计算复数z的正弦(double complex)
float complex csinf (float complex z); 计算复数z的正弦(float complex)
long double complex csinl (long double complex z); 计算复数z的正弦(long double complex)
double complex csinh (double complex z); 计算复数z的双曲正弦(double complex)
float complex csinhf (float complex z); 计算复数z的双曲正弦(float complex)
long double complex csinhl (long double complex z); 计算复数z的双曲正弦(long double complex)
double complex ctan (double complex z); 计算复数z的正切(double complex)
float complex ctanf (float complex z); 计算复数z的正切(float complex)
long double complex ctanl (long double complex z); 计算复数z的正切(long double complex)
double complex ctanh (double complex z); 计算复数z的双曲正切(double complex)
float complex ctanhf (float complex z); 计算复数z的双曲正切(float complex)
long double complex ctanhl (long double complex z); 计算复数z的双曲正切(long double complex)
double complex cexp (double complex z); 计算复数z的指数基数e(double complex)
float complex cexpf (float complex z); 计算复数z的指数基数e(float complex)
long double complex cexpl (long double complex z); 计算复数z的指数基数e(long double complex)
double complex clog (double complex z); 计算复数z的自然对数(以e为底)(double complex)
float complex clogf (float complex z); 计算复数z的自然对数(以e为底)(float complex)
long double complex clogl (long double complex z); 计算复数z的自然对数(以e为底)(long double complex)
double complex conj (double complex z); 计算复数z的共轭(double complex)
float complex conjf (float complex z); 计算复数z的共轭(float complex)
long double complex conjl (long double complex z); 计算复数z的共轭(long double complex)
double complex cpow (double complex x, double complex y); 计算x的y次方值 (double complex)
float complex cpowf (float complex x, float complex y); 计算x的y次方值 (float complex)
long double complex cpowl (long double complex x, long double complex y); 计算x的y次方值 (double complex)
double complex cproj (double complex z); 计算复数z在黎曼球面上的投影(double complex)
float complex cprojf (float complex z); 计算复数z在黎曼球面上的投影(float complex)
long double complex cprojl (long double complex z); 计算复数z在黎曼球面上的投影(long double complex)
double complex csqrt (double complex z); 计算复数z的平方根(double complex)
float complex csqrtf (float complex z); 计算复数z的平方根(float complex)
long double complex csqrtl (long double complex z); 计算复数z的平方根(long double complex)

1. cabs,cabsf,cabsl

1.1 函数说明

函数声明 函数功能
double cabs (double complex z); 计算复数 z 的绝对值(double)
float cabsf (float complex z); 计算复数 z 的绝对值(float)
long double cabsl (long double complex z); 计算复数 z 的绝对值(long double)

1.2 演示示例

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
// Huazie
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex z;
double x = 2.0, y = 2.0, val;
z = x + y * I; // I 代指 虚数单位 i
val = cabs(z); // 计算复数 z 的绝对值

float complex zf;
float xf = 2.0, yf = 2.0, valf;
zf = xf + yf * I;
valf = cabsf(zf);

long double complex zL;
long double xL = 2.0, yL = 2.0, valL;
zL = xL + yL * I;
valL = cabsl(zL);

printf("The absolute value of (%.4lf + %.4lfi) is %.20lf\n", x, y, val);
printf("The absolute value of (%.4f + %.4fi) is %.20f\n", xf, yf, valf);
printf("The absolute value of (%.4Lf + %.4Lfi) is %.20Lf", xL, yL, valL);

return 0;
}

1.3 运行结果

2. creal,crealf,creall

2.1 函数说明

函数声明 函数功能
double creal (double complex z); 计算复数z的实部(double)
float crealf (float complex z); 计算复数z的实部(float)
long double creall (long double complex z); 计算复数z的实部(long double)

2.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Huazie
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex z;
double x = 2.0, y = 1.0;
z = x + y * I; // I 代指 虚数单位 i

float complex zf;
float xf = 3.0, yf = 1.0;
zf = xf + yf * I;

long double complex zL;
long double xL = 4.0, yL = 1.0;
zL = xL + yL * I;

printf("The real part of (%.4lf + %.4lfi) is %.4lf\n", x, y, creal(z));
printf("The real part of (%.4f + %.4fi) is %.4f\n", xf, yf, crealf(zf));
printf("The real part of (%.4Lf + %.4Lfi) is %.4Lf", xL, yL, creall(zL));

return 0;
}

2.3 运行结果

3. cimag,cimagf,cimagl

3.1 函数说明

函数声明 函数功能
double cimag (double complex z); 计算复数z的虚部(double)
float cimagf (float complex z); 计算复数z的虚部(float)
long double cimagl (long double complex z); 计算复数z的虚部(long double)

3.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Huazie
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex z;
double x = 1.0, y = 2.0;
z = x + y * I; // I 代指 虚数单位 i

float complex zf;
float xf = 1.0, yf = 3.0;
zf = xf + yf * I;

long double complex zL;
long double xL = 1.0, yL = 4.0;
zL = xL + yL * I;

printf("The imaginary part of (%.4lf + %.4lfi) is %.4lf\n", x, y, cimag(z));
printf("The imaginary part of (%.4f + %.4fi) is %.4f\n", xf, yf, cimagf(zf));
printf("The imaginary part of (%.4Lf + %.4Lfi) is %.4Lf", xL, yL, cimagl(zL));

return 0;
}

3.3 运行结果

4. carg,cargf,cargl

4.1 函数说明

函数声明 函数功能
double carg (double complex z); 计算复数z的相位角 (double)
float cargf (float complex z); 计算复数z的相位角(float)
long double cargl (long double complex z); 计算复数z的相位角(long double)

相位角是描述波形在时间轴上的位置的一个重要参数,它决定了波形的起始位置和变化状态。在实际应用中,相位角的测量和控制对于电路设计和信号处理至关重要。通过对相位角的理解和应用,可以更好地分析和控制波动现象,从而实现对电力系统和通信系统的优化。

4.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex z;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i

float complex zf;
zf = 1.0f + 2.0f * I;

long double complex zL;
zL = (long double) 1.0 + (long double) 2.0 * I;

printf("The phase angle of (%.4lf + %.4lfi) is %.60lf\n", creal(z), cimag(z), carg(z));
printf("The phase angle of (%.4f + %.4fi) is %.60f\n", crealf(zf), cimagf(zf), cargf(zf));
printf("The phase angle of (%.4Lf + %.4Lfi) is %.60Lf", creall(zL), cimagl(zL), cargl(zL));

return 0;
}

4.3 运行结果

5. cacos,cacosf,cacosl

5.1 函数说明

函数声明 函数功能
double complex cacos (double complex z); 计算复数z的反余弦 (double complex)
float complex cacosf (float complex z); 计算复数z的反余弦(float complex)
long double complex cacosl (long double complex z); 计算复数z的反余弦(long double complex)

反余弦函数是三角函数中余弦函数的逆函数,其主要作用是在已知余弦函数值的情况下,求解对应的角度值。反余弦函数的定义域为[-1, 1],值域为[0, π]

反余弦函数的图像是一个在[0, π]区间内单调递减的曲线。

  • x=1时,arccos(1)=0,表示余弦值为1的角度是0度;
  • x=-1时,arccos(-1)=π,表示余弦值为-1的角度是180度。
  • 随着x值从1减小到-1,arccos(x)的值从0增加到π。

5.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcacos;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcacos = cacos(z); // 计算复数z的反余弦

float complex zf, zcacosf;
zf = 1.0f + 2.0f * I;
zcacosf = cacosf(zf);

long double complex zL, zcacosl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcacosl = cacosl(zL);

double zimag = cimag(zcacos);
float zimagf = cimagf(zcacosf);
long double zimagl = cimagl(zcacosl);
if (zimag < 0)
printf("The arc cosine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcacos), fabs(zimag));
else
printf("The arc cosine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcacos), zimag);

if (zimagf < 0)
printf("The arc cosine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcacosf), fabsf(zimagf));
else
printf("The arc cosine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcacosf), zimagf);

if (zimagl < 0)
printf("The arc cosine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcacosl), fabsl(zimagl));
else
printf("The arc cosine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcacosl), zimagl);
return 0;
}

5.3 运行结果

6. cacosh,cacoshf,cacoshl

6.1 函数说明

函数声明 函数功能
double complex cacosh (double complex z); 计算复数z的反双曲余弦(double complex)
float complex cacoshf (float complex z); 计算复数z的反双曲余弦(float complex)
long double complex cacoshl (long double complex z); 计算复数z的反双曲余弦(long double complex)

反双曲余弦函数,表示为双曲余弦函数的反函数,其定义为:对于任意实数x(x≥1),存在一个唯一的实数y,使得cosh(y) = x。此时,y就被称为x的反双曲余弦值。

6.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcacosh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcacosh = cacosh(z); // 反双曲余弦

float complex zf, zcacoshf;
zf = 1.0f + 2.0f * I;
zcacoshf = cacoshf(zf);

long double complex zL, zcacoshl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcacoshl = cacoshl(zL);

double zimag = cimag(zcacosh);
float zimagf = cimagf(zcacoshf);
long double zimagl = cimagl(zcacoshl);
if (zimag < 0)
printf("The inverse hyperbolic cosine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcacosh), fabs(zimag));
else
printf("The inverse hyperbolic cosine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcacosh), zimag);

if (zimagf < 0)
printf("The inverse hyperbolic cosine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcacoshf), fabsf(zimagf));
else
printf("The inverse hyperbolic cosine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcacoshf), zimagf);

if (zimagl < 0)
printf("The inverse hyperbolic cosine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcacoshl), fabsl(zimagl));
else
printf("The inverse hyperbolic cosine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcacoshl), zimagl);
return 0;
}

6.3 运行结果

7. casin,casinf,casinl

7.1 函数说明

函数声明 函数功能
double complex casin (double complex z); 计算复数z的反正弦(double complex)
float complex casinf (float complex z); 计算复数z的反正弦(float complex)
long double complex casinl (long double complex z); 计算复数z的反正弦(long double complex)

反正弦函数是正弦函数的反函数。也就是说,如果sinθ=x,且x[-1,1]的范围内,那么θ就是x的反正弦值。

7.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcasin;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcasin = casin(z); // 反正弦

float complex zf, zcasinf;
zf = 1.0f + 2.0f * I;
zcasinf = casinf(zf);

long double complex zL, zcasinl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcasinl = casinl(zL);

double zimag = cimag(zcasin);
float zimagf = cimagf(zcasinf);
long double zimagl = cimagl(zcasinl);
if (zimag < 0)
printf("The arcsine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcasin), fabs(zimag));
else
printf("The arcsine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcasin), zimag);

if (zimagf < 0)
printf("The arcsine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcasinf), fabsf(zimagf));
else
printf("The arcsine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcasinf), zimagf);

if (zimagl < 0)
printf("The arcsine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcasinl), fabsl(zimagl));
else
printf("The arcsine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcasinl), zimagl);
return 0;
}

7.3 运行结果

8. casinh,casinhf,casinhl

8.1 函数说明

函数声明 函数功能
double complex casinh (double complex z); 计算复数z的反双曲正弦(double complex)
float complex casinhf (float complex z); 计算复数z的反双曲正弦(float complex)
long double complex casinhl (long double complex z); 计算复数z的反双曲正弦(long double complex)

反双曲正弦函数是双曲正弦函数的反函数。

8.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcasinh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcasinh = casinh(z); // 反双曲正弦

float complex zf, zcasinhf;
zf = 1.0f + 2.0f * I;
zcasinhf = casinhf(zf);

long double complex zL, zcasinhl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcasinhl = casinhl(zL);

double zimag = cimag(zcasinh);
float zimagf = cimagf(zcasinhf);
long double zimagl = cimagl(zcasinhl);
if (zimag < 0)
printf("The inverse hyperbolic sine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcasinh), fabs(zimag));
else
printf("The inverse hyperbolic sine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcasinh), zimag);

if (zimagf < 0)
printf("The inverse hyperbolic sine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcasinhf), fabsf(zimagf));
else
printf("The inverse hyperbolic sine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcasinhf), zimagf);

if (zimagl < 0)
printf("The inverse hyperbolic sine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcasinhl), fabsl(zimagl));
else
printf("The inverse hyperbolic sine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcasinhl), zimagl);
return 0;
}

8.3 运行结果

9. catan,catanf,catanl

9.1 函数说明

函数声明 函数功能
double complex catan (double complex z); 计算复数z的反正切(double complex)
float complex catanf (float complex z); 计算复数z的反正切(float complex)
long double complex catanl (long double complex z); 计算复数z的反正切(long double complex)

反正切函数是正切函数的反函数。如果给定一个正切值x,反正切函数会返回对应的角度值θ,该角度值θ满足tan(θ)=x,并且θ(-π/2, π/2)范围内。

9.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcatan;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcatan = catan(z); // 反正切

float complex zf, zcatanf;
zf = 1.0f + 2.0f * I;
zcatanf = catanf(zf);

long double complex zL, zcatanl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcatanl = catanl(zL);

double zimag = cimag(zcatan);
float zimagf = cimagf(zcatanf);
long double zimagl = cimagl(zcatanl);
if (zimag < 0)
printf("The arc tangent of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcatan), fabs(zimag));
else
printf("The arc tangent of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcatan), zimag);

if (zimagf < 0)
printf("The arc tangent of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcatanf), fabsf(zimagf));
else
printf("The arc tangent of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcatanf), zimagf);

if (zimagl < 0)
printf("The arc tangent of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcatanl), fabsl(zimagl));
else
printf("The arc tangent of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcatanl), zimagl);
return 0;
}

9.3 运行结果

10. catanh,catanhf,catanhl

10.1 函数说明

函数声明 函数功能
double complex catanh (double complex z); 计算复数z的反双曲正切(double complex)
float complex catanhf (float complex z); 计算复数z的反双曲正切(float complex)
long double complex catanhl (long double complex z); 计算复数z的反双曲正切(long double complex)

反双曲正切函数是双曲正切函数的反函数。

10.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcatanh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcatanh = catanh(z); // 反双曲正切

float complex zf, zcatanhf;
zf = 1.0f + 2.0f * I;
zcatanhf = catanhf(zf);

long double complex zL, zcatanhl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcatanhl = catanhl(zL);

double zimag = cimag(zcatanh);
float zimagf = cimagf(zcatanhf);
long double zimagl = cimagl(zcatanhl);
if (zimag < 0)
printf("The inverse hyperbolic tangent of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcatanh), fabs(zimag));
else
printf("The inverse hyperbolic tangent of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcatanh), zimag);

if (zimagf < 0)
printf("The inverse hyperbolic tangent of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcatanhf), fabsf(zimagf));
else
printf("The inverse hyperbolic tangent of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcatanhf), zimagf);

if (zimagl < 0)
printf("The inverse hyperbolic tangent of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcatanhl), fabsl(zimagl));
else
printf("The inverse hyperbolic tangent of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcatanhl), zimagl);
return 0;
}

10.3 运行结果

11. ccos,ccosf,ccosl

11.1 函数说明

函数声明 函数功能
double complex ccos (double complex z); 计算复数z的余弦(double complex)
float complex ccosf (float complex z); 计算复数z的余弦(float complex)
long double complex ccosl (long double complex z); 计算复数z的余弦(long double complex)

11.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zccos;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zccos = ccos(z); // 余弦

float complex zf, zccosf;
zf = 1.0f + 2.0f * I;
zccosf = ccosf(zf);

long double complex zL, zccosl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zccosl = ccosl(zL);

double zimag = cimag(zccos);
float zimagf = cimagf(zccosf);
long double zimagl = cimagl(zccosl);
if (zimag < 0)
printf("The cosine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zccos), fabs(zimag));
else
printf("The cosine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zccos), zimag);

if (zimagf < 0)
printf("The cosine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zccosf), fabsf(zimagf));
else
printf("The cosine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zccosf), zimagf);

if (zimagl < 0)
printf("The cosine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zccosl), fabsl(zimagl));
else
printf("The cosine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zccosl), zimagl);
return 0;
}

11.3 运行结果

12. ccosh,ccoshf,ccoshl

12.1 函数说明

函数声明 函数功能
double complex ccosh (double complex z); 计算复数z的双曲余弦(double complex)
float complex ccoshf (float complex z); 计算复数z的双曲余弦(float complex)
long double complex ccoshl (long double complex z); 计算复数z的双曲余弦(long double complex)

12.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zccosh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zccosh = ccosh(z); // 双曲余弦

float complex zf, zccoshf;
zf = 1.0f + 2.0f * I;
zccoshf = ccoshf(zf);

long double complex zL, zccoshl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zccoshl = ccoshl(zL);

double zimag = cimag(zccosh);
float zimagf = cimagf(zccoshf);
long double zimagl = cimagl(zccoshl);
if (zimag < 0)
printf("The hyperbolic cosine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zccosh), fabs(zimag));
else
printf("The hyperbolic cosine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zccosh), zimag);

if (zimagf < 0)
printf("The hyperbolic cosine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zccoshf), fabsf(zimagf));
else
printf("The hyperbolic cosine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zccoshf), zimagf);

if (zimagl < 0)
printf("The hyperbolic cosine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zccoshl), fabsl(zimagl));
else
printf("The hyperbolic cosine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zccoshl), zimagl);
return 0;
}

12.3 运行结果

13. csin,csinf,csinl

13.1 函数说明

函数声明 函数功能
double complex csin (double complex z); 计算复数z的正弦(double complex)
float complex csinf (float complex z); 计算复数z的正弦(float complex)
long double complex csinl (long double complex z); 计算复数z的正弦(long double complex)

13.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcsin;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcsin = csin(z); // 正弦

float complex zf, zcsinf;
zf = 1.0f + 2.0f * I;
zcsinf = csinf(zf);

long double complex zL, zcsinl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcsinl = csinl(zL);

double zimag = cimag(zcsin);
float zimagf = cimagf(zcsinf);
long double zimagl = cimagl(zcsinl);
if (zimag < 0)
printf("The sine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcsin), fabs(zimag));
else
printf("The sine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcsin), zimag);

if (zimagf < 0)
printf("The sine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsinf), fabsf(zimagf));
else
printf("The sine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsinf), zimagf);

if (zimagl < 0)
printf("The sine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcsinl), fabsl(zimagl));
else
printf("The sine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcsinl), zimagl);
return 0;
}

13.3 运行结果

14. csinh,csinhf,csinhl

14.1 函数说明

函数声明 函数功能
double complex csinh (double complex z); 计算复数z的双曲正弦(double complex)
float complex csinhf (float complex z); 计算复数z的双曲正弦(float complex)
long double complex csinhl (long double complex z); 计算复数z的双曲正弦(long double complex)

14.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcsinh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcsinh = csinh(z); // 双曲正弦

float complex zf, zcsinhf;
zf = 1.0f + 2.0f * I;
zcsinhf = csinhf(zf);

long double complex zL, zcsinhl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcsinhl = csinhl(zL);

double zimag = cimag(zcsinh);
float zimagf = cimagf(zcsinhf);
long double zimagl = cimagl(zcsinhl);
if (zimag < 0)
printf("The hyperbolic sine of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcsinh), fabs(zimag));
else
printf("The hyperbolic sine of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcsinh), zimag);

if (zimagf < 0)
printf("The hyperbolic sine of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsinhf), fabsf(zimagf));
else
printf("The hyperbolic sine of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsinhf), zimagf);

if (zimagl < 0)
printf("The hyperbolic sine of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcsinhl), fabsl(zimagl));
else
printf("The hyperbolic sine of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcsinhl), zimagl);
return 0;
}

14.3 运行结果

15. ctan,ctanf,ctanl

15.1 函数说明

函数声明 函数功能
double complex ctan (double complex z); 计算复数z的正切(double complex)
float complex ctanf (float complex z); 计算复数z的正切(float complex)
long double complex ctanl (long double complex z); 计算复数z的正切(long double complex)

15.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zctan;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zctan = ctan(z); // 正切

float complex zf, zctanf;
zf = 1.0f + 2.0f * I;
zctanf = ctanf(zf);

long double complex zL, zctanl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zctanl = ctanl(zL);

double zimag = cimag(zctan);
float zimagf = cimagf(zctanf);
long double zimagl = cimagl(zctanl);
if (zimag < 0)
printf("The tangent of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zctan), fabs(zimag));
else
printf("The tangent of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zctan), zimag);

if (zimagf < 0)
printf("The tangent of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zctanf), fabsf(zimagf));
else
printf("The tangent of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zctanf), zimagf);

if (zimagl < 0)
printf("The tangent of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zctanl), fabsl(zimagl));
else
printf("The tangent of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zctanl), zimagl);
return 0;
}

15.3 运行结果

16. ctanh,ctanhf,ctanhl

16.1 函数说明

函数声明 函数功能
double complex ctanh (double complex z); 计算复数z的双曲正切(double complex)
float complex ctanhf (float complex z); 计算复数z的双曲正切(float complex)
long double complex ctanhl (long double complex z); 计算复数z的双曲正切(long double complex)

16.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zctanh;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zctanh = ctanh(z); // 双曲正切

float complex zf, zctanhf;
zf = 1.0f + 2.0f * I;
zctanhf = ctanhf(zf);

long double complex zL, zctanhl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zctanhl = ctanhl(zL);

double zimag = cimag(zctanh);
float zimagf = cimagf(zctanhf);
long double zimagl = cimagl(zctanhl);
if (zimag < 0)
printf("The inverse hyperbolic tangent of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zctanh), fabs(zimag));
else
printf("The inverse hyperbolic tangent of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zctanh), zimag);

if (zimagf < 0)
printf("The inverse hyperbolic tangent of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zctanhf), fabsf(zimagf));
else
printf("The inverse hyperbolic tangent of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zctanhf), zimagf);

if (zimagl < 0)
printf("The inverse hyperbolic tangent of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zctanhl), fabsl(zimagl));
else
printf("The inverse hyperbolic tangent of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zctanhl), zimagl);
return 0;
}

16.3 运行结果

17. cexp,cexpf,cexpl

17.1 函数说明

函数声明 函数功能
double complex cexp (double complex z); 计算复数z的指数基数e(double complex)
float complex cexpf (float complex z); 计算复数z的指数基数e(float complex)
long double complex cexpl (long double complex z); 计算复数z的指数基数e(long double complex)

17.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcexp;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcexp = cexp(z); // 指数基数e

float complex zf, zcexpf;
zf = 1.0f + 2.0f * I;
zcexpf = cexpf(zf);

long double complex zL, zcexpl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zcexpl = cexpl(zL);

double zimag = cimag(zcexp);
float zimagf = cimagf(zcexpf);
long double zimagl = cimagl(zcexpl);
if (zimag < 0)
printf("The base-e exponential of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcexp), fabs(zimag));
else
printf("The base-e exponential of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcexp), zimag);

if (zimagf < 0)
printf("The base-e exponential of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcexpf), fabsf(zimagf));
else
printf("The base-e exponential of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcexpf), zimagf);

if (zimagl < 0)
printf("The base-e exponential of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcexpl), fabsl(zimagl));
else
printf("The base-e exponential of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcexpl), zimagl);
return 0;
}

17.3 运行结果

18. clog,clogf,clogl

18.1 函数说明

函数声明 函数功能
double complex clog (double complex z); 计算复数z的自然对数(以e为底)(double complex)
float complex clogf (float complex z); 计算复数z的自然对数(以e为底)(float complex)
long double complex clogl (long double complex z); 计算复数z的自然对数(以e为底)(long double complex)

18.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zclog;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zclog = clog(z); // 自然对数(以e为底)

float complex zf, zclogf;
zf = 1.0f + 2.0f * I;
zclogf = clogf(zf);

long double complex zL, zclogl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zclogl = clogl(zL);

double zimag = cimag(zclog);
float zimagf = cimagf(zclogf);
long double zimagl = cimagl(zclogl);
if (zimag < 0)
printf("The natural (base-e) logarithm of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zclog), fabs(zimag));
else
printf("The natural (base-e) logarithm of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zclog), zimag);

if (zimagf < 0)
printf("The natural (base-e) logarithm of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zclogf), fabsf(zimagf));
else
printf("The natural (base-e) logarithm of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zclogf), zimagf);

if (zimagl < 0)
printf("The natural (base-e) logarithm of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zclogl), fabsl(zimagl));
else
printf("The natural (base-e) logarithm of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zclogl), zimagl);
return 0;
}

18.3 运行结果

19. conj,conjf,conjl

19.1 函数说明

函数声明 函数功能
double complex conj (double complex z); 计算复数z的共轭(double complex)
float complex conjf (float complex z); 计算复数z的共轭(float complex)
long double complex conjl (long double complex z); 计算复数z的共轭(long double complex)

19.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zconj;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zconj = conj(z); // 共轭

float complex zf, zconjf;
zf = 1.0f + 2.0f * I;
zconjf = conjf(zf);

long double complex zL, zconjl;
zL = (long double) 1.0 + (long double) 2.0 * I;
zconjl = conjl(zL);

double zimag = cimag(zconj);
float zimagf = cimagf(zconjf);
long double zimagl = cimagl(zconjl);
if (zimag < 0)
printf("The conjugate of (%.4lf + %.4lfi) is (%.4lf - %.4lfi)\n", creal(z), cimag(z), creal(zconj), fabs(zimag));
else
printf("The conjugate of (%.4lf + %.4lfi) is (%.4lf + %.4lfi)\n", creal(z), cimag(z), creal(zconj), zimag);

if (zimagf < 0)
printf("The conjugate of (%.4f + %.4fi) is (%.4f - %.4fi)\n", crealf(zf), cimagf(zf), crealf(zconjf), fabsf(zimagf));
else
printf("The conjugate of (%.4f + %.4fi) is (%.4f + %.4fi)\n", crealf(zf), cimagf(zf), crealf(zconjf), zimagf);

if (zimagl < 0)
printf("The conjugate of (%.4Lf + %.4Lfi) is (%.4Lf - %.4Lfi)", creall(zL), cimagl(zL), creall(zconjl), fabsl(zimagl));
else
printf("The conjugate of (%.4Lf + %.4Lfi) is (%.4Lf + %.4Lfi)", creall(zL), cimagl(zL), creall(zconjl), zimagl);
return 0;
}

19.3 运行结果

20. cpow,cpowf,cpowl

20.1 函数说明

函数声明 函数功能
double complex cpow (double complex x, double complex y); 计算x的y次方值 (double complex)
float complex cpowf (float complex x, float complex y); 计算x的y次方值 (float complex)
long double complex cpowl (long double complex x, long double complex y); 计算x的y次方值 (double complex)

20.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex x, y, z;
x = 1.0 + 2.0 * I; // I 代指 虚数单位 i
y = 2.0 + 1.0 * I;
z = cpow(x, y); // x的y次方值

float complex xf, yf, zf;
xf = 1.0f + 2.0f * I;
yf = 2.0f + 1.0f * I;
zf = cpowf(xf, yf);

long double complex xL, yL, zL;
xL = (long double) 1.0 + (long double) 2.0 * I;
yL = (long double) 2.0 + (long double) 1.0 * I;
zL = cpowl(xL, yL);

double zimag = cimag(z);
float zimagf = cimagf(zf);
long double zimagl = cimagl(zL);
if (zimag < 0)
printf("the value of (%.4lf + %.4lfi) raised to the (%.4lf + %.4lfi) power is (%.20lf - %.20lfi)\n",
creal(x), cimag(x), creal(y), cimag(y), creal(z), fabs(zimag));
else
printf("the value of (%.4lf + %.4lfi) raised to the (%.4lf + %.4lfi) power is (%.20lf + %.20lfi)\n",
creal(x), cimag(x), creal(y), cimag(y), creal(z), zimag);

if (zimagf < 0)
printf("the value of (%.4f + %.4fi) raised to the (%.4f + %.4fi) power is (%.20f - %.20fi)\n",
crealf(xf), cimagf(xf), crealf(yf), cimagf(yf), crealf(zf), fabs(zimagf));
else
printf("the value of (%.4f + %.4fi) raised to the (%.4f + %.4fi) power is (%.20f + %.20fi)\n",
crealf(xf), cimagf(xf), crealf(yf), cimagf(yf), crealf(zf), zimagf);
if (zimagl < 0)
printf("the value of (%.4Lf + %.4Lfi) raised to the (%.4Lf + %.4Lfi) power is (%.20Lf - %.20Lfi)\n",
creall(xL), cimagl(xL), creall(yL), cimagl(yL), creall(zL), fabs(zimagl));
else
printf("the value of (%.4Lf + %.4Lfi) raised to the (%.4Lf + %.4Lfi) power is (%.20Lf + %.20Lfi)\n",
creall(xL), cimagl(xL), creall(yL), cimagl(yL), creall(zL), zimagl);
return 0;
}

20.3 运行结果

21. cproj,cprojf,cprojl

21.1 函数说明

函数声明 函数功能
double complex cproj (double complex z); 计算复数z在黎曼球面上的投影(double complex)
float complex cprojf (float complex z); 计算复数z在黎曼球面上的投影(float complex)
long double complex cprojl (long double complex z); 计算复数z在黎曼球面上的投影(long double complex)

黎曼球面上的投影是一种将三维空间中的黎曼球面与二维复平面通过立体投影方式建立一一对应关系的映射。

21.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcproj;
z = 1.0 + 2.0 * I; // I 代指 虚数单位 i
zcproj = cproj(z); // 计算复数z在黎曼球面上的投影

float complex zf, zcprojf;
zf = NAN + INFINITY * I;
zcprojf = cprojf(zf);

long double complex zL, zcprojl;
zL = INFINITY + (long double) 3.0 * I;
zcprojl = cprojl(zL); // 结果相当于 INFINITY + i*copysign(0.0, cimag(z)).

double zimag = cimag(zcproj);
float zimagf = cimagf(zcprojf);
long double zimagl = cimagl(zcprojl);
if (zimag < 0)
printf("The projection of the (%.4lf + %.4lf i) onto the Riemann sphere is (%.4lf - %.4lf i)\n", creal(z), cimag(z), creal(zcproj), fabs(zimag));
else
printf("The projection of the (%.4lf + %.4lf i) onto the Riemann sphere is (%.4lf + %.4lf i)\n", creal(z), cimag(z), creal(zcproj), zimag);

if (zimagf < 0)
printf("The projection of the (%.4f + %.4f i) onto the Riemann sphere is (%.4f - %.4f i)\n", crealf(zf), cimagf(zf), crealf(zcprojf), fabsf(zimagf));
else
printf("The projection of the (%.4f + %.4f i) onto the Riemann sphere is (%.4f + %.4f i)\n", crealf(zf), cimagf(zf), crealf(zcprojf), zimagf);

if (zimagl < 0)
printf("The projection of the (%.4Lf + %.4Lf i) onto the Riemann sphere is (%.4Lf - %.4Lf i)", creall(zL), cimagl(zL), creall(zcprojl), fabsl(zimagl));
else
printf("The projection of the (%.4Lf + %.4Lf i) onto the Riemann sphere is (%.4Lf + %.4Lf i)", creall(zL), cimagl(zL), creall(zcprojl), zimagl);
return 0;
}

21.3 运行结果

22. csqrt,csqrtf,csqrtl

22.1 函数说明

函数声明 函数功能
double complex csqrt (double complex z); 计算复数z的平方根(double complex)
float complex csqrtf (float complex z); 计算复数z的平方根(float complex)
long double complex csqrtl (long double complex z); 计算复数z的平方根(long double complex)

22.2 演示示例

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
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
double complex z, zcsqrt;
z = 9.0 + 9.0 * I; // I 代指 虚数单位 i
zcsqrt = csqrt(z); // 平方根

float complex zf, zcsqrtf;
zf = 4.0f + 4.0f * I;
zcsqrtf = csqrtf(zf);

long double complex zL, zcsqrtl;
zL = (long double) 16.0 + (long double) 16.0 * I;
zcsqrtl = csqrtl(zL);

double zimag = cimag(zcsqrt);
float zimagf = cimagf(zcsqrtf);
long double zimagl = cimagl(zcsqrtl);
if (zimag < 0)
printf("The square root of (%.4lf + %.4lfi) is (%.20lf - %.20lfi)\n", creal(z), cimag(z), creal(zcsqrt), fabs(zimag));
else
printf("The square root of (%.4lf + %.4lfi) is (%.20lf + %.20lfi)\n", creal(z), cimag(z), creal(zcsqrt), zimag);

if (zimagf < 0)
printf("The square root of (%.4f + %.4fi) is (%.20f - %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsqrtf), fabsf(zimagf));
else
printf("The square root of (%.4f + %.4fi) is (%.20f + %.20fi)\n", crealf(zf), cimagf(zf), crealf(zcsqrtf), zimagf);

if (zimagl < 0)
printf("The square root of (%.4Lf + %.4Lfi) is (%.20Lf - %.20Lfi)", creall(zL), cimagl(zL), creall(zcsqrtl), fabsl(zimagl));
else
printf("The square root of (%.4Lf + %.4Lfi) is (%.20Lf + %.20Lfi)", creall(zL), cimagl(zL), creall(zcsqrtl), zimagl);
return 0;
}

22.3 运行结果

参考

  1. 【MATH-标准C库】

C语言函数大全--b开头的函数

总览

函数声明 函数功能
void bar(int left, int top, int right, int bottom); 画一个二维条形图
void bar3d(int left, int top, int right, int bottom, int depth, int topflag); 画一个三维条形图
int bdos(int dosfun, unsigned dosdx, unsigned dosal); DOS系统调用
int bdosptr(int dosfun, void *argument, unsigned dosal); DOS系统调用
int bioscom(int cmd, char abyte, int port); 串行I/O通信
int biosdisk(int cmd, int drive, int head, int track, int sector, int nsects, void *buffer); 软硬盘I/O
int bioskey(int cmd); 直接使用BIOS服务的键盘接口
int biosmemory(void); 返回存储块大小,以K为单位
int biosprint(int cmd, int byte, int port); 直接使用BIOS服务的打印机I/O
long biostime(int cmd, long newtime); 读取或设置BIOS时间
int brk(void *endds); 用来改变分配给调用程序的数据段的空间数量
void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *)); 二分法搜索

1. bar

1.1 函数说明

函数声明 函数功能
void bar(int left, int top, int right, int bottom); 画一个二维条形图

关注点: 绘制二维条形图需要左上角和右下角的坐标。 left 指定左上角的 X 坐标,top 指定左上角的 Y 坐标,right 指定右下角的 X 坐标,bottom 指定右下角的 Y 坐标。 当前填充图案和填充颜色用于填充条形图。 要更改填充图案和填充颜色,请使用 setfillstyle

1.2 演示示例

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
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;

/* loop through the fill patterns */
for (i=SOLID_FILL; i<USER_FILL; i++)
{
/* set the fill style */
setfillstyle(i, getmaxcolor());

/* draw the bar */
bar(midx-50, midy-50, midx+50, midy+50);

getch();
}

/* clean up */
closegraph();
return 0;
}

1.3 运行结果

2. bar3d

2.1 函数说明

函数声明 函数功能
void bar3d(int left, int top, int right, int bottom, int depth, int topflag); 画一个三维条形图

关注点: 绘制三维条形图需要条形左上角和右下角的坐标。 left 指定左上角的 X 坐标,top 指定左上角的 Y 坐标,right 指定右下角的 X 坐标,bottom 指定右下角的 Y 坐标,depth 指定条的深度 以像素为单位,topflag 确定是否将 3 维顶部放置在条形图上(如果它不为零,则放置否则不放置)。 当前填充图案和填充颜色用于填充条形图。 要更改填充图案和填充颜色,请使用 setfillstyle

2.2 演示示例

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
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;

/* initialize graphics, local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with error code */
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;

/* loop through the fill patterns */
for (i=EMPTY_FILL; i<USER_FILL; i++)
{
/* set the fill style */
setfillstyle(i, getmaxcolor());

/* draw the 3-d bar */
bar3d(midx-50, midy-50, midx+50, midy+50, 10, 1);

getch();
}

/* clean up */
closegraph();
return 0;
}

2.3 运行结果

3. bdos

3.1 函数说明

函数声明 函数功能
int bdos(int dosfun, unsigned dosdx, unsigned dosal); DOS系统调用

参数介绍:

  • int dosfun: 指定了要调用的DOS功能号。功能号决定了bdos函数要执行的具体DOS操作。例如,功能号0x09用于在屏幕上显示字符串,功能号0x3C用于创建或打开文件。
  • unsigned dosdx : 用于传递额外的参数或数据给DOS功能。具体用途取决于所调用的DOS功能号,例如,在功能号0x09(显示字符串)中,dosdx通常指向包含要显示字符串的内存地址。
  • unsigned dosal : 用于传递额外的参数或数据给DOS功能。与dosdx一样,它的具体用途取决于所调用的DOS功能号。例如,在文件操作中,dosal 可能用于指定文件的访问模式(如只读、写入等)。

3.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <dos.h>

/* Get current drive as 'A', 'B', ... */
char current_drive(void)
{
char curdrive;

/* Get current disk as 0, 1, ... */
curdrive = bdos(0x19, 0, 0);
return('A' + curdrive);
}

int main(void)
{
printf("The current drive is %c:\n", current_drive());
return 0;
}

4. bdosptr

4.1 函数说明

函数声明 函数功能
int bdosptr(int dosfun, void *argument, unsigned dosal); DOS系统调用

参数介绍:

  • int dosfun: 指定了要调用的DOS功能号。
  • void *argument : 一个指向任意类型数据的指针,通常用于传递额外的参数或数据给DOS功能。如果 dosfun 对应的功能是文件读取,那么 argument 可能指向一个包含文件名、文件缓冲区地址、读取字节数等信息的数据结构。
  • unsigned dosal : 用于提供与 argument 相关的一些辅助信息,比如参数的长度或者其他特定于 dosfun 所调用功能的计数信息。

4.2 演示示例

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
#include <string.h>
#include <stdio.h>
#include <dir.h>
#include <dos.h>
#include <errno.h>
#include <stdlib.h>

#define BUFLEN 80

int main(void)
{
char buffer[BUFLEN];
int test;

printf("Enter full pathname of a directory\n");
gets(buffer);

test = bdosptr(0x3B,buffer,0);
if(test)
{
printf("DOS error message: %d\n", errno);
/* See errno.h for error listings */
exit (1);
}

getcwd(buffer, BUFLEN);
printf("The current directory is: %s\n", buffer);

return 0;
}

5. bioscom

5.1 函数说明

函数声明 函数功能
int bioscom(int cmd, char abyte, int port); 串行I/O通信

参数介绍:

  • int cmd: 指定了要执行的串行通信命令。常见的命令包括初始化串行端口、发送数据、接收数据、检查接收缓冲区是否有数据等。
  • char abyte : 用于发送数据时指定要发送的字节。当执行发送数据的命令时,abyte 参数的值将被发送到指定的串行端口。
  • int port : 指定了要使用的串行端口号。端口号通常是一个介于0到3之间的数字,对应于计算机上的COM1到COM4(或其他更高编号的端口,但这取决于计算机的硬件配置)。

5.2 演示示例

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
#include <bios.h>
#include <conio.h>

#define COM1 0
#define DATA_READY 0x100
#define TRUE 1
#define FALSE 0

#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)

int main(void)
{
int in, out, status, DONE = FALSE;

bioscom(0, SETTINGS, COM1);
cprintf("... BIOSCOM [ESC] to exit ...\n");
while (!DONE)
{
status = bioscom(3, 0, COM1);
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)
putch(out);
if (kbhit())
{
if ((in = getch()) == '\x1B')
DONE = TRUE;
bioscom(1, in, COM1);
}
}
return 0;
}

6. biosdisk

6.1 函数说明

函数声明 函数功能
int biosdisk(int cmd, int drive, int head, int track, int sector, int nsects, void *buffer); 软硬盘I/O

参数介绍:

  • int cmd: 要执行的磁盘操作命令。常见的命令包括读取磁盘扇区(通常是0x02)、写入磁盘扇区(通常是0x03)、检查磁盘扇区是否存在(可能是0x08或0x10,具体取决于BIOS版本)等。
  • int drive : 指定了要访问的磁盘驱动器号。在大多数情况下,驱动器号是通过位移来表示的,其中0x00表示第一个硬盘(通常是C:),0x80表示第一个软盘驱动器(通常是A:),以此类推。
  • int head : 指定了要访问的磁头号。在传统的磁盘驱动器中,每个磁道(track)都由一个或多个磁头来读写数据。磁头号的范围通常是从0到某个最大值(取决于磁盘的几何结构)。
  • int track : 指定了要访问的磁道号。磁道号定义了磁盘上的圆周路径,数据就存储在这些路径上。磁道号的范围也是根据磁盘的几何结构来确定的。
  • int sector : 指定了要访问的扇区号。每个磁道都被划分为多个扇区,每个扇区包含固定数量的字节(通常是512字节)。扇区号的范围取决于每个磁道的扇区数。
  • int nsects : 指定了要连续读取或写入的扇区数。该参数允许一次性读取或写入多个扇区,从而提高了磁盘操作的效率。
  • void *buffer : 用于存储读取的数据或提供要写入的数据。缓冲区的大小应该足够容纳指定数量的扇区数据(每个扇区通常是512字节)。在读取操作中,BIOS会将数据从磁盘传输到这个缓冲区中;在写入操作中,BIOS会从这个缓冲区中读取数据并写入到磁盘上。

6.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bios.h>
#include <stdio.h>

int main(void)
{
int result;
char buffer[512];

printf("Testing to see if drive a: is ready\n");
result = biosdisk(4,0,0,0,0,1,buffer);
result &= 0x02;
(result) ? (printf("Drive A: Ready\n")) : (printf("Drive A: Not Ready\n"));

return 0;
}

7. bioskey

7.1 函数说明

函数声明 函数功能
int bioskey(int cmd); 直接使用BIOS服务的键盘接口

参数介绍:

  • int cmd: 指定要执行的键盘操作类型。
    • 0: 返回下一个在键盘键入的值【一个16位的二进制数】。函数将等待直到有键按下为止。当按下一个普通键时,低8位数存放该字符的ASCII码,高8位存放该键的扫描码。对于特殊键(如方向键、F1~F12等),低8位为0,高8位字节存放该键的扫描码。
    • 1: 检测是否有键按下。没有键按下时返回0。有键按下时返回按键码(任何按键码都不为0),但此时并不将检测到的按键码从键盘缓冲队列中清除。
    • 2: 返回Shift、Ctrl、Alt、ScrollLock、NumLock、CapsLock、Insert等控制键的状态。各键状态存放在返回值的低8位字节中。某位的值为1时,表示相应的键已被按过或相应的控制功能已打开;某位的值为0时,表示相应的键没被按过或相应的控制功能未打开。

7.2 演示示例

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
#include <stdio.h>
#include <bios.h>
#include <ctype.h>

#define RIGHT 0x01
#define LEFT 0x02
#define CTRL 0x04
#define ALT 0x08

int main(void)
{
int key, modifiers;

/* function 1 returns 0 until a key is pressed */
while (bioskey(1) == 0);

/* function 0 returns the key that is waiting */
key = bioskey(0);

/* use function 2 to determine if shift keys were used */
modifiers = bioskey(2);
if (modifiers)
{
printf("[");
if (modifiers & RIGHT) printf("RIGHT");
if (modifiers & LEFT) printf("LEFT");
if (modifiers & CTRL) printf("CTRL");
if (modifiers & ALT) printf("ALT");
printf("]");
}
/* print out the character read */
if (isalnum(key & 0xFF))
printf("'%c'\n", key);
else
printf("%#02x\n", key);
return 0;
}

8. biosmemory

8.1 函数说明

函数声明 函数功能
int biosmemory(void); 返回存储块大小,以K为单位

8.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <bios.h>

int main(void)
{
int memory_size;

memory_size = biosmemory(); /* returns value up to 640K */
printf("RAM size = %dK\n",memory_size);
return 0;
}

9. biosprint

9.1 函数说明

函数声明 函数功能
int biosprint(int cmd, int byte, int port); 直接使用BIOS服务的打印机I/O

参数介绍:

  • int cmd: 指定要执行的打印机操作命令。
    • 0 :向打印机输出一个字符。此时,byte 参数的值表示要输出的字符的 ASCII 码或汉字的内码。
    • 1 :初始化打印机。这个命令通常用于设置打印机的初始状态或重置打印机。
    • 2 :读取打印机的状态。函数返回值将表示当前打印机的状态,具体含义由返回值的位组合决定。
  • int byte : 当 cmd 为 0 时,byte 参数的值表示要输出到打印机的字符的 ASCII 码或汉字的内码。当 cmd 为 1 或 2 时,byte 参数的值可能不被使用或具有特定的含义(取决于 BIOS 的实现和打印机的类型),但在大多数情况下,可以将其设置为 0 或一个不影响操作的任意值。
  • int port : 指定打印机并行口的编号。
    • 0 :LPT1(第一个并行打印端口)
    • 1 :LPT2(第二个并行打印端口)
    • 2 :LPT3(第三个并行打印端口)
    • 以此类推,具体取决于计算机的配置和 BIOS 的支持。

返回值:

  • 当 cmd 为 0 或 1 时,函数通常返回一个整数值,表示操作的结果或状态。返回值的具体含义可能因 BIOS 的实现和打印机的类型而异,但通常 0 表示成功,非 0 值表示错误或特定的状态信息。
  • 当 cmd 为 2 时,函数返回一个整数值,该值的低 8 位有效,用于表示当前打印机的状态。例如,0x01 表示设备超时,0x08 表示输入/输出错误,0x40 表示认可,0x80 表示打印机不忙等。

9.2 演示示例

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
#include <stdio.h>
#include <conio.h>
#include <bios.h>

int main(void)
{
#define STATUS 2 /* printer status command */
#define PORTNUM 0 /* port number for LPT1 */

int status, abyte=0;

printf("Please turn off your printer. Press any key to continue\n");
getch();
status = biosprint(STATUS, abyte, PORTNUM);
if (status & 0x01)
printf("Device time out.\n");
if (status & 0x08)
printf("I/O error.\n");

if (status & 0x10)
printf("Selected.\n");
if (status & 0x20)
printf("Out of paper.\n");

if (status & 0x40)
printf("Acknowledge.\n");
if (status & 0x80)
printf("Not busy.\n");

return 0;
}

10. biostime

10.1 函数说明

函数声明 函数功能
long biostime(int cmd, long newtime); 读取或设置BIOS时间

参数介绍:

  • int cmd: 指定要执行的操作类型。
    • 0 :读取计时器的当前值。此时,newtime 参数将被忽略,函数将返回从午夜开始(00:00)到当前时刻的计时器值,该值以时钟滴答声为单位。
    • 1 :设置计时器的新值。此时,newtime 参数的值将被用作新的计时器值。请注意,直接设置计时器值可能会影响系统的正常计时功能,因此应谨慎使用此操作。
  • int newtime : 当 cmd 为 1 时,newtime 参数指定要设置的计时器新值。计时器值以时钟滴答声为单位。在大多数BIOS中,每秒的时钟滴答声频率约为18.2次(但具体值可能因计算机而异)。当 cmd 为 0 时,newtime 参数的值将被忽略。

10.2 演示示例

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
#include <stdio.h>
#include <bios.h>
#include <time.h>
#include <conio.h>

int main(void)
{
long bios_time;

clrscr();
cprintf("The number of clock ticks since midnight is:\r\n");
cprintf("The number of seconds since midnight is:\r\n");
cprintf("The number of minutes since midnight is:\r\n");
cprintf("The number of hours since midnight is:\r\n");
cprintf("\r\nPress any key to quit:");
while(!kbhit())
{
bios_time = biostime(0, 0L);

gotoxy(50, 1);
cprintf("%lu", bios_time);

gotoxy(50, 2);
cprintf("%.4f", bios_time / CLK_TCK);

gotoxy(50, 3);
cprintf("%.4f", bios_time / CLK_TCK / 60);

gotoxy(50, 4);
cprintf("%.4f", bios_time / CLK_TCK / 3600);
}
return 0;
}

11. brk

11.1 函数说明

函数声明 函数功能
int brk(void *endds); 用来改变分配给调用程序的数据段的空间数量

参数介绍:

  • void *endds: 指定新的数据段结束地址。

该函数可以将进程的数据段结束地址移动到该地址处,从而实现动态内存分配。如果传递的地址比当前数据段结束地址高,则数据段会向上扩展(即增加内存空间);如果传递的地址比当前数据段结束地址低,则数据段会向下收缩(即释放部分内存空间,但需要注意不要释放已在使用的内存,否则可能会导致程序崩溃)。

11.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <alloc.h>

int main(void)
{
char *ptr;

printf("Changing allocation with brk()\n");
ptr = malloc(1);
printf("Before brk() call: %lu bytes free\n", coreleft());
brk(ptr+1000);
printf(" After brk() call: %lu bytes free\n", coreleft());
return 0;
}

12. bsearch

12.1 函数说明

函数声明 函数功能
void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *)); 二分法搜索

参数介绍:

  • const void *key: 指向要搜索的元素的指针。key 指向的元素必须与 base 指向的数组中的元素具有相同的类型和大小。
  • const void *base : 指向要搜索的数组的起始地址。数组中的元素必须已经按照某种顺序(通常是升序)排列好。
  • size_t *nelem : 数组中元素的数量。
  • size_t width: 数组中每个元素的大小(以字节为单位)。
  • int(*fcmp)(const void *, const *) : 指向比较函数的指针。比较函数的返回值如下:
    • 小于0 :表示第一个元素小于第二个元素。
    • 等于0 :表示两个元素相等。
    • 大于0 :表示第一个元素大于第二个元素。

12.2 演示示例

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
#include <stdlib.h>
#include <stdio.h>

#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))

int numarray[] = {1, 2, 3, 5, 6, 8, 9, 10, 12, 14};

int numeric (const int *p1, const int *p2)
{
return(*p1 - *p2);
}

int lookup(int key)
{
int *itemptr;

/* The cast of (int(*)(const void *,const void*)) is needed to avoid a type mismatch error at compile time */
itemptr = (int(*))bsearch(&key, numarray, NELEMS(numarray), sizeof(int), (int(*)(const void *,const void *))numeric);
return (itemptr != NULL);
}

int main(void)
{
int a;
printf("Please input key: ");
scanf("%d", &a);
if (lookup(a))
printf("%d is in the table.\n", a);
else
printf("%d isn't in the table.\n", a);
return 0;
}

12.3 运行结果


参考

  1. [API Reference Document]
  2. [c语言中的 bar 函数]
  3. [c语言中的 bar3d 函数]

C语言函数大全--a开头的函数

总览

函数声明 函数功能
void abort(void); 异常终止一个进程
int abs(int i); 求整数的绝对值
int absread(int drive, int nsects, int sectno, void *buffer); 从drive指定的驱动器磁盘上,sectno指定的逻辑扇区号开始读取nsects个(最多64K个)扇区的内容,储存于buffer所指的缓冲区中。
int abswrite(int drive, int nsects, int sectno, void *buffer); 将指定内容写入磁盘上的指定扇区
int access(const char *filename, int amode); 确定文件的访问权限
double acos(double x); 反余弦函数
int allocmem(unsigned size, unsigned *seg); 分配DOS存储段
void arc(int x, int y, int stangle, int endangle, int radius); 画一弧线
char *asctime(const struct tm *tblock); 转换日期和时间为ASCII码
double asin(double x); 反正弦函数
void assert(int test); 测试一个条件并可能使程序终止
double atan(double x); 反正切函数
double atan2(double y, double x); 计算Y/X的反正切值
int atexit(atexit_t func); 注册终止函数
double atof(const char *nptr); 把字符串转换成浮点数
int atoi(const char *nptr); 把字符串转换成整型数
long atol(const char *nptr); 把字符串转换成长整型数

1. abort

1.1 函数说明

函数声明 函数功能
void abort(void); 异常终止一个进程

注意: abort() 函数用于终止当前程序的执行。当程序调用 abort() 函数时,它会立即退出,并生成一个错误信号,通知操作系统程序非正常终止。如果程序已经打开了一些文件或句柄,但尚未关闭它们,则这些资源可能无法被正确释放。

1.2 演示示例

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("Calling abort()\n");
abort();
printf("already abort()"); // 这里永远也到不了
return 0;
}

1.3 运行结果

2. abs

2.1 函数说明

函数声明 函数功能
int abs(int i); 求整数的绝对值

2.2 演示示例

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <math.h>

int main(void)
{
int number = -666;
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}

2.3 运行结果

3. absread

3.1 函数说明

函数声明 函数功能
int absread(int drive, int nsects, int sectno, void *buffer); 从drive指定的驱动器磁盘上,sectno指定的逻辑扇区号开始读取nsects个(最多64K个)扇区的内容,储存于buffer所指的缓冲区中。

3.2 演示示例

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
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dos.h>

int main(void)
{
int i, strt, ch_out, sector;
char buf[512];

printf("Insert a diskette into drive A and press any key\n");
getch();
sector = 0;
if (absread(0, 1, sector, &buf) != 0)
{
perror("Disk problem");
exit(1);
}
printf("Read OK\n");
strt = 3;
for (i=0; i<80; i++)
{
ch_out = buf[strt+i];
putchar(ch_out);
}
printf("\n");
return(0);
}

上述的代码实现了从 A 驱动器读取一个扇区的数据,并将其中一些字符输出到屏幕上。

  • 首先提示用户插入一个软盘到 A 驱动器中。
  • 然后读取 A 驱动器上第 0 个扇区的数据到缓冲区 buf 中。
  • 接着检查读取是否成功。如果不成功,输出错误信息并退出程序。
  • 最后将 buf 缓冲区中偏移量为 3 到偏移量为 82 的字符依次输出到屏幕上。

注意: 程序中使用了一些 DOS 特定的函数,比如 absread()getch(),可能不适用于其他操作系统或编译器环境。

4. abswrite

4.1 函数说明

函数声明 函数功能
int abswrite(int drive, int nsects, int sectno, void *buffer); 将指定内容写入磁盘上的指定扇区

4.2 演示示例

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
#include <dos.h>
#include <stdio.h>

unsigned char buff[512];

int main()
{
int i;
char c;
printf("\nQuick Format 1.44MB\n");
printf("Program by ChenQingyang.\n");
printf("ALL DATA IN THE FLOPPY DISK WILL BE LOST!!\n");
printf("\nInsert a diskette for drive A:\n");
printf("and press ENTER when ready. . .");
c=getchar();
printf("\n\nCleaning FAT area. . .");
buff[0]=0xf0;
buff[1]=buff[2]=0xff;
for (i=3;i<512;i++)
buff[i]=0;
abswrite(0,1,1,buff);
abswrite(0,1,10,buff);
for (i=0;i<512;i++)
buff[i]=0;
for (i=2;i<10;i++)
abswrite (0,1,i,buff);
for (i=11;i<19;i++)
abswrite (0,1,i,buff);
printf("\nCleaning ROOT area. . .");
for (i=19;i<33;i++)
abswrite (0,1,i,buff);
printf("\n\nQuickFormat Completed!\n");
}

上述代码是一个使用 DOS 命令格式化软盘的程序。它会提示用户输入软盘,然后清空软盘的FAT和根目录区域,并在完成后打印 “QuickFormat Completed!” 的信息。程序使用了 <dos.h><stdio.h> 头文件,其中包含了一些 DOS 和标准输入输出函数。

5. access

5.1 函数说明

函数声明 函数功能
int access(const char *filename, int amode); 确定文件的访问权限

5.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <io.h>

int file_exists(char *filename);

int main(void)
{
printf("Does students1.txt exist: %s\n",
file_exists("students1.txt") ? "YES" : "NO");
return 0;
}

int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}

5.3 运行结果


6. acos

6.1 函数说明

函数声明 函数功能
double acos(double x); 反余弦函数

6.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <math.h>

int main(void)
{
double result;
double x = 0.5;

result = acos(x);
printf("The arc cosine of %lf is %lf\n", x, result);
return 0;
}

6.3 运行结果

7. allocmem

7.1 函数说明

函数声明 函数功能
int allocmem(unsigned size, unsigned *seg); 分配DOS存储段

7.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <dos.h>
#include <alloc.h>
#include <stdio.h>

int main(void)
{
unsigned int size, segp;
int stat;
size = 64; /* (64 x 16) = 1024 bytes */
stat = allocmem(size, &segp);
if (stat == -1)
printf("Allocated memory at segment: %x\n", segp);
else
printf("Failed: maximum number of paragraphs available is %u\n",stat);
return 0;
}

在上述的示例代码,

  • 首先调用了 allocmem() 函数来分配内存,其中传递了两个参数:size 表示请求的内存大小(以段为单位),这里设置为 64 段;&segp 表示返回的内存段地址将存储在此变量中。
  • 如果成功分配内存,allocmem()函数将返回 -1,并打印出已分配内存的段地址;
  • 否则,它将返回最大可用段数,并打印出失败的消息。

8. arc

8.1 函数说明

函数声明 函数功能
void arc(int x, int y, int stangle, int endangle, int radius); 画一弧线

8.2 演示示例

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
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;

/* initialize graphics and local variables */
char ch[] = "";
initgraph(&gdriver, &gmode, ch);

/* read result of initialization */
errorcode = graphresult(); /* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());

/* draw arc */
arc(midx, midy, stangle, endangle, radius);

/* clean up */
getch();
closegraph();
return 0;
}

8.3 运行结果

9. asctime

9.1 函数说明

函数声明 函数功能
char *asctime(const struct tm *tblock); 转换日期和时间为ASCII码

9.2 演示示例

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
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void)
{
struct tm t;
char str[80];

/* sample loading of tm structure */

t.tm_sec = 1; /* Seconds */
t.tm_min = 30; /* Minutes */
t.tm_hour = 9; /* Hour */
t.tm_mday = 22; /* Day of the Month */
t.tm_mon = 11; /* Month */
t.tm_year = 56; /* Year - does not include century */
t.tm_wday = 4; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */

/* converts structure to null terminated
string */

strcpy(str, asctime(&t));
printf("%s\n", str);

return 0;
}

9.3 运行结果

10. asin

10.1 函数说明

函数声明 函数功能
double asin(double x); 反正弦函数

10.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <math.h>

int main(void)
{
double result;
double x = 0.5;

result = asin(x);
printf("The arc sin of %lf is %lf\n", x, result);
return(0);
}

10.3 运行结果

11. assert

11.1 函数说明

函数声明 函数功能
void assert(int test); 测试一个条件并可能使程序终止

11.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct ITEM {
int key;
int value;
};

/* add item to list, make sure list is not null */
void additem(struct ITEM *itemptr) {
assert(itemptr != NULL);
/* add item to list */
}

int main(void)
{
additem(NULL);
return 0;
}

11.3 运行结果

12. atan

12.1 函数说明

函数声明 函数功能
double atan(double x); 反正切函数

12.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <math.h>

int main(void)
{
double result;
double x = 0.5;

result = atan(x);
printf("The arc tangent of %lf is %lf\n", x, result);
return(0);
}

12.3 运行结果

13. atan2

13.1 函数说明

函数声明 函数功能
double atan2(double y, double x); 计算Y/X的反正切值

13.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <math.h>

int main(void)
{
double result;
double x = 60.0, y = 30.0;

result = atan2(y, x);
printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);
return 0;
}

13.3 运行结果

14. atexit

14.1 函数说明

函数声明 函数功能
int atexit(atexit_t func); 注册终止函数

14.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>

void exit_fn1(void)
{
printf("Exit function #1 called\n");
}

void exit_fn2(void)
{
printf("Exit function #2 called\n");
}

int main(void)
{
/* post exit function #1 */
atexit(exit_fn1);
/* post exit function #2 */
atexit(exit_fn2);
return 0;
}

14.3 运行结果

15. atof

15.1 函数说明

函数声明 函数功能
double atof(const char *nptr); 把字符串转换成浮点数

15.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
float f;
char *str = "1234.5678";

f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}

15.3 运行结果

16. atoi

16.1 函数说明

函数声明 函数功能
int atoi(const char *nptr); 把字符串转换成整型数

16.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int n;
char *str = "1234.5678";

n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}

16.3 运行结果

17. atol

17.1 函数说明

函数声明 函数功能
long atol(const char *nptr); 把字符串转换成长整型数

17.2 演示示例

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
long l;
char *lstr = "87654321";

l = atol(lstr);
printf("string = %s integer = %ld\n", lstr, l);
return(0);
}

17.3 运行结果

参考

  1. [API Reference Document]

Java并发编程学习15-深入探索任务关闭机制(非正常线程终止与JVM关闭详解)

引言

在软件开发中,任务关闭是一个至关重要的环节,它关乎到应用程序的稳定性和资源的有效管理。在之前的篇章中,我们已经初步探讨了任务关闭的一些基本概念和原则。然而,任务关闭的复杂性在于,它不仅仅涉及到正常情况下的资源释放,还需要处理各种异常情况,如非正常的线程终止以及JVM的关闭。这些异常情况如果处理不当,可能会导致资源泄露、数据丢失甚至系统崩溃。因此,本文将深入探索非正常线程终止的处理机制以及JVM关闭时的注意事项,帮助开发者更好地掌握任务关闭的精髓,确保应用程序能够优雅地处理各种关闭场景。

Read More

Java并发编程学习14-探索基于线程的服务如何优雅关闭

引言

我们知道,应用程序通常会创建拥有多个线程的服务,例如线程池,并且这些服务的生命周期通常比创建它们的方法的生命周期更长。如果应用程序准备退出,那么这些服务所拥有的线程也需要结束。由于无法通过抢占式的方法来停止线程,因此它们需要自行结束。

线程应该有一个相应的所有者,即创建该线程的类。而线程池就是其工作者线程的所有者,如果要停止这些线程,那么应当通过线程池来操作。

应用程序可以拥有服务,服务也可以拥有工作者线程,但应用程序并不能拥有工作者线程,因此应用程序不能直接停止工作者线程。

Read More

Java并发编程学习11-任务执行演示

引言

上一篇博文带大家了解了任务执行和 Executor 框架的基础知识,本篇将结合这些内容,演示一些不同版本的任务执行Demo,并且每个版本都实现了不同程度的并发性。

以下的示例是要实现浏览器程序中的页面渲染功能:将 HTML 页面绘制到图像缓存中【为了简便,假设 HTML 页面只包含标签文本、预定义大小的图片和URL】。

Read More