![]() ![]() You can provide the policies as additional parameters to jkj::dragonbox::to_decimal or jkj::dragonbox::to_chars or jkj::dragonbox::to_chars_n. Those policies live in the namespace jkj::dragonbox::policy. There are currently five different kinds of policies that you can specify: sign policy, trailing zero policy, decimal-to-binary (parsing) rounding policy, binary-to-decimal (formatting) rounding policy, and cache policy. Most of the time the default policies will be sufficient, but for some situation this customizability might be useful. Policiesĭragonbox provides several policies that the user can select. jkj::dragonbox::to_chars and jkj::dragonbox::to_chars_n work fine for any inputs. The behavior of it when given with infinities/NaN's/ +0/ -0 is undefined. jkj::dragonbox::to_decimal is designed to work only with finite nonzero inputs. But the return type and the return value can change if you specify policy parameters. # include "dragonbox/dragonbox.h " double x = 1.234 // Also works for float // Here, x should be a nonzero finite number! // The return value v is a struct with three members: // significand : decimal significand (1234 in this case) // it is of type std::uint64_t for double, std::uint32_t for float // exponent : decimal exponent (-3 in this case) it is of type int // is_negative : as the name suggests it is of type bool auto v = jkj::dragonbox::to_decimal(x) īy default, jkj::dragonbox::to_decimal returns a struct with three members ( significand, exponent, and is_negative). The following will create platform-specific build files on your directory: Since there are only three files, it should be not difficult to set up this library manually if you want, but you can also use it via CMake as explained below. There are two additional files needed for that: include/dragonbox/dragonbox_to_chars.h and source/dragonbox_to_chars.cpp. Nevertheless, a string generation procedure is included in the library. The header file include/dragonbox/dragonbox.h includes everything needed for this (it is header-only). Dragonbox just outputs two integers (the decimal significand/exponent) that can be consumed by a string generation procedure. How to UseĪlthough Dragonbox is intended for float-to-string conversion routines, the actual string generation is not officially a part of the algorithm. Since another name of the pigeonhole principle is Dirichlet's box principle, I decided to call my algorithm "Dragonbox" to honor its origins: Schubfach (box) and Grisu (dragon). The name Schubfach is coming from the German name of the pigeonhole principle, Schubfachprinzip, meaning "drawer principle". The core idea of Schubfach, which Dragonbox is based on, is a continuous analogue of discrete pigeonhole principle. The output is correctly rounded: the number generated by Dragonbox is the closest to the actual value of the input number among possible outputs of minimum number of digits. The output is of the shortest length that is, no other output strings that are interpreted as the input number can contain less number of significand digits than the output of Dragonbox. It has the roundtrip guarantee that is, a correct parser interprets the generated output string as the original input floating-point number. These integers can then be used for string generation of decimal representation of the input floating-point number, the procedure commonly called ftoa or dtoa. Introductionĭragonbox generates a pair of integers from a floating-point number: the decimal significand and the decimal exponent of the input floating-point number. Dragonbox is further inspired by Grisu and Grisu-Exact. This library is a reference implementation of Dragonbox in C++.ĭragonbox is a float-to-string conversion algorithm based on a beautiful algorithm Schubfach, developed by Raffaello Giulietti in 2017-2018.
0 Comments
Leave a Reply. |