-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmk_fraction.h
233 lines (199 loc) · 5.88 KB
/
dmk_fraction.h
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
233
#pragma once
#include "dmk.h"
#include <iostream>
namespace dmk
{
class fraction
{
public:
// Calculates the greatest common divisor
static int64_t gcd( int64_t a, int64_t b )
{
int64_t r;
while ( b != 0 )
{
r = a % b;
a = b;
b = r;
}
return a;
}
static int64_t lcm( int64_t a, int64_t b )
{
return a / gcd( a, b ) * b;
}
public:
fraction( ) : m_numerator( 0 ), m_denominator( 1 )
{
}
fraction( int64_t n ) : m_numerator( n ), m_denominator( 1 )
{
}
fraction( int64_t n, int64_t d, bool do_normalize = true )
{
if ( n == 0 )
{
m_numerator = 0;
m_denominator = 1;
}
else
{
m_numerator = n;
m_denominator = d;
if ( do_normalize )
{
normalize( );
}
}
}
int64_t numerator( ) const
{
return m_numerator;
}
int64_t denominator( ) const
{
return m_denominator;
}
void normalize( )
{
int sign = 1;
if ( m_numerator < 0 )
{
sign = -sign;
m_numerator = -m_numerator;
}
if ( m_denominator < 0 )
{
sign = -sign;
m_denominator = -m_denominator;
}
int64_t tmp = gcd( m_numerator, m_denominator );
m_numerator = m_numerator / tmp;
m_denominator = m_denominator / tmp;
if ( sign < 0 )
m_numerator = -m_numerator;
}
fraction normalized( ) const
{
fraction f( *this );
f.normalize( );
return f;
}
int64_t as_int64_t( ) const
{
return m_numerator / m_denominator;
}
double as_double( ) const
{
return ( double )m_numerator / m_denominator;
}
explicit operator int64_t( ) const
{
return as_int64_t( );
}
explicit operator double( ) const
{
return as_double( );
}
private:
int64_t m_numerator, m_denominator;
};
inline fraction operator-( const fraction& lhs )
{
return fraction( -lhs.numerator( ), lhs.denominator( ) );
}
inline fraction operator+( const fraction& lhs, const fraction& rhs )
{
int64_t e = fraction::lcm( lhs.denominator( ), rhs.denominator( ) );
return fraction( lhs.numerator( ) * ( e / lhs.denominator( ) ) +
rhs.numerator( ) * ( e / rhs.denominator( ) ),
e );
}
inline fraction& operator+=( fraction& lhs, const fraction& rhs )
{
lhs = lhs + rhs;
return lhs;
}
inline fraction operator-( const fraction& lhs, const fraction& rhs )
{
int64_t e = fraction::lcm( lhs.denominator( ), rhs.denominator( ) );
return fraction(
lhs.numerator( ) * e / lhs.denominator( ) - rhs.numerator( ) * e / rhs.denominator( ), e );
}
inline fraction& operator-=( fraction& lhs, const fraction& rhs )
{
lhs = lhs - rhs;
return lhs;
}
inline fraction operator*( const fraction& lhs, const fraction& rhs )
{
fraction tmp( lhs.numerator( ) * rhs.numerator( ), lhs.denominator( ) * rhs.denominator( ) );
return tmp;
}
inline fraction& operator*=( fraction& lhs, const fraction& rhs )
{
fraction tmp( lhs.numerator( ) * rhs.numerator( ), lhs.denominator( ) * rhs.denominator( ) );
lhs = tmp;
return lhs;
}
inline fraction operator*( int64_t lhs, const fraction& rhs )
{
fraction tmp( lhs * rhs.numerator( ), rhs.denominator( ) );
return tmp;
}
inline fraction operator*( const fraction& rhs, int64_t lhs )
{
fraction tmp( lhs * rhs.numerator( ), rhs.denominator( ) );
return tmp;
}
inline fraction operator/( const fraction& lhs, const fraction& rhs )
{
fraction tmp( lhs.numerator( ) * rhs.denominator( ), lhs.denominator( ) * rhs.numerator( ) );
return tmp;
}
inline fraction& operator/=( fraction& lhs, const fraction& rhs )
{
fraction tmp( lhs.numerator( ) * rhs.denominator( ), lhs.denominator( ) * rhs.numerator( ) );
lhs = tmp;
return lhs;
}
inline bool operator==( const fraction& lhs, const fraction& rhs )
{
fraction difference = lhs - rhs;
return difference.numerator( ) == 0;
}
inline bool operator!=( const fraction& lhs, const fraction& rhs )
{
fraction difference = lhs - rhs;
return difference.numerator( ) != 0;
}
inline bool operator<( const fraction& lhs, const fraction& rhs )
{
fraction difference = lhs - rhs;
return difference.numerator( ) < 0;
}
inline bool operator>( const fraction& lhs, const fraction& rhs )
{
fraction difference = lhs - rhs;
return difference.numerator( ) > 0;
}
inline bool operator<=( const fraction& lhs, const fraction& rhs )
{
fraction difference = lhs - rhs;
return difference.numerator( ) <= 0;
}
inline bool operator>=( const fraction& lhs, const fraction& rhs )
{
fraction difference = lhs - rhs;
return difference.numerator( ) >= 0;
}
inline std::ostream& operator<<( std::ostream& os, const fraction& a )
{
os << a.numerator( );
if ( a.denominator( ) > 1 )
{
os << "/" << a.denominator( );
}
return os;
}
} // namespace dmk