From 8b849ebab0de4da1f64cc29dea9967adfcace6cc Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Thu, 14 Dec 2023 17:53:30 -0500 Subject: [PATCH] Documentation regarding the integer types --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1ca0b7e..2028051 100644 --- a/README.md +++ b/README.md @@ -4,17 +4,21 @@ [![Ubuntu 22.04 CI (GCC 11)](https://github.com/fastfloat/fast_float/actions/workflows/ubuntu22.yml/badge.svg)](https://github.com/fastfloat/fast_float/actions/workflows/ubuntu22.yml) The fast_float library provides fast header-only implementations for the C++ from_chars -functions for `float` and `double` types. These functions convert ASCII strings representing -decimal values (e.g., `1.3e10`) into binary types. We provide exact rounding (including +functions for `float` and `double` types as well as integer types. These functions convert ASCII strings representing decimal values (e.g., `1.3e10`) into binary types. We provide exact rounding (including round to even). In our experience, these `fast_float` functions many times faster than comparable number-parsing functions from existing C++ standard libraries. -Specifically, `fast_float` provides the following two functions with a C++17-like syntax (the library itself only requires C++11): +Specifically, `fast_float` provides the following two functions to parse floating-point numbers with a C++17-like syntax (the library itself only requires C++11): ```C++ from_chars_result from_chars(const char* first, const char* last, float& value, ...); from_chars_result from_chars(const char* first, const char* last, double& value, ...); ``` +You can also parse integer types: + + + + The return type (`from_chars_result`) is defined as the struct: ```C++ struct from_chars_result { @@ -103,6 +107,43 @@ We support Visual Studio, macOS, Linux, freeBSD. We support big and little endia We assume that the rounding mode is set to nearest (`std::fegetround() == FE_TONEAREST`). + +## Integer types + +You can also parse integer types using different bases (e.g., 2, 10, 16). The following code will +print the number 22250738585072012 three times: + + +```C++ + uint64_t i; + const char str[] = "22250738585072012"; + auto answer = fast_float::from_chars(str, str + strlen(str), i); + if (answer.ec != std::errc()) { + std::cerr << "parsing failure\n"; + return EXIT_FAILURE; + } + std::cout << "parsed the number "<< i << std::endl; + + const char binstr[] = "1001111000011001110110111001001010110100111000110001100"; + + answer = fast_float::from_chars(binstr, binstr + strlen(binstr), i, 2); + if (answer.ec != std::errc()) { + std::cerr << "parsing failure\n"; + return EXIT_FAILURE; + } + std::cout << "parsed the number "<< i << std::endl; + + + const char hexstr[] = "4f0cedc95a718c"; + + answer = fast_float::from_chars(hexstr, hexstr + strlen(hexstr), i, 16); + if (answer.ec != std::errc()) { + std::cerr << "parsing failure\n"; + return EXIT_FAILURE; + } + std::cout << "parsed the number "<< i << std::endl; +``` + ## C++20: compile-time evaluation (constexpr) In C++20, you may use `fast_float::from_chars` to parse strings