转自:

C/C++ 取整函数ceil(),floor()

C/C++ 取整函数ceil(),floor()

#include <math.h> double floor(double x);

float floorf(float x);
long double floorl(long double x);

double floor(double x);

double ceil(double x);

使用floor函数。floor(x)返回的是小于或等于x的最大整数。
如:     floor(10.5) == 10    floor(-10.5) == -11

使用ceil函数。ceil(x)返回的是大于x的最小整数。
如:     ceil(10.5) == 11    ceil(-10.5) ==-10

floor()是向负无穷大舍入,floor(-10.5) == -11;
ceil()是向正无穷大舍入,ceil(-10.5) == -10

不得不说点:/ % 四舍五入 向上取整(ceil()) 向下取整(floor)

1. /

//Test "/"
    cout << "Test \"/\"!" << endl;
    cout << "7   / 2   = " << 7/2 << endl;      //3
    cout << "7   / 2.0 = " << 7/2.0 << endl;    //3.5
    cout << "7.0 / 2   = " << 7.0/2 << endl;    //3.5
    cout << "7.0 / 2.0 = " << 7.0/2.0 << endl; //3.5
    cout << "7   / 3   = " << 7/3 << endl;      //2
    cout << endl;

2. %
    //Test "%"
    cout << "Test \"%\"!" << endl;
    cout << "9   % 3   = " << 9%3 << endl;      //0
    cout << "9   % 4   = " << 9%4 << endl;      //1
    //cout << "9.0 % 3   = " << 9.0%3 << endl;
    //cout << "9   % 3.0 = " << 9%3.0 << endl;
    cout << endl;

3. 四舍五入
    //Test round()
    cout << "Test \"四舍五入\"!" << endl;
    double dRoundA = 1.4;
    double dRoundB = 1.6;
    double dRoundLowA = -1.4;
    double dRoundLowB = -1.6;
    double dRoundLowC = 0.0;
    cout << dRoundA << " = " << RoundEx(dRoundA) << endl;         //1
    cout << dRoundB << " = " << RoundEx(dRoundB) << endl;         //2
    cout << dRoundLowA << " = " << RoundEx(dRoundLowA) << endl;   //-1
    cout << dRoundLowB << " = " << RoundEx(dRoundLowB) << endl;   //-2
    cout << dRoundLowC << " = " << RoundEx(dRoundLowC) << endl;   //0
    cout << endl;

double RoundEx(const double& dInput)
{
    double dIn = dInput;
    if (dInput >= 0.0)    //???
    {
        return int(dIn + 0.5);
    } 
    else
    {
        return int(dIn - 0.5);
    }
}

4. ceil() 向上取整
    //Test ceil() 向上取整
    cout << "Test ceil() 向上取整!" << endl; 
    cout << "ceil 1.2 = " << ceil(1.2) << endl;      //2
    cout << "ceil 1.8 = " << ceil(1.8) << endl;      //2
    cout << "ceil -1.2 = " << ceil(-1.2) << endl;    //-1
    cout << "ceil -1.8 = " << ceil(-1.8) << endl;    //-1
    cout << "ceil 0.0 = " << ceil(0.0) << endl;      //0
    cout << endl;

5. floor() 向下取整
    //Test floor() 向下取整
    cout << "Test floor() 向下取整!" << endl;
    cout << "floor 1.2 = " << floor(1.2) << endl;    //1
    cout << "floor 1.8 = " << floor(1.8) << endl;    //1
    cout << "floor -1.2 = " << floor(-1.2) << endl; //-2
    cout << "floor -1.8 = " << floor(-1.8) << endl; //-2
    cout << "floor 0.0 = " << floor(0.0) << endl;    //0
    cout << endl;

16.1.21:标红的部分需要记住,便于理解记忆

C/C++ 取整函数ceil(),floor(),向上取整,向下取整相关推荐

  1. php 取整 ceil,PHP取整函数:ceil,floor,round,intval有什么不同

    PHP取整函数:ceil,floor,round,intval有什么不同 发布时间:2021-02-13 09:03:11 来源:亿速云 阅读:76 作者:Leah PHP取整函数:ceil,floo ...

  2. c 语言浮点数向上取整,(转)C/C++ 取整函数ceil(),floor(),向上取整,向下取整...

    #include  double floor(doublex); float floorf(floatx); long double floorl(long doublex); double floo ...

  3. C/C++取整函数ceil(向上取整)和floor(向下取整)

    向上取整: ceil(1.2) = 2 ceil(0.0) = 0 ceil(-1.2) = -1 向下取整: floor(1.2) = 1 floor(0.0) = 0 floor(-1.2) = ...

  4. php坐标轴取整,PHP取整函数:ceil,floor,round,intval的区别详细解析

    我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. ceil -- 进一法取整说明float ceil ( float value ) 返回不小于 value 的下 ...

  5. php 上取整函数是,PHP取整函数:ceil,floor,round,intval的区别详细解析

    搜索热词 PHP取整函数,主要是:ceil,floor,round,intval. 说明float ceil ( float value ) 返回不小于 value 的下一个整数,value 如果有小 ...

  6. php 取整函数 ceil floor round intval 随笔

    ceil() 进一法取整 echo ceil(4.5);// 5 //float类型 floor() 去一法取整 echo floor(4.5);// 4 //float类型 round(float ...

  7. C++取整函数ceil()floor()fix()round()

    这几个都是函数是标准库里的函数,例如: //fix,朝零方向取整 fix(-1.3)=-1; fix(1.3)=1; //floor,朝负无穷方向取整 loor(-1.3)=-2; floor(1.3 ...

  8. C/C++ 取整函数 ceil()、floor()、trunc()

    向上取整函数 ceil() 向下取整函数 floor() 舍尾取整函数 trunc() 这三个函数都在头文件 math.h 中 floor(x)返回的是小于或等于x的最大整数. ceil(x)返回的是 ...

  9. php 取整 ceil,php取整函数ceil、floor、round、intval用法区别

    本节内容: php取整函数ceil.floor.round.intval 在php编程中,遇到数据处理取整时,有以上四个函数可供选择. 1.ceil - 进一法取整 说明 float ceil ( f ...

  10. ceiling php,php取整函数ceil

    php取整函数ceil,floor,round,intval函数的区别 开发过程中,遇到数据处理取整的时候,你会用哪个呢,小涛来介绍一下:PHP取整函数有ceil,floor,round,intval ...

最新文章

  1. 如何在树莓派上进行python编程_设置并使用树莓派进行Python和C语言编程 (下)
  2. js调试之console.log()
  3. TF之data_format:data_format中的NHWCNCHW简介、转换的详细攻略
  4. iOS开发之ReplayKit框架学习
  5. 交互系统的构建之(一)重写Makefile编译TLD系统
  6. encode_chunked=req.has_header(‘Transfer-encoding‘))问题解决方法
  7. 进入大厂的面试经验详细总结(P7 拿 offer)
  8. Openstack学习笔记(十五)-Horizon源代码学习笔记(五)
  9. ipp for windows下载与安装 vs2017
  10. FreeSWITCH(二) - 安装部署、试用
  11. 汽车零部件开发工具OSEK NM协议栈源代码及配置功能
  12. ODOO_posbox_打印出的小票(收据)如何修改?
  13. 白细胞直方图C语言,白细胞三分群及其直方图
  14. Linux 之四 Ubuntu 20.04 WiFi 无法使用、设置无法显示、远程桌面、常用快捷键、SSH、Git、PPA、FFmpeg 等各问题记录
  15. 模拟位置 定位 钉钉打卡 运动轨迹 MD
  16. 网络性能指标及测试方法
  17. windows 10远程连接ubuntu 18.04 Gnome桌面:NoMachine工具使用详解
  18. html 截取指定字符串长度,前端js截取指定长度个数字符 v2.0.0
  19. python中的if判断,和while循环的用法
  20. 联想笔记本如何关闭Fn功能键

热门文章

  1. linux测试进程内存,怎么测试进程运行时的内存用量
  2. data spring 指定时区_听说过spring-data-jdbc么?来个最佳实践
  3. @data注解_SpringBoot入门实践(七)-Spring-Data-JPA实现数据访问
  4. 通过表名导出DDL语句(包含建表、索引、注释、主键)
  5. (day 36 - 滑动窗口)剑指 Offer 57 - II. 和为s的连续正数序列
  6. 原生mysql 怎么创表_Mysql的基础使用之SQL原生语句的使用:表的 创建 删除 修改 (一)...
  7. java 多项式拟合最多的项数_Matlab polyfit 详解 | 方程组求解的稳定性 | 条件数
  8. HighCharts:plotLines基准线与数据相差过大不显示
  9. SQL:postgresql中实现查询某字段总数量和该字段不同值各自的数量
  10. Pannellum:实例之通过全景图游览