赞
踩
这个库就是简单的整理一下吧,主要包含了各种常用数学函数。简要整理一下。目前只会整理一下自己遇到的函数,所有的函数用法可以参考:< cmath > (math.h)。
#include <math.h>
1.cos
/* cos example */
#include <stdio.h> /* printf */
#include <math.h> /* cos */
#define PI 3.14159265
int main ()
{
double param, result;
param = 60.0;
result = cos ( param * PI / 180.0 );
printf ("The cosine of %f degrees is %f.\n", param, result );
return 0;
}
1.exp
/* exp example */
#include <stdio.h> /* printf */
#include <math.h> /* exp */
int main ()
{
double param, result;
param = 5.0;
result = exp (param);
printf ("The exponential value of %f is %f.\n", param, result );
return 0;
}
//Output:
//The exponential value of 5.000000 is 148.413159.
2.frexp
将一个浮点数转换成0.5~1.0之间的数和2的整数指数。
/* frexp example */
#include <stdio.h> /* printf */
#include <math.h> /* frexp */
int main ()
{
double param, result;
int n;
param = 8.0;
result = frexp (param , &n);
printf ("%f = %f * 2^%d\n", param, result, n);
return 0;
}
//Output:
//8.000000 = 0.500000 * 2^4
3.ldexp
frexp函数逆运算,根据0.5~1.0之间的浮点数与n求出对应的浮点数。
/* ldexp example */
#include <stdio.h> /* printf */
#include <math.h> /* ldexp */
int main ()
{
double param, result;
int n;
param = 0.95;
n = 4;
result = ldexp (param , n);
printf ("%f * 2^%d = %f\n", param, n, result);
return 0;
}
//Output:
//0.950000 * 2^4 = 15.200000
4.log
计算一个数的自然对数:log(x),以e为底。
/* log example */
#include <stdio.h> /* printf */
#include <math.h> /* log */
int main ()
{
double param, result;
param = 5.5;
result = log (param);
printf ("log(%f) = %f\n", param, result );
return 0;
}
//Output:
//log(5.500000) = 1.704748
5.log10
计算一个数的以10为底的对数:log10(x)。
/* log10 example */
#include <stdio.h> /* printf */
#include <math.h> /* log10 */
int main ()
{
double param, result;
param = 1000.0;
result = log10 (param);
printf ("log10(%f) = %f\n", param, result );
return 0;
}
//Output:
//log10(1000.000000) = 3.000000
6.modf
将一个数分为整数与小数部分。
/* modf example */
#include <stdio.h> /* printf */
#include <math.h> /* modf */
int main ()
{
double param, fractpart, intpart;
param = 3.14159265;
fractpart = modf (param , &intpart);
printf ("%f = %f + %f \n", param, intpart, fractpart);
return 0;
}
//Output:
//3.141593 = 3.000000 + 0.14159
7.exp2
求一个数为指数,2为底数的幂:2^x。
/* exp2 example */
#include <stdio.h> /* printf */
#include <math.h> /* exp2 */
int main ()
{
double param, result;
param = 8.0;
result = exp2 (param);
printf ("2 ^ %f = %f.\n", param, result );
return 0;
}
//Output:
//2 ^ 8.000000 is 256.000000.
8.expm1
计算函数:e^x - 1的值。对于较小值的x,e^x - 1 比 exp(x) - 1更精确。
/* expm1 example */
#include <stdio.h> /* printf */
#include <math.h> /* expm1 */
int main ()
{
double param, result;
param = 1.0;
result = expm1 (param);
printf ("expm1 (%f) = %f.\n", param, result );
return 0;
}
//Output:
//expm1 (1.000000) = 1.718282.
9.log2
计算以2为底的对数。
/* log2 example */
#include <stdio.h> /* printf */
#include <math.h> /* log2 */
int main ()
{
double param, result;
param = 1024.0;
result = log2 (param);
printf ("log2 (%f) = %f.\n", param, result );
return 0;
}
//Output:
//log2 (1024.000000) = 10.000000
1.pow
计算乘积值。
/* pow example */
#include <stdio.h> /* printf */
#include <math.h> /* pow */
int main ()
{
printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) );
printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) );
printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) );
return 0;
}
//Output:
//7 ^ 3 = 343.000000
//4.73 ^ 12 = 125410439.217423
//32.01 ^ 1.54 = 208.036691
2.sqrt
/* sqrt example */
#include <stdio.h> /* printf */
#include <math.h> /* sqrt */
int main ()
{
double param, result;
param = 1024.0;
result = sqrt (param);
printf ("sqrt(%f) = %f\n", param, result );
return 0;
}
//Output:
//sqrt(1024.000000) = 32.000000
3.cbrt
计算一个数的立方根。
/* cbrt example */
#include <stdio.h> /* printf */
#include <math.h> /* cbrt */
int main ()
{
double param, result;
param = 27.0;
result = cbrt (param);
printf ("cbrt (%f) = %f\n", param, result);
return 0;
}
//Output:
//cbrt (27.000000) = 3.000000
4.hypot
计算三角形的第三边。
/* hypot example */
#include <stdio.h> /* printf */
#include <math.h> /* hypot */
int main ()
{
double leg_x, leg_y, result;
leg_x = 3;
leg_y = 4;
result = hypot (leg_x, leg_y);
printf ("%f, %f and %f form a right-angled triangle.\n",leg_x,leg_y,result);
return 0;
}
//Output:
//3.000000, 4.000000 and 5.000000 form a right-angled triangle.
1.ceil
对一个小数向上取整数。
/* ceil example */
#include <stdio.h> /* printf */
#include <math.h> /* ceil */
int main ()
{
printf ( "ceil of 2.3 is %.1f\n", ceil(2.3) );
printf ( "ceil of 3.8 is %.1f\n", ceil(3.8) );
printf ( "ceil of -2.3 is %.1f\n", ceil(-2.3) );
printf ( "ceil of -3.8 is %.1f\n", ceil(-3.8) );
return 0;
}
//Output:
//ceil of 2.3 is 3.0
//ceil of 3.8 is 4.0
//ceil of -2.3 is -2.0
//ceil of -3.8 is -3.0
2.floor
对一个小数向下取整数
/* floor example */
#include <stdio.h> /* printf */
#include <math.h> /* floor */
int main ()
{
printf ( "floor of 2.3 is %.1lf\n", floor (2.3) );
printf ( "floor of 3.8 is %.1lf\n", floor (3.8) );
printf ( "floor of -2.3 is %.1lf\n", floor (-2.3) );
printf ( "floor of -3.8 is %.1lf\n", floor (-3.8) );
return 0;
}
//Output:
//floor of 2.3 is 2.0
//floor of 3.8 is 3.0
//floor of -2.3 is -3.0
//floor of -3.8 is -4.0
1.fabs
浮点数取绝对值。
/* fabs example */
#include <stdio.h> /* printf */
#include <math.h> /* fabs */
int main ()
{
printf ("The absolute value of 3.1416 is %f\n", fabs (3.1416) );
printf ("The absolute value of -10.6 is %f\n", fabs (-10.6) );
return 0;
}
//Output:
//The absolute value of 3.1416 is 3.141600
//The absolute value of -10.6 is 10.600000
2.abs
整数取绝对值。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。