OR-Tools  8.2
lp_print_utils.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // Utilities to display linear expression in a human-readable way.
15 
16 #ifndef OR_TOOLS_LP_DATA_LP_PRINT_UTILS_H_
17 #define OR_TOOLS_LP_DATA_LP_PRINT_UTILS_H_
18 
19 #include <string>
20 
21 #include "absl/strings/str_format.h"
24 
25 namespace operations_research {
26 namespace glop {
27 
28 // Returns a string representing a floating-point number in decimal,
29 // with a precision corresponding to the type of the argument.
30 inline std::string Stringify(const float a) {
31  return absl::StrFormat("%.7g", a);
32 }
33 
34 inline std::string Stringify(const double a) {
35  return absl::StrFormat("%.16g", a);
36 }
37 
38 inline std::string Stringify(const long double a) {
39  return absl::StrFormat("%.19g", a);
40 }
41 
42 // Returns a string "num/den" representing the rational approximation of x.
43 // The absolute difference between the output fraction and the input "x" will
44 // not exceed "precision".
45 std::string StringifyRational(const double x, const double precision);
46 
47 // If fraction is true, returns a string corresponding to the rational
48 // approximation or a decimal approximation otherwise. Note that the absolute
49 // difference between the output fraction and "x" will never exceed
50 // std::numeric_limits<T>::epsilon().
51 std::string Stringify(const Fractional x, bool fraction);
52 
53 // Pretty prints a monomial a*x using Stringify(x, fraction) to display a,
54 // taking care of the sign of x, whether a is 0, 1, -1, integer. Note that the
55 // absolute difference between the output fraction and "x" will never exceed
56 // std::numeric_limits<T>::epsilon().
57 std::string StringifyMonomial(const Fractional a, const std::string& x,
58  bool fraction);
59 
60 } // namespace glop
61 } // namespace operations_research
62 
63 #endif // OR_TOOLS_LP_DATA_LP_PRINT_UTILS_H_
std::string StringifyMonomial(const Fractional a, const std::string &x, bool fraction)
std::string Stringify(const Fractional x, bool fraction)
std::string StringifyRational(const double x, const double precision)
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...