Skip to content

Instantly share code, notes, and snippets.

@ksherlock
Created February 23, 2021 16:33
Show Gist options
  • Save ksherlock/d896a8eb5af93dad258a11c20c980ddc to your computer and use it in GitHub Desktop.
Save ksherlock/d896a8eb5af93dad258a11c20c980ddc to your computer and use it in GitHub Desktop.
rint.c
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#define _(x) { x, #x }
static struct { int mode; char *name; } modes[] = {
_(FE_DOWNWARD),
_(FE_TONEAREST),
_(FE_TOWARDZERO),
_(FE_UPWARD),
};
static double data[] = {
1.25, 1.5, 1.75, 2.5, -1.25, -1.5, -1.75, -2.5
};
int main(int argc, char **argv) {
int i, j;
int ok;
for (i = 0; i < 4; ++i) {
ok = fesetround(modes[i].mode);
printf("fesetround(%s): %d\n", modes[i].name, ok);
for (j = 0; j < 8; ++j) {
printf("rint(%f): %f\n", data[j], rint(data[j]));
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment