Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
mathutil.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#ifndef OR_TOOLS_BASE_MATHUTIL_H_
15#define OR_TOOLS_BASE_MATHUTIL_H_
16
17#include <math.h>
18
19#include <algorithm>
20#include <cstdint>
21#include <vector>
22
23#include "absl/base/casts.h"
25#include "ortools/base/macros.h"
26
27namespace operations_research {
28class MathUtil {
29 public:
30 // CeilOfRatio<IntegralType>
31 // FloorOfRatio<IntegralType>
32 // Returns the ceil (resp. floor) of the ratio of two integers.
33 //
34 // IntegralType: any integral type, whether signed or not.
35 // numerator: any integer: positive, negative, or zero.
36 // denominator: a non-zero integer, positive or negative.
37 template <typename IntegralType>
38 static IntegralType CeilOfRatio(IntegralType numerator,
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;
49 return ceil_of_ratio;
50 }
51 template <typename IntegralType>
52 static IntegralType FloorOfRatio(IntegralType numerator,
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;
64 }
65
66 // Returns the greatest common divisor of two unsigned integers x and y.
67 static unsigned int GCD(unsigned int x, unsigned int y) {
68 while (y != 0) {
69 unsigned int r = x % y;
70 x = y;
71 y = r;
72 }
73 return x;
74 }
75
76 // Returns the least common multiple of two unsigned integers. Returns zero
77 // if either is zero.
78 static unsigned int LeastCommonMultiple(unsigned int a, unsigned int b) {
79 if (a > b) {
80 return (a / MathUtil::GCD(a, b)) * b;
81 } else if (a < b) {
82 return (b / MathUtil::GCD(b, a)) * a;
83 } else {
84 return a;
85 }
86 }
87
88 // Absolute value of x.
89 // Works correctly for unsigned types and
90 // for special floating point values.
91 // Note: 0.0 and -0.0 are not differentiated by Abs (Abs(0.0) is -0.0),
92 // which should be OK: see the comment for Max above.
93 template <typename T>
94 static T Abs(const T x) {
95 return x > 0 ? x : -x;
96 }
97
98 // Returns the square of x.
99 template <typename T>
100 static T Square(const T x) {
101 return x * x;
102 }
103
104 // Euclid's Algorithm.
105 // Returns: the greatest common divisor of two unsigned integers x and y.
106 static int64_t GCD64(int64_t x, int64_t y) {
107 DCHECK_GE(x, 0);
108 DCHECK_GE(y, 0);
109 while (y != 0) {
110 int64_t r = x % y;
111 x = y;
112 y = r;
113 }
114 return x;
115 }
116
117 template <typename T>
118 static T IPow(T base, int exp) {
119 return pow(base, exp);
120 }
121
122 template <class IntOut, class FloatIn>
123 static IntOut Round(FloatIn x) {
124 // We don't use sgn(x) below because there is no need to distinguish the
125 // (x == 0) case. Also note that there are specialized faster versions
126 // of this function for Intel, ARM and PPC processors at the bottom
127 // of this file.
128 if (x > -0.5 && x < 0.5) {
129 // This case is special, because for largest floating point number
130 // below 0.5, the addition of 0.5 yields 1 and this would lead
131 // to incorrect result.
132 return static_cast<IntOut>(0);
133 }
134 return static_cast<IntOut>(x < 0 ? (x - 0.5) : (x + 0.5));
135 }
136
137 static int64_t FastInt64Round(double x) { return Round<int64_t>(x); }
138};
139} // namespace operations_research
140
141#endif // OR_TOOLS_BASE_MATHUTIL_H_
static T Square(const T x)
Returns the square of x.
Definition mathutil.h:100
static T IPow(T base, int exp)
Definition mathutil.h:118
static IntOut Round(FloatIn x)
Definition mathutil.h:123
static int64_t FastInt64Round(double x)
Definition mathutil.h:137
static unsigned int LeastCommonMultiple(unsigned int a, unsigned int b)
Definition mathutil.h:78
static unsigned int GCD(unsigned int x, unsigned int y)
Returns the greatest common divisor of two unsigned integers x and y.
Definition mathutil.h:67
static T Abs(const T x)
Definition mathutil.h:94
static int64_t GCD64(int64_t x, int64_t y)
Definition mathutil.h:106
static IntegralType CeilOfRatio(IntegralType numerator, IntegralType denominator)
Definition mathutil.h:38
static IntegralType FloorOfRatio(IntegralType numerator, IntegralType denominator)
Definition mathutil.h:52
int64_t b
Definition table.cc:45
int64_t a
Definition table.cc:44
In SWIG mode, we don't want anything besides these top-level includes.
const Variable x
Definition qp_tests.cc:127