forked from dtolnay/dtoa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_test_data.cc
95 lines (85 loc) · 3.22 KB
/
gen_test_data.cc
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <stdio.h>
#include <nlohmann/json.hpp>
#include <fstream>
#include <string>
#include <random>
void write_one_f64(std::ofstream &file, double raw) {
file.write(reinterpret_cast<const char*>(&raw), sizeof(raw));
nlohmann::json json_ob = raw;
auto stringValue = json_ob.dump();
int stringSize = stringValue.size();
file.write(reinterpret_cast<const char*>(&stringSize), sizeof(int));
file.write(stringValue.c_str(), stringSize);
}
void write_one_f32(std::ofstream &file, float raw) {
file.write(reinterpret_cast<const char*>(&raw), sizeof(raw));
nlohmann::json json_ob = raw;
auto stringValue = json_ob.dump();
int stringSize = stringValue.size();
file.write(reinterpret_cast<const char*>(&stringSize), sizeof(int));
file.write(stringValue.c_str(), stringSize);
}
int main() {
double multiple = -1.00007;
std::ofstream file_f64("nholmann_json_f64.txt", std::ios::binary);
if (file_f64.is_open()) {
// boundaries
write_one_f64(file_f64, 0.0);
write_one_f64(file_f64, -0.0);
write_one_f64(file_f64, 1.0);
write_one_f64(file_f64, -1.0);
write_one_f64(file_f64, std::numeric_limits<double>::min());
write_one_f64(file_f64, std::numeric_limits<double>::max());
write_one_f64(file_f64, std::numeric_limits<double>::quiet_NaN());
write_one_f64(file_f64, std::numeric_limits<double>::epsilon());
write_one_f64(file_f64, std::numeric_limits<double>::infinity());
write_one_f64(file_f64, -std::numeric_limits<double>::infinity());
// known bads
write_one_f64(file_f64, 4599.99999);
write_one_f64(file_f64, 9.904578032905936e+15);
// return 0;
double current = 1.0;
while (current > 0.0) {
//double raw = (double)dis(gen);
current = current/multiple;
write_one_f64(file_f64, current);
}
current = 0.00001;
while (current != std::numeric_limits<double>::quiet_NaN()
&& current != std::numeric_limits<double>::infinity()) {
current = current*multiple;
write_one_f64(file_f64, current);
}
}
std::ofstream file_f32("nholmann_json_f32.txt", std::ios::binary);
if (file_f32.is_open()) {
// boundaries
write_one_f32(file_f32, 0.0);
write_one_f32(file_f32, -0.0);
write_one_f32(file_f32, 1.0);
write_one_f32(file_f32, -1.0);
write_one_f32(file_f32, std::numeric_limits<double>::min());
write_one_f32(file_f32, std::numeric_limits<double>::max());
write_one_f32(file_f32, std::numeric_limits<double>::quiet_NaN());
write_one_f32(file_f32, std::numeric_limits<double>::epsilon());
write_one_f32(file_f32, std::numeric_limits<double>::infinity());
write_one_f32(file_f32, -std::numeric_limits<double>::infinity());
// known bads
write_one_f32(file_f32, 4599.99999);
write_one_f32(file_f32, 9.904578032905936e+15);
write_one_f32(file_f32, 2.220446049250313e-16);
double current = 1.0;
while (current > 0.0) {
//double raw = (double)dis(gen);
current = current/multiple;
write_one_f32(file_f32, current);
}
current = 0.000001;
while (current != std::numeric_limits<double>::quiet_NaN()
&& current != std::numeric_limits<double>::infinity()) {
current = current*multiple;
write_one_f32(file_f32, current);
}
}
}