00001
00002 #ifndef vnl_bignum_h_
00003 #define vnl_bignum_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #include <vcl_iostream.h>
00069 #include <vcl_string.h>
00070
00071 class vnl_bignum;
00072
00073
00074
00075 int magnitude_cmp(const vnl_bignum&, const vnl_bignum&);
00076 void add(const vnl_bignum&, const vnl_bignum&, vnl_bignum&);
00077 void subtract(const vnl_bignum&, const vnl_bignum&, vnl_bignum&);
00078 void multiply_aux(const vnl_bignum&, unsigned short d, vnl_bignum&, unsigned short i);
00079 unsigned short normalize(const vnl_bignum&, const vnl_bignum&, vnl_bignum&, vnl_bignum&);
00080 void divide_aux(const vnl_bignum&, unsigned short, vnl_bignum&, unsigned short&);
00081 unsigned short estimate_q_hat(const vnl_bignum&, const vnl_bignum&, unsigned short);
00082 unsigned short multiply_subtract(vnl_bignum&, const vnl_bignum&, unsigned short, unsigned short);
00083 void divide(const vnl_bignum&, const vnl_bignum&, vnl_bignum&, vnl_bignum&);
00084 vnl_bignum left_shift(const vnl_bignum& b1, int l);
00085 vnl_bignum right_shift(const vnl_bignum& b1, int l);
00086 void decrement (vnl_bignum& bnum);
00087 void increment (vnl_bignum& bnum);
00088
00089
00090
00091 vcl_ostream& operator<<(vcl_ostream& s, vnl_bignum const& r);
00092
00093
00094
00095 vcl_istream& operator>>(vcl_istream& s, vnl_bignum& r);
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 class vnl_bignum
00140 {
00141 unsigned short count;
00142 int sign;
00143 unsigned short* data;
00144 public:
00145 vnl_bignum();
00146 vnl_bignum(long);
00147 vnl_bignum(unsigned long);
00148 vnl_bignum(int);
00149 vnl_bignum(unsigned int);
00150 vnl_bignum(float);
00151 vnl_bignum(double);
00152 vnl_bignum(long double);
00153 vnl_bignum(vnl_bignum const&);
00154 vnl_bignum(const char*);
00155 ~vnl_bignum();
00156
00157 operator short() const;
00158 operator int() const;
00159 operator long() const;
00160 operator float() const;
00161 operator double() const;
00162 operator long double() const;
00163 inline operator short() { return ((const vnl_bignum*)this)->operator short(); }
00164 inline operator int() { return ((const vnl_bignum*)this)->operator int(); }
00165 inline operator long() { return ((const vnl_bignum*)this)->operator long(); }
00166 inline operator float() { return ((const vnl_bignum*)this)->operator float(); }
00167 inline operator double() { return ((const vnl_bignum*)this)->operator double(); }
00168 inline operator long double() { return ((const vnl_bignum*)this)->operator long double(); }
00169
00170 vnl_bignum operator-() const;
00171 inline vnl_bignum operator+() const { return *this; }
00172
00173 vnl_bignum& operator=(const vnl_bignum&);
00174
00175 vnl_bignum operator<<(int l) const;
00176 vnl_bignum operator>>(int l) const;
00177 vnl_bignum operator+(vnl_bignum const& r) const;
00178 inline vnl_bignum& operator+=(vnl_bignum const& r) { return *this = operator+(r); }
00179 inline vnl_bignum& operator-=(vnl_bignum const& r) { return *this = operator+(-r); }
00180 vnl_bignum& operator*=(vnl_bignum const& r);
00181 vnl_bignum& operator/=(vnl_bignum const& r);
00182 vnl_bignum& operator%=(vnl_bignum const& r);
00183 inline vnl_bignum& operator<<=(int l) { return *this = *this << l; }
00184 inline vnl_bignum& operator>>=(int l) { return *this = *this >> l; }
00185
00186
00187 vnl_bignum& operator++();
00188
00189 vnl_bignum& operator--();
00190
00191 inline vnl_bignum operator++(int) { vnl_bignum b=(*this); operator++(); return b; }
00192
00193 inline vnl_bignum operator--(int) { vnl_bignum b=(*this); operator--(); return b; }
00194
00195 bool operator==(vnl_bignum const&) const;
00196 bool operator< (vnl_bignum const&) const;
00197 inline bool operator!=(vnl_bignum const& r) const { return !operator==(r); }
00198 inline bool operator> (vnl_bignum const& r) const { return r<(*this); }
00199 inline bool operator<=(vnl_bignum const& r) const { return !operator>(r); }
00200 inline bool operator>=(vnl_bignum const& r) const { return !operator<(r); }
00201 inline bool operator==(long r) const { return operator==(vnl_bignum(r)); }
00202 inline bool operator!=(long r) const { return !operator==(vnl_bignum(r)); }
00203 inline bool operator< (long r) const { return operator<(vnl_bignum(r)); }
00204 inline bool operator> (long r) const { return vnl_bignum(r) < (*this); }
00205 inline bool operator<=(long r) const { return !operator>(vnl_bignum(r)); }
00206 inline bool operator>=(long r) const { return !operator<(vnl_bignum(r)); }
00207 inline bool operator==(int r) const { return operator==(long(r)); }
00208 inline bool operator!=(int r) const { return !operator==(long(r)); }
00209 inline bool operator< (int r) const { return operator<(long(r)); }
00210 inline bool operator> (int r) const { return vnl_bignum(long(r)) < (*this); }
00211 inline bool operator<=(int r) const { return !operator>(long(r)); }
00212 inline bool operator>=(int r) const { return !operator<(long(r)); }
00213 inline bool operator==(double r) const { return r == this->operator double(); }
00214 inline bool operator!=(double r) const { return r != this->operator double(); }
00215 inline bool operator< (double r) const { return r > this->operator double(); }
00216 inline bool operator> (double r) const { return r < this->operator double(); }
00217 inline bool operator<=(double r) const { return r >= this->operator double(); }
00218 inline bool operator>=(double r) const { return r <= this->operator double(); }
00219 inline bool operator==(long double r) const { return r == this->operator long double(); }
00220 inline bool operator!=(long double r) const { return r != this->operator long double(); }
00221 inline bool operator< (long double r) const { return r > this->operator long double(); }
00222 inline bool operator> (long double r) const { return r < this->operator long double(); }
00223 inline bool operator<=(long double r) const { return r >= this->operator long double(); }
00224 inline bool operator>=(long double r) const { return r <= this->operator long double(); }
00225
00226 inline vnl_bignum abs() const { return operator<(0L) ? operator-() : *this; }
00227
00228
00229 inline bool is_infinity() const { return count==1 && data[0]==0; }
00230 inline bool is_plus_infinity() const { return is_infinity() && sign==1; }
00231 inline bool is_minus_infinity() const { return is_infinity() && sign==-1; }
00232
00233 void dump(vcl_ostream& = vcl_cout) const;
00234
00235 friend int magnitude_cmp(const vnl_bignum&, const vnl_bignum&);
00236 friend void add(const vnl_bignum&, const vnl_bignum&, vnl_bignum&);
00237 friend void subtract(const vnl_bignum&, const vnl_bignum&, vnl_bignum&);
00238 friend void increment (vnl_bignum& bnum);
00239 friend void decrement (vnl_bignum& bnum);
00240 friend void multiply_aux(const vnl_bignum&, unsigned short, vnl_bignum&, unsigned short);
00241 friend unsigned short normalize(const vnl_bignum&, const vnl_bignum&, vnl_bignum&, vnl_bignum&);
00242 friend void divide_aux(const vnl_bignum&, unsigned short, vnl_bignum&, unsigned short&);
00243 friend unsigned short estimate_q_hat(const vnl_bignum&, const vnl_bignum&, unsigned short);
00244 friend unsigned short multiply_subtract(vnl_bignum&, const vnl_bignum&, unsigned short, unsigned short);
00245 friend void divide(const vnl_bignum&, const vnl_bignum&, vnl_bignum&, vnl_bignum&);
00246 friend vnl_bignum left_shift(const vnl_bignum& b1, int l);
00247 friend vnl_bignum right_shift(const vnl_bignum& b1, int l);
00248 friend vcl_ostream& operator<< (vcl_ostream&, const vnl_bignum&);
00249 friend vcl_istream& operator>> (vcl_istream&, vnl_bignum&);
00250 friend vcl_string& vnl_bignum_to_string (vcl_string& s, const vnl_bignum& b);
00251 friend vnl_bignum& vnl_bignum_from_string (vnl_bignum& b, const vcl_string& s);
00252
00253 private:
00254 void xtoBigNum(const char *s);
00255 int dtoBigNum(const char *s);
00256 void otoBigNum(const char *s);
00257 void exptoBigNum(const char *s);
00258
00259 void resize(short);
00260 vnl_bignum& trim();
00261 };
00262
00263
00264
00265
00266 vcl_string& vnl_bignum_to_string (vcl_string& s, const vnl_bignum& b);
00267
00268
00269
00270 vnl_bignum& vnl_bignum_from_string (vnl_bignum& b, const vcl_string& s);
00271
00272
00273
00274 inline vnl_bignum operator+(vnl_bignum const& r1, long r2) { return r1+vnl_bignum(r2); }
00275 inline vnl_bignum operator+(vnl_bignum const& r1, int r2) { return r1+long(r2); }
00276 inline vnl_bignum operator+(vnl_bignum const& r1, double r2) { return r1+vnl_bignum(r2); }
00277 inline vnl_bignum operator+(vnl_bignum const& r1, long double r2) { return r1+vnl_bignum(r2); }
00278 inline vnl_bignum operator+(long r2, vnl_bignum const& r1) { return r1 + r2; }
00279 inline vnl_bignum operator+(int r2, vnl_bignum const& r1) { return r1 + r2; }
00280 inline vnl_bignum operator+(double r2, vnl_bignum const& r1) { return r1 + r2; }
00281 inline vnl_bignum operator+(long double r2, vnl_bignum const& r1) { return r1 + r2; }
00282
00283
00284
00285 inline vnl_bignum operator-(vnl_bignum const& r1, vnl_bignum const& r2) { return r1 + (-r2); }
00286 inline vnl_bignum operator-(vnl_bignum const& r1, long r2) { return r1 + (-r2); }
00287 inline vnl_bignum operator-(vnl_bignum const& r1, int r2) { return r1 + (-r2); }
00288 inline vnl_bignum operator-(vnl_bignum const& r1, double r2) { return r1 + (-r2); }
00289 inline vnl_bignum operator-(vnl_bignum const& r1, long double r2) { return r1 + (-r2); }
00290 inline vnl_bignum operator-(long r2, vnl_bignum const& r1) { return -(r1 + (-r2)); }
00291 inline vnl_bignum operator-(int r2, vnl_bignum const& r1) { return -(r1 + (-r2)); }
00292 inline vnl_bignum operator-(double r2, vnl_bignum const& r1) { return -(r1 + (-r2)); }
00293 inline vnl_bignum operator-(long double r2, vnl_bignum const& r1) { return -(r1 + (-r2)); }
00294
00295
00296
00297 inline vnl_bignum operator*(vnl_bignum const& r1, vnl_bignum const& r2)
00298 {
00299 vnl_bignum result(r1); return result *= r2;
00300 }
00301
00302 inline vnl_bignum operator*(vnl_bignum const& r1, long r2)
00303 {
00304 vnl_bignum result(r1); return result *= vnl_bignum(r2);
00305 }
00306
00307 inline vnl_bignum operator*(vnl_bignum const& r1, int r2)
00308 {
00309 vnl_bignum result(r1); return result *= (long)r2;
00310 }
00311
00312 inline vnl_bignum operator*(vnl_bignum const& r1, double r2)
00313 {
00314 vnl_bignum result(r1); return result *= (vnl_bignum)r2;
00315 }
00316
00317 inline vnl_bignum operator*(vnl_bignum const& r1, long double r2)
00318 {
00319 vnl_bignum result(r1); return result *= (vnl_bignum)r2;
00320 }
00321
00322 inline vnl_bignum operator*(long r2, vnl_bignum const& r1)
00323 {
00324 vnl_bignum result(r1); return result *= r2;
00325 }
00326
00327 inline vnl_bignum operator*(int r2, vnl_bignum const& r1)
00328 {
00329 vnl_bignum result(r1); return result *= (long)r2;
00330 }
00331
00332 inline vnl_bignum operator*(double r2, vnl_bignum const& r1)
00333 {
00334 vnl_bignum result(r1); return result *= (vnl_bignum)r2;
00335 }
00336
00337 inline vnl_bignum operator*(long double r2, vnl_bignum const& r1)
00338 {
00339 vnl_bignum result(r1); return result *= (vnl_bignum)r2;
00340 }
00341
00342
00343
00344 inline vnl_bignum operator/(vnl_bignum const& r1, vnl_bignum const& r2)
00345 {
00346 vnl_bignum result(r1); return result /= r2;
00347 }
00348
00349 inline vnl_bignum operator/(vnl_bignum const& r1, long r2)
00350 {
00351 vnl_bignum result(r1); return result /= r2;
00352 }
00353
00354 inline vnl_bignum operator/(vnl_bignum const& r1, int r2)
00355 {
00356 vnl_bignum result(r1); return result /= (long)r2;
00357 }
00358
00359 inline vnl_bignum operator/(vnl_bignum const& r1, double r2)
00360 {
00361 vnl_bignum result(r1); return result /= (vnl_bignum)r2;
00362 }
00363
00364 inline vnl_bignum operator/(vnl_bignum const& r1, long double r2)
00365 {
00366 vnl_bignum result(r1); return result /= (vnl_bignum)r2;
00367 }
00368
00369 inline vnl_bignum operator/(long r1, vnl_bignum const& r2)
00370 {
00371 vnl_bignum result(r1); return result /= r2;
00372 }
00373
00374 inline vnl_bignum operator/(int r1, vnl_bignum const& r2)
00375 {
00376 vnl_bignum result((long)r1); return result /= r2;
00377 }
00378
00379 inline vnl_bignum operator/(double r1, vnl_bignum const& r2)
00380 {
00381 vnl_bignum result(r1); return result /= r2;
00382 }
00383
00384 inline vnl_bignum operator/(long double r1, vnl_bignum const& r2)
00385 {
00386 vnl_bignum result(r1); return result /= r2;
00387 }
00388
00389
00390
00391 inline vnl_bignum operator%(vnl_bignum const& r1, vnl_bignum const& r2)
00392 {
00393 vnl_bignum result(r1); return result %= r2;
00394 }
00395
00396 inline vnl_bignum operator%(vnl_bignum const& r1, long r2)
00397 {
00398 vnl_bignum result(r1); return result %= vnl_bignum(r2);
00399 }
00400
00401 inline vnl_bignum operator%(vnl_bignum const& r1, int r2)
00402 {
00403 vnl_bignum result(r1); return result %= vnl_bignum((long)r2);
00404 }
00405
00406 inline vnl_bignum operator%(long r1, vnl_bignum const& r2)
00407 {
00408 vnl_bignum result(r1); return result %= r2;
00409 }
00410
00411 inline vnl_bignum operator%(int r1, vnl_bignum const& r2)
00412 {
00413 vnl_bignum result((long)r1); return result %= r2;
00414 }
00415
00416
00417
00418 inline bool operator==(long r1, vnl_bignum const& r2) { return r2==r1; }
00419 inline bool operator!=(long r1, vnl_bignum const& r2) { return r2!=r1; }
00420 inline bool operator< (long r1, vnl_bignum const& r2) { return r2> r1; }
00421 inline bool operator> (long r1, vnl_bignum const& r2) { return r2< r1; }
00422 inline bool operator<=(long r1, vnl_bignum const& r2) { return r2>=r1; }
00423 inline bool operator>=(long r1, vnl_bignum const& r2) { return r2<=r1; }
00424
00425 inline vnl_bignum vnl_math_abs(vnl_bignum const& x) { return x.abs(); }
00426 inline vnl_bignum vnl_math_squared_magnitude(vnl_bignum const& x) { return x*x; }
00427 inline vnl_bignum vnl_math_sqr(vnl_bignum const& x) { return x*x; }
00428 inline bool vnl_math_isnan(vnl_bignum const& ) { return false; }
00429 inline bool vnl_math_isfinite(vnl_bignum const& x) { return ! x.is_infinity(); }
00430
00431 #endif // vnl_bignum_h_