c++ 取整之ceil、floor、round、fix用法
头文件: #include <cmath>
一、ceil函数
朝上取整。
ceil(-3.14) = -3;
ceil(4.56) = 5;
二、floor函数
朝下取整。
floor(-3.14) = -4;
floor(4.56) = 4;
三、fix函数
朝0取整。
fix(-3.14) = -3;
fix(4.56) = 4;
四、round函数
可理解为:
if x>0 then floor(x+0.5);
if x<0 then ceil(x-0.5);
round(-3.14) = -3;
round(4.56) = 5;
round(11.5) = 12;
round(-11.5) = -12;
五、整除
C/C++中的整数除法运算符“/”本身就有取整功能,整数除法对正数的取整是舍去小数部分。
但是整数除法对负数的取整结果和使用的C编译器有关。
以VS2013编译器为例,其处理整数除法对于正负数均采用舍去小数部分(类似于fix函数)
cout << 7/2 ; // 3
cout << 8/2; // 4
cout << -7/2; // -3
cout << -8/2; // -4
六、余数
上节已经说了整数除法是按照舍去法的,那么余数怎么计算呢?
很简单,被除数 - (除数 * 商)就可以得到了,而且余数的正负性与被除数的正负性相同。
举例如下:
被除数 | 除数 | 商数 | 余数 |
---|---|---|---|
11 | 2 | 5 | 1 |
-11 | 2 | -5 | -1 |
11 | -2 | -5 | 1 |
-11 | -2 | 5 | -1 |
c++ 取整之ceil、floor、round、fix用法相关推荐
- 向上取整和向下取整(ceil、round)
Floor and ceiling functions - Wikipedia Useful Properties of the Floor and Ceil Functions 0. 基础 向上取整 ...
- C++ 多种取整函数的使用和区别: ceil() floor() round() trunc() rint() nearbyint()
取整函数: ceil() 右向取整:数轴上右边最靠近的整数,向大的方向取值:ceil "天花板" floor() 左向取整:数轴上左边最靠近的整数,向小的方向取值:fl ...
- python中math.ceil是什么意思_python中的数字取整(ceil,floor,round)概念和用法
python中的数学运算函数(ceil,floor,round)的主要任务是截掉小数以后的位数.总体来说 就是取整用的.只是三者之间有微妙的区别: floor() :把数字变小 ceil() : 把数 ...
- php坐标轴取整,PHP取整函数:ceil,floor,round,intval的区别详细解析
我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. ceil -- 进一法取整说明float ceil ( float value ) 返回不小于 value 的下 ...
- C++:取整操作ceil()、floor()、round()
取整操作ceil().floor().round() 在数学计算中有三种常用的取整操作:向上取整.向下取整.四舍五入取整,在 C++ 中分别通过 ceil().floor().round() 三个函数 ...
- php 取整 ceil,PHP取整函数:ceil,floor,round,intval有什么不同
PHP取整函数:ceil,floor,round,intval有什么不同 发布时间:2021-02-13 09:03:11 来源:亿速云 阅读:76 作者:Leah PHP取整函数:ceil,floo ...
- php 取整函数 ceil floor round intval 随笔
ceil() 进一法取整 echo ceil(4.5);// 5 //float类型 floor() 去一法取整 echo floor(4.5);// 4 //float类型 round(float ...
- 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) = ...
- SQL 向上取整、向下取整、四舍五入取整的实例!round、rounddown、roundup
sql server ==================================================== [四舍五入取整截取] select round(54.56,0) === ...
最新文章
- 46 jQuery属性操作
- python中整数类型的表示需加引导符号_Python内置类型及常见问题
- ZOJ 1743 Concert Hall Scheduling(DP)
- eclipse python_【Python基础】Python基础语法
- java调用 火眼臻睛,连接创新,发现未来,臻识科技受邀CCF-GAIR,论道浪潮之巅...
- 学习笔记(42):Python实战编程-pyinstaller程序打包
- java this用法_java中this用法小结
- mysql如何给表字段加密_Mysql 字段加密
- 文献检索与论文写作书籍(一)
- linux 多线程超时中断,c#中的线程超时
- python selenium 等待元素_python3 selenium 设置元素等待的三种方法
- Atitit.手机验证码的破解---伪随机数
- samp服务器显示,如何测量我的(SAMP)服务器的带宽使用情况?
- hplus java_Java HijrahDate plus(long, TemporalUnit)用法及代码示例
- 我为什么要选软件工程专业
- jquery-seat-charts 使用-自定义座位号 及 重新加载数据
- jarvis oj typo
- 基于FreeRTOS的物联网智能车库管理系统(可进行车牌识别)
- [ STK ](十一)STK 中搭建铱星星座
- emg采集精度_EMG