简体   繁体   English

c++ std::nextbefore function?

[英]c++ std::nextbefore function?

There exists std::nextafter function however no nextbefore .存在std::nextafter function 但是没有nextbefore Naively I would do天真地我会做

double a = 1.0;
double b = 2.0;

std::cout.precision(std::numeric_limits<double>::max_digits10);
std::cout << std::nextafter(a,b) << std::endl;
std::cout << std::nextafter(a,2*a-b) << std::endl;

to get要得到

1.0000000000000002
0.99999999999999989

But using 2a-b seems sketchy in the general case.但是在一般情况下使用2a-b似乎很粗略。 Is there a more robust way to achieve nextbefore.有没有更稳健的方法来实现 nextbefore。 ie: the next floating point number from a in the opposite direction from a to b ?即:从ab的相反方向下一个浮点数?

Demo https://godbolt.org/z/sKsK49oa3演示https://godbolt.org/z/sKsK49oa3

It's a kludge, but what about:这是一个kludge,但怎么样:

std::nextafter(a, (b > a) ? 
    -std::numeric_limits<double>::infinity() : 
    std::numeric_limits<double>::infinity())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM