C Standard Library 简明教程
C Library - <tgmath.h>
C 库 <tgmath.h> 包含头文件 <math.h> 和 <complex.h>,并定义了各种类型的宏。此函数适用于根据参数(类型)调用的实函数或复函数。
The C library <tgmath.h> contains the headers <math.h> and <complex.h> and defines various types of macros. This function is applicable when real or complex function called on the basis of parameters(types).
Syntax
以下是 C 库头文件 <tgmath.h> 提供的语法,它提供多种内置函数−
Following is the syntax of the C library header <tgmath.h> provides various built-in functions−
pow(var1, var2)
Or,
fmod(var1, var2)
Or,
sin(var1, var2) etc.
Parameter
上述语法使用各种函数,这些函数只接受两个参数来表示数学表达式。例如,计算两个数字的幂、获取三角函数值等。var1 和 var2 属于相同或不同的数据类型。
The above syntaxes uses various fuction which accepts only two parameters to express the mathematical expression. For example- finding the power of two numbers, getting trigonometric value, etc. The var1 and var2 belongs to same or different data types.
Return Type
该函数根据传递给宏的参数类型进行返回。
The function returns based on the type of parameters passed to the macros.
确保 <tgmath.h> 简化代码维护,并借助类型通用的宏使用数学运算。
Ensure that, <tgmath.h> simplify the code maintenance and uses mathematical operation with the help of type-generic macros.
Example 1
C 库头文件 <tgmath.h> 说明了函数 fmod(),该函数接受两个不同数据类型的参数,并计算两个数字相除的余数。
The C library header <tgmath.h> illustrate the function fmod() which accepts two parameters of different datatypes and calculates the remainders of the division of two numbers.
#include <tgmath.h>
#include <stdio.h>
int main() {
float f = 3.0f;
double d = 3.0;
// result is of type double
double result = fmod(f, d);
printf("The result of fmod(%f, %f) = %f\n", f, d, result);
return 0;
}
以上代码生成以下结果 −
The above code produces the following result −
The result of fmod(3.000000, 3.000000) = 0.000000
Example 2
以下示例说明了 cabs() 函数的用法,该函数确定复数的绝对值并显示结果。
Following example illustrates the usage of cabs() function that determine the abosolute value of complex number and display the result.
#include <tgmath.h>
#include <complex.h>
#include <stdio.h>
int main() {
float complex fc = 3.0f + 4.0f * I;
double complex dc = 3.0 + 4.0 * I;
long double complex ldc = 3.0l + 4.0l * I;
// res_fc is of type float
float res_fc = cabs(fc);
// res_dc is of type double
double res_dc = cabs(dc);
// res_ldc is of type long double
long double res_ldc = cabs(ldc);
printf("cabs(%f + %fi) = %f\n", crealf(fc), cimagf(fc), res_fc);
printf("cabs(%f + %fi) = %f\n", creal(dc), cimag(dc), res_dc);
printf("cabs(%Lf + %Lfi) = %Lf\n", creall(ldc), cimagl(ldc), res_ldc);
return 0;
}
执行以上代码,将获得以下结果 −
On execution of above code, we get the following result −
cabs(3.000000 + 4.000000i) = 5.000000
cabs(3.000000 + 4.000000i) = 5.000000
cabs(3.000000 + 4.000000i) = 5.000000
Example 3
这里,我们使用三角函数来表示数据类型的值−浮点、双精度和长双精度。
Here, we use the trigonometric function to represent the value of data types − float, double, and long double.
#include <tgmath.h>
#include <stdio.h>
int main() {
float f = 3.0f;
double d = 3.0;
long double ld = 3.0l;
// result_f is of type float
double result_f = sin(f);
// result_d is of type double
double result_d = sin(d);
// result_ld is of type long double
double result_ld = sin(ld);
printf("The value of float [sin(%f)] = %f\n", f, result_f);
printf("The value of double [sin(%f)] = %f\n", d, result_d);
printf("The value of long [double sin(%Lf)] = %f\n", ld, result_ld);
return 0;
}
执行上述代码后,我们将获得以下结果:
After executing the above code, we get the following result −
The value of float [sin(3.000000)] = 0.141120
The value of double [sin(3.000000)] = 0.141120
The value of long [double sin(3.000000)] = 0.141120