📜  C库math.h函数

📅  最后修改于: 2021-05-26 02:33:02             🧑  作者: Mango

math.h标头定义了各种数学函数和一个宏。该库中所有可用的函数都将double作为参数,并返回double作为结果。让我们一一讨论一些重要的功能。
1. double ceil(double x) :C库函数double ceil(double x)返回大于或等于x的最小整数值。

syntax : double ceil(double x)
C
// C code to illustrate
// the use of ceil function.
#include 
#include 
 
int main ()
{
float val1, val2, val3, val4;
 
val1 = 1.6;
val2 = 1.2;
val3 = -2.8;
val4 = -2.3;
 
printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));
     
return(0);
}


C
// C code to illustrate
// the use of floor function
#include 
#include 
 
int main ()
{
   float val1, val2, val3, val4;
 
   val1 = 1.6;
   val2 = 1.2;
   val3 = -2.8;
   val4 = -2.3;
 
   printf("Value1 = %.1lf\n", floor(val1));
   printf("Value2 = %.1lf\n", floor(val2));
   printf("Value3 = %.1lf\n", floor(val3));
   printf("Value4 = %.1lf\n", floor(val4));
    
   return(0);
}


C
// C code to illustrate
// the use of fabs function
#include 
#include 
 
int main ()
{
   int a, b;
   a = 1234;
   b = -344;
   
   printf("The absolute value of %d is %lf\n", a, fabs(a));
   printf("The absolute value of %d is %lf\n", b, fabs(b));
    
   return(0);
}


C
// C code to illustrate
// the use of log function
 
#include 
#include 
 
int main ()
{
   double x, ret;
   x = 2.7;
 
   /* finding log(2.7) */
   ret = log(x);
   printf("log(%lf) = %lf", x, ret);
    
   return(0);
}


C
// C code to illustrate
// the use of log10 function
#include 
#include 
 
int main ()
{
   double x, ret;
   x = 10000;
   
   /* finding value of log1010000 */
   ret = log10(x);
   printf("log10(%lf) = %lf\n", x, ret);
    
   return(0);
}


C
// C code to illustrate
// the use of fmod function
#include 
#include 
 
int main ()
{
   float a, b;
   int c;
   a = 8.2;
   b = 5.7;
   c = 3;
   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);
}


C
// C code to illustrate
// the use of sqrt function
#include 
#include 
 
int main ()
{
 
   printf("Square root of %lf is %lf\n", 225.0, sqrt(225.0) );
   printf("Square root of %lf is %lf\n", 300.0, sqrt(300.0) );
    
   return(0);
}


C
// C code to illustrate
// the use of pow function
#include 
#include 
 
int main ()
{
   printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));
 
   printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
    
   return(0);
}


C
// C code to illustrate
// the use of modf function
#include
#include
 
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);
}


C
// C code to illustrate
// the use of exp function
#include 
#include 
 
int main ()
{
   double x = 0;
   
   printf("The exponential value of %lf is %lf\n", x, exp(x));
   printf("The exponential value of %lf is %lf\n", x+1, exp(x+1));
   printf("The exponential value of %lf is %lf\n", x+2, exp(x+2));
    
   return(0);
}


C
// C code to illustrate
// the use of cos function
#include 
#include 
 
#define PI 3.14159265
 
int main ()
{
   double x, ret, val;
 
   x = 60.0;
   val = PI / 180.0;
   ret = cos( x*val );
   printf("The cosine of %lf is %lf degrees\n", x, ret);
    
   x = 90.0;
   val = PI / 180.0;
   ret = cos( x*val );
   printf("The cosine of %lf is %lf degrees\n", x, ret);
    
   return(0);
}


C
// C code to illustrate
// the use of acos function
#include 
#include 
 
#define PI 3.14159265
 
int main ()
{
   double x, ret, val;
 
   x = 0.9;
   val = 180.0 / PI;
 
   ret = acos(x) * val;
   printf("The arc cosine of %lf is %lf degrees", x, ret);
    
   return(0);
}


C
// C code to illustrate
// the use of tanh function
#include 
#include 
 
int main ()
{
   double x, ret;
   x = 0.5;
 
   ret = tanh(x);
   printf("The hyperbolic tangent of %lf is %lf degrees", x, ret);
    
   return(0);
}


输出:

value1 = 2.0
value2 = 2.0
value3 = -2.0
value4 = -2.0

2. double floor(double x): C库函数double floor(double x)返回小于或等于x的最大整数值。

syntax : double floor(double x)

C

// C code to illustrate
// the use of floor function
#include 
#include 
 
int main ()
{
   float val1, val2, val3, val4;
 
   val1 = 1.6;
   val2 = 1.2;
   val3 = -2.8;
   val4 = -2.3;
 
   printf("Value1 = %.1lf\n", floor(val1));
   printf("Value2 = %.1lf\n", floor(val2));
   printf("Value3 = %.1lf\n", floor(val3));
   printf("Value4 = %.1lf\n", floor(val4));
    
   return(0);
}

输出:

Value1 = 1.0
Value2 = 1.0
Value3 = -3.0
Value4 = -3.0

3. double fabs(double x) :C库函数double fabs(double x)返回x的绝对值。

syntax : double fabs(double x)

C

// C code to illustrate
// the use of fabs function
#include 
#include 
 
int main ()
{
   int a, b;
   a = 1234;
   b = -344;
   
   printf("The absolute value of %d is %lf\n", a, fabs(a));
   printf("The absolute value of %d is %lf\n", b, fabs(b));
    
   return(0);
}

输出:

The absolute value of 1234 is 1234.000000
The absolute value of -344 is 344.000000

4. double log(double x) :C库函数double log(double x)返回x的自然对数(以e为底的对数)。

syntax : double log(double x)

C

// C code to illustrate
// the use of log function
 
#include 
#include 
 
int main ()
{
   double x, ret;
   x = 2.7;
 
   /* finding log(2.7) */
   ret = log(x);
   printf("log(%lf) = %lf", x, ret);
    
   return(0);
}

输出:

log(2.700000) = 0.993252

5. double log10(double x) :C库函数double log10(double x)返回x的公共对数(以10为底的对数)。

syntax : double log10(double x)

C

// C code to illustrate
// the use of log10 function
#include 
#include 
 
int main ()
{
   double x, ret;
   x = 10000;
   
   /* finding value of log1010000 */
   ret = log10(x);
   printf("log10(%lf) = %lf\n", x, ret);
    
   return(0);
}

输出:

log10(10000.000000) = 4.000000

6. double fmod(double x,double y) :C库函数double fmod(double x,double y)返回x的其余部分除以y。

syntax : double fmod(double x, double y) 

C

// C code to illustrate
// the use of fmod function
#include 
#include 
 
int main ()
{
   float a, b;
   int c;
   a = 8.2;
   b = 5.7;
   c = 3;
   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 8.200000 / 3 is 2.200000
Remainder of 8.200000 / 5.700000 is 2.500000

7. double sqrt(double x) :C库函数double sqrt(double x)返回x的平方根。

syntax : double sqrt(double x)

C

// C code to illustrate
// the use of sqrt function
#include 
#include 
 
int main ()
{
 
   printf("Square root of %lf is %lf\n", 225.0, sqrt(225.0) );
   printf("Square root of %lf is %lf\n", 300.0, sqrt(300.0) );
    
   return(0);
}

输出:

Square root of 225.000000 is 15.000000
Square root of 300.000000 is 17.320508

8. double pow(double x,double y) :C库函数double pow(double x,double y)返回x升为y的幂,即xy。

syntax : double pow(double x, double y)

C

// C code to illustrate
// the use of pow function
#include 
#include 
 
int main ()
{
   printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));
 
   printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
    
   return(0);
}

输出:

Value 8.0 ^ 3 = 512.000000
Value 3.05 ^ 1.98 = 9.097324

9. double modf(double x,double * integer) :C库函数double modf(double x,double * integer)返回小数部分(小数点后的部分),并将整数设置为整数部分。

syntax : double modf(double x, double *integer)

C

// C code to illustrate
// the use of modf function
#include
#include
 
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 

10. double exp(double x) :C库函数double exp(double x)返回将e的值提高到x的幂。

syntax : double exp(double x)

以下代码代表

C

// C code to illustrate
// the use of exp function
#include 
#include 
 
int main ()
{
   double x = 0;
   
   printf("The exponential value of %lf is %lf\n", x, exp(x));
   printf("The exponential value of %lf is %lf\n", x+1, exp(x+1));
   printf("The exponential value of %lf is %lf\n", x+2, exp(x+2));
    
   return(0);
}

输出:

The exponential value of 0.000000 is 1.000000
The exponential value of 1.000000 is 2.718282
The exponential value of 2.000000 is 7.389056

11. double cos(double x) :C库函数double cos(double x)返回弧度角x的余弦。

syntax : double cos(double x) 

注意:相同的语法可用于其他三角函数,例如sin,tan等。

C

// C code to illustrate
// the use of cos function
#include 
#include 
 
#define PI 3.14159265
 
int main ()
{
   double x, ret, val;
 
   x = 60.0;
   val = PI / 180.0;
   ret = cos( x*val );
   printf("The cosine of %lf is %lf degrees\n", x, ret);
    
   x = 90.0;
   val = PI / 180.0;
   ret = cos( x*val );
   printf("The cosine of %lf is %lf degrees\n", x, ret);
    
   return(0);
}

输出:

The cosine of 60.000000 is 0.500000 degrees
The cosine of 90.000000 is 0.000000 degrees

12. double acos(double x): C库函数double acos(double x)返回弧度x的反余弦值。

syntax : double acos(double x)

注意:相同的语法可用于其他弧三角函数,如asin,atan等。

C

// C code to illustrate
// the use of acos function
#include 
#include 
 
#define PI 3.14159265
 
int main ()
{
   double x, ret, val;
 
   x = 0.9;
   val = 180.0 / PI;
 
   ret = acos(x) * val;
   printf("The arc cosine of %lf is %lf degrees", x, ret);
    
   return(0);
}

输出:

The arc cosine of 0.900000 is 25.855040 degrees

13. double tanh(double x): C库函数double tanh(double x)返回x的双曲正切值。

syntax : double tanh(double x) 

注意:相同的语法可用于其他双曲三角函数,例如sinh,cosh等。

C

// C code to illustrate
// the use of tanh function
#include 
#include 
 
int main ()
{
   double x, ret;
   x = 0.5;
 
   ret = tanh(x);
   printf("The hyperbolic tangent of %lf is %lf degrees", x, ret);
    
   return(0);
}

输出:

The hyperbolic tangent of 0.500000 is 0.462117 degrees
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。