-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberRepr.cpp
232 lines (210 loc) · 5.97 KB
/
NumberRepr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// Created by Lennart Oymanns on 03.05.17.
//
#include "NumberRepr.hpp"
using namespace Equation;
NumberRepr::NumberRepr() {
value_double = 0.0;
isValid = false;
rational = Rational_t(0);
}
NumberRepr::NumberRepr(const std::string &s) {
auto dot_pos = s.find_first_of("eE.");
if (dot_pos != std::string::npos) {
value_double = strtod(s.c_str(), NULL);
isFraction = false;
} else {
rational = Integer_t(s);
isFraction = true;
}
isValid = true;
}
/** SetFromInt sets the value of this number to fraction num/denom. */
void NumberRepr::SetFromInt(long num, long denom) {
isFraction = true;
rational = Rational_t(num, denom);
value_double = 0.0;
if (denom == 0) {
isValid = false;
} else {
isValid = true;
}
}
/** normal *= operator. If *this and rhs are both fractions the result is also a
* fraction. Otherwise the result is a floating point number. */
NumberRepr &NumberRepr::operator*=(const NumberRepr &rhs) {
if (!rhs.isValid) {
isValid = false;
}
if (rhs.isFraction && isFraction) {
rational *= rhs.rational;
return *this;
}
SetFromDouble(Double() * rhs.Double());
return *this;
}
/** normal /= operator. If *this and rhs are both fractions the result is also a
* fraction. Otherwise the result is a floating point number. If rhs is a
* fraction and equal to zero, this number is set to invalid. */
NumberRepr &NumberRepr::operator/=(const NumberRepr &rhs) {
if (!rhs.isValid) {
isValid = false;
}
if (isFraction && rhs.isFraction) {
if (rhs.rational == Rational_t(0)) {
isValid = false;
return *this;
}
rational /= rhs.rational;
return *this;
}
SetFromDouble(Double() / rhs.Double());
return *this;
}
/** normal += operator. If *this and rhs are both fractions the result is also a
* fraction. Otherwise the result is a floating point number. */
NumberRepr &NumberRepr::operator+=(const NumberRepr &rhs) {
if (!rhs.isValid) {
isValid = false;
}
if (rhs.isFraction && isFraction) {
rational += rhs.rational;
return *this;
}
SetFromDouble(Double() + rhs.Double());
return *this;
}
/** normal -= operator. If *this and rhs are both fractions the result is also a
* fraction. Otherwise the result is a floating point number. */
NumberRepr &NumberRepr::operator-=(const NumberRepr &rhs) {
if (!rhs.isValid) {
isValid = false;
}
if (rhs.isFraction && isFraction) {
rational -= rhs.rational;
return *this;
}
SetFromDouble(Double() - rhs.Double());
return *this;
}
std::ostream &operator<<(std::ostream &os, const NumberRepr &obj) {
os << obj.String();
return os;
}
/** Pow returns the result of base^exponent.
If there are any problems (e.g. the exponent is too large), Pow returns an
invalid number. */
NumberRepr NumberRepr::Pow(const NumberRepr &base, const NumberRepr &exp) {
if (!base.isValid || !exp.isValid) {
// power of an invalid number is also invalid.
auto inv = NumberRepr(0l);
inv.isValid = false;
return inv;
}
if (!base.IsFraction() || !exp.IsFraction()) {
// if either base or exponent are floating point numbers, calculate the
// floating point result.
double b = base.Double();
double e = exp.Double();
return NumberRepr(pow(b, e));
}
if (exp.Denominator() == Integer_t(1)) {
// calculate power if it is not a nth root.
Integer_t abs_enum = boost::multiprecision::abs(exp.Numerator());
if (abs_enum > Integer_t(100)) {
// if the exponent is too large, return a invalid number
auto inv = NumberRepr(0l);
inv.isValid = false;
return inv;
}
unsigned e = abs_enum.convert_to<unsigned>();
auto rat = base.rational;
Rational_t result_num =
boost::multiprecision::pow(boost::multiprecision::numerator(rat), e);
Rational_t result_denom =
boost::multiprecision::pow(boost::multiprecision::denominator(rat), e);
if (exp.Numerator() >= Integer_t(0)) {
Rational_t res = result_num / result_denom;
return NumberRepr(res);
}
Rational_t res = result_denom / result_num;
return NumberRepr(res);
}
// calculate nth root numerically
auto inv = NumberRepr(pow(base.Double(), exp.Double()));
inv.isValid = false;
return inv;
}
/** Double returns a floating point representation of the number. */
double NumberRepr::Double() const {
if (isFraction) {
return rational.convert_to<double>();
}
return value_double;
}
/** String returns a string representation of the number. */
std::string NumberRepr::String() const {
if (!isValid) {
return "nan";
}
std::stringstream ss;
if (isFraction) {
ss << Numerator().str();
if (Denominator() != Integer_t(1)) {
ss << " / " << Denominator().str();
}
return ss.str();
}
ss << std::setprecision(17) << Double();
return ss.str();
}
bool NumberRepr::operator==(const NumberRepr &n) const {
if (!isValid || !n.isValid) {
return false;
}
if (isFraction && n.isFraction) {
return rational == n.rational;
}
if (!isFraction && !n.isFraction) {
return Double() == n.Double();
}
return false;
}
/** ToLatex writes a latex representation of this number to stream s. */
void NumberRepr::ToLatex(std::ostream &s) const {
if (IsFraction()) {
if (Denominator() != Integer_t(1)) {
s << "\\frac{" << Numerator() << "}{" << Denominator() << "}";
} else {
s << Numerator();
}
return;
}
s << std::setprecision(17) << Double();
}
bool NumberRepr::operator<(const NumberRepr &n) {
if (!isValid || !n.isValid) {
return false;
}
if (isFraction && n.isFraction) {
return rational < n.rational;
}
return Double() < n.Double();
}
bool NumberRepr::operator>(const NumberRepr &n) {
if (*this < n) {
return false;
}
if (*this == n) {
return false;
}
return true;
}
/** SetFromDouble set the value of this number to the floating point value l.
*/
void NumberRepr::SetFromDouble(double l) {
isFraction = false;
value_double = l;
isValid = true;
rational = boost::multiprecision::cpp_rational(0);
}