add a few more tools. most useful: round.c
[tools.git] / math / round.c
diff --git a/math/round.c b/math/round.c
new file mode 100644 (file)
index 0000000..e422fd0
--- /dev/null
@@ -0,0 +1,16 @@
+#include <jh.h>
+#include <math.h>
+
+int main(int argc, char **argv) {
+  if (argc != 2) xperror("invocation: round <target>", 0);
+  double target = strtod(argv[1], NULL);
+  char line[128];
+  while (fgets(line, sizeof(line), stdin)) {
+    double n = strtod(line, NULL);
+    double quot = n / target;
+    quot = round(quot);
+    n = quot * target;
+    printf("%f\n", n);
+  }
+  return 0;
+}