/usr/include/eclib/bigrat.h is in libec-dev 20160101-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | // bigrat.h: declaration of bigrational number class
//////////////////////////////////////////////////////////////////////////
//
// Copyright 1990-2012 John Cremona
//
// This file is part of the eclib package.
//
// eclib is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// eclib is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eclib; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
//////////////////////////////////////////////////////////////////////////
#if !defined(_BIGRATIONAL_H)
#define _BIGRATIONAL_H 1 //flags that this file has been included
#include "marith.h"
#include "rat.h"
class bigrational {
public:
// constructors
bigrational(bigint num_val=BIGINT(0), bigint den_val=BIGINT(1));
bigrational(const bigrational& q);
bigrational(const rational& q);
void operator=(const bigrational& q);
void operator=(const rational& q);
// bigrational manipulations
void cancel(); // cancel *this in situ
friend bigint num(const bigrational&); // the numerator
friend bigint den(const bigrational&); // the denominator
friend bigrational recip(const bigrational&); // reciprocal
friend bigint round(const bigrational&); // nearest integer
// Binary Operator Functions
friend bigrational operator+(const bigrational&, const bigrational&);
friend bigrational operator+(bigint, const bigrational&);
friend bigrational operator+(const bigrational&, bigint);
friend bigrational operator-(const bigrational&, const bigrational&);
friend bigrational operator-(bigint, const bigrational&);
friend bigrational operator-(const bigrational&, bigint);
friend bigrational operator*(const bigrational&, const bigrational&);
friend bigrational operator*(const bigrational&, bigint);
friend bigrational operator*(bigint, const bigrational&);
friend bigrational operator/(const bigrational&, const bigrational&);
friend bigrational operator/(const bigrational&, bigint);
friend bigrational operator/(bigint, const bigrational&);
friend int operator==(const bigrational&, const bigrational&);
friend int operator!=(const bigrational&, const bigrational&);
friend int operator<(const bigrational&, const bigrational&);
friend int operator>(const bigrational&, const bigrational&);
friend ostream& operator<< (ostream&s, const bigrational&);
friend istream& operator>> (istream& is, bigrational& r);
bigrational& operator+=(const bigrational&);
bigrational& operator+=(bigint);
bigrational& operator-=(const bigrational&);
bigrational& operator-=(bigint);
bigrational& operator*=(const bigrational&);
bigrational& operator*=(bigint);
bigrational& operator/=(const bigrational&);
bigrational& operator/=(bigint);
bigrational operator+();
bigrational operator-();
friend bigint floor(const bigrational& r);
friend bigint ceil(const bigrational& r);
operator bigfloat(); // conversion operator
// Implementation
private:
bigint n, d;
};
// Inline bigrational functions
inline void bigrational::cancel() // cancel *this in situ
{
bigint g = gcd(n,d);
if (g>1) {n/=g; d/=g;}
if (d<0) {n=-n; d=-d;}
}
inline bigrational::bigrational(bigint num_val, bigint den_val)
{
n=num_val; d=den_val;
(*this).cancel();
}
inline bigrational::bigrational(const bigrational& q) :n(q.n), d(q.d) {;}
inline bigrational::bigrational(const rational& q) :n(BIGINT(q.n)), d(BIGINT(q.d)) {;}
inline void bigrational::operator=(const bigrational& q) {n=q.n; d=q.d;}
inline void bigrational::operator=(const rational& q) {n=BIGINT(q.n); d=BIGINT(q.d);}
inline bigrational bigrational::operator+()
{
return *this;
}
inline bigrational bigrational::operator-()
{
return bigrational(-n, d);
}
// Definitions of compound-assignment operator member functions
inline bigrational& bigrational::operator+=(const bigrational& q2)
{
n = n*q2.d+d*q2.n;
d *= q2.d;
(*this).cancel();
return *this;
}
inline bigrational& bigrational::operator+=(bigint num_val2)
{
n += d*num_val2;
return *this;
}
inline bigrational& bigrational::operator-=(const bigrational& q2)
{
n = n*q2.d-d*q2.n;
d *= q2.d;
(*this).cancel();
return *this;
}
inline bigrational& bigrational::operator-=(bigint num_val2)
{
n -= d*num_val2;
return *this;
}
inline bigrational& bigrational::operator*=(bigint num_val2)
{
n*=num_val2;
(*this).cancel();
return *this;
}
inline bigrational& bigrational::operator/=(bigint num_val2)
{
d*=num_val2;
(*this).cancel();
return *this;
}
inline bigrational::operator bigfloat() {return I2bigfloat(n)/I2bigfloat(d);}
// Definitions of non-member bigrational functions
inline bigint num(const bigrational& q)
{
return q.n;
}
inline bigint den(const bigrational& q)
{
return q.d;
}
inline bigrational recip(const bigrational& q)
{
return bigrational(q.d, q.n);
}
inline bigint round(const bigrational& q)
{
return q.n / q.d; //provisional -- should fix rounding direction.
}
// Definitions of non-member binary operator functions
inline bigrational operator+(const bigrational& q1, const bigrational& q2)
{
return bigrational(q1.n*q2.d + q2.n*q1.d, q1.d * q2.d);
}
inline bigrational operator+(bigint num_val1, const bigrational& q2)
{
return bigrational(num_val1*q2.d + q2.n, q2.d);
}
inline bigrational operator+(const bigrational& q1, bigint num_val2)
{
return bigrational(q1.n + num_val2*q1.d, q1.d);
}
inline bigrational operator-(const bigrational& q1, const bigrational& q2)
{
return bigrational(q1.n*q2.d - q2.n*q1.d, q1.d * q2.d);
}
inline bigrational operator-(bigint num_val1, const bigrational& q2)
{
return bigrational(num_val1*q2.d - q2.n, q2.d);
}
inline bigrational operator-(const bigrational& q1, bigint num_val2)
{
return bigrational(q1.n - num_val2*q1.d, q1.d);
}
inline bigrational operator*(const bigrational& q1, bigint num_val2)
{
return bigrational(q1.n*num_val2, q1.d);
}
inline bigrational operator*(bigint num_val1, const bigrational& q2)
{
return bigrational(q2.n*num_val1, q2.d);
}
inline bigrational operator*(const bigrational& q1, const bigrational& q2)
{
return bigrational(q1.n*q2.n, q1.d*q2.d);
}
inline bigrational operator/(const bigrational& q1, bigint num_val2)
{
return bigrational(q1.n, q1.d*num_val2);
}
inline bigrational operator/(const bigrational& q1, const bigrational& q2)
{
return bigrational(q1.n*q2.d, q1.d*q2.n);
}
inline bigrational operator/(bigint num_val1, const bigrational& q2)
{
return bigrational(q2.d*num_val1, q2.n);
}
inline int operator==(const bigrational& q1, const bigrational& q2)
{
return q1.n*q2.d == q2.n*q1.d;
}
inline int operator!=(const bigrational& q1, const bigrational& q2)
{
return q1.n*q2.d != q2.n*q1.d;
}
inline int operator<(const bigrational& q1, const bigrational& q2)
{
return q1.n*q2.d < q2.n*q1.d;
}
inline int operator>(const bigrational& q1, const bigrational& q2)
{
return q1.n*q2.d > q2.n*q1.d;
}
inline ostream& operator<<(ostream& s, const bigrational& q)
{
if(q.d==0) s<<"oo";
else
{
s << q.n;
if (q.d!=1) {s << "/" << q.d;}
}
return s;
}
inline istream& operator>> (istream& is, bigrational& r)
{
char c;
bigint n,d=BIGINT(1);
is>>n;
if(!is.eof())
{
is.get(c);
if(c=='/')
{
is>>d;
}
else
{
is.putback(c);
}
}
r=bigrational(n,d);
return is;
}
// NB gcd(n,d)=1 and d>0:
inline bigint floor(const bigrational& r)
{
return (r.n-(r.n%r.d))/r.d;
}
inline bigint ceil(const bigrational& r)
{
if(r.d==BIGINT(1)) return r.n;
return BIGINT(1) + (r.n-(r.n%r.d))/r.d;
}
#endif
|