0
点赞
收藏
分享

微信扫一扫

C/C++库函数math用法案例篇一


文章目录

  • ​​1、绝对值函数(abs,fabs,labs)​​
  • ​​2、三角函数(cos,sin,tan)​​
  • ​​3、反三角函数(acos,asin,atan)​​
  • ​​4、指数和对数函数(exp,log,log10)​​
  • ​​5、幂指数和开方函数(pow,sqrt)​​

1、绝对值函数(abs,fabs,labs)

  • abs(X)返回整数X的绝对值

#include <stdio.h>
#include <math.h>
void main()
{
int X;
printf("请输入一个负整数:\n");
scanf("%d", &X);
printf("%d的绝对值= %d", X, abs(X));
}

  • fabs(X)返回浮点数X的绝对值

#include <stdio.h>
#include <math.h>
void main()
{
double X;
printf("请输入一个负小数:\n");
scanf("%lf", &X);
printf("%lf的绝对值= %lf", X, fabs(X));
}

  • labs(X)返回长整型数X的绝对值

#include <stdio.h>
#include <math.h>
void main()
{
long X;
printf("请输入一个负长整型数:\n");
scanf("%ld", &X);
printf("%ld的绝对值= %ld", X, labs(X));
}

2、三角函数(cos,sin,tan)

  • cos余弦函数

#include <stdio.h>
#include <math.h>
#define
void main()
{
double angle, cc;
angle= 60.0; //60度
cc= cos(angle*PI/180);
printf("X=%lf, cosX=%lf\n", angle, cc);
angle= 120; //120度
cc= cos(angle*PI/180);
printf("Y=%lf, cosY=%lf", angle, cc);
}

  • sin正弦函数

#include <stdio.h>
#include <math.h>
#define
void main()
{
double angle, cc;
angle= 30.0; //30度
cc= sin(angle*PI/180);
printf("X=%lf, cosX=%lf\n", angle, cc);
angle= 330; //-30度
cc= sin(angle*PI/180);
printf("Y=%lf, cosY=%lf", angle, cc);
}

  • tan正切函数

#include <stdio.h>
#include <math.h>
#define
void main()
{
double angle;
angle= -PI/4;
while(angle<PI/2)
{
printf("X=%lf, tanX=%lf \n", angle, tan(angle));
angle+=PI/8;
}
}

3、反三角函数(acos,asin,atan)

  • acos即arcos(),反余弦函数

#include <stdio.h>
#include <math.h>
void main()
{
double X= -1.0;
while(X<=1.0)
{
printf("arcos(%-6.3lf)=%.7f\n", X, acos(X));
X+=0.4;
}
}

  • asin即arcsin(),反正弦函数

#include <stdio.h>
#include <math.h>
void main()
{
double X= -1.0;
while(X<=1.0)
{
printf("arcsin(%-6.3lf)=%.7f\n", X, asin(X));
X+=0.4;
}
}

  • atan即为arctan(),反正切函数

#include <stdio.h>
#include <math.h>
void main()
{
double X= -2.0;
while(X<=2.0)
{
printf("arctan(%-6.3lf)=%.7f\n", X, atan(X));
X+=0.5;
}
}

4、指数和对数函数(exp,log,log10)

  • exp(X)求以自然对数e为底,X为指数的结果

#include <stdio.h>
#include <math.h>
void main()
{
double X, R;
printf("请输入指数:\n");
scanf("%lf", &X);
R= exp(X);
printf("e的%lf次方=%lf\n", X, R);
}

  • log(X)以自然对数e为底,X为真值的对数值

#include <stdio.h>
#include <math.h>
void main()
{
double X, R;
printf("请输入真值:\n");
scanf("%lf", &X);
R= log(X);
printf("ln(%lf)= %lf\n", X, R);
}

  • log10(X)是以10为底,X为真值的对数值

#include <stdio.h>
#include <math.h>
void main()
{
double X, R;
printf("请输入真值:\n");
scanf("%lf", &X);
R= log10(X);
printf("log10(%lf)= %lf\n", X, R);
}

5、幂指数和开方函数(pow,sqrt)

  • pow(X,Y)求X的Y次方的结果

#include <stdio.h>
#include <math.h>
void main()
{
double X, Y, R;
printf("请输入底数:\n");
scanf("%lf", &X);
printf("请输入指数:\n");
scanf("%lf", &Y);
R= pow(X, Y);
printf("%lf的%lf次方是%lf", X, Y, R);
}

  • sqrt(X)求根号下X的结果,也就是对X开方

#include <stdio.h>
#include <math.h>
void main()
{
double X, R;
printf("请输入需要开方的数:\n");
scanf("%lf", &X);
R= sqrt(X);
printf("%lf的开方值= %lf", X, R);
}


举报

相关推荐

0 条评论