File: max-float-precision.md

package info (click to toggle)
glaze 6.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,948 kB
  • sloc: cpp: 121,839; sh: 99; ansic: 26; makefile: 13
file content (50 lines) | stat: -rw-r--r-- 2,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Maximum Floating Point Precision (Writing)

Glaze provides a compile time option (`float_max_write_precision`) to limit the precision when writing numbers to JSON. This improves performance when high precision is not needed in the resulting JSON.

The wrappers `glz::write_float32` and `glz::write_float64` set this compile time option on individual numbers, arrays, or objects. The wrapper `glz::write_float_full` turns off higher level float precision wrapper options.

> [!TIP]
> For more control over float formatting (decimal places, scientific notation, etc.), see the [`float_format` option](options.md#float_format) and [`glz::float_format` wrapper](wrappers.md#float_format) which use `std::format` syntax.

Example unit tests:

```c++
struct write_precision_t
{
   double pi = std::numbers::pi_v<double>;
   
   struct glaze
   {
      using T = write_precision_t;
      static constexpr auto value = glz::object("pi", glz::write_float32<&T::pi>);
   };
};

suite max_write_precision_tests = [] {
   "max_write_precision"_test = [] {
      double pi = std::numbers::pi_v<double>;
      std::string json_double = glz::write_json(pi);
      
      constexpr glz::opts options{.float_max_write_precision = glz::float_precision::float32};
      std::string json_float = glz::write<options>(pi);
      expect(json_double != json_float);
      expect(json_float == glz::write_json(std::numbers::pi_v<float>));
      expect(!glz::read_json(pi, json_float));
      
      std::vector<double> double_array{ pi, 2 * pi };
      json_double = glz::write_json(double_array);
      json_float = glz::write<options>(double_array);
      expect(json_double != json_float);
      expect(json_float == glz::write_json(std::array{std::numbers::pi_v<float>, 2 * std::numbers::pi_v<float>}));
      expect(!glz::read_json(double_array, json_float));
   };
   
   "write_precision_t"_test = [] {
      write_precision_t obj{};
      std::string json_float = glz::write_json(obj);
      expect(json_float == R"({"pi":3.1415927})") << json_float;
   };
};
```