C++最接近整数的浮点运算
| Function | return |
|---|---|
| ceil | 不小于给定值的最接近整数值 |
| floor | 不大于给定值的最接近整数 |
| trunc (C++11) | 绝对值不大于给定值的最接近整数 |
| round(C++11) | 最接近整数,中间情况下舍入到远离零 |
| lround(C++11) | 最接近整数,中间情况下舍入到远离零 |
| llround (C++11) | 最接近整数,中间情况下舍入到远离零 |
1.ceil–向上取整
/*
函数原型
float ceil(float arg);(1)
double ceil(double arg);(2)
long double ceil(long double arg);(3)
double ceil(Integral arg);(4) (C++11 起)
*/
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << fixed
<< "ceil(+2.4) = " << ceil(+2.4) << '\n'
<< "ceil(-2.4) = " << ceil(-2.4) << '\n'
<< "ceil(-0.0) = " << ceil(-0.0) << '\n'
<< "ceil(-Inf) = " << ceil(-INFINITY) << '\n';
}
输出:
ceil(+2.4) = 3.000000
ceil(-2.4) = -2.000000
ceil(-0.0) = -0.000000
ceil(-Inf) = -INF
2.floor–向下取整
/*函数原型
float floor( float arg );(1)
double floor( double arg );(2)
long double floor( long double arg );(3)
double floor( Integral arg );(4) (C++11 起)
*/
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << fixed
<< "floor(+2.7) = " <<floor(+2.7) << '\n'
<< "floor(-2.7) = " << floor(-2.7) << '\n'
<< "floor(-0.0) = " << floor(-0.0) << '\n'
<< "floor(-Inf) = " << floor(-INFINITY) << '\n';
}
输出:
floor(+2.7) = 2.000000
floor(-2.7) = -3.000000
floor(-0.0) = -0.000000
floor(-Inf) = -inf
3.trunc—保留整数部分
//函数原型
float trunc( float arg );
double trunc( double arg );
long double trunc( long double arg );
double trunc( Integral arg );
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << fixed
<< "trunc(+2.7) = " << trunc(+2.7) << '\n'
<< "trunc(-2.9) = " << trunc(-2.9) << '\n'
<< "trunc(-0.0) = " << strunc(-0.0) << '\n'
<< "trunc(-Inf) = " << strunc(-INFINITY) << '\n';
}
可能的输出:
trunc(+2.7) = 2.000000
trunc(-2.9) = -2.000000
trunc(-0.0) = -0.000000
trunc(-Inf) = -inf
4.round,lround,llround–四舍五入
函数原型:
float round( float arg );
double round( double arg );
long double round( long double arg );
double round( Integral arg );
long lround( float arg );
long lround( double arg );
long lround( long double arg );
long lround( Integral arg );
long long llround( float arg );
long long llround( double arg );
long long llround( long double arg );
long long llround( Integral arg );
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// round
cout << "round(+2.3) = " << round(2.3)
<< " round(+2.5) = " << round(2.5)
<< " round(+2.7) = " << round(2.7) << endl
<< "round(-2.3) = " << round(-2.3)
<< " round(-2.5) = " << round(-2.5)
<< " round(-2.7) = " << round(-2.7) << endl;
cout << "round(-0.0) = " << round(-0.0) << endl
<< "round(-Inf) = " << round(-INFINITY) << endl;
// lround
cout << "lround(+2.3) = " << lround(2.3)
<< " lround(+2.5) = " << lround(2.5)
<< " lround(+2.7) = " << lround(2.7) << endl
<< "lround(-2.3) = " << lround(-2.3)
<< " lround(-2.5) = " << lround(-2.5)
<< " lround(-2.7) = " << lround(-2.7) << endl;
cout << "lround(-0.0) = " << lround(-0.0) << endl
<< "lround(-Inf) = " << lround(-INFINITY) << endl;
return 0;
}
输出
round(+2.3) = 2 round(+2.5) = 3 round(+2.7) = 3
round(-2.3) = -2 round(-2.5) = -3 round(-2.7) = -3
round(-0.0) = -0
round(-Inf) = -inf
lround(+2.3) = 2 lround(+2.5) = 3 lround(+2.7) = 3
lround(-2.3) = -2 lround(-2.5) = -3 lround(-2.7) = -3
lround(-0.0) = 0
lround(-Inf) = 0
C++最接近整数的浮点运算的更多相关文章
- Delphi代码优化
文章编目 1. 字符串优化 1.1. 不重复初始化 1.2. 使用SetLength预分配长字符串(AnsiString) 1.3. 字符串与动态数组的线程安全(Thread Safety) 1.4. ...
- MCS-51单片机实用子程序库
目前已有若干版本的子程序库公开发表,它们各有特色.本程序库中的开平方算法为快速逼近算法,它能达到牛顿迭代法同样的精度,而速度加快二十倍左右,超过双字节定点除法的速度. 本子程序库对<单片机应用程 ...
- 《深入理解JAVA虚拟机》笔记1
java程序运行时的内存空间,按照虚拟机规范有下面几项: )程序计数器 指示下条命令执行地址.当然是线程私有,不然线程怎么能并行的起来. 不重要,占内存很小,忽略不计. )方法区 这个名字很让我迷惑. ...
- Azure机器学习入门(一)
我们开始深入学习Azure机器学习的基本原理并为您开启伟大的数据科学之门.Azure 机器学习的一个重要特征就是在构建预测分析方案时,它能够方便地将开发模式集成为可重复的工作流模式.这就使得Azure ...
- 优化C/C++代码的小技巧
说明: 无意看到一篇小短文,猜测作者应该是一个图形学领域的程序员或专家,介绍了在光线(射线)追踪程序中是如何优化C/C++代码的.倒也有一些参考意义,当然有的地方我并不赞同或者说我也不完全理解,原文在 ...
- [Effective JavaScript 笔记] 第1章:让自己习惯javascript小结
在这里整理一下,每条对应的提示 第1条:了解使用的js版本 确定应用程序支持的js的版本(浏览器也是应用程序噢) 确保使用的js特性是应用程序支持的(要不写了也运行不了) 总是在严格模式下编写和测试代 ...
- Python 向上取整的算法
一.初衷: 有时候我们分页展示数据的时候,需要计算页数.一般都是向上取整,例如counts=205 pageCouts=20 ,pages= 11 页. 一般的除法只是取整数部分,达不到要求. 二.方 ...
- C/C++常用头文件及函数汇总
转自: C/C++常用头文件及函数汇总 C/C++头文件一览 C #include <assert.h> //设定插入点#include <ctype.h> //字符处理#in ...
- Shell脚本编程的常识
(这些往往是经常用到,但是各种网络上的材料都语焉不详的东西,个人认为比较有用) 七种文件类型 d 目录 ...
随机推荐
- 【学习笔记】HTML基础:列表、表格与媒体元素
一.列表是信息资源的一种展现形式,它可以使信息结构化和条理化,并以列表的样式显示出来,以便浏览者能够快速的获取相应的信息. 1.无需列表 <ul> <li>第一项</li ...
- FTP FileZilla Server 本地加密C# 实现
最近公司要做一个资料管理模块,因系统是C/S架构,原来小文件都是直接使用7Z压缩后保存到SQL Server数据库 而资料管理模块也就是文件上传,下载加权限管理,考虑文件较多,还可能比较大,所以打算在 ...
- CSS Hack兼容
CSS中有很多标签在不同浏览器中有不同的兼容性问题,问了让网页的功能更好的不同浏览器中显示正常, 需要通过hack的方式来解决浏览器兼容问题. CSS hack问题由来已久,目前我的了解甚少,以下是转 ...
- 日期函数new Date()浏览器兼容性问题
项目上与时间相关的地方特别多,与时间格式相关都使用了moment.js轻量级日期处理库,在开发中出现了几次浏览器兼容性问题,所以总结一下new Date()和moment.js在各大浏览器中兼容性问题 ...
- ArcGIS Enterprise 10.5.1 静默安装部署记录(Centos 7.2 minimal)- 5、安装Datastore
安装Datastore 解压datastore安装包,tar -xzvf ArcGIS_DataStore_Linux_1051_156441.tar.gz 切换到arcgis账户静默安装server ...
- python递归锁与信号量
递归锁 一把大锁在加一把小锁. import threading import time def run1(): print("grab the first part data") ...
- Java Web 常用在线api汇总(不定时更新)
1.Hibernate API Documentation (3.2.2.ga) http://www.hibernate.org/hib_docs/v3/api/ 2.Spring Framewor ...
- python实现oracle数据泵导出功能
脚本如下:[oracle@ycr python]$ more dump.py #/usr/bin/python#coding:utf8 import sysimport osimport time n ...
- 每天一个linux命令:du 命令
Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...
- Android(java)学习笔记54:Android 调用Vibrator震动功能
1. 之前我编写的代码是如下: package com.himi.vibrate; import android.app.Activity; import android.app.Service; i ...