打印
[牛人杂谈]

math.h中一些常用函数

[复制链接]
606|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
稳稳の幸福|  楼主 | 2016-10-29 15:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Library Functions

Following are the functions defined in the header math.h −

S.N.Function & Description
1

double acos(double x)

Returns the arc cosine of x in radians.

2

double asin(double x)

Returns the arc sine of x in radians.

3

double atan(double x)

Returns the arc tangent of x in radians.

4

double atan2(doubly y, double x)

Returns the arc tangent in radians of y/x based on the signs of both values to determine the correct quadrant.

5

double cos(double x)

Returns the cosine of a radian angle x.

6

double cosh(double x)

Returns the hyperbolic cosine of x.

7

double sin(double x)

Returns the sine of a radian angle x.

8

double sinh(double x)

Returns the hyperbolic sine of x.

9

double tanh(double x)

Returns the hyperbolic tangent of x.

10

double exp(double x)

Returns the value of e raised to the xth power.

11

double frexp(double x, int *exponent)

The returned value is the mantissa and the integer pointed to by exponent is the exponent. The resultant value is x = mantissa * 2 ^ exponent.

12

double ldexp(double x, int exponent)

Returns x multiplied by 2 raised to the power of exponent.

13

double log(double x)

Returns the natural logarithm (base-e logarithm) of x.

14

double log10(double x)

Returns the common logarithm (base-10 logarithm) of x.

15

double modf(double x, double *integer)

The returned value is the fraction component (part after the decimal), and sets integer to the integer component.

16

double pow(double x, double y)

Returns x raised to the power of y.

17

double sqrt(double x)

Returns the square root of x.

18

double ceil(double x)

Returns the smallest integer value greater than or equal to x.

19

double fabs(double x)

Returns the absolute value of x.

20

double floor(double x)

Returns the largest integer value less than or equal to x.

21

double fmod(double x, double y)

Returns the remainder of x divided by y.


沙发
稳稳の幸福|  楼主 | 2016-10-29 15:31 | 只看该作者

math.h中一些常用函数


abs(计算整型数的绝对值)

相关函数: abs, fabs

表头文件: #include<stdlib.h>

定义函数: int abs (int j)

函数说明: abs()用来计算参数j的绝对值,然后将结果返回。

返回值: 返回参数j的绝对值结果。

范例:

#ingclude <stdlib.h>

main()

{

int ansert;

answer = abs(-12);

printf("|-12| = %d/n", answer);

}

执行:

|-12| = 12


ceil(取不小于参数的最小整型数)

相关函数: fabs

表头文件: #include <math.h>

定义函数: double ceil (double x);

函数说明: ceil()会返回不小于参数x的最小整数值,结果以double形态返回。

返回值: 返回不小于参数x的最小整数值。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double value[ ]={4.8,1.12,-2.2,0};

int i;

for (i=0;value!=0;i++)

printf("%f=>%f/n",value,ceil(value));

}

执行:

4.800000=>5.000000

1.120000=>2.000000

-2.200000=>-2.000000


sin(取正弦函数值)

相关函数: acos,asin,atan,atan2,cos,tan

表头文件: #include<math.h>

定义函数: double sin(double x);

函数说明: sin()用来计算参数x的正弦值,然后将结果返回。

返回值: 返回-1 至1之间的计算结果。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double answer = sin (0.5);

printf("sin(0.5) = %f/n",answer);

}

执行:

sin(0.5) = 0.479426


cos(取余弦函数值)

相关函数: acos,asin,atan,atan2,sin,tan

表头文件: #include<math.h>

定义函数: double cos(double x);

函数说明: cos()用来计算参数x 的余弦值,然后将结果返回。

返回值: 返回-1至1之间的计算结果。

附加说明: 使用GCC编译时请加入-lm。

范例

#include<math.h>

main()

{

double answer = cos(0.5);

printf("cos (0.5) = %f/n",answer);

}

执行:

cos(0.5) = 0.877583


tan(取正切函数值)

相关函数: atan,atan2,cos,sin

表头文件: #include <math.h>

定义函数: double tan(double x);

函数说明: tan()用来计算参数x的正切值,然后将结果返回。

返回值: 返回参数x的正切值。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double answer = tan(0.5);

printf("tan (0.5) = %f/n",answer);

}

执行:

tan(0.5) = 0.546302


asin(取反正弦函数值)

相关函数: acos , atan , atan2 , cos , sin , tan

表头文件: #include <math.h>

定义函数: double asin (double x)

函数说明: asin()用来计算参数x的反正弦值,然后将结果返回。参数x范围为-1至1之间,超过此范围则会失败。

返回值: 返回-PI/2之PI/2之间的计算结果。

错误代码: EDOM参数x超出范围

附加说明: 使用GCC编译时请加入-lm

范例:

#include<math.h>

main()

{

double angle;

angle = asin (0.5);

printf("angle = %f/n",angle);

}

执行:

angle = 0.523599


acos(取反余弦函数数值)

相关函数: asin , atan , atan2 , cos , sin , tan

表头文件: #include <math.h>

定义函数: double acos (double x);

函数说明: acos()用来计算参数x的反余弦值,然后将结果返回。参数x范围为-1至1之间,超过此范围则会失败。

返回值: 返回0至PI之间的计算结果,单位为弧度,在函数库中角度均以弧度来表示。

错误代码: EDOM参数x超出范围。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include <math.h>

main ()

{

double angle;

angle = acos(0.5);

printf("angle = %f/n", angle);

}

执行:

angle = 1.047198


使用特权

评论回复
板凳
稳稳の幸福|  楼主 | 2016-10-29 15:33 | 只看该作者

atan(取反正切函数值)

相关函数: acos,asin,atan2,cos,sin,tan

表头文件: #include<math.h>

定义函数: double atan(double x);

函数说明: atan()用来计算参数x的反正切值,然后将结果返回。

返回值: 返回-PI/2至PI/2之间的计算结果。

附加说明: 使用GCC编译时请加入-lm

范例:

#include<math.h>

main()

{

double angle;

angle =atan(1);

printf("angle = %f/n",angle);

}

执行

angle = 1.570796


atan2(取得反正切函数值)

相关函数: acos,asin,atan,cos,sin,tan

表头文件: #include<math.h>

定义函数: double atan2(double y,double x);

函数说明: atan2()用来计算参数y/x的反正切值,然后将结果返回。

返回值: 返回-PI/2 至PI/2 之间的计算结果。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double angle;

angle = atan2(1,2);

printf("angle = %f/n", angle);

}

执行:

angle = 0.463648


sinh(取双曲线正弦函数值)

相关函数: cosh,tanh

表头文件: #include<math.h>

定义函数: double sinh( double x);

函数说明: sinh()用来计算参数x的双曲线正弦值,然后将结果返回。数学定义式为:(exp(x)-exp(-x))/2。

返回值: 返回参数x的双曲线正弦值。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double answer = sinh (0.5);

printf("sinh(0.5) = %f/n",answer);

}

执行:

sinh(0.5) = 0.521095


cosh(取双曲线余弦函数值)

相关函数: sinh,tanh

表头文件: #include<math.h>

定义函数: double cosh(double x);

函数说明: cosh()用来计算参数x的双曲线余弦值,然后将结果返回。数学定义式为:(exp(x)+exp(-x))/2。

返回值: 返回参数x的双曲线余弦值。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double answer = cosh(0.5);

printf("cosh(0.5) = %f/n",answer);

}

执行:

cosh(0.5) = 1.127626


tanh(取双曲线正切函数值)

相关函数: cosh,sinh

表头文件: #include<math.h>

定义函数: double tanh(double x);

函数说明: tanh()用来计算参数x的双曲线正切值,然后将结果返回。数学定义式为:sinh(x)/cosh(x)。

返回值: 返回参数x的双曲线正切值。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double answer = tanh(0.5);

printf("tanh(0.5) = %f/n",answer);

}

执行:

tanh(0.5) = 0.462117


使用特权

评论回复
地板
稳稳の幸福|  楼主 | 2016-10-29 15:34 | 只看该作者

exp(计算指数)

相关函数: log,log10,pow

表头文件: #include<math.h>

定义函数: double exp(double x);

函数说明: exp()用来计算以e为底的x次方值,即ex值,然后将结果返回。

返回值: 返回e的x次方计算结果。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double answer;

answer = exp (10);

printf("e^10 =%f/n", answer);

}

执行:

e^10 = 22026.465795


frexp(将浮点型数分为底数与指数)

相关函数: ldexp,modf

表头文件: #include<math.h>

定义函数: double frexp( double x, int *exp);

函数说明: frexp()用来将参数x 的浮点型数切割成底数和指数。底数部分直接返回,指数部分则借参数exp 指针返回,将返回值乘以2 的exp次方即为x的值。

返回值: 返回参数x的底数部分,指数部分则存于exp指针所指的地址。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include <math.h>

main()

{

int exp;

double fraction;

fraction = frexp (1024,&exp);

printf("exp = %d/n",exp);

printf("fraction = %f/n", fraction);

}

执行:

exp = 11

fraction = 0.500000 /* 0.5*(2^11)=1024*/


ldexp(计算2的次方值)

相关函数: frexp

表头文件: #include<math.h>

定义函数: double ldexp(double x,int exp);

函数说明: ldexp()用来将参数x乘上2的exp次方值,即x*2exp。

返回值: 返回计算结果。

附加说明: 使用GCC编译时请加入-lm。

范例:

/* 计算3*(2^2)=12 */

#include<math.h>

main()

{

int exp;

double x,answer;

answer = ldexp(3,2);

printf("3*2^(2) = %f/n",answer);

}

执行:

3*2^(2) = 12.000000


log(计算以e 为底的对数值)

相关函数: exp,log10,pow

表头文件: #include <math.h>

定义函数: double log (double x);

函数说明: log()用来计算以e为底的x 对数值,然后将结果返回。

返回值: 返回参数x的自然对数值。

错误代码: EDOM 参数x为负数,ERANGE 参数x为零值,零的对数值无定义。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double answer;

answer = log (100);

printf("log(100) = %f/n",answer);

}

执行:

log(100) = 4.605170



使用特权

评论回复
5
稳稳の幸福|  楼主 | 2016-10-29 15:34 | 只看该作者

log10(计算以10 为底的对数值)

相关函数: exp,log,pow

表头文件: #include<math.h>

定义函数: double log10(double x);

函数说明: log10()用来计算以10为底的x对数值,然后将结果返回。

返回值: 返回参数x以10为底的对数值。

错误代码: EDOM参数x为负数。RANGE参数x为零值,零的对数值无定义。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include<math.h>

main()

{

double answer;

answer = log10(100);

printf("log10(100) = %f/n",answer);

}

执行:

log10(100) = 2.000000


pow(计算次方值)

相关函数: exp,log,log10

表头文件: #include<math.h>

定义函数: double pow(double x,double y);

函数说明: pow()用来计算以x为底的y次方值,即xy值,然后将结果返回。

返回值: 返回x的y次方计算结果。

错误代码: EDOM 参数x为负数且参数y不是整数。

附加说明: 使用GCC编译时请加入-lm。

范例:

#include <math.h>

main()

{

double answer;

answer =pow(2,10);

printf("2^10 = %f/n", answer);

}

执行:

2^10 = 1024.000000


sqrt(计算平方根值)

相关函数: hypotq

表头文件: #include<math.h>

定义函数: double sqrt(double x);

函数说明: sqrt()用来计算参数x的平方根,然后将结果返回。参数x必须为正数。

返回值: 返回参数x的平方根值。

错误代码: EDOM 参数x为负数。

附加说明: 使用GCC编译时请加入-lm。

范例:

/* 计算200的平方根值*/

#include<math.h>

main()

{

double root;

root = sqrt (200);

printf("answer is %f/n",root);

}

执行:

answer is 14.142136


使用特权

评论回复
6
wahahaheihei| | 2016-10-29 17:26 | 只看该作者
这些函数应用的当的话,能在需要计算的时候方便不少。

使用特权

评论回复
7
gejigeji521| | 2016-10-30 12:04 | 只看该作者
以前只知道用,不知道这些好多都是双精度类型啊,还以为简单的浮点运算嗯。

使用特权

评论回复
8
稳稳の幸福|  楼主 | 2016-11-7 12:51 | 只看该作者
合理的运用这些函数,可以减轻自己编写代码的负担。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

170

主题

3162

帖子

8

粉丝