Google OR-Tools v9.12
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-2025 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
39// Returns a string "num/den" representing the rational approximation of x.
40// The absolute difference between the output fraction and the input "x" will
41// not exceed "precision".
42std::string StringifyRational(double x, double precision);
43
44// If fraction is true, returns a string corresponding to the rational
45// approximation or a decimal approximation otherwise. Note that the absolute
46// difference between the output fraction and "x" will never exceed
47// std::numeric_limits<T>::epsilon().
48std::string Stringify(Fractional x, bool fraction);
49
50// Pretty prints a monomial a*x using Stringify(x, fraction) to display a,
51// taking care of the sign of x, whether a is 0, 1, -1, integer. Note that the
52// absolute difference between the output fraction and "x" will never exceed
53// std::numeric_limits<T>::epsilon().
54std::string StringifyMonomial(Fractional a, absl::string_view x, bool fraction);
55
56} // namespace glop
57} // namespace operations_research
58
59#endif // OR_TOOLS_LP_DATA_LP_PRINT_UTILS_H_
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.