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 目录 ...
随机推荐
- URL工具类
UrlUtils = { /** * 判断url是否存在(存在跨域问题) * @param {String} url */ isTrueUrl: function(_url) { result = f ...
- Android中的Service与进程间通信(IPC)详解
Service 什么是Service 在后台长期运行的没有界面的组件.其他组件可以启动Service让他在后台运行,或者绑定Service与它进行交互,甚至实现进程间通信(IPC).例如,可以让服务在 ...
- 【Linux】Vmware&Linux 网络配置
Vmware 网络设置 Linux 网络设置 注:本实验使用Linux 版本为 RedHat 6.3 Vmware 网络设置 1.Vmware 网络工作模式 bridged(桥接模式) -- 网络能提 ...
- ZT 父子进程共享文件描述符
转贴自倒霉熊的博客 [linux学习笔记-2]父子进程共享文件描述符 (2009-03-02 23:03:17) 转载▼ 标签: 学习 linux 子进程 文件描述符 杂谈 分类: 学习 #inclu ...
- 第二次作业(Git and Github)
第二次作业(Git and Github) 1.Github项目地址: https://github.com/YanSiJu/JavaWebProject.git 具体介绍详见READ.md 2 ...
- OC 方法声明使用
Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; } - (void) ...
- hdu-4288 Coder---线段树+离线处理&离散化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4288 题目大意: 维护一个有序数列{An},有三种操作: 1.添加一个元素. 2.删除一个元素. 3 ...
- bzoj1965 [Ahoi2005]洗牌
Description 为了表彰小联为Samuel星球的探险所做出的贡献,小联被邀请参加Samuel星球近距离载人探险活动. 由于Samuel星球相当遥远,科学家们要在飞船中度过相当长的一段时间,小联 ...
- bzoj2004 [Hnoi2010]公交线路
Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距 离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决 ...
- 计算次数,POJ(1207)
题目链接:http://poj.org/problem?id=1207 #include <stdio.h> #include <algorithm> using namesp ...