C 参考手册
- C 语言
- C 关键词
- 预处理器
- C 标准库头文件
- 类型支持
- 程序支持工具
- 变参数函数
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- 算法
- 数值
- 常用数学函数
- MATH_ERRNO, MATH_ERREXCEPT, math_errhandling
- log, logf, logl
- log10, log10f, log10l
- log1p, log1pf, log1pl
- log2, log2f, log2l
- cbrt, cbrtf, cbrtl
- fmax, fmaxf, fmaxl
- fmin, fminf, fminl
- fdim
- NAN
- exp, expf, expl
- exp2, exp2f, exp2l
- expm1, expm1f, expm1l
- fmod, fmodf, fmodl
- remainder, remainderf, remainderl
- remquo, remquof, remquol
- fma, fmaf, fmal
- div, ldiv, lldiv, imaxdiv
- fabs, fabsf, fabsl
- sqrt, sqrtf, sqrtl
- sin, sinf, sinl
- abs, labs, llabs, imaxabs
- hypot, hypotf, hypotl
- pow, powf, powl
- cos, cosf, cosl
- tan, tanf, tanl
- asin, asinf, asinl
- acos, acosf, acosl
- atan, atanf, atanl
- atan2, atan2f, atan2l
- sinh, sinhf, sinhl
- cosh, coshf, coshl
- tanh, tanhf, tanhl
- asinh, asinhf, asinhl
- acosh, acoshf, acoshl
- atanh, atanhf, atanhl
- erf, erff, erfl
- erfc, erfcf, erfcl
- lgamma, lgammaf, lgammal
- tgamma, tgammaf, tgammal
- ceil, ceilf, ceill
- floor, floorf, floorl
- round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl
- trunc, truncf, truncl
- nearbyint, nearbyintf, nearbyintl
- rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl
- ldexp, ldexpf, ldexpl
- scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl
- ilogb, ilogbf, ilogbl
- logb, logbf, logbl
- frexp, frexpf, frexpl
- modf, modff, modfl
- nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl
- copysign, copysignf, copysignl
- fpclassify
- isfinite
- isinf
- isnan
- isnormal
- signbit
- isgreater
- isgreaterequal
- isless
- islessequal
- islessgreater
- isunordered
- float_t, double_t
- HUGE_VALF, HUGE_VAL, HUGE_VALL
- INFINITY
- FP_NORMAL, FP_SUBNORMAL, FP_ZERO, FP_INFINITE, FP_NAN
- 浮点环境
- 伪随机数生成
- 复数算术
- 泛型数学
- 文件输入/输出
- 本地化支持
- 原子操作库
- 线程支持库
- 实验性 C 标准库
- 有用的资源
- 符号索引
- 注释
rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl
若不出现错误,则按照当前舍入模式的最接近整数值。
错误处理
报告 math_errhandling 中指定的错误。
若 lrint
或 llrint
的结果在返回类型的可表示范围外,则可能出现定义域错误或值域错误。
若实现支持 IEEE 浮点算术( IEC 60559 ),则
- 对于
rint
函数:
- 若
arg
为 ±∞ ,则返回不修改的参数 - 若
arg
为 ±0 ,则返回不修改的参数 - 若
arg
为 NaN ,则返回 NaN
- 对于
lrint
和llrint
函数:
- 若
arg
为 ±∞ ,则引发 FE_INVALID 并返回实现定义值 - 若舍入结果在返回类型范围外,则引发 FE_INVALID 并返回实现定义值
- 若
arg
为 NaN ,则引发 FE_INVALID 并返回实现定义值
注意
POSIX 指定 lrint
或 llrint
引发 FE_INEXACT 的所有情况都是定义域错误。
如指定于 math_errhandling , rint
在舍入非整数有限值时可以(但不在非 IEEE 浮点平台上要求)引发 FE_INEXACT 。
rint
和 nearbyint 间仅有的区别是 nearbyint 决不引发 FE_INEXACT 。
所有标准浮点格式中,最大可表示浮点值都是准确的整数,故 rint
自身决不上溢;然而在存储结果于整数对象时,结果可能溢出任何整数类型(包含 intmax_t )。
若当前舍入模式为……
- FE_DOWNWARD ,则
rint
等价于 floor 。 - FE_UPWARD ,则
rint
等价于 ceil 。 - FE_TOWARDZERO ,则
rint
等价于 trunc 。 - FE_TONEAREST ,则
rint
在中点情况和 round 的区别是前者始终舍入到偶数,而非远离零。
示例
运行此代码
#include <stdio.h> #include <math.h> #include <fenv.h> #include <limits.h> int main(void) { #pragma STDC FENV_ACCESS ON fesetround(FE_TONEAREST); printf("rounding to nearest (halfway cases to even):\n" "rint(+2.3) = %+.1f ", rint(2.3)); printf("rint(+2.5) = %+.1f ", rint(2.5)); printf("rint(+3.5) = %+.1f\n", rint(3.5)); printf("rint(-2.3) = %+.1f ", rint(-2.3)); printf("rint(-2.5) = %+.1f ", rint(-2.5)); printf("rint(-3.5) = %+.1f\n", rint(-3.5)); fesetround(FE_DOWNWARD); printf("rounding down: \nrint(+2.3) = %+.1f ", rint(2.3)); printf("rint(+2.5) = %+.1f ", rint(2.5)); printf("rint(+3.5) = %+.1f\n", rint(3.5)); printf("rint(-2.3) = %+.1f ", rint(-2.3)); printf("rint(-2.5) = %+.1f ", rint(-2.5)); printf("rint(-3.5) = %+.1f\n", rint(-3.5)); printf("rounding down with lrint: \nlrint(+2.3) = %ld ", lrint(2.3)); printf("lrint(+2.5) = %ld ", lrint(2.5)); printf("lrint(+3.5) = %ld\n", lrint(3.5)); printf("lrint(-2.3) = %ld ", lrint(-2.3)); printf("lrint(-2.5) = %ld ", lrint(-2.5)); printf("lrint(-3.5) = %ld\n", lrint(-3.5)); printf("lrint(-0.0) = %ld\n", lrint(-0.0)); printf("lrint(-Inf) = %ld\n", lrint(-INFINITY)); // 引发 FE_INVALID // 错误处理 feclearexcept(FE_ALL_EXCEPT); printf("rint(1.1) = %.1f\n", rint(1.1)); if(fetestexcept(FE_INEXACT)) puts(" FE_INEXACT was raised"); feclearexcept(FE_ALL_EXCEPT); printf("lrint(LONG_MIN-2048.0) = %ld\n", lrint(LONG_MIN-2048.0)); if(fetestexcept(FE_INVALID)) puts(" FE_INVALID was raised"); }
可能的输出:
rounding to nearest (halfway cases to even): rint(+2.3) = +2.0 rint(+2.5) = +2.0 rint(+3.5) = +4.0 rint(-2.3) = -2.0 rint(-2.5) = -2.0 rint(-3.5) = -4.0 rounding down: rint(+2.3) = +2.0 rint(+2.5) = +2.0 rint(+3.5) = +3.0 rint(-2.3) = -3.0 rint(-2.5) = -3.0 rint(-3.5) = -4.0 rounding down with lrint: lrint(+2.3) = 2 lrint(+2.5) = 2 lrint(+3.5) = 3 lrint(-2.3) = -3 lrint(-2.5) = -3 lrint(-3.5) = -4 lrint(-0.0) = 0 lrint(-Inf) = -9223372036854775808 rint(1.1) = 1.0 FE_INEXACT was raised lrint(LONG_MIN-2048.0) = -9223372036854775808 FE_INVALID was raised
引用
- C11 standard (ISO/IEC 9899:2011):
- 7.12.9.4 The rint functions (p: 252)
- 7.12.9.5 The lrint and llrint functions (p: 252)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.6.4 The rint functions (p: 527)
- F.10.6.5 The lrint and llrint functions (p: 527)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.9.4 The rint functions (p: 232-233)
- 7.12.9.5 The lrint and llrint functions (p: 233)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.6.4 The rint functions (p: 463)
- F.9.6.5 The lrint and llrint functions (p: 463)
参阅
(C99)(C99)(C99) |
取整到绝对值不大于给定值的最接近整数 (函数) |
(C99)(C99)(C99) |
用当前舍入模式取整到整数 (函数) |
(C99)(C99) |
获得或设置数字的舍入方向 (函数) |