编程基础——math数学库函数

编程——math函数应用

数学库头文件:  #include<cmath>
  • 求绝对值: abs(int num) fabs(double num);
#include<iostream>
#include<cmath>
using namespace std;

int main(){
    
    int a=2;
    int num1=abs(a);
    cout<<num1<<endl;        //2

    double b=-3.7;
    double num2=fabs(b);
    cout<<num2;              //3.7

    return 0;
}
  • 求次方幂: pow(a,b) 即a^b
#include<iostream>
#include<cmath>
using namespace std;

int main(){
    cout<<pow(2,3,7);
   return 0;
}
  • 求根下
    法一: sqrt(a) 开平方根
    法二: pow(a,b) 求任意次幂
#include<iostream>
#include<cmath>
using namespace std;

int main(){

    int a=3;
    int num1=sqrt(a);
    cout<<num1<<endl;              //1

    double b=3.7;
    double num2=sqrt(b);
    cout<<num2<<endl;              //1.92354

    int c=3;
    cout<<pow(c,1.0/3)<<endl;      //1.44225  3次根下3

    double d=2.5;
    double e=1.0;
    cout<<pow(d,e/6);              //1.16499  6次根下2.5

    return 0;
}
  • 取指数、对数:
    e的指数: exp(参数)
    取ln对数: log(参数)
#include<iostream>
#include<cmath>
using namespace std;

int main(){

    int a=3;
    double num1=exp(a);
    double num11=log(a);
    cout<<num1<<" "<<num11<<endl;        //20.0855 1.09861

    double b=3.7;
    double num2=exp(b);
    double num22=log(b);
    cout<<num2<<" "<<num22<<endl;        //40.4473 1.30833

    return 0;
}
  • 天花板、地板函数
    天花板函数:大于等于x的最小整数 ceil(a)
    地板函数: 小于等于x的最大整数 floor(a)
#include<iostream>
#include<cmath>
using namespace std;

int main(){

    double a=3.3;
    cout<<ceil(a)<<endl;    //4
    cout<<floor(a);         //3

    return 0;
}
  • 四舍五入:round(double num),返回double类型数据,需要进行强制转换类型为int
#include<cstdio>
#include<cmath>
int main(){
   double num=round(3.14159);
   printf("%d", (int)num);     //3
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值