目录
1.math.h 头文件的常用函数
a.signbit(求浮点数是否含有符号)
b.三角函数汇总
c.双曲函数
d.指数函数对数函数
e.分解浮点数(详解如下)frexp
f.取浮点数的指数部分ilogb
g.反分解浮点数(详解如下)ldexp
h.浮点数的取整和取小 modf
i.四舍五入(round)
j.浮点数砍去小数部分(trunc)
k.向上取整(ceil)
l.向下取整(floor)
m.浮点数取余fmod
n.x的y次方pow函数
o.平方根sqrt
p.立方根cbrt
q.绝对值fabs
r.已知直角边求斜边hypot
s.两数取大小 fmax fmin
2.其余常用函数
a.全排列
b.最大公因数 最小公倍数
1.math.h 头文件的常用函数
a.signbit(求浮点数是否含有符号)
#include
#include
int main(void)
{
float a = 1, b = -1;
int c,d;
c = signbit(a);
d = signbit(b);
printf("%d %d", c,d);
return 0;
}
由于signbit返回值是布尔类型,因此我们可以用整形来存储值。
输出结果如下
0 1
在二进制补码中 0开头是代表真值是正数,1代表真值是负数。
b.三角函数汇总
有
一个输入:正弦sin sinf sinl 余弦cos cosf cosl 正切tan tanf tanl 反正弦asin asinf asinl 反余弦acos acosf acosl 反正切atan atanf atanl
两个输入:求偏转角 atan2 atan2f atan2l(原点与输入点的连线与y轴正半轴夹角的弧度值)
一个输入:例
#include
#include
#define pi 3.1415926
int main(void)
{
double a = sin(0.2 * pi);
double b = cos(0.2 * pi);
double c = tan(0.2 * pi);
double d = asin(0.2 * pi);
double e = acos(0.2 * pi);
double f = atan(0.2 * pi);
float a1 = sinf(0.2 * pi);
float b1 = cosf(0.2 * pi);
float c1 = tanf(0.2 * pi);
float d1 = asinf(0.2 * pi);
float e1 = acosf(0.2 * pi);
float f1 = atanf(0.2 * pi);
long double a2 = sinl(0.2 * pi);
long double b2 = cosl(0.2 * pi);
long double c2 = tanl(0.2 * pi);
long double d2 = asinl(0.2 * pi);
long double e2 = acosl(0.2 * pi);
long double f2 = atanl(0.2 * pi);
printf("%f %f %f %f %f %f\n", a1, b1, c1, d1, e1, f1);
printf("%lf %lf %lf %lf %lf %lf\n", a, b, c, d, e, f);
printf("%llf %llf %llf %llf %llf %llf", a2, b2, c2, d2, e2, f2);
return 0;
}
输出如下
0.587785 0.809017 0.726543 0.679390 0.891406 0.560982
0.587785 0.809017 0.726543 0.679390 0.891406 0.560982
0.587785 0.809017 0.726543 0.679390 0.891406 0.560982
两个输入:例
#include
#include
int main(void)
{
double a=atan2(1,-1);
double b = a / 3.1415926;
printf("%lf*pi\n", b);
float a1 = atan2f(1, -1);
float b1 = a1 / 3.1415926;
printf("%f*pi\n", b1);
long double a2 = atan2l(1, -1);
long double b2 = a2 / 3.1415926;
printf("%lf*pi", b2);
return 0;
}
输出如下
0.750000*pi
0.750000*pi
0.750000*pi
注:atan2 atan2f atan2l 求的是原点与输入点所连线与y轴正半轴的夹角。
c.双曲函数
有
双曲正弦sinh sinhf sinhl 双曲余弦cosh coshf coshl 双曲正切tanh tanhf tanhl 反双曲正弦asinh asinhf asinhl 反双曲余弦acosh acoshf scoshl
反双曲正切atanh atanhf atanhl
#include
#include
#define pi 3.1415926
int main(void)
{
double a = sinh(0.3 * pi);
double b = cosh(0.3 * pi);
double c = tanh(0.3 * pi);
double d = asinh(0.3 * pi);
double e = acosh(pi);
double f = atanh(0.3 * pi);
float a1 = sinhf(0.3 * pi);
float b1 = coshf(0.3 * pi);
float c1 = tanhf(0.3 * pi);
float d1 = asinhf(0.3 * pi);
float e1 = acoshf(pi);
float f1 = atanhf(0.3 * pi);
long double a2 = sinhl(0.3 * pi);
long double b2 = coshl(0.3 * pi);
long double c2 = tanhl(0.3 * pi);
long double d2 = asinhl(0.3 * pi);
long double e2 = acoshl( pi);
long double f2 = atanhl(0.3 * pi);
printf("%lf %lf %lf %lf %lf %lf\n", a, b, c, d, e, f);
printf("%f %f %f %f %f %f\n", a1, b1, c1, d1, e1, f1);
printf("%llf %llf %llf %llf %llf %llf", a2, b2, c2, d2, e2, f2);
return 0;
}
输出如下
1.088336 1.477997 0.736359 0.840109 1.811526 1.759774
1.088336 1.477997 0.736359 0.840109 1.811526 1.759774
1.088336 1.477997 0.736359 0.840109 1.811526 1.759774
d.指数函数 对数函数
有
e的x次方 exp explf expl 2的x次方 exp2 exp2f exp2l e的x次方减一 expm1 expm1f expm1l
ln(x):log logf logl log2(x): log2 log2f log2l log10(x): log10 log10f log10l
log2(x)取整logb logbf logbl
#include
#include
int main(void)
{
double a = exp(2);
double b = exp2(2);
double c = expm1(2);
double d = log(2);
double e = log2(2);
double f = log10(2);
double g = logb(2);
float a1 = expf(2);
float b1= exp2f(2);
float c1 = expm1f(2);
float d1 = logf(2);
float e1 = log2f(2);
float f1 = log10f(2);
float g1 = logbf(2);
long double a2 = expl(2);
long double b2 = exp2l(2);
long double c2 = expm1l(2);
long double d2 = logl(2);
long double e2 = log2l(2);
long double f2 = log10l(2);
long double g2 = logbl(2);
printf("%lf %lf %lf %lf %lf %lf %lf\n", a, b, c, d, e, f, g);
printf("%f %f %f %f %f %f %f\n", a1, b1, c1, d1, e1, f1, g1);
printf("%llf %llf %llf %llf %llf %llf %llf\n", a2, b2, c2, d2, e2, f2, g2);
return 0;
}
输出如下
7.389056 4.000000 6.389056 0.693147 1.000000 0.301030 1.000000
7.389056 4.000000 6.389056 0.693147 1.000000 0.301030 1.000000
7.389056 4.000000 6.389056 0.693147 1.000000 0.301030 1.000000
e.分解浮点数(详解如下)frexp
有 frexp frexpf frexpl
若a为一个浮点数 令b为整形 c为一个浮点数 若a=c*exp2(b)
c=frexp(a,&b);
例:
#include
#include
int main(void)
{
double a = 123.4567;
int b = 10;
double c;
c = frexp(a, &b);
float d;
d = frexpf(a, &b);
long double e;
e = frexpl(a, &b);
printf("%lf %f %llf\n", c, d, e);
/*验算*/
double f = c * exp2(b);
printf("%lf", f);
return 0;
}
输出如下
0.964505 0.964505 0.964505
123.456700
验算正确
f.取浮点数的指数部分ilogb
有 ilogb ilogbf ilogbl
若 a为一个浮点数 b为一个整数
a=exp2(b);
b=ilogb(a);
(向下取) ilogb(256.000000)=8;
ilogb(257.000000)=8;
ilogb(255.000000)=7;
例:
#include
#include
int main(void)
{
int b;
double a = 255;
b = ilogb(a);
printf("%d ", b);
int b1;
float a1 = 255;
b1 = ilogbf(a1);
printf("%d ", b1);
int b2;
long double a2 = 255;
b2 = ilogbl(a2);
printf("%d", b2);
return 0;
}
输出如下
7 7 7
g.反分解浮点数(详解如下)ldexp
有ldexp ldexpf ldexpl
若a为一个浮点数 令b为整形 c为一个浮点数 若a=c*exp2(b)
c=frexp(a,&b);
q=ldexp(c,b);
例
#include
#include
int main(void)
{
double a;
float a1;
long double a2;
double b = 4;
int c = 3;
a = ldexp(b, c);
a1 = ldexpf(b, c);
a2 = ldexp(b, c);
printf("%lf %f %llf", a, a1, a2);
return 0;
}
输出如下
32.000000 32.000000 32.000000
h.浮点数的取整和取小 modf
有modff modfl
若a为浮点数 b为整数 c为小于1的浮点数 a=b+c
那么 有c=modf(a,&b);
例:
#include
#include
int main(void)
{
double c;
float c1;
long double c2;
double b;
float b1;
long double b2;
double a = 1.2345;
float a1 = 1.2345;
long double a2 = 1.2345;
c = modf(a, &b);
c1 = modff(a1, &b1);
c2= modfl(a2, &b2);
printf("%lf %lf\n", b, c);
printf("%f %f\n", b1, c1);
printf("%llf %llf", b2, c2);
return 0;
}
输出如下
1.000000 0.234500
1.000000 0.234500
1.000000 0.234500
i.四舍五入(round)
round为传统的四舍五入
返回值为浮点型:有 round roundf roundl
返回值为整形:有lroung lrounff lroundl llround llrounff llroundl
返回值为浮点形:
#include
#include
int main(void)
{
double a = 1.234;
float a1 = 1.234;
long double a2 = 1.234;
double b;
float b1;
long double b2;
b = round(a);
b1 = round(a1);
b2 = round(a2);
printf("%lf %f %llf", b, b1, b2);
return 0;
}
输出为
1.000000 1.000000 1.000000
返回值为整形:
#include
#include
int main(void)
{
long a, a1, a2;
long long b, b1, b2;
double c = 1.234;
float c1 = 1.234;
long double c2 = 1.234;
a = lround(c);
a1 = lroundf(c1);
a2 = lround(c2);
b = llround(c);
b1 = llround(c1);
b2 = llround(c2);
printf("%ld %ld %ld\n", a, a1, a2);
printf("%lld %lld %lld", b, b1, b2);
return 0;
}
输出如下
1 1 1
1 1 1
j.浮点数砍去小数部分(trunc)
有
trunc truncf truncl
#include
#include
int main(void)
{
double a = 1.234;
float a1 = 1.234;
long double a2 = 1.234;
double b;
float b1;
long double b2;
b = trunc(a);
b1 = truncf(a1);
b2 = truncl(a2);
printf("%lf %f %llf", b, b1, b2);
return 0;
}
输出如下
1.000000 1.000000 1.000000
k.向上取整(ceil)
有ceilf ceill
#include
#include
int main(void)
{
double a = 1.234;
float a1 = 1.234;
long double a2 = 1.234;
double b;
float b1;
long double b2;
b = ceil(a);
b1 = ceilf(a1);
b2 = ceill(a2);
printf("%lf %f %llf", b, b1, b2);
return 0;
}
输出如下
2.000000 2.000000 2.000000
l.向下取整(floor)
#include
#include
int main(void)
{
double a = 1.234;
float a1 = 1.234;
long double a2 = 1.234;
double b;
float b1;
long double b2;
b = floor(a);
b1 = floorf(a1);
b2 = floorl(a2);
printf("%lf %f %llf", b, b1, b2);
return 0;
}
输出如下
1.000000 1.000000 1.000000
m.浮点数取余fmod
有 fmodf fmodl
#include
#include
int main(void)
{
double a = fmod(5.5, 3.3);
float b = fmodf(5.5, 3.3);
long double c = fmod(5.5, 3.3);
printf("%lf %f %llf", a, b, c);
return 0;
}
输出如下
2.200000 2.200000 2.200000
n.x的y次方pow函数
有 powf powl形式
#include
#include
int main(void)
{
double a = pow(2, 2);
float b = powf(3, 3);
long double c = powl(4, 4);
printf("%lf %f %llf", a, b, c);
return 0;
}
输出如下
4.000000 27.000000 256.000000
o.平方根sqrt
有sqrtf sqrtl的形式
#include
#include
int main(void)
{
double a = sqrt(3);
float b = sqrtf(3);
long double c = sqrtl(3);
printf("%lf %f %llf", a, b, c);
return 0;
}
输出为
1.732051 1.732051 1.732051
p.立方根cbrt
有cbrtf cbrtl的形式
#include
#include
int main(void)
{
double a = cbrt(3);
float b = cbrtf(3);
long double c = cbrtl(3);
printf("%lf %f %llf", a, b, c);
return 0;
}
输出为:
1.442250 1.442250 1.442250
q.绝对值fabs
有fabsf fabsl的形式
#include
#include
int main(void)
{
double a = fabs(-3);
float b = fabsf(-3);
long double c = fabsl(-3);
printf("%lf %f %llf", a, b, c);
return 0;
}
输出为
3.000000 3.000000 3.000000
r.已知直角边求斜边hypot
有 hypotf hypotl的形式
#include
#include
int main(void)
{
double a = hypot(3, 4);
float b = hypotf(6, 8);
long double c = hypotl(4, 3);
printf("%lf %f %llf", a, b, c);
return 0;
}
输出如下
5.000000 10.000000 5.000000
s.两数取大小 fmax fmin
有fmaxf fmaxl fminf fminl的形式
#include
#include
int main(void)
{
double a = fmax(1.1, 2.2);
float b = fmaxf(1.1, 2.2);
long double c = fmaxl(1.2, 2.2);
double a1 = fmin(1.1, 2.2);
float b1 = fminf(1.1, 2.2);
long double c1 = fminl(1.2, 2.2);
printf("%lf %f %llf\n", a, b, c);
printf("%lf %f %llf", a1, b1, c1);
return 0;
}
输出如下
2.200000 2.200000 2.200000
1.100000 1.100000 1.200000
2.其余常用函数
a.全排列
int quan(int i)
{
if (i == 1)
{
return 1;
}
else
{
return i * quan(i - 1);
}
}
b.最大公因数 最小公倍数
最大公因数
int gcd(int a, int b)
{
if (a % b == 0)
{
return b;
}
else
{
return gcd(b, a % b);
}
}
最小公倍数(两数之积除以最大公因数)
int gcd(int a, int b)
{
if (a % b == 0)
{
return b;
}
else
{
return gcd(b, a % b);
}
}
int bei(int a, int b)
{
int c;
c = (a * b) / gcd(a, b);
return c;
}
总结:本文内容为偏向初学者的基础性知识,是解决复杂问题的基础性工具,希望本篇文章对你的生活和学习有所帮助。