Analog of C11 stdlib function nextafter?

I asked question on SO: https://stackoverflow.com/questions/56348835/how-can-i-convert-a-f64-to-f32-and-get-the-closest-approximation-and-the-next-gr?noredirect=1#56348835

But because of asking about “what crate has suitable functionality” is not approapriate,
I am asking here,
is any crate around that provide functionality similar to nextafter function from stdlib:
https://en.cppreference.com/w/c/numeric/math/nextafter
?

I was gonna say you could just call the C stdlib functions themselves, but it looks like they’re not included in the libc crate :confused:.

So you could submit a PR to libc to add them, then call them (if you don’t mind using your own fork of libc while you wait for them to get merged/published).

3 Likes

Someone on SO linked you to https://docs.rs/float_extras/0.1.6/float_extras/f64/fn.nextafter.html (re-linking for those who may end up finding this thread when searching for a similar issue)

You could also temporarily keep a version of the function declaration in your project until it’s added to libc, here could be how: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=bcbda5a0f59b6ce51d0fe8030b826bfb

1 Like

These are also relatively easy to implement yourself, using {to|from}_bits.

A starting demonstration: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f25616329fd806604d2f1a0be590ac4b

this is not looks similar to real implementation: https://android.googlesource.com/platform/bionic/+/ics-mr1-release/libm/src/s_nextafterf.c

many edge cases missed?

1 Like

It says “starting” for a reason. More obviously, the “demonstration” only has one parameter, not two.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.