14#ifndef OR_TOOLS_BASE_MATHUTIL_H_
15#define OR_TOOLS_BASE_MATHUTIL_H_
23#include "absl/base/casts.h"
37 template <
typename IntegralType>
39 IntegralType denominator) {
40 DCHECK_NE(0, denominator);
41 const IntegralType rounded_toward_zero = numerator / denominator;
42 const IntegralType intermediate_product = rounded_toward_zero * denominator;
43 const bool needs_adjustment =
44 (rounded_toward_zero >= 0) &&
45 ((denominator > 0 && numerator > intermediate_product) ||
46 (denominator < 0 && numerator < intermediate_product));
47 const IntegralType adjustment =
static_cast<IntegralType
>(needs_adjustment);
48 const IntegralType ceil_of_ratio = rounded_toward_zero + adjustment;
51 template <
typename IntegralType>
53 IntegralType denominator) {
54 DCHECK_NE(0, denominator);
55 const IntegralType rounded_toward_zero = numerator / denominator;
56 const IntegralType intermediate_product = rounded_toward_zero * denominator;
57 const bool needs_adjustment =
58 (rounded_toward_zero <= 0) &&
59 ((denominator > 0 && numerator < intermediate_product) ||
60 (denominator < 0 && numerator > intermediate_product));
61 const IntegralType adjustment =
static_cast<IntegralType
>(needs_adjustment);
62 const IntegralType floor_of_ratio = rounded_toward_zero - adjustment;
63 return floor_of_ratio;
67 static unsigned int GCD(
unsigned int x,
unsigned int y) {
69 unsigned int r =
x % y;
95 return x > 0 ?
x : -
x;
106 static int64_t
GCD64(int64_t
x, int64_t y) {
117 template <
typename T>
118 static T
IPow(T base,
int exp) {
119 return pow(base, exp);
122 template <
class IntOut,
class FloatIn>
128 if (
x > -0.5 &&
x < 0.5) {
132 return static_cast<IntOut
>(0);
134 return static_cast<IntOut
>(
x < 0 ? (
x - 0.5) : (
x + 0.5));
static T Square(const T x)
Returns the square of x.
static T IPow(T base, int exp)
static IntOut Round(FloatIn x)
static int64_t FastInt64Round(double x)
static unsigned int LeastCommonMultiple(unsigned int a, unsigned int b)
static unsigned int GCD(unsigned int x, unsigned int y)
Returns the greatest common divisor of two unsigned integers x and y.
static int64_t GCD64(int64_t x, int64_t y)
static IntegralType CeilOfRatio(IntegralType numerator, IntegralType denominator)
static IntegralType FloorOfRatio(IntegralType numerator, IntegralType denominator)
In SWIG mode, we don't want anything besides these top-level includes.