C++最接近整数的浮点运算
2024-10-20 08:40:57
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
最新文章
- 如何删除选中的checkbox
- Oracle Segments可以跨多个data files吗?
- 2013年ACM湖南省赛总结
- 动态input file多文件上传到后台没反应的解决方法!!!
- iOS NSString中字符串的删除,替换
- Oracle数据库如何授权收费(Database Licensing)
- 转载:SQL Server高效 -- 设计(ITPUT 讨论汇总
- DWR入门实例(二)
- Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)
- java生成PDF文件(itext)
- 《JAVA程序设计与实例》记录与归纳--继承与多态
- 嵌入式LINUX环境下视频采集知识
- 【斐波那契数列】java探究
- 带着新人学springboot的应用13(springboot+热部署)
- Luogu 2540 斗地主增强版(搜索,动态规划)
- GNU make使用(二)
- SUSE_LINUX 11 SP3 安装 IBM MQ 7.5
- 数学的语言 化无形为可见 (Keith Devlin 著)
- Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误
- python将控制台输出保存至文件
热门文章
- mysql主从复制报错 :Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
- 对HTML的大致了解
- Personal Geodatabase - Can&#39;t Create New or Open Existing
- java 将long类型的数值转无符号数
- log4j2单独的配置与使用&;log4j2+slf4j的结合的配置与使用
- file中mkdirs和mkdir的区别-文件上传
- Microsoft Translator:打破语言障碍 拓展全球沟通新机遇
- 缓存溢出Buffer Overflow
- 1.windows下GIT 服务安装
- Oracle 查看session级别信息