Wireshark 4.5.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
dtoa.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <wireshark.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif /* __cplusplus */
18
19/* Stores the closest decimal approximation to value in buf;
20 * it suffices to declare buf
21 * char buf[32];
22 *
23 * Specifically, this finds the shortest possible string that when converted
24 * back to a double will be equal to the original value. There is no single
25 * value that can be passed to snprintf("%.*g") that will work for all cases.
26 *
27 * E.g., for the IEEE 754 double closest to 1/7th (0x1.2492492492492p-3) 17
28 * (DBL_DECIMAL_DIG) digits are required; neither "0.1428571428571428" nor
29 * "0.1428571428571429" suffice, converting to 0x1.249249249249p-3 and
30 * 0x1.2492492492494p-3, respectively. However, for the double closest to
31 * 0.2 (0x1.999999999999ap-3), the closest string with 17 significant digits
32 * is "0.20000000000000001", not "0.2", even though both convert *to* the
33 * same double and would test as equal. So DBL_DECIMAL_DIG is *sufficient*
34 * for serialization but not necessary in all cases and can look particularly
35 * worse in formats where trailing zeros are removed.
36 *
37 * Note C++17 provides std::to_chars to provide the same result, though the
38 * difficulty in implementation caused this to be one of the last widely
39 * supported features across C++ standard libraries. It is not part of the
40 * C standard library functions.
41 */
42WS_DLL_PUBLIC char *dtoa_g_fmt(char *buf, double value);
43
44#ifdef __cplusplus
45}
46#endif /* __cplusplus */