C++中的绝对值函数

C++中有两个库函数下有对应的求绝对值的函数:

  1. #include<stdlib.h>内,有abs()函数,可以对整型变量求绝对值。示例如下:
    #include<iostream>
    #include<stdlib.h>
    using namespace std;
    int main()
    {
        int a=-1;
        cout<<abs(a);
        return 0;
    }

    输出即为a的绝对值1。

  2. #include<math.h>内,有fabs()函数,可以对浮点型变量求绝对值。示例如下:
    #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
        int a=-1;
        cout<<fabs(a);
        return 0;
    }

     

posted @ 2020-02-12 19:40  一笑奈何DF  阅读(26993)  评论(0编辑  收藏  举报
作者:一笑奈何DF
出处:https://home.cnblogs.com/u/ManbaDF99/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。