C 语言中的数学函数
C 语言中的数学函数
C 语言提供了各种函数来对数字执行数学运算,例如计算三角比、计算对数和指数、对数字进行四舍五入等等。要在 C 语言程序中使用这些数学函数,您需要包含 math.h 头文件。
我们将数学函数分为以下几类:
三角函数
math.h 库定义了函数 sin()、cos() 和 tan() 分别返回角度的三角比、正弦、余弦和正切。
这些函数返回给定双精度类型对应的比率,该类型表示以弧度表示的角度。所有函数都返回双精度值。
double sin(double x) double cos(double x) double tan(double x)
对于以上所有函数,参数"x"均为以弧度表示的角度。
示例
以下示例展示了如何在 C 语言中使用三角函数 -
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double x, sn, cs, tn, val; x = 45.0; val = PI / 180; sn = sin(x*val); cs = cos(x*val); tn = tan(x*val); printf("sin(%f) : %f ", x, sn); printf("cos(%f) : %f ", x, cs); printf("tan(%f) : %f ", x, tn); return(0); }
输出
运行此代码时,将产生以下输出 -
sin(45.000000) : 0.707107 cos(45.000000) : 0.707107 tan(45.000000) : 1.000000
反三角函数
math.h 库还包含反三角函数,也称为弧函数或反三角函数。它们是基本三角函数的反函数。例如,asin(x) 等价于 $\mathrm{sin^{-1}(x)}$。其他反函数包括 acos()、atan() 和 atan2()。
以下 asin() 函数 返回 x 在 [-pi/2, +pi/2] 弧度区间内的反正弦值 -
double asin(double x)
以下 acos() 函数 返回 x 在 [0, pi] 弧度区间内的反余弦值 -
double acos(double x)
以下 atan() 函数 返回区间 [-pi/2, +pi/2] 弧度内 x 的主反正切值。
double atan(double x)
示例 1
以下示例演示了如何在 C 程序中使用反三角函数 -
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double x, asn, acs, atn, val; x = 0.9; val = 180/PI; asn = asin(x); acs = acos(x); atn = atan(x); printf("asin(%f) : %f in radians ", x, asn); printf("acos(%f) : %f in radians ", x, acs); printf("atan(%f) : %f in radians ", x, atn); asn = (asn * 180) / PI; acs = (acs * 180) / PI; atn = (atn * 180) / PI; printf("asin(%f) : %f in degrees ", x, asn); printf("acos(%f) : %f in degrees ", x, acs); printf("atan(%f) : %f in degrees ", x, atn); return(0); }
输出
运行此代码时,将产生以下输出 -
asin(0.900000) : 1.119770 in radians acos(0.900000) : 0.451027 in radians atan(0.900000) : 0.732815 in radians asin(0.900000) : 64.158067 in degrees acos(0.900000) : 25.841933 in degrees atan(0.900000) : 41.987213 in degrees
atan2() 函数根据两个值的符号,返回"y/x"的反正切值(以弧度为单位),以确定正确的象限。
double atan2(double y, double x)
此函数返回 [-pi, +pi] 弧度区间内"y / x"的主反正切值。
示例 2
请看以下示例 -
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double x, y, ret, val; x = -7.0; y = 7.0; val = 180.0 / PI; ret = atan2 (y,x) * val; printf("The arc tangent of x = %lf, y = %lf ", x, y); printf("is %lf degrees ", ret); return(0); }
输出
运行代码并检查其输出 −
The arc tangent of x = -7.000000, y = 7.000000 is 135.000000 degrees
双曲函数
在数学中,双曲函数类似于三角函数,但它是用双曲线而不是圆来定义的。 math.h 头文件提供了 sinh()、cosh() 和 tanh() 函数。
double sinh(double x)
此函数返回 x 的双曲正弦值。
double cosh(double x)
此函数返回 x 的双曲余弦值。
double tanh(double x)
此函数返回 x 的双曲正切值。
示例
以下示例展示了如何在 C 程序中使用双曲函数 -
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double x,val, sh, ch, th; x = 45; val = PI/180.0 ; sh = sinh(x*val); ch = cosh(x*val); th = tanh(x*val); printf("The sinh(%f) = %lf ", x, sh); printf("The cosh(%f) = %lf ", x, ch); printf("The tanh(%f) = %lf ", x, th); return(0); }
输出
运行代码并检查其输出 −
The sinh(45.000000) = 0.868671 The cosh(45.000000) = 1.324609 The tanh(45.000000) = 0.655794
指数和对数函数
"math.h"库包含以下与指数和对数相关的函数 -
exp() 函数:返回 e 的 x 次方值。(e 的值 - 欧拉数约为 2.718)
double exp(double x)
log() 函数:返回 x 的自然对数(以 e 为底的对数)。
double log(double x)
请注意,对数函数等价于指数函数的逆。
log10() 函数:返回 x 的常用对数(以 10 为底的对数)。
double log10(double x)
示例
以下示例展示了如何在 C 程序中使用指数和对数函数 -
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main () { double x = 2; double e, ln, ls; e = exp(2); ln = log(e); printf("exp(%f): %f log(%f): %f ",x, e, e, ln); ln = log(x); printf("log(%f): %f ",x,ln); ls = log10(x); printf("log10(%f): %f ",x,ls); return(0); }
输出
运行此代码时,将产生以下输出 -
exp(2.000000): 7.389056 log(7.389056): 2.000000 log(2.000000): 0.693147 log10(2.000000): 0.301030
浮点函数
frexp() 函数
"math.h"头文件还包含 frexp() 函数。它将浮点数分解为有效数字和指数。
double frexp(double x, int *exponent)
其中,"x"表示待计算的浮点值,"exponent"表示指向一个 int 对象的指针,该对象用于存储指数的值。
此函数返回标准化的分数。
示例
请看以下示例 -
#include <stdio.h> #include <math.h> int main () { double x = 1024, fraction; int e; fraction = frexp(x, &e); printf("x = %.2lf = %.2lf * 2^%d ", x, fraction, e); return(0); }
Output
运行代码并检查其输出 −
x = 1024.00 = 0.50 * 2^11
ldexp() 函数
ldexp() 函数将有效数字和指数组合成一个浮点数。其语法如下:-
double ldexp(double x, int exponent)
其中,"x"是表示有效数字的浮点值,"exponent"是指数的值。此函数返回 (x * 2 exp)
示例
以下示例展示了如何在 C 程序中使用 ldexp() 函数:-
#include <stdio.h> #include <math.h> int main () { double x, ret; int n; x = 0.65; n = 3; ret = ldexp(x ,n); printf("%f * 2 %d = %f ", x, n, ret); return(0); }
Output
运行代码并检查其输出 −
0.650000 * 2^3 = 5.200000
幂函数和平方根函数
pow() 和 sqrt() 函数用于计算给定数字的幂和平方根。
pow() 函数
此函数返回 x 的 y 次幂,即 xy。
double pow(double x, double y)
sqrt() 函数
返回 x 的平方根。
double sqrt(double x)
sqrt(x) 函数返回的值与 pow(x, 0.5) 相同
示例
以下示例展示了如何在 C 程序中使用 pow() 和 sqrt() 函数 -
#include <stdio.h> #include <math.h> int main() { double x = 9, y=2; printf("Square root of %lf is %lf ", x, sqrt(x)); printf("Square root of %lf is %lf ", x, pow(x, 0.5) ); printf("%lf raised to power %lf ", x, pow(x, y)); return(0); }
输出
运行此代码时,将产生以下输出 -
Square root of 9.000000 is 3.000000 Square root of 9.000000 is 3.000000 9.000000 raised to power 81.000000
舍入函数
math.h 库包含 ceil()、floor() 和 round() 函数,用于对给定的浮点数进行四舍五入。
ceil() 函数
此函数返回大于或等于 x 的最小整数值。
double ceil(double x)
此函数返回不小于 x 的最小整数值。
floor() 函数
此函数返回小于或等于x。
double floor(double x)
参数 x:浮点值。此函数返回不大于 x 的最大整数值。
round() 函数
此函数用于将作为参数传递给它的 double、float 或 long double 值四舍五入为最接近的整数值。
double round( double x )
返回的值是最接近的浮点整数。
示例
以下示例演示了如何在 C 程序中使用舍入函数 -
#include <stdio.h> #include <math.h> int main() { float val1, val2, val3, val4; val1 = 1.2; val2 = 1.6; val3 = 2.8; val4 = -2.3; printf ("ceil(%lf) = %.1lf ", val1, ceil(val1)); printf ("floor(%lf) = %.1lf ", val2, floor(val2)); printf ("ceil(%lf) = %.1lf ", val3, ceil(val3)); printf ("floor(%lf) = %.1lf ", val4, floor(val4)); printf("round(%lf) = %.1lf ", val1, round(val1)); printf("round(%lf) = %.1lf", val4, round(val4)); return(0); }
输出
运行此代码时,将产生以下输出 -
ceil(1.200000) = 2.0 floor(1.600000) = 1.0 ceil(2.800000) = 3.0 floor(-2.300000) = -3.0 round(1.200000) = 1.0 round(-2.300000) = -2.0
模函数
"math.h"库包含以下此类别的函数:
modf() 函数
modf() 函数返回小数部分(小数点后的部分),并将整数部分设置为整数部分。
double modf(double x, double *integer)
其中,"x"是浮点值,"integer"是指向存储整数部分的对象指针。
此函数返回"x"的小数部分,其值与符号。
示例
请看以下示例 -
#include <stdio.h> #include <math.h> int main () { double x, fractpart, intpart; x = 8.123456; fractpart = modf(x, &intpart); printf("Integral part = %lf ", intpart); printf("Fraction Part = %lf ", fractpart); return(0); }
输出
运行此代码时,将产生以下输出 -
Integral part = 8.000000 Fraction Part = 0.123456
fmod() 函数
fmod() 函数返回 x 除以 y 的余数。
double fmod(double x, double y)
这里,x 是分子,y 是分母。该函数返回 x / y 的余数。
示例
请看以下示例 -
#include <stdio.h> #include <math.h> int main () { float a, b; int c; a = 9.2; b = 3.7; c = 2; printf("Remainder of %f / %d is %lf ", a, c, fmod(a,c)); printf("Remainder of %f / %f is %lf ", a, b, fmod(a,b)); return(0); }
输出
运行此代码时,将产生以下输出 -
Remainder of 9.200000 / 2 is 1.200000 Remainder of 9.200000 / 3.700000 is 1.800000
请注意,模数运算符 (%) 仅适用于整数操作数。