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 标准库
- 有用的资源
- 符号索引
- 注释
位置:首页 > C 参考手册 >数值 >常用数学函数 > nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl
nextafter, nextafterf, nextafterl, nexttoward, nexttowardf, nexttowardl
定义于头文件 <math.h>
|
||
float nextafterf( float from, float to ); |
(1) | (C99 起) |
double nextafter( double from, double to ); |
(2) | (C99 起) |
long double nextafterl( long double from, long double to ); |
(3) | (C99 起) |
float nexttowardf( float from, long double to ); |
(4) | (C99 起) |
double nexttoward( double from, long double to ); |
(5) | (C99 起) |
long double nexttowardl( long double from, long double to ); |
(6) | (C99 起) |
定义于头文件 <tgmath.h>
|
||
#define nextafter(from, to) |
(7) | (C99 起) |
#define nexttoward(from, to) |
(8) | (C99 起) |
1-3) 首先,转换两个参数为函数的类型,然后返回
from
于 to
方向的下个可表示值。若 from
等于 to
,则返回 to
。4-6) 首先,转换两个参数为函数的类型,然后返回
from
于 to
方向的下个可表示值。若 from
等于 to
,则返回从 long double 转换到函数返回类型的 to
,而不带范围或精度的损失。7) 泛型宏:若任何参数拥有 long double 类型,则调用
nextafterl
。否则,若任何参数拥有整数类型或 double 类型,则调用 nextafter
。否则调用 nextafterf
。8) 泛型宏:若任何参数拥有 long double 类型,则调用
nexttowardl
。否则,若任何参数拥有整数类型或 double 类型,则调用 nexttoward
。否则调用 nexttowardf
。参数
from, to | - | 浮点值 |
返回值
若不出现错误,则返回 from
于 to
的方向的下个可表示值。若 from
等于 to
,则返回 to
,转换到函数的类型。
若出现上溢所致的值域错误,则返回 ±HUGE_VAL
、 ±HUGE_VALF
或 ±HUGE_VALL
(所带符号同 from
)。
若出现下溢所致的值域错误,则返回正确结果。
错误处理
报告 math_errhandling 中指定的错误。
若实现支持 IEEE 浮点算术( IEC 60559 ),则
- 若
from
有限,但期待的结果无限,则引发 FE_INEXACT 和 FE_OVERFLOW 。 - 若
from
不等于to
且结果为非正规或零,则引发 FE_INEXACT 和 FE_UNDERFLOW 。 - 任何情况下,返回值独立于当前舍入模式。
- 若
from
或to
为 NaN ,则返回 NaN
注意
POSIX 指定上溢和下溢条件是值域错误(可以设置 errno )。
IEC 60559 推荐凡在 from==to
时返回 from
。这些函数替而返回 to
,这使得围绕零的行为一致: nextafter(-0.0, +0.0)
返回 +0.0
而 nextafter(+0.0, -0.0)
返回 –0.0
。
示例
运行此代码
#include <math.h> #include <stdio.h> #include <float.h> #include <fenv.h> int main(void) { float from1 = 0, to1 = nextafterf(from1, 1); printf("The next representable float after %.2f is %.20g (%a)\n", from1, to1, to1); float from2 = 1, to2 = nextafterf(from2, 2); printf("The next representable float after %.2f is %.20f (%a)\n", from2, to2, to2); double from3 = nextafter(0.1, 0), to3 = 0.1; printf("The number 0.1 lies between two valid doubles:\n" " %.56f (%a)\nand %.55f (%a)\n", from3, from3, to3, to3); // nextafter 和 nexttoward 间的差异: long double dir = nextafterl(from1, 1); // 第一个非正规 long double float x = nextafterf(from1, dir); // 首先转换 dir 为 float ,给出 0 printf("Using nextafter, next float after %.2f (%a) is %.20g (%a)\n", from1, from1, x, x); x = nexttowardf(from1, dir); printf("Using nexttoward, next float after %.2f (%a) is %.20g (%a)\n", from1, from1, x, x); // 特殊值 { #pragma STDC FENV_ACCESS ON feclearexcept(FE_ALL_EXCEPT); double from4 = DBL_MAX, to4 = nextafter(from4, INFINITY); printf("The next representable double after %.2g (%a) is %.23f (%a)\n", from4, from4, to4, to4); if(fetestexcept(FE_OVERFLOW)) puts(" raised FE_OVERFLOW"); if(fetestexcept(FE_INEXACT)) puts(" raised FE_INEXACT"); } // 结束 FENV_ACCESS 块 float from5 = 0.0, to5 = nextafter(from5, -0.0); printf("nextafter(+0.0, -0.0) gives %.2g (%a)\n", to5, to5); }
输出:
The next representable float after 0.00 is 1.4012984643248170709e-45 (0x1p-149) The next representable float after 1.00 is 1.00000011920928955078 (0x1.000002p+0) The number 0.1 lies between two valid doubles: 0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4) and 0.1000000000000000055511151231257827021181583404541015625 (0x1.999999999999ap-4) Using nextafter, next float after 0.00 (0x0p+0) is 0 (0x0p+0) Using nexttoward, next float after 0.00 (0x0p+0) is 1.4012984643248170709e-45 (0x1p-149) The next representable double after 1.8e+308 (0x1.fffffffffffffp+1023) is inf (inf) raised FE_OVERFLOW raised FE_INEXACT nextafter(+0.0, -0.0) gives -0 (-0x0p+0)
引用
- C11 standard (ISO/IEC 9899:2011):
- 7.12.11.3 The nextafter functions (p: 256)
- 7.12.11.4 The nexttoward functions (p: 257)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.8.3 The nextafter functions (p: 529)
- F.10.8.4 The nexttoward functions (p: 529)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.11.3 The nextafter functions (p: 237)
- 7.12.11.4 The nexttoward functions (p: 238)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.8.3 The nextafter functions (p: 466)
- F.9.8.4 The nexttoward functions (p: 466)