命名空间
变体
操作

std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl, std::llround, std::llroundf

来自 cppreference.cn
< cpp‎ | 数值‎ | 数学
 
 
 
常用数学函数
函数
基本操作
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数函数
(C++11)
(C++11)

(C++11)
(C++11)
幂函数
(C++11)
(C++11)
三角
双曲函数
(C++11)
(C++11)
(C++11)

误差函数和伽马函数
(C++11)
(C++11)
(C++11)
(C++11)
取整浮点运算
roundlroundllround
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮点操纵函数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分类和比较
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型
(C++11)
(C++11)
(C++11)
宏常量
分类
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定义于头文件 <cmath>
舍入到浮点类型
(1)
float       round ( float num );

double      round ( double num );

long double round ( long double num );
(C++11 起)
(直至 C++23)
constexpr /* 浮点类型 */
            round ( /* 浮点类型 */ num );
(C++23 起)
float       roundf( float num );
(2) (C++11 起)
(C++23 起为 constexpr)
long double roundl( long double num );
(3) (C++11 起)
(C++23 起为 constexpr)
舍入到 long
(4)
long lround ( float num );

long lround ( double num );

long lround ( long double num );
(C++11 起)
(直至 C++23)
constexpr long lround( /* 浮点类型 */ num );
(C++23 起)
long lroundf( float num );
(5) (C++11 起)
(C++23 起为 constexpr)
long lroundl( long double num );
(6) (C++11 起)
(C++23 起为 constexpr)
舍入到 long long
(7)
long long llround ( float num );

long long llround ( double num );

long long llround ( long double num );
(C++11 起)
(直至 C++23)
constexpr long long llround( /* 浮点类型 */ num );
(C++23 起)
long long llroundf( float num );
(8) (C++11 起)
(C++23 起为 constexpr)
long long llroundl( long double num );
(9) (C++11 起)
(C++23 起为 constexpr)
定义于头文件 <cmath>
template< class Integer >
double round( Integer num );
(A) (C++11 起)
(C++23 起为 constexpr)
template< class Integer >
long lround( Integer num );
(B) (C++11 起)
(C++23 起为 constexpr)
template< class Integer >
long long llround( Integer num );
(C) (C++11 起)
(C++23 起为 constexpr)
1-3) 计算最接近 num 的整数值(浮点格式),半值远离零舍入,而无论当前舍入模式如何。 库为所有 cv-不限定浮点类型提供了 std::round 的重载作为参数 num 的类型。(C++23 起)
4-9) 计算最接近 num 的整数值(整数格式),半值远离零舍入,而无论当前舍入模式如何。 库为所有 cv-不限定浮点类型提供了 std::lroundstd::llround 的重载作为参数 num 的类型。(C++23 起)
A-C) 为所有整数类型提供额外重载,它们被当作 double 处理。

目录

[编辑] 参数

num - 浮点值或整数值

[编辑] 返回值

若无错误发生,返回最接近 num 的整数值,半值远离零舍入。

返回值
math-round away zero.svg
num

若发生定义域错误,返回实现定义的值。

[编辑] 错误处理

错误按 math_errhandling 指定的方式报告。

std::lroundstd::llround 的结果超出返回类型可表示的范围,可能发生定义域错误或范围错误。

如果实现支持 IEEE 浮点运算 (IEC 60559),

对于 std::round 函数
  • 当前 舍入模式 无效。
  • num 为 ±∞,则返回它,不修改。
  • num 为 ±0,则返回它,不修改。
  • num 为 NaN,则返回 NaN。
对于 std::lroundstd::llround 函数
  • 永不引发 FE_INEXACT
  • 当前 舍入模式 无效。
  • num 为 ±∞,则引发 FE_INVALID 并返回实现定义的值。
  • 若舍入结果超出返回类型的范围,则引发 FE_INVALID 并返回实现定义的值。
  • num 为 NaN,则引发 FE_INVALID 并返回实现定义的值。

[编辑] 注释

当舍入非整数有限值时,std::round 可能会(但非必须)引发 FE_INEXACT

所有标准浮点格式中,最大的可表示浮点值都是精确整数,所以 std::round 本身从不溢出;然而,结果在存储到整数变量时可能溢出任何整数类型(包括 std::intmax_t)。

POSIX 指定 std::lroundstd::llround 引发 FE_INEXACT 的所有情况都是定义域错误。

std::rounddouble 版本行为如同以下方式实现

#include <cfenv>
#include <cmath>
 
#pragma STDC FENV_ACCESS ON
 
double round(double x)
{
    const int save_round = std::fegetround();
    std::fesetround(FE_TOWARDZERO);
    const double result = std::rint(std::copysign(0.5 + std::fabs(x), x));
    std::fesetround(save_round);
    return result;
}

额外重载无需完全按照 (A-C) 提供。它们只需足以确保对于其整数类型的参数 num

  • std::round(num) 具有与 std::round(static_cast<double>(num)) 相同的效果。
  • std::lround(num) 具有与 std::lround(static_cast<double>(num)) 相同的效果。
  • std::llround(num) 具有与 std::llround(static_cast<double>(num)) 相同的效果。

[编辑] 示例

#include <cassert>
#include <cfenv>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iostream>
 
// #pragma STDC FENV_ACCESS ON
 
double custom_round(double x)
{
    const int save_round = std::fegetround();
    std::fesetround(FE_TOWARDZERO);
    const double result = std::rint(std::copysign(0.5 + std::fabs(x), x));
    std::fesetround(save_round);
    return result;
}
 
void test_custom_round()
{
    for (const double x :
        {
            0.0, 0.3,
            0.5 - DBL_EPSILON / 2,
            0.5,
            0.5 + DBL_EPSILON / 2,
            0.7, 1.0, 2.3, 2.5, 2.7, 3.0,
            static_cast<double>(INFINITY)
        })
        assert(round(+x) == custom_round(+x) && round(-x) == custom_round(-x));
}
 
int main()
{
    test_custom_round();
 
    std::cout << std::showpos;
 
    // round
    std::cout << "round(+2.3) = " << std::round(2.3)
              << "  round(+2.5) = " << std::round(2.5)
              << "  round(+2.7) = " << std::round(2.7) << '\n'
              << "round(-2.3) = " << std::round(-2.3)
              << "  round(-2.5) = " << std::round(-2.5)
              << "  round(-2.7) = " << std::round(-2.7) << '\n';
 
    std::cout << "round(-0.0) = " << std::round(-0.0)  << '\n'
              << "round(-Inf) = " << std::round(-INFINITY) << '\n';
 
    // lround
    std::cout << "lround(+2.3) = " << std::lround(2.3)
              << "  lround(+2.5) = " << std::lround(2.5)
              << "  lround(+2.7) = " << std::lround(2.7) << '\n'
              << "lround(-2.3) = " << std::lround(-2.3)
              << "  lround(-2.5) = " << std::lround(-2.5)
              << "  lround(-2.7) = " << std::lround(-2.7) << '\n';
 
    std::cout << "lround(-0.0) = " << std::lround(-0.0)  << '\n'
              << "lround(-Inf) = " << std::lround(-INFINITY) << '\n';
 
    // error handling
    std::feclearexcept(FE_ALL_EXCEPT);
 
    std::cout << "std::lround(LONG_MAX+1.5) = "
              << std::lround(LONG_MAX + 1.5) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID was raised\n";
}

可能的输出

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) = -9223372036854775808
std::lround(LONG_MAX+1.5) = -9223372036854775808
    FE_INVALID was raised

[编辑] 参阅

(C++11)(C++11)
不大于给定值的最接近整数
(函数) [编辑]
(C++11)(C++11)
不小于给定值的最接近整数
(函数) [编辑]
(C++11)(C++11)(C++11)
不大于给定值幅度的最接近整数
(函数) [编辑]
C 文档 关于 round