]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | # <small>nlohmann::basic_json::</small>number_float_t |
2 | ||
3 | ```cpp | |
4 | using number_float_t = NumberFloatType; | |
5 | ``` | |
6 | ||
7 | The type used to store JSON numbers (floating-point). | |
8 | ||
9 | [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: | |
10 | > The representation of numbers is similar to that used in most programming languages. A number is represented in base | |
11 | > 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may | |
12 | > be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that | |
13 | > cannot be represented in the grammar below (such as Infinity and NaN) are not permitted. | |
14 | ||
15 | This description includes both integer and floating-point numbers. However, C++ allows more precise storage if it is | |
16 | known whether the number is a signed integer, an unsigned integer or a floating-point number. Therefore, three different | |
17 | types, [`number_integer_t`](number_integer_t.md), [`number_unsigned_t`](number_unsigned_t.md) and `number_float_t` are | |
18 | used. | |
19 | ||
20 | To store floating-point numbers in C++, a type is defined by the template parameter `NumberFloatType` which chooses the | |
21 | type to use. | |
22 | ||
23 | ## Notes | |
24 | ||
25 | #### Default type | |
26 | ||
27 | With the default values for `NumberFloatType` (`double`), the default value for `number_float_t` is `#!cpp double`. | |
28 | ||
29 | #### Default behavior | |
30 | ||
31 | - The restrictions about leading zeros is not enforced in C++. Instead, leading zeros in floating-point literals will be | |
32 | ignored. Internally, the value will be stored as decimal number. For instance, the C++ floating-point literal `01.2` | |
33 | will be serialized to `1.2`. During deserialization, leading zeros yield an error. | |
34 | - Not-a-number (NaN) values will be serialized to `null`. | |
35 | ||
36 | #### Limits | |
37 | ||
38 | [RFC 8259](https://tools.ietf.org/html/rfc8259) states: | |
39 | > This specification allows implementations to set limits on the range and precision of numbers accepted. Since software | |
40 | > that implements IEEE 754-2008 binary64 (double precision) numbers is generally available and widely used, good | |
41 | > interoperability can be achieved by implementations that expect no more precision or range than these provide, in the | |
42 | > sense that implementations will approximate JSON numbers within the expected precision. | |
43 | ||
44 | This implementation does exactly follow this approach, as it uses double precision floating-point numbers. Note values | |
45 | smaller than `-1.79769313486232e+308` and values greater than `1.79769313486232e+308` will be stored as NaN internally | |
46 | and be serialized to `null`. | |
47 | ||
48 | #### Storage | |
49 | ||
50 | Floating-point number values are stored directly inside a `basic_json` type. | |
51 | ||
52 | ## Examples | |
53 | ||
54 | ??? example | |
55 | ||
56 | The following code shows that `number_float_t` is by default, a typedef to `#!cpp double`. | |
57 | ||
58 | ```cpp | |
59 | --8<-- "examples/number_float_t.cpp" | |
60 | ``` | |
61 | ||
62 | Output: | |
63 | ||
64 | ```json | |
65 | --8<-- "examples/number_float_t.output" | |
66 | ``` | |
67 | ||
68 | ## Version history | |
69 | ||
70 | - Added in version 1.0.0. |