14 #ifndef OR_TOOLS_BASE_MATHUTIL_H_
15 #define OR_TOOLS_BASE_MATHUTIL_H_
22 #include "absl/base/casts.h"
38 template <
typename IntegralType>
40 IntegralType denominator) {
42 const IntegralType rounded_toward_zero = numerator / denominator;
43 const IntegralType intermediate_product = rounded_toward_zero * denominator;
44 const bool needs_adjustment =
45 (rounded_toward_zero >= 0) &&
46 ((denominator > 0 && numerator > intermediate_product) ||
47 (denominator < 0 && numerator < intermediate_product));
48 const IntegralType adjustment =
static_cast<IntegralType
>(needs_adjustment);
49 const IntegralType ceil_of_ratio = rounded_toward_zero + adjustment;
52 template <
typename IntegralType>
54 IntegralType denominator) {
56 const IntegralType rounded_toward_zero = numerator / denominator;
57 const IntegralType intermediate_product = rounded_toward_zero * denominator;
58 const bool needs_adjustment =
59 (rounded_toward_zero <= 0) &&
60 ((denominator > 0 && numerator < intermediate_product) ||
61 (denominator < 0 && numerator > intermediate_product));
62 const IntegralType adjustment =
static_cast<IntegralType
>(needs_adjustment);
63 const IntegralType floor_of_ratio = rounded_toward_zero - adjustment;
64 return floor_of_ratio;
68 static unsigned int GCD(
unsigned int x,
unsigned int y) {
70 unsigned int r = x % y;
95 static T
Abs(
const T x) {
96 return x > 0 ? x : -x;
100 template <
typename T>
107 static int64_t
GCD64(int64_t x, int64_t y) {
118 template <
typename T>
119 static T
IPow(T base,
int exp) {
120 return pow(base, exp);
123 template <
class IntOut,
class FloatIn>
129 if (x > -0.5 && x < 0.5) {
133 return static_cast<IntOut
>(0);
135 return static_cast<IntOut
>(x < 0 ? (x - 0.5) : (x + 0.5));
#define DCHECK_NE(val1, val2)
#define DCHECK_GE(val1, val2)
static IntegralType CeilOfRatio(IntegralType numerator, IntegralType denominator)
static int64_t FastInt64Round(double x)
static unsigned int GCD(unsigned int x, unsigned int y)
static T IPow(T base, int exp)
static IntegralType FloorOfRatio(IntegralType numerator, IntegralType denominator)
static T Square(const T x)
static IntOut Round(FloatIn x)
static unsigned int LeastCommonMultiple(unsigned int a, unsigned int b)
static int64_t GCD64(int64_t x, int64_t y)
Collection of objects used to extend the Constraint Solver library.