Skip to content

Instantly share code, notes, and snippets.

@mikhailramalho
Created October 17, 2016 15:25
Show Gist options
  • Save mikhailramalho/df4f94e674906355ae2874d98caae7c4 to your computer and use it in GitHub Desktop.
Save mikhailramalho/df4f94e674906355ae2874d98caae7c4 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <assert.h>
float my_nextafterf( float from, float to )
{
union {
float a;
int b;
} from_union = { .a = from };
if(from > to)
from_union.b--;
else
from_union.b++;
return from_union.a;
}
int main(void)
{
float from = 1;
float to = my_nextafterf(from, 2);
printf("%.64f (%a)\n", to, to);
assert(to == 0x1.000002p+0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment