博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++最接近整数的浮点运算
阅读量:5237 次
发布时间:2019-06-14

本文共 3539 字,大约阅读时间需要 11 分钟。

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 
#include
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.000000ceil(-2.4) = -2.000000ceil(-0.0) = -0.000000ceil(-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 
#include
using namespace std;int main(){ cout << fixed << "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.000000floor(-2.7) = -3.000000floor(-0.0) = -0.000000floor(-Inf) = -inf

3.trunc—保留整数部分

//函数原型float       trunc( float arg );double      trunc( double arg );long double trunc( long double arg );double      trunc( Integral arg );
#include 
#include
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.000000trunc(-2.9) = -2.000000trunc(-0.0) = -0.000000trunc(-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 
#include
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) = 3round(-2.3) = -2  round(-2.5) = -3  round(-2.7) = -3round(-0.0) = -0round(-Inf) = -inflround(+2.3) = 2  lround(+2.5) = 3  lround(+2.7) = 3lround(-2.3) = -2  lround(-2.5) = -3  lround(-2.7) = -3lround(-0.0) = 0lround(-Inf) = 0

转载于:https://www.cnblogs.com/FlyerBird/p/9052547.html

你可能感兴趣的文章
【C#】【Thread】Monitor和Lock
查看>>
UVALive - 3635 - Pie(二分)
查看>>
集合类List,set,Map 的遍历方法,用法和区别
查看>>
Scala入门系列(十):函数式编程之集合操作
查看>>
pulseaudio的交叉编译
查看>>
Cracking The Coding Interview 1.1
查看>>
vb.net 浏览文件夹读取指定文件夹下的csv文件 并验证,显示错误信息
查看>>
NetworkInterface的使用
查看>>
元素自动居中显示
查看>>
JDBC 时间处理
查看>>
hadopp 环境搭建
查看>>
【2018】听懂你能看懂的句子
查看>>
mybatis源代码分析:深入了解mybatis延迟加载机制
查看>>
Flask三剑客
查看>>
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
【BZOJ3052】【UOJ#58】【WC2013】糖果公园(树上莫队)
查看>>
荷兰国旗问题
查看>>
Process 启动参数问题
查看>>
提高PHP性能的10条建议
查看>>