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 目录 ...
随机推荐
- 1229:密码截获----java
题目描述 Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码 进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解.比 ...
- python搭建本地服务器
python搭建本地服务器 python3以上版本 'python3 -m http.server 8000' 默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:800 ...
- struts2返回结果类型
在action下还有result标签 1.result不只有name,其实还有type result返回类型在struts-default.xml默认的配置文件中有定义,可以看到有result-typ ...
- angular2-动画
使用动画: import { Component, Input } from '@angular/core'; import { trigger, state, style, animate, tra ...
- Iscrool下拉刷新
简易下拉刷新 css样式: *{ margin: 0px; padding: 0px; } #wrapper{ width: 100%; height: 150px; border: 1px soli ...
- iOS 在Host App 与 App Extension 之间发送通知
如何从你的一个App发送通知给另一个App? (例:搜狗输入法下载皮肤完成后使用皮肤) 注:搜狗输入法是App.而键盘是Extension 当你为你的App 添加 App Extension时,如果想 ...
- Android 环信(Android)设置头像和昵称的方法
最近,经常有朋友问到,如何集成环信头像,怎么才能快速显示头像,因时间紧急,很多朋友都没有时间慢慢的研究代码,这里大家稍微花10分钟看一下文章,看完后再花5分钟改一下代码,即可达到你们所要的效果. 当然 ...
- 64位MATLAB和C混合编程以及联合调试
[环境说明] 电脑操作系统:win 7 旗舰版,64位 MATLAB版本:R2013b VS版本:Microsoft Visual Studio 2010 [操作说明] 1.在原来工程的基础上添加下列 ...
- Java—集合框架Set
Set接口及其实现类——HashSet Set是元素无序并且不可以重复的集合,被称作集. HashSet—哈希集,是Set的一个重要实现类. Set的使用 HashSet没有像List一样的set ...
- C/S与B/S架构的区别和优缺点
C/S 架构的概念 C/S是Client/Server,即客户端/服务器端架构,一种典型的两层架构. 客户端包含一个或多个在用户的电脑上运行的程序 服务器端有两种,一种是数据库服务器端,客户端通过数据 ...