Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lp_print_utils.h
Go to the documentation of this file.
1// Copyright 2010-2024 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"
22#include "absl/strings/string_view.h"
23#include "ortools/base/types.h"
25
26namespace operations_research {
27namespace glop {
28
29// Returns a string representing a floating-point number in decimal,
30// with a precision corresponding to the type of the argument.
31inline std::string Stringify(const float a) {
32 return absl::StrFormat("%.7g", a);
33}
34
35inline std::string Stringify(const double a) {
36 return absl::StrFormat("%.16g", a);
37}
38
39inline std::string Stringify(const long double a) {
40 return absl::StrFormat("%.19g", a);
41}
42
43// Returns a string "num/den" representing the rational approximation of x.
44// The absolute difference between the output fraction and the input "x" will
45// not exceed "precision".
46std::string StringifyRational(double x, double precision);
47
48// If fraction is true, returns a string corresponding to the rational
49// approximation or a decimal approximation otherwise. Note that the absolute
50// difference between the output fraction and "x" will never exceed
51// std::numeric_limits<T>::epsilon().
52std::string Stringify(Fractional x, bool fraction);
53
54// Pretty prints a monomial a*x using Stringify(x, fraction) to display a,
55// taking care of the sign of x, whether a is 0, 1, -1, integer. Note that the
56// absolute difference between the output fraction and "x" will never exceed
57// std::numeric_limits<T>::epsilon().
58std::string StringifyMonomial(Fractional a, absl::string_view x, bool fraction);
59
60} // namespace glop
61} // namespace operations_research
62
63#endif // OR_TOOLS_LP_DATA_LP_PRINT_UTILS_H_
int64_t a
Definition table.cc:44
std::string StringifyRational(const double x, const double precision)
std::string Stringify(const Fractional x, bool fraction)
std::string StringifyMonomial(const Fractional a, absl::string_view x, bool fraction)
In SWIG mode, we don't want anything besides these top-level includes.
const Variable x
Definition qp_tests.cc:127