summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/dblround.c
diff options
context:
space:
mode:
Diffstat (limited to 'reference/C/CONTRIB/SNIP/dblround.c')
-rwxr-xr-xreference/C/CONTRIB/SNIP/dblround.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/reference/C/CONTRIB/SNIP/dblround.c b/reference/C/CONTRIB/SNIP/dblround.c
new file mode 100755
index 0000000..5870fda
--- /dev/null
+++ b/reference/C/CONTRIB/SNIP/dblround.c
@@ -0,0 +1,35 @@
+/*
+** DBLROUND.C - Rounds a double to the nearest whole number
+** public domain by Ross Cottrell
+*/
+
+#include <float.h>
+#include <limits.h>
+#include <assert.h>
+
+double round(double x)
+{
+ assert(1 == FLT_ROUNDS);
+ x += 1.0 / DBL_EPSILON;
+ return x - 1.0 / DBL_EPSILON;
+}
+
+#ifdef TEST
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void main(int argc, char *argv[])
+{
+ double val;
+ char *dummy;
+
+ while (--argc)
+ {
+ val = strtod((const char *)(*(++argv)), &dummy);
+ printf("round(%g) = ", val);
+ printf("%.12g\n", round(val));
+ }
+}
+
+#endif /* TEST */