-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pfloat.hpp
194 lines (140 loc) · 6.09 KB
/
Pfloat.hpp
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
#ifndef PFLOAT_HPP
#define PFLOAT_HPP
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstdlib>
#include <string.h>
#include "LinkedList.cpp"
#define STANDARD_PRECISION 24
// === display modes ===
#define AUTOMATIC (-1254)
#define NORMAL (-7273)
#define SCIENTIFIC (+5828)
#define DEBUG (-9827)
using namespace std;
class Pfloat{
private:
static int mode;
LinkedList<int>* digits;
/// @brief digits*10^exponent
long exponent;
/// @brief tells whether or not the given Pfloat object is negative
bool neg;
/// @brief max number of digits
int precision;
static bool check_string (std::string const str );
static bool check_exp_string(std::string const str );
/// @brief standard toString function
/// @return string with this format : '-0.000123'
const string toString ( ) const;
/// @brief toString function with this format : xxxeXXX
/// @return string representation of the Pfloat object
const string toeString ( ) const;
/// @brief toString function with this format : +/- [x, x, x, x] e = X
/// @return
const string debugToString ( ) const;
/// @brief tidy up the number
/// @return true if successful
bool tidy ( );
/// @brief we assume the exponents of the two Pfloats are the same and they are both tydied
/// @return true if b is greater than this
bool rec_inf (const Pfloat b, int index_cmp ) const;
/// @brief divides a Pfloat object by another
/// @param x divisor
/// @return Integer Pfloat object
Pfloat quotient (const Pfloat& x ) const;
/// @brief
/// @param x POSITIVE INTEGER ONLY
/// @return
Pfloat pow_rec (const int x, const int p ) const;
Pfloat (const Pfloat& n, const int p );
public:
~Pfloat();
// === Constructor ===
Pfloat();
Pfloat(const Pfloat& n);
/// @brief creates a new Pfloat from a long
/// @param n warning, n must have less than 8 digits
Pfloat(long double n);
/// @brief creates a new Pfloat from a string
/// @param str format XXX.xxx or XXX.XXXeXXX
Pfloat(const std::string str);
// === Static ===
/// @brief sets the display mode : NORMAL : 0.000125 | SCIENTIFIC : 1.25e-4 | DEBUG : +[1, 2, 5] e = -4 | AUTOMATIC : switches between NORMAL and SCIENTIFIC
/// @param display_mode NORMAL | SCIENTIFIC | DEBUG | AUTOMATIC
/// @return if the displaye has been set or not. If not, be sure to enter one of the 4 types
static bool set_display ( const int display_mode );
static Pfloat sqrt ( const Pfloat x );
static Pfloat pi ( );
// === Methods ===
/// @brief gets the exponent of the Pfloat (debug purposes)
/// @return the exponent
int getExponent ( ) const;
/// @brief increase or decrease the exponent of the Pfloat object
/// @param shift positive integer for increase, negative integer for decrease
void exp_shift (const int shift);
/// @brief powers the Pfloat object
/// @param x the power of the Pfloat
/// @return new Pfloat result
Pfloat pow (const int& x ) const;
/// @brief gets the absolute value of a Pfloat
/// @return the copied Pfloat object non negative
Pfloat abs ( ) const;
// === Operators ===
friend std::ostream& operator<<(std::ostream& os, const Pfloat& x);
Pfloat operator - (const Pfloat& x ) const;
Pfloat operator - (const long double& x ) const;
Pfloat operator - (const std::string& str ) const;
Pfloat& operator -= (const Pfloat& x );
Pfloat& operator -= (const long double& x );
Pfloat& operator -= (const std::string& str );
Pfloat operator + (const Pfloat& x ) const;
Pfloat operator + (const long double& x ) const;
Pfloat operator + (const std::string& str ) const;
Pfloat& operator += (const Pfloat& x );
Pfloat& operator += (const long double& x );
Pfloat& operator += (const std::string& str );
Pfloat operator * (const Pfloat& x ) const;
Pfloat operator * (const long double& x ) const;
Pfloat operator * (const std::string& str ) const;
Pfloat& operator *= (const Pfloat& x );
Pfloat& operator *= (const long double& x );
Pfloat& operator *= (const std::string& str );
Pfloat operator / (const Pfloat& x ) const;
Pfloat operator / (const long double& x ) const;
Pfloat operator / (const std::string& str ) const;
Pfloat& operator /= (const Pfloat& x );
Pfloat& operator /= (const long double& x );
Pfloat& operator /= (const std::string& str );
Pfloat operator % (const Pfloat& x ) const;
Pfloat operator % (const long double& x ) const;
Pfloat operator % (const std::string& str ) const;
Pfloat& operator %= (const Pfloat& x );
Pfloat& operator %= (const long double& x );
Pfloat& operator %= (const std::string& str );
Pfloat& operator = (const Pfloat& n );
Pfloat& operator = (const long double& n );
Pfloat& operator = (const std::string& str );
bool operator == (const Pfloat& x ) const;
bool operator == (const long double& x ) const;
bool operator == (const std::string& str ) const;
bool operator != (const Pfloat& x ) const;
bool operator != (const long double& x ) const;
bool operator != (const std::string& str ) const;
bool operator < (const Pfloat& x ) const;
bool operator < (const long double& x ) const;
bool operator < (const std::string& str ) const;
bool operator <= (const Pfloat& x ) const;
bool operator <= (const long double& x ) const;
bool operator <= (const std::string& str ) const;
bool operator > (const Pfloat& x ) const;
bool operator > (const long double& x ) const;
bool operator > (const std::string& str ) const;
bool operator >= (const Pfloat& x ) const;
bool operator >= (const long double& x ) const;
bool operator >= (const std::string& str ) const;
};
#endif