07-26-2023, 04:03 PM
What exactly is the reason behind this peculiar interface of `nextafter` (and `nexttoward`) [functions][1]? We specify the *direction* by specifying the value we want to move *toward*.
At the first sight it feels as if something non-obvious is hidden behind this idea. In my (naive) opinion the first choice for such functions would be something like a pair of single-parameter functions `nextafter(a)`/`nextbefore(a)`. The next choice would be a two-parameter function `nextafter(a, dir)` in which the direction `dir` is specified explicitly (`-1` and `+1`, some standard enum, etc.).
But instead we have to specify a *value* we want to move *toward*. Hence a number of questions
1. (A vague one). There might be some clever idea or idiomatic pattern that is so valuable that it influenced this choice of interface in these standard functions. Is there?
2. What if decide to just blindly use `-DBL_MAX` and `+DBL_MAX` as the second argument for `nextafter` to specify the negative and positive direction respectively. Are there any pitfalls in doing so?
3. (A refinement of 2). If I know for sure that `b` is [slightly] greater than `a`, is there any reason to prefer `nextafter(a, b)` over `nextafter(a, DBL_MAX)`? E.g. is there a chance of better performance for `nextafter(a, b)` version?
4. Is `nextafter` generally a *heavy* operation? I know that it is implementation-dependent. But, again, assuming an implementation that is based in IEEE 754 representations, is it fairly "difficult" to find the adjacent floating-point value?
[1]:
At the first sight it feels as if something non-obvious is hidden behind this idea. In my (naive) opinion the first choice for such functions would be something like a pair of single-parameter functions `nextafter(a)`/`nextbefore(a)`. The next choice would be a two-parameter function `nextafter(a, dir)` in which the direction `dir` is specified explicitly (`-1` and `+1`, some standard enum, etc.).
But instead we have to specify a *value* we want to move *toward*. Hence a number of questions
1. (A vague one). There might be some clever idea or idiomatic pattern that is so valuable that it influenced this choice of interface in these standard functions. Is there?
2. What if decide to just blindly use `-DBL_MAX` and `+DBL_MAX` as the second argument for `nextafter` to specify the negative and positive direction respectively. Are there any pitfalls in doing so?
3. (A refinement of 2). If I know for sure that `b` is [slightly] greater than `a`, is there any reason to prefer `nextafter(a, b)` over `nextafter(a, DBL_MAX)`? E.g. is there a chance of better performance for `nextafter(a, b)` version?
4. Is `nextafter` generally a *heavy* operation? I know that it is implementation-dependent. But, again, assuming an implementation that is based in IEEE 754 representations, is it fairly "difficult" to find the adjacent floating-point value?
[1]:
[To see links please register here]