aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/fpconv.h
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-04-27 16:49:38 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-04-27 16:49:38 -0400
commitbaf990e65c62f42e45fe25ac385db9536d3f1788 (patch)
tree7b89cecde1d6cd3b600b9295e0ad22a702cd1065 /src/stdlib/fpconv.h
parent1a8a8bc3e2ebd9c282db8131902f9bb5de8c03cb (diff)
Update stdlib to use `print` instead of `printf` in all cases. This
means bringing in fpconv to do float-to-string conversion and a few updates to integer and number methods for string formatting.
Diffstat (limited to 'src/stdlib/fpconv.h')
-rw-r--r--src/stdlib/fpconv.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/stdlib/fpconv.h b/src/stdlib/fpconv.h
new file mode 100644
index 00000000..360c1f96
--- /dev/null
+++ b/src/stdlib/fpconv.h
@@ -0,0 +1,36 @@
+#ifndef FPCONV_H
+#define FPCONV_H
+
+// This file defines a function to convert floating point numbers to strings.
+// For license, see: fpconv_license.txt
+
+/* Fast and accurate double to string conversion based on Florian Loitsch's
+ * Grisu-algorithm[1].
+ *
+ * Input:
+ * fp -> the double to convert, dest -> destination buffer.
+ * The generated string will never be longer than 24 characters.
+ * Make sure to pass a pointer to at least 24 bytes of memory.
+ * The emitted string will not be null terminated.
+ *
+ * Output:
+ * The number of written characters.
+ *
+ * Exemplary usage:
+ *
+ * void print(double d)
+ * {
+ * char buf[24 + 1] // plus null terminator
+ * int str_len = fpconv_dtoa(d, buf);
+ *
+ * buf[str_len] = '\0';
+ * printf("%s", buf);
+ * }
+ *
+ */
+
+int fpconv_dtoa(double fp, char dest[24]);
+
+#endif
+
+/* [1] http://florian.loitsch.com/publications/dtoa-pldi2010.pdf */