TBTK
Need a break? Support the development by playing Polarity Puzzles
Real.h
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
17 
24 #ifndef COM_DAFER45_TBTK_ARBITRARY_PRECISION_REAL
25 #define COM_DAFER45_TBTK_ARBITRARY_PRECISION_REAL
26 
27 #include "TBTK/Streams.h"
28 
29 #include <string>
30 
31 //cstddef is needed to work around compilation issue. (See
32 //https://gcc.gnu.org/gcc-4.9/porting_to.html)
33 #include <cstddef>
34 #include <gmpxx.h>
35 
36 namespace TBTK{
37 namespace ArbitraryPrecision{
38 
39 class Real{
40 public:
45  Real();
46 
48  ~Real();
49 
53  Real(const Real &real);
54 
58  Real(unsigned int precision);
59 
63  Real(unsigned int precision, double value);
64 
68  Real(unsigned int precision, const std::string &value);
69 
75  Real& operator=(const Real &real);
76 
82  Real& operator=(double rhs);
83 
89  Real& operator=(const std::string &rhs);
90 
96  Real& operator+=(const Real &rhs);
97 
103  Real operator+(const Real &rhs) const;
104 
110  Real& operator-=(const Real &rhs);
111 
117  Real operator-(const Real &rhs) const;
118 
124  Real& operator*=(const Real &rhs);
125 
131  Real operator*(const Real &rhs) const;
132 
138  Real& operator/=(const Real &rhs);
139 
145  Real operator/(const Real &rhs) const;
146 
150  Real operator-() const;
151 
156  friend std::ostream& operator<<(std::ostream &os, const Real &real);
157 
161  const mpf_t& getValue() const;
162 
166  double getDouble() const;
167 
171  mp_bitcnt_t getPrecision() const;
172 private:
174  mpf_t value;
175 
177  bool isInitialized;
178 };
179 
180 inline Real::Real(){
181  isInitialized = false;
182 }
183 
184 inline Real::~Real(){
185  if(isInitialized)
186  mpf_clear(value);
187 }
188 
189 inline Real::Real(const Real &real){
190  mpf_init2(value, real.getPrecision());
191  mpf_set(value, real.value);
192  isInitialized = true;
193 }
194 
195 inline Real::Real(unsigned int precision){
196  mpf_init2(value, precision);
197  isInitialized = true;
198 }
199 
200 inline Real::Real(unsigned int precision, double value){
201  mpf_init2(this->value, precision);
202  operator=(value);
203  isInitialized = true;
204 }
205 
206 inline Real::Real(unsigned int precision, const std::string &value){
207  mpf_init2(this->value, precision);
208  operator=(value);
209  isInitialized = true;
210 }
211 
212 inline Real& Real::operator=(const Real &rhs){
213  if(this != &rhs){
214  if(isInitialized)
215  mpf_clear(value);
216 
217  mpf_init2(value, rhs.getPrecision());
218  mpf_set(value, rhs.value);
219  isInitialized = true;
220  }
221 
222  return *this;
223 }
224 
225 inline Real& Real::operator=(double rhs){
226  mpf_set_d(value, rhs);
227 
228  return *this;
229 }
230 
231 inline Real& Real::operator=(const std::string &rhs){
232  mpf_set_str(value, rhs.c_str(), 10);
233 
234  return *this;
235 }
236 
237 inline Real& Real::operator+=(const Real &rhs){
238  mpf_add(value, value, rhs.value);
239 
240  return *this;
241 }
242 
243 inline Real Real::operator+(const Real &rhs) const{
244  Real real = *this;
245 
246  return real += rhs;
247 }
248 
249 inline Real& Real::operator-=(const Real &rhs){
250  mpf_sub(value, value, rhs.value);
251 
252  return *this;
253 }
254 
255 inline Real Real::operator-(const Real &rhs) const{
256  Real real = *this;
257 
258  return real -= rhs;
259 }
260 
261 inline Real& Real::operator*=(const Real &rhs){
262  mpf_mul(value, value, rhs.value);
263 
264  return *this;
265 }
266 
267 inline Real Real::operator*(const Real &rhs) const{
268  Real real = *this;
269 
270  return real *= rhs;
271 }
272 
273 inline Real& Real::operator/=(const Real &rhs){
274  mpf_div(value, value, rhs.value);
275 
276  return *this;
277 }
278 
279 inline Real Real::operator/(const Real &rhs) const{
280  Real real = *this;
281 
282  return real /= rhs;
283 }
284 
285 inline Real Real::operator-() const{
286  Real real;
287  mpf_init2(real.value, getPrecision());
288  mpf_neg(real.value, value);
289  real.isInitialized = true;
290 
291  return real;
292 }
293 
294 inline std::ostream& operator<<(std::ostream &os, const Real &real){
295  os << real.value;
296 
297  return os;
298 }
299 
300 inline const mpf_t& Real::getValue() const{
301  return value;
302 }
303 
304 inline double Real::getDouble() const{
305  return mpf_get_d(value);
306 }
307 
308 inline mp_bitcnt_t Real::getPrecision() const{
309  return mpf_get_prec(value);
310 }
311 
312 }; //End of namespace ArbitraryPrecision
313 }; //End of namesapce TBTK
314 
315 #endif
316 
Real & operator/=(const Real &rhs)
Definition: Real.h:103
Real()
Definition: Real.h:36
Real & operator+=(const Real &rhs)
Definition: Real.h:70
Real & operator*=(const Real &rhs)
Definition: Real.h:92
Definition: Boolean.h:32
std::ostream & operator<<(std::ostream &stream, const HoppingAmplitude &hoppingAmplitude)
Definition: HoppingAmplitude.h:315
Real & operator=(double rhs)
Definition: Real.h:59
const Vector2d operator*(double lhs, const Vector2d &rhs)
Definition: Vector2d.h:129
Streams for TBTK output.
Real & operator-=(const Real &rhs)
Definition: Real.h:81