标准库提供所有以无 cv 限定的浮点类型作为形参from的类型的std::nexttoward重载。然而如果from对应的实参具有扩展浮点类型,那么对std::nexttoward的调用非良构,因为下个可表示值(或to)不保证能以longdouble表示。 (C++23 起) A)为算术类型的所有其他组合提供额外的std::nextafter重载。
nexttoward 间的差异:longdoubledir=std::nextafter(from1, 1.0L);// 首个非正规 long doublefloatx=nextafter(from1, dir);// 首先转换 dir 为 float ,给出 0std::cout<<"With nextafter, next float after "<<from1<<" is "<<x<<'\n';x=std::nexttoward(from1, dir);std::cout<<"With ...
std::nextafter,std::nextafterf,std::nextafterl,std::nexttoward,std::nexttowardf,std::nexttowardl Defined in header<cmath> (1) floatnextafter(floatfrom,floatto); doublenextafter(doublefrom,doubleto); longdoublenextafter(longdoublefrom,longdoubleto); ...
IEC 60559 推荐凡在 from==to 时返回 from 。这些函数替而返回 to ,这使得围绕零的行为一致: std::nextafter(-0.0, +0.0) 返回+0.0 而std::nextafter(+0.0, -0.0) 返回–0.0。 示例运行此代码 #include <cmath> #include <iomanip> #include <iostream> #include <cfloat> #include <cfenv> int main...
I tested the following code and found that the execution time was too slow compared to gcc. #include <cmath> #include <benchmark/benchmark.h> void nextafter(benchmark::State& s) { while (s.KeepRunning()) std::nextafter(1.25e+100, INFINITY); } BENCHMARK(nextafter); BENCHMARK_MAIN()...