From 758cdb72daf192bdcfae01f23ceebcc2f0d43754 Mon Sep 17 00:00:00 2001 From: Matheus Afonso Martins Moreira Date: Thu, 2 Nov 2023 06:42:28 -0300 Subject: [PATCH] examples: add a parsefloat example Essentially an expanded version of the basic test case posted in GitHub issue #456. Also includes examples from documentation. References: https://github.com/jart/cosmopolitan/issues/456 https://en.cppreference.com/w/c/string/byte/strtof --- examples/parsefloat.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/parsefloat.c diff --git a/examples/parsefloat.c b/examples/parsefloat.c new file mode 100644 index 000000000..ea0fb87d1 --- /dev/null +++ b/examples/parsefloat.c @@ -0,0 +1,31 @@ +#include + +#define PARSE_AND_PRINT(type, scan_fmt, print_fmt, str) \ + do { \ + type val; int ret; \ + ret = sscanf(str, scan_fmt, &val); \ + printf("\"%s\" => " print_fmt " = %d\n", str, val, ret); \ + } while (0) + +int main() +{ + PARSE_AND_PRINT(float, "%f", "%f", "0.3715"); + PARSE_AND_PRINT(float, "%f", "%f", ".3715"); + PARSE_AND_PRINT(float, "%f", "%f", "3715"); + PARSE_AND_PRINT(float, "%f", "%f", "111.11"); + PARSE_AND_PRINT(float, "%f", "%f", "-2.22"); + PARSE_AND_PRINT(float, "%f", "%f", "Nan"); + PARSE_AND_PRINT(float, "%f", "%f", "nAn(2)"); + PARSE_AND_PRINT(float, "%f", "%f", "-NAN(_asdfZXCV1234_)"); + PARSE_AND_PRINT(float, "%f", "%f", "-nan"); + PARSE_AND_PRINT(float, "%f", "%f", "+nan"); + PARSE_AND_PRINT(float, "%f", "%f", "inF"); + PARSE_AND_PRINT(float, "%f", "%f", "iNfINiTy"); + PARSE_AND_PRINT(float, "%f", "%f", "+inf"); + PARSE_AND_PRINT(float, "%f", "%f", "-inf"); + PARSE_AND_PRINT(float, "%f", "%f", "0X1.BC70A3D70A3D7P+6"); + PARSE_AND_PRINT(float, "%f", "%f", "1.18973e+4932zzz"); + PARSE_AND_PRINT(float, "%f", "%.10f", " -0.0000000123junk"); + PARSE_AND_PRINT(float, "%f", "%f", "junk"); + return 0; +}