22 #ifndef OR_TOOLS_UTIL_FP_UTILS_H_
23 #define OR_TOOLS_UTIL_FP_UTILS_H_
26 #pragma fenv_access(on)
32 #include <xmmintrin.h>
42 static inline double isnan(
double value) {
return _isnan(
value); }
43 static inline double round(
double value) {
return floor(
value + 0.5); }
44 #elif defined(__APPLE__) || __GNUC__ >= 5
65 #elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
73 #elif defined(__x86_64__) && defined(__GLIBC__)
81 #elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__) && \
84 excepts &= FE_ALL_EXCEPT;
85 #if defined(__APPLE__)
86 fenv_.__control &= ~excepts;
87 #elif defined(__FreeBSD__)
88 fenv_.__x87.__control &= ~excepts;
90 fenv_.__control_word &= ~excepts;
92 fenv_.__mxcsr &= ~(excepts << 7);
100 #elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
102 mutable fenv_t saved_fenv_;
106 template <
typename FloatType>
108 return x == std::numeric_limits<FloatType>::infinity() ||
109 x == -std::numeric_limits<FloatType>::infinity();
119 template <
typename FloatType>
121 FloatType relative_tolerance,
122 FloatType absolute_tolerance) {
129 const FloatType difference = fabs(x - y);
130 if (difference <= absolute_tolerance) {
133 const FloatType largest_magnitude =
std::max(fabs(x), fabs(y));
134 return difference <= largest_magnitude * relative_tolerance;
140 template <
typename FloatType>
142 FloatType absolute_tolerance) {
147 return fabs(x - y) <= absolute_tolerance;
152 template <
typename FloatType>
155 return x <= y + tolerance *
std::max(1.0,
std::min(std::abs(x), std::abs(y)));
160 template <
typename FloatType>
164 return std::abs(x - std::round(x)) <= tolerance;
170 #define EXPECT_COMPARABLE(expected, obtained, epsilon) \
171 EXPECT_TRUE(operations_research::AreWithinAbsoluteOrRelativeTolerances( \
172 expected, obtained, epsilon, epsilon)) \
173 << obtained << " != expected value " << expected \
174 << " within epsilon = " << epsilon;
176 #define EXPECT_NOTCOMPARABLE(expected, obtained, epsilon) \
177 EXPECT_FALSE(operations_research::AreWithinAbsoluteOrRelativeTolerances( \
178 expected, obtained, epsilon, epsilon)) \
179 << obtained << " == expected value " << expected \
180 << " within epsilon = " << epsilon;
206 int64 max_absolute_sum,
207 double* scaling_factor,
215 const std::vector<double>& lb,
216 const std::vector<double>& ub,
217 int64 max_absolute_sum);
227 const std::vector<double>& lb,
228 const std::vector<double>& ub,
229 const double scaling_factor,
231 double* max_scaled_sum_error);
238 double scaling_factor);
241 template <
typename FloatType>
242 inline FloatType
Interpolate(FloatType x, FloatType y, FloatType alpha) {
243 return alpha * x + (1 - alpha) * y;
#define DCHECK_LE(val1, val2)
#define CHECK_EQ(val1, val2)
#define DCHECK_GT(val1, val2)
void EnableExceptions(int excepts)
~ScopedFloatingPointEnv()
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
bool IsSmallerWithinTolerance(FloatType x, FloatType y, FloatType tolerance)
bool IsIntegerWithinTolerance(FloatType x, FloatType tolerance)
FloatType Interpolate(FloatType x, FloatType y, FloatType alpha)
bool AreWithinAbsoluteOrRelativeTolerances(FloatType x, FloatType y, FloatType relative_tolerance, FloatType absolute_tolerance)
int64 ComputeGcdOfRoundedDoubles(const std::vector< double > &x, double scaling_factor)
void ComputeScalingErrors(const std::vector< double > &input, const std::vector< double > &lb, const std::vector< double > &ub, double scaling_factor, double *max_relative_coeff_error, double *max_scaled_sum_error)
bool AreWithinAbsoluteTolerance(FloatType x, FloatType y, FloatType absolute_tolerance)
bool IsPositiveOrNegativeInfinity(FloatType x)
double GetBestScalingOfDoublesToInt64(const std::vector< double > &input, const std::vector< double > &lb, const std::vector< double > &ub, int64 max_absolute_sum)
static int input(yyscan_t yyscanner)
double max_relative_coeff_error