Cprogramming 简明教程

Math Functions in C

C Math Functions

C 语言提供各种函数来执行数字上的数学操作,比如求解三角比例、计算对数和指数、四舍五入数字,等等。为了在 C 程序中使用这些函数,你需要包含 math functions

C language provides various functions to perform mathematical operations on numbers such as finding trigonometric ratios, calculating log and exponentials, rounding the numbers, etc.. To use these math functions in a C program, you need to include math.h header file.

我们将数学函数归类为以下几类 −

We have categorized math functions into the following categories −

Trigonometric Functions

math.h 库定义了函数 sin()cos() 和 tan() — 它们返回一个角度的相应三角比例:正弦、余弦和正切。

The math.h library defines the function sin(), cos(), and tan() – that return the respective trigonometric ratios, sine, cosine and tangent of an angle.

这些函数返回一个双精度类型表示的角度(以弧度表示)的相应比例。所有函数返回一个双精度值。

These functions return the respective ratio for a given double type representing the angle expressed in radians. All the functions return a double value.

double sin(double x)
double cos(double x)
double tan(double x)

对于以上所有函数,“x”参数都是弧度角。

For all the above functions, the argument "x" is the angle in radians.

Example

以下示例显示了如何在 C 中使用三角函数 −

The following example shows how you can use trigonometric functions in 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\n", x, sn);
   printf("cos(%f) : %f\n", x, cs);
   printf("tan(%f) : %f\n", x, tn);

   return(0);
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

sin(45.000000) : 0.707107
cos(45.000000) : 0.707107
tan(45.000000) : 1.000000

Inverse Trigonometric Functions

math.h 库还包括反三角函数,也称为圆弧函数或反三角函数。它们是基本三角函数的反函数。例如, asin(x) 等于 $\mathrm{sin^{-1}(x)}$ 。其他反函数是 acos(), atan()atan2()

The math.h library also includes inverse trigonometric functions, also known as arcus functions or anti-trigonometric functions. They are the inverse functions of basic trigonometric functions. For example, asin(x) is equivalent to $\mathrm{sin^{-1}(x)}$. The other inverse functions are acos(), atan() and atan2().

以下 asin() function 返回“x”的反正弦,其间距在[-pi/2, +pi/2]弧度 −

The following asin() function returns the arc sine of "x" in the interval [-pi/2, +pi/2] radians −

double asin(double x)

以下 acos() function 返回“x”的主反正余弦,其间距在[0, pi]弧度 −

The following acos() function returns principal arc cosine of "x" in the interval [0, pi] radians −

double acos(double x)

以下 atan() function 返回“x”的主反正切,其间距在[-pi/2, +pi/2]弧度。

The following atan() function returns the principal arc tangent of "x" in the interval [-pi/2, +pi/2] radians.

double atan(double x)

Example 1

以下示例演示了如何在 C 程序中使用反三角函数 −

The following example demonstrates how you can use inverse trigonometric functions in a C program −

#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\n", x, asn);
   printf("acos(%f) : %f in radians\n", x, acs);
   printf("atan(%f) : %f in radians\n", x, atn);

   asn = (asn * 180) / PI;
   acs = (acs * 180) / PI;
   atn = (atn * 180) / PI;

   printf("asin(%f) : %f in degrees\n", x, asn);
   printf("acos(%f) : %f in degrees\n", x, acs);
   printf("atan(%f) : %f in degrees\n", x, atn);

   return(0);
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

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() function 根据两个值的符号来返回“y/x”的弧度反正切,以确定正确的象限。

The atan2() function returns the arc tangent in radians of "y/x" based on the signs of both values to determine the correct quadrant.

double atan2(double y, double x)

此函数返回“y / x”的主反正切,其间距在[-pi, +pi]弧度。

This function returns the principal arc tangent of "y / x" in the interval [-pi, +pi] radians.

Example 2

请看以下示例:

Take a look at the following example −

#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\n", ret);

   return(0);
}

运行代码并检查其输出:

Run the code and check its output −

The arc tangent of x = -7.000000, y = 7.000000 is 135.000000 degrees

Hyperbolic Functions

在数学中,双曲函数与三角函数类似,但定义时使用双曲线而不是圆。math.h 头文件提供 sinh()cosh()tanh() 函数。

In Mathematics, hyperbolic functions are similar to trigonometric functions but are defined using the hyperbola rather than the circle. The math.h header file provides sinh(), cosh(), and tanh() functions.

double sinh(double x)

此函数返回 x 的双曲正弦值。

This function returns hyperbolic sine of x.

double cosh(double x)

此函数返回 x 的双曲余弦值。

This function returns hyperbolic cosine of x.

double tanh(double x)

此函数返回 x 的双曲正切值。

This function returns hyperbolic tangent of x.

Example

以下示例展示了如何在 C 程序中使用双曲函数 −

The following example shows how you can use hyperbolic functions in a C program −

#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\n", x, sh);
   printf("The cosh(%f) = %lf\n", x, ch);
   printf("The tanh(%f) = %lf\n", x, th);

   return(0);
}

运行代码并检查其输出:

Run the code and check its output −

The sinh(45.000000) = 0.868671
The cosh(45.000000) = 1.324609
The tanh(45.000000) = 0.655794

Exponentiation and Logarithmic Functions

“math.h”库包含与指数和对数相关的以下函数 −

The "math.h" library includes the following functions related to exponentiation and logarithms −

exp() Function :它返回 x 次方的 e 值。(e 的值 - 欧拉数 - 约为 2.718)

exp() Function: It returns the value of e raised to the xth power. (Value of e - Euler’s number – is 2.718 approx)

double exp(double x)

log() Function :它返回“x”的自然对数(以 e 为底)。

log() Function: It returns the natural logarithm (base-e logarithm) of "x".

double log(double x)

请注意,对数函数等同于指数函数的逆函数。

Note that Logarithmic functions are equivalent to the exponential function’s inverse.

log10() Function :它返回“x”的常用对数(以 10 为底)。

log10() Function: It returns the common logarithm (base-10 logarithm) of "x".

double log10(double x)

Example

以下示例展示了如何在 C 程序中使用指数和对数函数 −

The following example shows how you can use exponentiation and logarithmic functions in a C program −

#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\n",x, e, e, ln);

   ln = log(x);
   printf("log(%f): %f\n",x,ln);
   ls = log10(x);
   printf("log10(%f): %f\n",x,ls);

   return(0);
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

exp(2.000000): 7.389056 log(7.389056): 2.000000
log(2.000000): 0.693147
log10(2.000000): 0.301030

Floating-Point Functions

frexp()ldexp() 函数用于浮点操作。

The frexp() and ldexp() functions are used for floating-point manipulation.

frexp() Function

“math.h”头文件还包含 frexp() 函数。它可将一个浮点数字分解为其有效数字和指数。

The "math.h" header file also includes the frexp() function. It breaks a floating-point number into its significand and exponent.

double frexp(double x, int *exponent)

在此处,“ x ”是要计算的浮点值,而“ exponent ”是要存储指数值的一个 int 对象的指针。

Here, "x" is the floating point value to be computed and "exponent" is the pointer to an int object where the value of the exponent is to be stored.

此函数返回归一化的分数。

This function returns the normalized fraction.

请看以下示例:

Take a look at the following example −

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

int main () {

   double x = 1024, fraction;
   int e;

   fraction = frexp(x, &e);
   printf("x = %.2lf = %.2lf * 2^%d\n", x, fraction, e);

   return(0);
}

Output

运行代码并检查其输出:

Run the code and check its output −

x = 1024.00 = 0.50 * 2^11

ldexp() Function

ldexp() 函数将一个有效数字和一个指数组合以形成一个浮点数。其语法如下 −

The ldexp() function combines a significand and an exponent to form a floating-point number. Its syntax is as follows −

double ldexp(double x, int exponent)

在此处,“ x ”是要表示有效数字的浮点值,而“ exponent ”是要指数值。此函数返回 (x * 2 exp)

Here, "x" is the floating point value representing the significand and "exponent" is the value of the exponent. This function returns (x * 2 exp)

以下示例展示了如何在 C 程序中使用该 ldexp() 函数 −

The following example shows how you can use this ldexp() function in a C program −

#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\n", x, n, ret);

   return(0);
}

Output

运行代码并检查其输出:

Run the code and check its output −

0.650000 * 2^3 = 5.200000

Power and Square Root Functions

pow()sqrt() 函数用于计算给定数的幂和平方根。

The pow() and sqrt() functions are used to calculate the power and square root of the given number.

pow() Function

此函数返回提升为 y 次方的 x,即 x^y。

This function returns x raised to the power of y i.e. xy.

double pow(double x, double y)

sqrt() Function

返回 x 的平方根。

returns the square root of x.

double sqrt(double x)

sqrt(x) 函数返回与 pow(x, 0.5) 相同的值。

The sqrt(x) function returns a value which is same as pow(x, 0.5)

Example

以下示例展示了如何在 C 程序中使用 pow() 和 sqrt() 函数 -

The following example shows how you can use the pow() and sqrt() functions in a C program −

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

int main() {

   double x = 9, y=2;

   printf("Square root of %lf is %lf\n", x, sqrt(x));
   printf("Square root of %lf is %lf\n", x, pow(x, 0.5) );
   printf("%lf raised to power %lf\n", x, pow(x, y));

   return(0);
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Square root of 9.000000 is 3.000000
Square root of 9.000000 is 3.000000
9.000000 raised to power 81.000000

Rounding Functions

math.h 函数库包括 ceil()floor() 和 round() 函数,它们对给定的浮点类型进行四舍五入。

The math.h library includes ceil(), floor(), and round() functions that round off the given floating point number.

ceil() Function

返回大于或等于 x 的最小整数。

This returns the smallest integer value greater than or equal to x.

double ceil(double x)

此函数返回不大于 x 的最小整数。

This function returns the smallest integral value not less than x.

floor() Function

此函数返回小于或等于 x 的最大整数。

This function returns the largest integer value less than or equal to x.

double floor(double x)

参数 x:这是浮点类型值。此函数返回不大于 x 的最大整数。

Parameter x : This is the floating point value. This function returns the largest integral value not greater than x.

round() Function

此函数用于把传递给它的参数 double、float 或 long double 值四舍五入到最近的整数。

This function is used to round off the double, float or long double value passed to it as a parameter to the nearest integral value.

double round( double x )

返回的值是四舍五入为浮点类型的最整数。

The value returned is the nearest integer represented as floating point

以下示例演示了如何在 C 程序中使用舍入函数 -

The following example demonstrates how you can use the rounding functions in a C program −

#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\n", val1, ceil(val1));
   printf ("floor(%lf) = %.1lf\n", val2, floor(val2));
   printf ("ceil(%lf) = %.1lf\n", val3, ceil(val3));
   printf ("floor(%lf) = %.1lf\n", val4, floor(val4));

   printf("round(%lf) = %.1lf\n", val1, round(val1));
   printf("round(%lf) = %.1lf", val4, round(val4));

   return(0);
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

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

Modulus Functions

"math.h" 库在此类别中包括以下函数:

The "math.h" library includes the following functions in this category:

modf() Function

modf() function 返回分数部分(小数点后的部分),并把整数设置为整数部分。

The modf() function returns the fraction component (part after the decimal), and sets integer to the integer component.

double modf(double x, double *integer)

此处," x " 是浮点类型值,"integer" 是要存储整数部分的对象的指针。

Here, "x" is the floating point value and "integer" is the pointer to an object where the integral part is to be stored.

此函数返回与符号相同的 " x " 的分数部分。

This function returns the fractional part of "x" with the same sign.

请看以下示例:

Take a look at the following example −

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

int main () {

   double x, fractpart, intpart;

   x = 8.123456;
   fractpart = modf(x, &intpart);

   printf("Integral part = %lf\n", intpart);
   printf("Fraction Part = %lf \n", fractpart);

   return(0);
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Integral part = 8.000000
Fraction Part = 0.123456

fmod() Function

fmod() function 返回 x 除以 y 的余数。

The fmod() function returns the remainder of x divided by y.

double fmod(double x, double y)

此处," x " 是分子," y " 是分母。函数返回 " x / y " 的余数。

Here, "x" is the numerator and "y" is the denominator. The function returns remainder of "x / y".

请看以下示例:

Take a look at the following example −

#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\n", a, c, fmod(a,c));
   printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b));

   return(0);
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Remainder of 9.200000 / 2 is 1.200000
Remainder of 9.200000 / 3.700000 is 1.800000

请注意,取模运算符 (%) 仅用于整数值运算。

Note that the modulus operator (%) works only with integer operands.