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\n", x, sn);
   printf("cos(%f) : %f\n", x, cs);
   printf("tan(%f) : %f\n", 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() 函式 返回“x”在區間 [-pi/2, +pi/2] 弧度內的主值反正切值。

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\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);
}

輸出

執行此程式碼時,將產生以下輸出:

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

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] 弧度內的主值反正切值。

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

輸出

執行此程式碼時,將產生以下輸出:

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

浮點數函式

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

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

   return(0);
}

輸出

執行程式碼並檢查其輸出:

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

   return(0);
}

輸出

執行程式碼並檢查其輸出:

0.650000 * 2^3 = 5.200000

冪和平方根函式

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

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\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);
}

輸出

執行此程式碼時,將產生以下輸出:

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 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\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);
}
輸出

執行此程式碼時,將產生以下輸出:

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() 函式

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”是指向一個物件的指標,其中儲存整數部分。

此函式返回“x”的小數部分,符號相同。

示例

請檢視以下示例:

#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);
}
輸出

執行此程式碼時,將產生以下輸出:

Integral part = 8.000000
Fraction Part = 0.123456

fmod() 函式

The fmod() function returns the remainder of x divided by 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\n", a, c, fmod(a,c));
   printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b));

   return(0);
}
輸出

執行此程式碼時,將產生以下輸出:

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

請注意,模運算子 (%) 僅適用於整數運算元。

廣告