22#ifndef OR_TOOLS_UTIL_FP_UTILS_H_
23#define OR_TOOLS_UTIL_FP_UTILS_H_
34#include "absl/log/check.h"
35#include "absl/types/span.h"
38#pragma fenv_access(on)
48static inline double isnan(
double value) {
return _isnan(
value); }
50#elif defined(__APPLE__) || __GNUC__ >= 5
71#elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
72 CHECK_EQ(0, fegetenv(&saved_fenv_));
79#elif defined(__x86_64__) && defined(__GLIBC__)
80 CHECK_EQ(0, fesetenv(&saved_fenv_));
87#elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__) && \
89 CHECK_EQ(0, fegetenv(&fenv_));
90 excepts &= FE_ALL_EXCEPT;
92 fenv_.__control &= ~excepts;
93#elif (defined(__FreeBSD__) || defined(__OpenBSD__))
94 fenv_.__x87.__control &= ~excepts;
96 fenv_.__control_word &= ~excepts;
98 fenv_.__mxcsr &= ~(excepts << 7);
99 CHECK_EQ(0, fesetenv(&fenv_));
106#elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
108 mutable fenv_t saved_fenv_;
112template <
typename FloatType>
114 return x == std::numeric_limits<FloatType>::infinity() ||
115 x == -std::numeric_limits<FloatType>::infinity();
125template <
typename FloatType>
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);
135 const FloatType difference = fabs(
x -
y);
136 if (difference <= absolute_tolerance) {
139 const FloatType largest_magnitude = std::max(fabs(
x), fabs(
y));
140 return difference <= largest_magnitude * relative_tolerance;
146template <
typename FloatType>
148 FloatType absolute_tolerance) {
149 DCHECK_LE(0.0, absolute_tolerance);
153 return fabs(
x -
y) <= absolute_tolerance;
158template <
typename FloatType>
161 return x <=
y + tolerance * std::max(1.0, std::min(std::abs(
x), std::abs(
y)));
166template <
typename FloatType>
168 DCHECK_LE(0.0, tolerance);
170 return std::abs(
x - std::round(
x)) <= tolerance;
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;
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;
211 int64_t max_absolute_sum,
212 double* scaling_factor,
220 absl::Span<const double> lb,
221 absl::Span<const double> ub,
222 int64_t max_absolute_sum);
232 absl::Span<const double> lb,
233 absl::Span<const double> ub,
double scaling_factor,
235 double* max_scaled_sum_error);
242 double scaling_factor);
245template <
typename FloatType>
247 return alpha *
x + (1 - alpha) *
y;
void EnableExceptions(int excepts)
~ScopedFloatingPointEnv()
In SWIG mode, we don't want anything besides these top-level includes.
int fast_ilogb(double value)
bool IsSmallerWithinTolerance(FloatType x, FloatType y, FloatType tolerance)
bool IsIntegerWithinTolerance(FloatType x, FloatType tolerance)
double fast_scalbn(double value, int exponent)
double GetBestScalingOfDoublesToInt64(absl::Span< const double > input, absl::Span< const double > lb, absl::Span< const double > ub, int64_t max_absolute_sum)
FloatType Interpolate(FloatType x, FloatType y, FloatType alpha)
Returns alpha * x + (1 - alpha) * y.
bool AreWithinAbsoluteOrRelativeTolerances(FloatType x, FloatType y, FloatType relative_tolerance, FloatType absolute_tolerance)
bool AreWithinAbsoluteTolerance(FloatType x, FloatType y, FloatType absolute_tolerance)
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)
int64_t ComputeGcdOfRoundedDoubles(absl::Span< const double > x, double scaling_factor)
bool IsPositiveOrNegativeInfinity(FloatType x)
void fast_scalbn_inplace(double &mutable_value, int exponent)
static int input(yyscan_t yyscanner)
double max_relative_coeff_error