Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

**dconv** is a header-only C++20 library for printing and parsing floating point numbers.

Double to string conversion is done using the **Grisu2** algorithm, described by **Florian Loitsch** in its publication [Printing Floating-Point Numbers Quickly and Accurately with Integers](https://florian.loitsch.com/publications).
Double to string conversion uses the **Grisu2** algorithm by default, described by **Florian Loitsch** in its publication [Printing Floating-Point Numbers Quickly and Accurately with Integers](https://florian.loitsch.com/publications).

The **Grisu** and **Grisu3** algorithms from the same publication are also available. Grisu prints up to 18 digits without looking for the shortest output, Grisu2 prints the shortest output for about 99.9% of the doubles, and Grisu3 detects the remaining cases and falls back to `std::to_chars`.

String to double conversion uses the **Eisel-Lemire** algorithm on the first 19 significant digits. Longer mantissas are bracketed between two Eisel-Lemire results; only when they disagree is the number rebuilt from the digits already read and passed to strtod. A stream is read once and never rewound; a buffer is only read again for mantissas longer than 19 digits.

Expand Down Expand Up @@ -72,6 +74,21 @@ char value[25];
char* end = dconv::dtoa (value, -2.22507e-308);
```

Each algorithm has its own header and namespace:

```cpp
#include <dconv/grisu3.hpp>

char value[25];
char* end = dconv::grisu3::dtoa (value, -2.22507e-308);
```

| Header | Function |
| :--- | :--- |
| `dconv/grisu3.hpp` | `dconv::grisu3::dtoa` |
| `dconv/grisu2.hpp` | `dconv::grisu2::dtoa` |
| `dconv/grisu.hpp` | `dconv::grisu::dtoa` |

The parsing API can be used this way:

```cpp
Expand All @@ -95,7 +112,28 @@ std::istringstream in ("-2.22507e-308");
bool ok = dconv::atod (in, value);
```

`dtoa` needs a buffer of at least 25 bytes and returns the end of the written characters.
A variant reads from a probe and stops on integers, so that integers can be kept as integers:

```cpp
#include <dconv/atod.hpp>

double value;
uint64_t significand;
int64_t exponent;
bool negative;
std::error_code ec;

dconv::StringProbe probe ("18446744073709551615");
if (dconv::atod (probe, value, significand, exponent, negative, ec) == -1 && ec == dconv::Errc::NotDouble)
{
// significand holds the integer, negative its sign.
}
```

It returns `0` and the converted value for a real number, or `-1` with `Errc::NotDouble` for an integer without fraction nor exponent whose magnitude fits in 64 bits, or `-1` with `Errc::InvalidNumber` when parsing fails.
`significand`, `exponent` and `negative` hold what was read until the end or the error.

`dtoa` needs a buffer of at least 25 bytes (26 bytes for `grisu::dtoa`) and returns the end of the written characters.
`atod` returns the end of the parsed characters, or `nullptr` on failure.
The probe and stream overloads return `false` on failure and stop right after the number on success.
They read the stream buffer directly: whitespaces are not skipped, the stream state is left unchanged and the characters read are consumed even on failure.
Expand Down
13 changes: 10 additions & 3 deletions dconv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
cmake_minimum_required(VERSION 3.22.1)

set(PUBLIC_HEADERS
include/dconv/error.hpp
include/dconv/probe.hpp
include/dconv/diyfp.hpp
include/dconv/eisellemirepow.hpp
include/dconv/eisellemire.hpp
include/dconv/atod.hpp
include/dconv/atodpow.hpp
include/dconv/digits.hpp
include/dconv/format.hpp
include/dconv/diyfp.hpp
include/dconv/grisupow.hpp
include/dconv/grisu.hpp
include/dconv/grisu2.hpp
include/dconv/grisu3.hpp
include/dconv/dtoa.hpp
include/dconv/dtoapow.hpp
)

add_library(${PROJECT_NAME} INTERFACE)
Expand Down
Loading
Loading