在线文档教程
C++
数字 | Numerics

std::nexttoward

STD::下一次,STD::Nextttoward

Defined in header
float nextafter( float from, float to (1)(since C++11)
double nextafter( double from, double to (2)(since C++11)
long double nextafter( long double from, long double to (3)(since C++11)
Promoted nextafter( Arithmetic from, Arithmetic to (4)(since C++11)
float nexttoward( float from, long double to (5)(since C++11)
double nexttoward( double from, long double to (6)(since C++11)
long double nexttoward( long double from, long double to (7)(since C++11)
double nexttoward( Integral from, long double to (8)(since C++11)

的下一个可表示值from向…的方向to...

1-3%29from等于to,,,to会被归还。

5-7%29from等于to,,,to返回,转换为long double函数的返回类型,而不丢失范围或精度。

4%29一组重载或函数模板,用于%281-3%29中未涵盖的所有算术类型参数组合。如果有任何争论积分型,它被铸造成double.如果有任何争论long double,则返回类型Promoted也是long double,否则返回类型总是double...

8%29一组重载或接受from任何论点积分型等效于%286%29%28的参数转换为double29%。

参数

from, to-floating point values

返回值

如果没有发生错误,则下一个可表示的值from向…的方向to.被归还。如果from等号to,然后to会被归还。

如果溢出导致范围错误,±HUGE_VAL,,,±HUGE_VALF,或±HUGE_VALL返回%28,其符号与from29%。

如果由于下垫流而发生范围错误,则返回正确的结果。

错误处理

错误按数学[医]错误处理...

如果实现支持ieee浮点算法%28IEC 60559%29,

  • 如果from是有限的,但预期的结果是无穷大的。FE_INEXACTFE_OVERFLOW

  • 如果from不等于to结果是低于正常值或者为零,FE_INEXACTFE_UNDERFLOW

  • 在任何情况下,返回的值都与当前舍入模式无关。

  • 如果fromto是南,南回来了

注记

POSIX指定溢出和下溢条件为范围错误%28 errno可设置为%29。

iec 60559建议from将在任何时候返回。from==to.这些功能返回to相反,这使得在零附近的行为保持一致:std::nextafter(-0.0, +0.0)回报+0.0std::nextafter(+0.0, -0.0)回报–0.0...

二次

#include <cmath> #include <iomanip> #include <iostream> #include <cfloat> #include <cfenv> int main() { float from1 = 0, to1 = std::nextafter(from1, 1.f std::cout << "The next representable float after " << std::setprecision(20) << from1 << " is " << to1 << std::hexfloat << " (" << to1 << ")\n" << std::defaultfloat; float from2 = 1, to2 = std::nextafter(from2, 2.f std::cout << "The next representable float after " << from2 << " is " << to2 << std::hexfloat << " (" << to2 << ")\n" << std::defaultfloat; double from3 = std::nextafter(0.1, 0), to3 = 0.1; std::cout << "The number 0.1 lies between two valid doubles:\n" << std::setprecision(56) << " " << from3 << std::hexfloat << " (" << from3 << ')' << std::defaultfloat << "\nand " << to3 << std::hexfloat << " (" << to3 << ")\n" << std::defaultfloat << std::setprecision(20 // difference between nextafter and nexttoward: long double dir = std::nextafter(from1, 1.0L // first subnormal long double float x = nextafter(from1, dir // first converts dir to float, giving 0 std::cout << "With nextafter, next float after " << from1 << " is " << x << '\n'; x = std::nexttoward(from1, dir std::cout << "With nexttoward, next float after " << from1 << " is " << x << '\n'; // special values { #pragma STDC FENV_ACCESS ON std::feclearexcept(FE_ALL_EXCEPT double from4 = DBL_MAX, to4 = std::nextafter(from4, INFINITY std::cout << "The next representable double after " << std::setprecision(6) << from4 << std::hexfloat << " (" << from4 << ')' << std::defaultfloat << " is " << to4 << std::hexfloat << " (" << to4 << ")\n" << std::defaultfloat; if(std::fetestexcept(FE_OVERFLOW)) std::cout << " raised FE_OVERFLOW\n"; if(std::fetestexcept(FE_INEXACT)) std::cout << " raised FE_INEXACT\n"; } // end FENV_ACCESS block float from5 = 0.0, to5 = std::nextafter(from5, -0.0 std::cout << "std::nextafter(+0.0, -0.0) gives " << std::fixed << to5 << '\n'; }

二次

产出:

二次

The next representable float after 0 is 1.4012984643248170709e-45 (0x1p-149) The next representable float after 1 is 1.0000001192092895508 (0x1.000002p+0) The number 0.1 lies between two valid doubles: 0.09999999999999999167332731531132594682276248931884765625 (0x1.9999999999999p-4) and 0.1000000000000000055511151231257827021181583404541015625 (0x1.999999999999ap-4) With nextafter, next float after 0 is 0 With nexttoward, next float after 0 is 1.4012984643248170709e-45 The next representable double after 1.79769e+308 (0x1.fffffffffffffp+1023) is inf (inf) raised FE_OVERFLOW raised FE_INEXACT std::nextafter(+0.0, -0.0) gives -0.000000

二次

另见

C以后的文档

*。

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/数值/数学/nextAfter