dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
math.cpp
1
7#include "dlinear/util/math.h"
8
9#include <cmath>
10#include <limits>
11
12#include "dlinear/util/exception.h"
13#include "dlinear/util/logging.h"
14
15namespace dlinear {
16bool IsInteger(const double v) {
17 // v should be in [int_min, int_max].
18 if (!((std::numeric_limits<int>::lowest() <= v) && (v <= std::numeric_limits<int>::max()))) {
19 return false;
20 }
21 double intpart; // dummy variable
22 return std::modf(v, &intpart) == 0.0;
23}
24
25bool IsInteger(const mpq_class &v) {
26 // v should be in [int_min, int_max].
27 if (!((std::numeric_limits<int>::lowest() <= v) && (v <= std::numeric_limits<int>::max()))) {
28 return false;
29 }
30 mpz_class f{v};
31 return v == f;
32}
33
34int ConvertInt64ToInt(const std::int64_t v) {
35 if (std::numeric_limits<int>::min() <= v && v <= std::numeric_limits<int>::max()) return static_cast<int>(v);
36 DLINEAR_RUNTIME_ERROR_FMT("Fail to convert a std::int64_t value {} to int", v);
37}
38
39double ConvertInt64ToDouble(const std::int64_t v) {
40 constexpr std::int64_t m{1UL << static_cast<unsigned>(std::numeric_limits<double>::digits)};
41 if (-m <= v && v <= m) return static_cast<double>(v);
42 DLINEAR_RUNTIME_ERROR_FMT("Fail to convert a std::int64_t value {} to double", v);
43}
44
45mpq_class ConvertInt64ToRational(std::int64_t v) { return mpq_class{v, 1}; }
46} // namespace dlinear
Global namespace for the dlinear library.
int ConvertInt64ToInt(const std::int64_t v)
Convert v of int64_t to int.
Definition math.cpp:34
double ConvertInt64ToDouble(const std::int64_t v)
Convert v of int64_t to double.
Definition math.cpp:39
bool IsInteger(const double v)
Returns true if v is represented by int.
Definition math.cpp:16
mpq_class ConvertInt64ToRational(std::int64_t v)
Convert v of int64_t to rational.
Definition math.cpp:45