Wildmeshing Toolkit
Rational.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Core>
4 
5 #include <gmp.h>
6 #include <iostream>
7 #include <string>
8 
9 
10 namespace wmtk {
11 
12 class Rational
13 {
14 public:
15  Rational(bool rounded = false);
16  Rational(int v, bool rounded = false);
17  Rational(double d, bool rounded = false);
18  Rational(const mpq_t& v_);
19  Rational(const Rational& other);
20  Rational(const Rational& other, bool rounded);
21  Rational(const Eigen::VectorX<char>& data);
22  Rational(const std::string& data, bool rounded = false);
23 
24  Rational& operator=(const Rational& x);
25  Rational& operator=(const double x);
26 
27  template <typename T>
28  void init(const T& v)
29  {
30  mpq_set(value, v);
31  m_is_rounded = false;
32  }
33 
34  ~Rational();
35 
36  void canonicalize();
37 
38  friend Rational operator+(const Rational& x, const Rational& y);
39  friend Rational operator-(const Rational& x, const Rational& y);
40 
41  friend Rational operator-(const Rational& x);
42 
43  friend Rational pow(const Rational& x, int p);
44  friend Rational abs(const Rational& r0);
45  int get_sign() const;
46 
47  friend Rational operator*(const Rational& x, const Rational& y);
48  friend Rational operator/(const Rational& x, const Rational& y);
49 
50  //> < ==
51  friend bool operator<(const Rational& r, const Rational& r1) { return cmp(r, r1) < 0; }
52  friend bool operator>(const Rational& r, const Rational& r1) { return cmp(r, r1) > 0; }
53  friend bool operator<=(const Rational& r, const Rational& r1) { return cmp(r, r1) <= 0; }
54  friend bool operator>=(const Rational& r, const Rational& r1) { return cmp(r, r1) >= 0; }
55 
56  friend bool operator==(const Rational& r, const Rational& r1);
57  friend bool operator!=(const Rational& r, const Rational& r1);
58 
59  // to double
60  double to_double() const;
61  explicit operator double() const;
62 
63  friend std::ostream& operator<<(std::ostream& os, const Rational& r);
64 
65  inline void round()
66  {
67  if (m_is_rounded) return;
68 
69  d_value = this->to_double();
70  m_is_rounded = true;
71  mpq_clear(value);
72  }
73 
74  inline bool can_be_rounded()
75  {
76  if (m_is_rounded) return true;
77 
78  return this->to_double() == *this;
79  }
80 
81  void init_from_binary(const std::string& v);
82  std::string to_binary() const;
83 
84  std::string serialize() const;
85 
86  inline bool is_rounded() const { return m_is_rounded; }
87 
88 
89 private:
90  mpq_t value;
91  double d_value;
93 
94  friend int cmp(const Rational& r, const Rational& r1);
95 
96  std::string numerator() const;
97  std::string denominator() const;
98 };
99 
100 
101 } // namespace wmtk
Rational(bool rounded=false)
Definition: Rational.cpp:22
friend bool operator<=(const Rational &r, const Rational &r1)
Definition: Rational.hpp:53
friend std::ostream & operator<<(std::ostream &os, const Rational &r)
Definition: Rational.cpp:340
double to_double() const
Definition: Rational.cpp:317
friend bool operator!=(const Rational &r, const Rational &r1)
Definition: Rational.cpp:311
friend Rational pow(const Rational &x, int p)
Definition: Rational.cpp:166
std::string numerator() const
Definition: Rational.cpp:427
Rational & operator=(const Rational &x)
Definition: Rational.cpp:230
bool can_be_rounded()
Definition: Rational.hpp:74
friend Rational operator*(const Rational &x, const Rational &y)
Definition: Rational.cpp:178
friend bool operator>(const Rational &r, const Rational &r1)
Definition: Rational.hpp:52
friend bool operator<(const Rational &r, const Rational &r1)
Definition: Rational.hpp:51
friend Rational operator+(const Rational &x, const Rational &y)
Definition: Rational.cpp:104
bool is_rounded() const
Definition: Rational.hpp:86
std::string serialize() const
Definition: Rational.cpp:366
friend Rational operator-(const Rational &x, const Rational &y)
Definition: Rational.cpp:130
void round()
Definition: Rational.hpp:65
friend bool operator==(const Rational &r, const Rational &r1)
Definition: Rational.cpp:286
void init_from_binary(const std::string &v)
Definition: Rational.cpp:346
friend bool operator>=(const Rational &r, const Rational &r1)
Definition: Rational.hpp:54
std::string denominator() const
Definition: Rational.cpp:449
friend Rational abs(const Rational &r0)
Definition: Rational.cpp:328
void canonicalize()
Definition: Rational.cpp:9
double d_value
Definition: Rational.hpp:91
friend int cmp(const Rational &r, const Rational &r1)
Definition: Rational.cpp:259
std::string to_binary() const
Definition: Rational.cpp:350
void init(const T &v)
Definition: Rational.hpp:28
friend Rational operator/(const Rational &x, const Rational &y)
Definition: Rational.cpp:204
int get_sign() const
Definition: Rational.cpp:15
bool m_is_rounded
Definition: Rational.hpp:92
Definition: Accessor.hpp:6