forked from rte-france/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp_roundtrip_conv.cc
137 lines (123 loc) · 5.22 KB
/
fp_roundtrip_conv.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2010-2022 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/util/fp_roundtrip_conv.h"
#include <array>
#include <charconv>
#include <limits>
#include <ostream>
#include <string>
#include <system_error> // NOLINT(build/c++11)
#include "absl/status/statusor.h"
#include "absl/strings/charconv.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "ortools/base/logging.h"
#include "ortools/base/status_builder.h"
#define OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
#if defined(__wasm__) || defined(ANDROID) || defined(_MSC_VER)
#undef OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
#undef OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
#endif
#if defined(__GNUC__) && !defined(__llvm__) && __GNUC__ < 11
#undef OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
#endif
namespace operations_research {
namespace {
// When using std::to_chars(), the maximum number of digits for the mantissa is
// std::numeric_limits<double>::max_digits10, which is 17. On top of that the
// max_exponent10 is 308, which takes at most 3 digits. We also have to take
// into account the "e+"/"e-", the "-" sign and the ".". Thus the buffer must be
// at least 17 + 3 + 2 + 1 + 1 = 24 bytes long.
//
// When using absl::SNPrintF() with "%.*g" with `max_digits10` for the
// precision, it prints at most `precision + 4` digits. The +4 occurs for
// numbers d.ddddd...eX where -1 <= X <= -4 since in that case the number is
// printed with some leading zeros (i.e. 0.000dddd...). Thus we must have a
// string at least 28 bytes long.
//
// To be safe we had some margin and use a buffer of 4 * 8 bytes.
using RoundTripDoubleBuffer = std::array<char, 32>;
// Writes the double to the provided buffer and returns a view on the written
// range.
absl::string_view RoundTripDoubleToBuffer(const double value,
RoundTripDoubleBuffer& buffer) {
#ifdef OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
const auto result =
std::to_chars(buffer.data(), buffer.data() + buffer.size(), value);
CHECK(result.ec == std::errc()) << std::make_error_code(result.ec).message();
return absl::string_view(buffer.data(), result.ptr - buffer.data());
#else // OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
// Here we use a version that use enough digits to have roundtrip. We lose the
// specification that we use the shortest string though.
//
// We use absl::SNPrintF() since it does not depend on the locale (contrary to
// std::snprintf()).
const int written =
absl::SNPrintF(buffer.data(), buffer.size(), "%.*g",
std::numeric_limits<double>::max_digits10, value);
CHECK_GT(written, 0);
CHECK_LT(written, buffer.size());
return absl::string_view(buffer.data(), written);
#endif // OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
}
} // namespace
#ifdef OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
ABSL_CONST_INIT const bool kStdToCharsDoubleIsSupported = true;
#else
ABSL_CONST_INIT const bool kStdToCharsDoubleIsSupported = false;
#endif
#undef OPERATION_RESEARCH_STD_TO_CHARS_DOUBLE_SUPPORTED
std::ostream& operator<<(std::ostream& out,
const RoundTripDoubleFormat& format) {
RoundTripDoubleBuffer buffer;
out << RoundTripDoubleToBuffer(format.value_, buffer);
return out;
}
std::string RoundTripDoubleFormat::ToString(const double value) {
RoundTripDoubleBuffer buffer;
return std::string(RoundTripDoubleToBuffer(value, buffer));
}
absl::StatusOr<double> RoundTripDoubleFormat::Parse(
const absl::string_view str_value) {
const char* const begin = str_value.data();
const char* const end = begin + str_value.size();
double ret = 0.0;
const auto result = absl::from_chars(begin, end, ret);
if (result.ec == std::errc()) {
if (result.ptr != end) {
return util::InvalidArgumentErrorBuilder()
<< '"' << absl::CEscape(str_value)
<< "\" has unexpected suffix starting at index "
<< (result.ptr - begin);
}
return ret;
}
switch (result.ec) {
case std::errc::invalid_argument:
return util::InvalidArgumentErrorBuilder()
<< '"' << absl::CEscape(str_value) << "\" is not a valid double";
case std::errc::result_out_of_range:
return util::InvalidArgumentErrorBuilder()
<< '"' << absl::CEscape(str_value)
<< "\" does not fit in a double precision float";
default:
return util::InternalErrorBuilder()
<< "parsing of \"" << absl::CEscape(str_value)
<< "\" failed with an unexpected error: "
<< std::make_error_code(result.ec).message();
}
}
} // namespace operations_research