carl  24.04
Computer ARithmetic Library
operations.h
Go to the documentation of this file.
1 /*
2  * File: operations_native.h
3  * Author: Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  *
5  * This file should never be included directly but only via operations.h
6  */
7 
8 #pragma once
9 
10 #ifndef INCLUDED_FROM_NUMBERS_H
11 static_assert(false, "This file may only be included indirectly by numbers.h");
12 #endif
13 
14 #include <cassert>
15 #include <cmath>
16 #include <limits>
17 #include <type_traits>
18 
19 namespace carl {
20 
21 /**
22  * Informational functions
23  *
24  * The following functions return informations about the given numbers.
25  */
26 
27 inline bool is_zero(double n) {
28  return std::fpclassify(n) == FP_ZERO;
29 }
30 
31 inline bool is_positive(double n) {
32  return n > 0;
33 }
34 
35 inline bool is_negative(double n) {
36  return n < 0;
37 }
38 
39 inline bool isNaN(double d) {
40  if constexpr (std::is_same<decltype(std::isnan(d)), bool>::value) {
41  return std::isnan(d);
42  } else if constexpr (std::is_same<decltype(std::isnan(d)), int>::value) {
43  return std::isnan(d) != 0;
44  }
45 }
46 inline bool isInf(double d) {
47  if constexpr (std::is_same<decltype(std::isinf(d)), bool>::value) {
48  return std::isinf(d);
49  } else if constexpr (std::is_same<decltype(std::isinf(d)), int>::value) {
50  return std::isinf(d) != 0;
51  }
52 }
53 
54 inline bool is_number(double d) {
55  return !isNaN(d) && !isInf(d);
56 }
57 
58 inline bool is_integer(double d) {
59  double tmp;
60  return std::fpclassify(std::modf(d, &tmp)) == FP_ZERO;
61 }
62 
63 inline bool is_integer(sint /*unused*/) {
64  return true;
65 }
66 
67 inline std::size_t bitsize(unsigned /*unused*/) {
68  return sizeof(unsigned) * 8;
69 }
70 
71 /**
72  * Conversion functions
73  *
74  * The following function convert types to other types.
75  */
76 
77 inline double to_double(sint n) {
78  return double(n);
79 }
80 inline double to_double(double n) {
81  return n;
82 }
83 
84 template<typename Integer>
85 inline Integer to_int(double n);
86 
87 template<>
88 inline sint to_int<sint>(double n) {
89  return sint(n);
90 }
91 
92 template<>
93 inline uint to_int<uint>(double n) {
94  return uint(n);
95 }
96 
97 template<>
98 inline double rationalize(double n) {
99  return n;
100 }
101 
102 template<typename T>
103 inline typename std::enable_if<std::is_arithmetic<typename remove_all<T>::type>::value, std::string>::type toString(const T& n, bool /*unused*/) {
104  return std::to_string(n);
105 }
106 //inline std::string toString(sint n, bool /*unused*/) {
107 // return std::to_string(n);
108 //}
109 //inline std::string toString(uint n, bool /*unused*/) {
110 // return std::to_string(n);
111 //}
112 //inline std::string toString(double n, bool /*unused*/) {
113 // return std::to_string(n);
114 //}
115 
116 /**
117  * Basic Operators
118  *
119  * The following functions implement simple operations on the given numbers.
120  */
121 inline double floor(double n) {
122  return std::floor(n);
123 }
124 inline double ceil(double n) {
125  return std::ceil(n);
126 }
127 inline double abs(double n) {
128  return std::abs(n);
129 }
130 
131 inline uint mod(uint n, uint m) {
132  return n % m;
133 }
134 inline sint mod(sint n, sint m) {
135  return n % m;
136 }
137 
138 inline sint remainder(sint n, sint m) {
139  return n % m;
140 }
141 inline sint div(sint n, sint m) {
142  assert(n % m == 0);
143  return n / m;
144 }
145 inline sint quotient(sint n, sint m) {
146  return n / m;
147 }
148 inline void divide(sint dividend, sint divisor, sint& quo, sint& rem) {
149  quo = quotient(dividend, divisor);
150  rem = remainder(dividend, divisor);
151 }
152 
153 inline double sin(double in) {
154  return std::sin(in);
155 }
156 
157 inline double cos(double in) {
158  return std::cos(in);
159 }
160 
161 inline double acos(double in) {
162  return std::acos(in);
163 }
164 
165 inline double sqrt(double in) {
166  return std::sqrt(in);
167 }
168 
169 inline std::pair<double, double> sqrt_safe(double in) {
170  return std::make_pair(std::sqrt(in), std::sqrt(in));
171 }
172 
173 inline double pow(double in, uint exp) {
174  return std::pow(in, exp);
175 }
176 
177 inline double log(double in) {
178  return std::log(in);
179 }
180 inline double log10(double in) {
181  return std::log10(in);
182 }
183 
184 /**
185  * Returns the highest power of two below n.
186  *
187  * Can also be seen as the highest bit set in n.
188  * @param n
189  * @return
190  */
191 template<typename Number>
192 inline Number highestPower(const Number& n) {
193  static_assert(std::is_fundamental<Number>::value, "Only works on native types.");
194  uint iterations = 0;
195  // Number has 2^k Bits, we do k iterations
196  if (sizeof(Number) == 2) {
197  iterations = 4;
198  } else if (sizeof(Number) == 4) {
199  iterations = 5;
200  } else if (sizeof(Number) == 8) {
201  iterations = 6;
202  }
203  assert(iterations > 0);
204 
205  Number res = n;
206  for (uint i = 0; i < iterations; i++) {
207  res |= res >> (1u << i);
208  }
209  res -= res >> 1;
210  return res;
211 }
212 
213 } // namespace carl
mpz_class Integer
carl is the main namespace for the library.
double sqrt(double in)
Definition: operations.h:165
T rationalize(const PreventConversion< mpq_class > &)
Interval< Number > acos(const Interval< Number > &i)
Definition: Trigonometry.h:58
Interval< Number > ceil(const Interval< Number > &_in)
Method which returns the next larger integer of the passed number or the number itself,...
Definition: Interval.h:1535
std::uint64_t uint
Definition: numbers.h:16
double sin(double in)
Definition: operations.h:153
std::string toString(Relation r)
Definition: Relation.h:73
Interval< Number > abs(const Interval< Number > &_in)
Method which returns the absolute value of the passed number.
Definition: Interval.h:1511
bool is_positive(const cln::cl_I &n)
Definition: operations.h:39
bool isInf(double d)
Definition: operations.h:46
Interval< Number > floor(const Interval< Number > &_in)
Method which returns the next smaller integer of this number or the number itself,...
Definition: Interval.h:1523
Interval< Number > quotient(const Interval< Number > &_lhs, const Interval< Number > &_rhs)
Implements the division with remainder.
Definition: Interval.h:1488
double pow(double in, uint exp)
Definition: operations.h:173
double abs(double n)
Definition: operations.h:127
cln::cl_I mod(const cln::cl_I &a, const cln::cl_I &b)
Calculate the remainder of the integer division.
Definition: operations.h:445
double ceil(double n)
Definition: operations.h:124
double floor(double n)
Basic Operators.
Definition: operations.h:121
bool isNaN(double d)
Definition: operations.h:39
Number highestPower(const Number &n)
Returns the highest power of two below n.
Definition: operations.h:192
Interval< Number > exp(const Interval< Number > &i)
Definition: Exponential.h:10
cln::cl_RA log10(const cln::cl_RA &n)
Definition: operations.h:393
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
Integer to_int(const Interval< Number > &_floatInterval)
Casts the Interval to an arbitrary integer type which has a constructor for a native int.
Definition: Interval.h:1500
Interval< Number > cos(const Interval< Number > &i)
Definition: Trigonometry.h:22
Interval< Number > sqrt(const Interval< Number > &i)
Definition: Power.h:51
uint to_int< uint >(const cln::cl_I &n)
Definition: operations.h:137
void divide(const cln::cl_I &dividend, const cln::cl_I &divisor, cln::cl_I &quotient, cln::cl_I &remainder)
Definition: operations.h:326
double acos(double in)
Definition: operations.h:161
Interval< Number > div(const Interval< Number > &_lhs, const Interval< Number > &_rhs)
Implements the division which assumes that there is no remainder.
Definition: Interval.h:1476
Interval< Number > log(const Interval< Number > &i)
Definition: Exponential.h:22
bool is_integer(const Interval< Number > &n)
Definition: Interval.h:1445
bool is_negative(const cln::cl_I &n)
Definition: operations.h:47
double to_double(const cln::cl_RA &n)
Converts the given fraction to a double.
Definition: operations.h:113
std::int64_t sint
Definition: numbers.h:17
Interval< Number > sin(const Interval< Number > &i)
Definition: Trigonometry.h:10
std::pair< cln::cl_RA, cln::cl_RA > sqrt_safe(const cln::cl_RA &a)
Calculate the square root of a fraction.
double log(double in)
Definition: operations.h:177
sint to_int< sint >(const cln::cl_I &n)
Definition: operations.h:131
std::size_t bitsize(const cln::cl_I &n)
Get the bit size of the representation of a integer.
Definition: operations.h:96
cln::cl_I remainder(const cln::cl_I &a, const cln::cl_I &b)
Calculate the remainder of the integer division.
Definition: operations.h:526
double log10(double in)
Definition: operations.h:180
bool is_number(double d)
Definition: operations.h:54
double cos(double in)
Definition: operations.h:157
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11