C++官网参考链接:https://cplusplus.com/reference/cmath/nextafter/
函数
<cmath> <ctgmath>
nextafter
C99
double nextafter(double x, double y);
float nextafterf(float x, float y);
long double nextafterl(long double x, long double y);
C++11
double nextafter(double x, double y );
float nextafter(float x, float y );
long double nextafter(long double x, long double y);
double nextafter(Type1 x, Type2 y); // additional overloads
下一个可表示的值
返回x之后沿y方向的下一个可表示值。
类似的函数nexttoward有相同的行为,但它的第二个实参是一个long double。
C99
头文件<tgmath.h>提供了该函数的类型泛型宏版本。
C++11
这个头文件(<cmath>)为其他算术类型(Type1和Type2)的组合提供了额外的重载:这些重载在计算之前有效地将其实参转换为double类型,除非至少有一个实参是long double类型(在这种情况下,两个实参都被转换为long double类型)。
形参
x
基础值。
y
返回值的近似值。
如果两个形参比较相等,函数返回y。
返回值
在y方向上x之后的下一个可表示的值。
如果x是该类型中可表示的最大有限值,而结果是无穷的或不可表示的,则会发生上溢范围错误。
如果发生上溢范围错误:
—math_errhandling设置了MATH_ERRNO:全局变量errno设置为ERANGE。
—math_errhandling设置了MATH_ERREXCEPT:引发FE_OVERFLOW。
用例
/* nextafter example */
#include <stdio.h> /* printf */
#include <math.h> /* nextafter */
int main ()
{
printf ("first representable value greater than zero: %e\n", nextafter(0.0,1.0));
printf ("first representable value less than zero: %e\n", nextafter(0.0,-1.0));
return 0;
}
可能的输出:
另请参考
nexttoward Next representable value toward precise value (function) (下一个可表示的朝向精确值的值(函数))