/usr/include/eclib/rat.h is in libec-dev 20160720-2.
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 | // rat.h: declaration of rational 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(_RATIONAL_H)
#define _RATIONAL_H 1 //flags that this file has been included
#include "arith.h"
class rational {
friend class bigrational;
public:
// constructors
rational(long num_val=0, long den_val=1);
rational(const rational& q);
void operator=(const rational& q);
// rational manipulations
void cancel(); // cancel *this in situ
friend long num(const rational&); // the numerator
friend long den(const rational&); // the denominator
friend rational recip(const rational&); // reciprocal
friend long round(const rational&); // nearest integer
// Binary Operator Functions
friend rational operator+(const rational&, const rational&);
friend rational operator+(long, const rational&);
friend rational operator+(const rational&, long);
friend rational operator-(const rational&, const rational&);
friend rational operator-(long, const rational&);
friend rational operator-(const rational&, long);
friend rational operator*(const rational&, const rational&);
friend rational operator*(const rational&, long);
friend rational operator*(long, const rational&);
friend rational operator/(const rational&, const rational&);
friend rational operator/(const rational&, long);
friend rational operator/(long, const rational&);
friend int operator==(const rational&, const rational&);
friend int operator!=(const rational&, const rational&);
friend ostream& operator<< (ostream&s, const rational&);
friend istream& operator>> (istream& is, rational& r);
rational& operator+=(const rational&);
rational& operator+=(long);
rational& operator-=(const rational&);
rational& operator-=(long);
rational& operator*=(const rational&);
rational& operator*=(long);
rational& operator/=(const rational&);
rational& operator/=(long);
rational operator+() const;
rational operator-() const;
friend long floor(const rational& r);
friend long ceil(const rational& r);
operator double(); // conversion operator
#ifdef NTL_ALL
operator bigfloat(); // conversion operator
#endif
// Implementation
private:
long n, d;
};
// Inline rational functions
inline void rational::cancel() // cancel *this in situ
{
long g = ::gcd(n,d);
if (g>1) {n/=g; d/=g;}
if (d<0) {n=-n; d=-d;}
}
inline rational::rational(long num_val, long den_val)
{
n=num_val; d=den_val;
(*this).cancel();
}
inline rational::rational(const rational& q) :n(q.n), d(q.d) {;}
inline void rational::operator=(const rational& q) {n=q.n; d=q.d;}
inline rational rational::operator+() const
{
return *this;
}
inline rational rational::operator-() const
{
return rational(-n, d);
}
// Definitions of compound-assignment operator member functions
inline rational& rational::operator+=(const rational& q2)
{
n = n*q2.d+d*q2.n;
d *= q2.d;
(*this).cancel();
return *this;
}
inline rational& rational::operator+=(long num_val2)
{
n += d*num_val2;
return *this;
}
inline rational& rational::operator-=(const rational& q2)
{
n = n*q2.d-d*q2.n;
d *= q2.d;
(*this).cancel();
return *this;
}
inline rational& rational::operator-=(long num_val2)
{
n -= d*num_val2;
return *this;
}
inline rational& rational::operator*=(long num_val2)
{
n*=num_val2;
(*this).cancel();
return *this;
}
inline rational& rational::operator/=(long num_val2)
{
d*=num_val2;
(*this).cancel();
return *this;
}
inline rational::operator double() {return double(n)/double(d);}
#ifdef NTL_ALL
inline rational::operator bigfloat() {return to_bigfloat(n)/to_bigfloat(d);}
#endif
// Definitions of non-member rational functions
inline long num(const rational& q)
{
return q.n;
}
inline long den(const rational& q)
{
return q.d;
}
inline rational recip(const rational& q)
{
return rational(q.d, q.n);
}
inline long round(const rational& q)
{
return q.n / q.d; //provisional -- should fix rounding direction.
}
// Definitions of non-member binary operator functions
inline rational operator+(const rational& q1, const rational& q2)
{
return rational(q1.n*q2.d + q2.n*q1.d, q1.d * q2.d);
}
inline rational operator+(long num_val1, const rational& q2)
{
return rational(num_val1*q2.d + q2.n, q2.d);
}
inline rational operator+(const rational& q1, long num_val2)
{
return rational(q1.n + num_val2*q1.d, q1.d);
}
inline rational operator-(const rational& q1, const rational& q2)
{
return rational(q1.n*q2.d - q2.n*q1.d, q1.d * q2.d);
}
inline rational operator-(long num_val1, const rational& q2)
{
return rational(num_val1*q2.d - q2.n, q2.d);
}
inline rational operator-(const rational& q1, long num_val2)
{
return rational(q1.n - num_val2*q1.d, q1.d);
}
inline rational operator*(const rational& q1, long num_val2)
{
return rational(q1.n*num_val2, q1.d);
}
inline rational operator*(long num_val1, const rational& q2)
{
return rational(q2.n*num_val1, q2.d);
}
inline rational operator*(const rational& q1, const rational& q2)
{
return rational(q1.n*q2.n, q1.d*q2.d);
}
inline rational operator/(const rational& q1, long num_val2)
{
return rational(q1.n, q1.d*num_val2);
}
inline rational operator/(const rational& q1, const rational& q2)
{
return rational(q1.n*q2.d, q1.d*q2.n);
}
inline rational operator/(long num_val1, const rational& q2)
{
return rational(q2.d*num_val1, q2.n);
}
inline int operator==(const rational& q1, const rational& q2)
{
return q1.n*q2.d == q2.n*q1.d;
}
inline int operator!=(const rational& q1, const rational& q2)
{
return q1.n*q2.d != q2.n*q1.d;
}
inline ostream& operator<<(ostream& s, const rational& 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, rational& r)
{
char c;
long n,d=1;
is>>n;
if(!is.eof())
{
is.get(c);
if(c=='/')
{
is>>d;
}
else
{
is.putback(c);
}
}
r=rational(n,d);
return is;
}
// NB gcd(n,d)=1 and d>0:
inline long floor(const rational& r)
{
return (r.n-(r.n%r.d))/r.d;
}
inline long ceil(const rational& r)
{
if(r.d==1) return r.n;
return 1 + (r.n-(r.n%r.d))/r.d;
}
#endif
|