Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
fp_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// Utility functions on IEEE floating-point numbers.
15// Implemented on float, double, and long double.
16//
17// Also a placeholder for tools controlling and checking FPU rounding modes.
18//
19// IMPORTANT NOTICE: you need to compile your binary with -frounding-math if
20// you want to use rounding modes.
21
22#ifndef OR_TOOLS_UTIL_FP_UTILS_H_
23#define OR_TOOLS_UTIL_FP_UTILS_H_
24
25#include <algorithm>
26#include <cmath>
27#include <cstdint>
28#include <cstdlib>
29#include <limits>
30// Needed before fenv_access. See https://github.com/microsoft/STL/issues/2613.
31#include <numeric> // IWYU pragma:keep.
32#include <vector>
33
34#include "absl/log/check.h"
35#include "absl/types/span.h"
36
37#if defined(_MSC_VER)
38#pragma fenv_access(on) // NOLINT
39#else
40#include <cfenv> // NOLINT
41#endif
42
43#ifdef __SSE__
44#include <xmmintrin.h>
45#endif
46
47#if defined(_MSC_VER)
48static inline double isnan(double value) { return _isnan(value); }
49static inline double round(double value) { return floor(value + 0.5); }
50#elif defined(__APPLE__) || __GNUC__ >= 5
51using std::isnan;
52#endif
53
54namespace operations_research {
55
56// ScopedFloatingPointEnv is used to easily enable Floating-point exceptions.
57// The initial state is automatically restored when the object is deleted.
58//
59// Note(user): For some reason, this causes an FPE exception to be triggered for
60// unknown reasons when compiled in 32 bits. Because of this, we do not turn
61// on FPE exception if __x86_64__ is not defined.
62//
63// TODO(user): Make it work on 32 bits.
64// TODO(user): Make it work on msvc, currently calls to _controlfp crash.
65
67 public:
69#if defined(_MSC_VER)
70 // saved_control_ = _controlfp(0, 0);
71#elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
72 CHECK_EQ(0, fegetenv(&saved_fenv_));
73#endif
74 }
75
77#if defined(_MSC_VER)
78 // CHECK_EQ(saved_control_, _controlfp(saved_control_, 0xFFFFFFFF));
79#elif defined(__x86_64__) && defined(__GLIBC__)
80 CHECK_EQ(0, fesetenv(&saved_fenv_));
81#endif
82 }
83
84 void EnableExceptions(int excepts) {
85#if defined(_MSC_VER)
86 // _controlfp(static_cast<unsigned int>(excepts), _MCW_EM);
87#elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__) && \
88 !defined(__ANDROID__)
89 CHECK_EQ(0, fegetenv(&fenv_));
90 excepts &= FE_ALL_EXCEPT;
91#if defined(__APPLE__)
92 fenv_.__control &= ~excepts;
93#elif (defined(__FreeBSD__) || defined(__OpenBSD__))
94 fenv_.__x87.__control &= ~excepts;
95#else // Linux
96 fenv_.__control_word &= ~excepts;
97#endif
98 fenv_.__mxcsr &= ~(excepts << 7);
99 CHECK_EQ(0, fesetenv(&fenv_));
100#endif
101 }
102
103 private:
104#if defined(_MSC_VER)
105 // unsigned int saved_control_;
106#elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
107 fenv_t fenv_;
108 mutable fenv_t saved_fenv_;
109#endif
110};
111
112template <typename FloatType>
113inline bool IsPositiveOrNegativeInfinity(FloatType x) {
114 return x == std::numeric_limits<FloatType>::infinity() ||
115 x == -std::numeric_limits<FloatType>::infinity();
116}
117
118// Tests whether x and y are close to one another using absolute and relative
119// tolerances.
120// Returns true if |x - y| <= a (with a being the absolute_tolerance).
121// The above case is useful for values that are close to zero.
122// Returns true if |x - y| <= max(|x|, |y|) * r. (with r being the relative
123// tolerance.)
124// The cases for infinities are treated separately to avoid generating NaNs.
125template <typename FloatType>
126bool AreWithinAbsoluteOrRelativeTolerances(FloatType x, FloatType y,
127 FloatType relative_tolerance,
128 FloatType absolute_tolerance) {
129 DCHECK_LE(0.0, relative_tolerance);
130 DCHECK_LE(0.0, absolute_tolerance);
131 DCHECK_GT(1.0, relative_tolerance);
133 return x == y;
134 }
135 const FloatType difference = fabs(x - y);
136 if (difference <= absolute_tolerance) {
137 return true;
138 }
139 const FloatType largest_magnitude = std::max(fabs(x), fabs(y));
140 return difference <= largest_magnitude * relative_tolerance;
141}
142
143// Tests whether x and y are close to one another using an absolute tolerance.
144// Returns true if |x - y| <= a (with a being the absolute_tolerance).
145// The cases for infinities are treated separately to avoid generating NaNs.
146template <typename FloatType>
147bool AreWithinAbsoluteTolerance(FloatType x, FloatType y,
148 FloatType absolute_tolerance) {
149 DCHECK_LE(0.0, absolute_tolerance);
151 return x == y;
152 }
153 return fabs(x - y) <= absolute_tolerance;
154}
155
156// Returns true if x is less than y or slighlty greater than y with the given
157// absolute or relative tolerance.
158template <typename FloatType>
159bool IsSmallerWithinTolerance(FloatType x, FloatType y, FloatType tolerance) {
160 if (IsPositiveOrNegativeInfinity(y)) return x <= y;
161 return x <= y + tolerance * std::max(1.0, std::min(std::abs(x), std::abs(y)));
162}
163
164// Returns true if x is within tolerance of any integer. Always returns
165// false for x equal to +/- infinity.
166template <typename FloatType>
167inline bool IsIntegerWithinTolerance(FloatType x, FloatType tolerance) {
168 DCHECK_LE(0.0, tolerance);
169 if (IsPositiveOrNegativeInfinity(x)) return false;
170 return std::abs(x - std::round(x)) <= tolerance;
171}
172
173// Handy alternatives to EXPECT_NEAR(), using relative and absolute tolerance
174// instead of relative tolerance only, and with a proper support for infinity.
175#define EXPECT_COMPARABLE(expected, obtained, epsilon) \
176 EXPECT_TRUE(operations_research::AreWithinAbsoluteOrRelativeTolerances( \
177 expected, obtained, epsilon, epsilon)) \
178 << obtained << " != expected value " << expected \
179 << " within epsilon = " << epsilon;
180
181#define EXPECT_NOTCOMPARABLE(expected, obtained, epsilon) \
182 EXPECT_FALSE(operations_research::AreWithinAbsoluteOrRelativeTolerances( \
183 expected, obtained, epsilon, epsilon)) \
184 << obtained << " == expected value " << expected \
185 << " within epsilon = " << epsilon;
186
187// Given an array of doubles, this computes a positive scaling factor such that
188// the scaled doubles can then be rounded to integers with little or no loss of
189// precision, and so that the L1 norm of these integers is <= max_sum. More
190// precisely, the following formulas will hold (x[i] is input[i], for brevity):
191// - For all i, |round(factor * x[i]) / factor - x[i]| <= error * |x[i]|
192// - The sum over i of |round(factor * x[i])| <= max_sum.
193//
194// The algorithm tries to minimize "error" (which is the relative error for one
195// coefficient). Note however than in really broken cases, the error might be
196// infinity and the factor zero.
197//
198// Note on the algorithm:
199// - It only uses factors of the form 2^n (i.e. ldexp(1.0, n)) for simplicity.
200// - The error will be zero in many practical instances. For example, if x
201// contains only integers with low magnitude; or if x contains doubles whose
202// exponents cover a small range.
203// - It chooses the factor as high as possible under the given constraints, as
204// a result the numbers produced may be large. To balance this, we recommend
205// to divide the scaled integers by their gcd() which will result in no loss
206// of precision and will help in many practical cases.
207//
208// TODO(user): incorporate the gcd computation here? The issue is that I am
209// not sure if I just do factor /= gcd that round(x * factor) will be the same.
210void GetBestScalingOfDoublesToInt64(absl::Span<const double> input,
211 int64_t max_absolute_sum,
212 double* scaling_factor,
214
215// Returns the scaling factor like above with the extra conditions:
216// - The sum over i of min(0, round(factor * x[i])) >= -max_sum.
217// - The sum over i of max(0, round(factor * x[i])) <= max_sum.
218// For any possible values of the x[i] such that x[i] is in [lb[i], ub[i]].
219double GetBestScalingOfDoublesToInt64(absl::Span<const double> input,
220 absl::Span<const double> lb,
221 absl::Span<const double> ub,
222 int64_t max_absolute_sum);
223// This computes:
224//
225// The max_relative_coeff_error, which is the maximum over all coeff of
226// |round(factor * x[i]) / (factor * x[i]) - 1|.
227//
228// The max_scaled_sum_error which is a bound on the maximum difference between
229// the exact scaled sum and the rounded one. One needs to divide this by
230// scaling_factor to have the maximum absolute error on the original sum.
231void ComputeScalingErrors(absl::Span<const double> input,
232 absl::Span<const double> lb,
233 absl::Span<const double> ub, double scaling_factor,
235 double* max_scaled_sum_error);
236
237// Returns the Greatest Common Divisor of the numbers
238// round(fabs(x[i] * scaling_factor)). The numbers 0 are ignored and if they are
239// all zero then the result is 1. Note that round(fabs()) is the same as
240// fabs(round()) since the numbers are rounded away from zero.
241int64_t ComputeGcdOfRoundedDoubles(absl::Span<const double> x,
242 double scaling_factor);
243
244// Returns alpha * x + (1 - alpha) * y.
245template <typename FloatType>
246inline FloatType Interpolate(FloatType x, FloatType y, FloatType alpha) {
247 return alpha * x + (1 - alpha) * y;
248}
249
250// This is a fast implementation of the C99 function ilogb for normalized
251// doubles with the caveat that it returns -1023 for zero, and 1024 for infinity
252// an NaNs.
253int fast_ilogb(double value);
254
255// This is a fast implementation of the C99 function scalbn, with the caveat
256// that it works on normalized numbers and if the result underflows, overflows,
257// or is applied to a NaN or an +-infinity, the result is undefined behavior.
258// Note that the version of the function that takes a reference, modifies the
259// given value.
260double fast_scalbn(double value, int exponent);
261void fast_scalbn_inplace(double& mutable_value, int exponent);
262
263} // namespace operations_research
264
265#endif // OR_TOOLS_UTIL_FP_UTILS_H_
IntegerValue y
int64_t value
In SWIG mode, we don't want anything besides these top-level includes.
int fast_ilogb(double value)
Definition fp_utils.cc:233
bool IsSmallerWithinTolerance(FloatType x, FloatType y, FloatType tolerance)
Definition fp_utils.h:159
bool IsIntegerWithinTolerance(FloatType x, FloatType tolerance)
Definition fp_utils.h:167
double fast_scalbn(double value, int exponent)
Definition fp_utils.cc:246
double GetBestScalingOfDoublesToInt64(absl::Span< const double > input, absl::Span< const double > lb, absl::Span< const double > ub, int64_t max_absolute_sum)
Definition fp_utils.cc:186
FloatType Interpolate(FloatType x, FloatType y, FloatType alpha)
Returns alpha * x + (1 - alpha) * y.
Definition fp_utils.h:246
bool AreWithinAbsoluteOrRelativeTolerances(FloatType x, FloatType y, FloatType relative_tolerance, FloatType absolute_tolerance)
Definition fp_utils.h:126
bool AreWithinAbsoluteTolerance(FloatType x, FloatType y, FloatType absolute_tolerance)
Definition fp_utils.h:147
void ComputeScalingErrors(absl::Span< const double > input, absl::Span< const double > lb, absl::Span< const double > ub, double scaling_factor, double *max_relative_coeff_error, double *max_scaled_sum_error)
Definition fp_utils.cc:177
int64_t ComputeGcdOfRoundedDoubles(absl::Span< const double > x, double scaling_factor)
Definition fp_utils.cc:209
bool IsPositiveOrNegativeInfinity(FloatType x)
Definition fp_utils.h:113
void fast_scalbn_inplace(double &mutable_value, int exponent)
Definition fp_utils.cc:242
static int input(yyscan_t yyscanner)
const Variable x
Definition qp_tests.cc:127
double max_relative_coeff_error
Definition lp_utils.cc:737