-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmath.h
More file actions
executable file
·194 lines (148 loc) · 3.81 KB
/
Copy pathmath.h
File metadata and controls
executable file
·194 lines (148 loc) · 3.81 KB
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 __MATH_H_
#define __MATH_H_
#include "common.h"
#include "mem.h"
typedef long mint;
typedef unsigned long umint;
namespace Math {
// Basic methods not bound to anything.
template <class T>
T divide( T top, T bottom, T* result, T* remainder ) {
// Calculate whether or not we need to flip the sign
// afterwards.
bool flipTop = (top < 0 );
bool flipBottom = (bottom < 0);
if ( flipTop ) top *= -1;
if ( flipBottom ) bottom *= -1;
// Test for some edge cases first.
if ( bottom == 0 ) {
*result = -1;
*remainder = -1;
return *result;
}
if ( top < bottom ) {
*result = 0;
*remainder = top;
return *result;
}
// Reset the pointer variables and create some temp
// containers.
int topVar = top, bottomVar = bottom;
*result = 0;
*remainder = 0;
// Do long division (note: everything should be positive now).
for (; topVar >= bottomVar; topVar -= bottomVar ) {
*(result) = *(result) + 1;
}
// Calculate the remainder.
*(remainder) = top - (*(result) * bottom);
// Do the flips.
if ( flipTop ) *(result) = *(result) * -1;
if ( flipBottom ) *(result) = *(result) * -1;
// Return the result.
return *(result);
}
template<class T>
T pow( T x, T y, bool standard ) {
if ( standard ) {
if ( y == 0 ) return 1;
if ( y == 1 ) return x;
}
T i = 0, r = x;
for ( ; i < y; i++ )
r = r * x;
return r;
}
template<class T>
T pow(T x, T y) {
return pow( x, y, true );
}
template<class T>
T getDigitCount(T num, T base) {
T top = num, result = 0, remainder = 0, returnVal = 0;
while ( top > 0 ) {
divide<T>( top, base, &result, &remainder );
top = result;
returnVal++;
}
return returnVal;
}
class kfloat {
public:
// Division operator override.
kfloat operator/(const kfloat &target){
if ( this == &target ) return *this;
// Do the math.
mint top = this->major, bottom = target.major, result = 0, remainder = 0, i;
// Now calculate the proper.
divide<mint>( top, bottom, &result, &remainder);
// Store the result.
this->major = result;
this->minor = 0;
// Calculate the decimal.
for ( i = this->precision; i > 0; i-- ) {
// Do the long division.
divide<mint>( remainder * 10, bottom, &result, &remainder );
this->minor += ( pow<mint>( 10, i - 1 ) * result );
}
// Return
return *this;
}
kfloat operator=(const mint &target) {
this->init( target, 0, 2 );
return *this;
}
// Constructors
kfloat( void ) { this->init( 0, 0, 0); }
kfloat( mint num ) { this->init( num, 0, 2 ); }
kfloat( mint num, mint dec ) { this->init( num, dec, 2 ); }
kfloat( mint num, mint dec, mint prec) { this->init( num, dec, prec ); }
// Basic assignment operations.
mint getMajor() {
return this->major;
}
mint getMinor() {
return this->minor;
}
mint getPrecision() {
return this->precision;
}
mint setPrecision(mint p) {
this->precision = p;
}
mint getBig1() {
return this->big1;
}
mint getBig2() {
return this->big2;
}
bool getIsLarge() {
return this->isLarge;
}
private:
mint major;
mint minor;
mint precision;
/* Note: this is used exclusivly for very large numbers */
bool isLarge;
mint big1;
mint big2;
void init( mint num, mint dec, mint prec ) {
// Check if we need to incorporate the big major.
if ( num > (umint)0xFFFFFF) {
// Yes.
this->big1 = (num & 0xFFFF0000) >> 16;// >> (4 * 4);
this->big2 = (num & 0x0000FFFF);
this->isLarge = true;
} else {
// No
this->big1 = num;
this->isLarge = false;
}
this->major = num;
this->minor = dec;
this->precision = prec;
}
};
};
#endif