前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++最接近整数的浮点运算

C++最接近整数的浮点运算

作者头像
Enterprise_
发布2019-02-21 17:17:46
1.2K0
发布2019-02-21 17:17:46
举报
文章被收录于专栏:小L的魔法馆

Function

return

ceil

不小于给定值的最接近整数值

floor

不大于给定值的最接近整数

trunc (C++11)

绝对值不大于给定值的最接近整数

round(C++11)

最接近整数,中间情况下舍入到远离零

lround(C++11)

最接近整数,中间情况下舍入到远离零

llround (C++11)

最接近整数,中间情况下舍入到远离零

1.ceil–向上取整

代码语言:javascript
复制
/*
函数原型
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';
}
代码语言:javascript
复制
输出:

ceil(+2.4) = 3.000000
ceil(-2.4) = -2.000000
ceil(-0.0) = -0.000000
ceil(-Inf) = -INF

2.floor–向下取整

代码语言:javascript
复制
/*函数原型
float       floor( float arg );(1)  
double      floor( double arg );(2) 
long double floor( long double arg );(3)    
double      floor( Integral arg );(4)   (C++11 起)
*/
代码语言:javascript
复制
#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—保留整数部分

代码语言:javascript
复制
//函数原型
float       trunc( float arg );
double      trunc( double arg );
long double trunc( long double arg );
double      trunc( Integral arg );
代码语言:javascript
复制
#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';
}
代码语言:javascript
复制
可能的输出:

trunc(+2.7) = 2.000000
trunc(-2.9) = -2.000000
trunc(-0.0) = -0.000000
trunc(-Inf) = -inf

4.round,lround,llround–四舍五入 函数原型:

代码语言:javascript
复制
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 );
代码语言:javascript
复制
#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;
}

输出

代码语言:javascript
复制
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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年03月20日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档