Go to the documentation of this file.00001
00002 #ifndef vnl_decnum_h_
00003 #define vnl_decnum_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
00069
00070
00071
00072
00073
00074
00075
00076
00077 #include <vcl_iostream.h>
00078 #include <vcl_string.h>
00079
00080 class vnl_decnum
00081 {
00082 char sign_;
00083 vcl_string data_;
00084
00085
00086
00087 long exp_;
00088
00089
00090 vnl_decnum(char s, vcl_string const& d, long e) : sign_(s), data_(d), exp_(e) {}
00091 public:
00092 vcl_string data() const { return data_; }
00093 char sign() const { return sign_; }
00094 long exp () const { return exp_; }
00095
00096 vnl_decnum() : sign_(' '), data_(""), exp_(0L) {}
00097
00098 vnl_decnum(vnl_decnum const& r)
00099 : sign_(r.sign_), data_(r.data_), exp_(r.exp_) {}
00100
00101
00102
00103
00104
00105
00106 vnl_decnum(vcl_string const&);
00107 vnl_decnum(char const* r) { operator=(vcl_string(r)); }
00108
00109 explicit vnl_decnum(unsigned long);
00110
00111
00112 explicit vnl_decnum(long r)
00113 : sign_(r<0 ? '-' : r>0 ? '+' : ' ')
00114 { vnl_decnum d((unsigned long)(r<0?-r:r)); data_=d.data(); exp_=d.exp(); }
00115
00116
00117 explicit vnl_decnum(unsigned int r)
00118 : sign_(r>0 ? '+' : ' ')
00119 { vnl_decnum d((unsigned long)r); data_=d.data(); exp_=d.exp(); }
00120
00121
00122 explicit vnl_decnum(int r)
00123 : sign_(r<0 ? '-' : r>0 ? '+' : ' ')
00124 { vnl_decnum d((unsigned long)(r<0?-r:r)); data_=d.data(); exp_=d.exp(); }
00125
00126
00127
00128 vnl_decnum(double);
00129
00130 ~vnl_decnum() {}
00131
00132
00133 operator vcl_string() const;
00134
00135 operator long() const;
00136 operator unsigned long() const;
00137 operator int() const;
00138 operator unsigned int() const;
00139
00140
00141 inline vnl_decnum operator+() const { return *this; }
00142
00143 inline vnl_decnum operator-() const { if (sign_==' ') return *this; else return vnl_decnum(sign_=='-'?'+':'-', data_, exp_); }
00144
00145
00146 inline vnl_decnum operator<<(long int r) const { return sign_==' ' ? *this : vnl_decnum(sign_, data_, exp_+r); }
00147 inline vnl_decnum operator<<(int r) const { return operator<<((long int)r); }
00148
00149 inline vnl_decnum operator>>(long int r) const { return sign_==' ' ? *this : vnl_decnum(sign_, data_, exp_-r); }
00150 inline vnl_decnum operator>>(int r) const { return operator>>((long int)r); }
00151
00152 inline vnl_decnum& operator<<=(long int r) { if (sign_!=' ') exp_ += r; return *this; }
00153 inline vnl_decnum& operator<<=(int r) { if (sign_!=' ') exp_ += r; return *this; }
00154
00155 inline vnl_decnum& operator>>=(long int r) { if (sign_!=' ') exp_ -= r; return *this; }
00156 inline vnl_decnum& operator>>=(int r) { if (sign_!=' ') exp_ -= r; return *this; }
00157
00158
00159
00160
00161
00162 vnl_decnum& compactify();
00163
00164
00165
00166 inline vnl_decnum& expand() { return *this = operator+(1L)-1L; }
00167
00168
00169 inline vnl_decnum& operator=(const vnl_decnum& r) { sign_=r.sign(); data_=r.data(); exp_=r.exp(); return *this; }
00170
00171 vnl_decnum operator+(vnl_decnum const& r) const;
00172
00173 inline vnl_decnum operator-(vnl_decnum const& r) const { return operator+(-r); }
00174
00175 vnl_decnum operator*(vnl_decnum const& r) const;
00176
00177
00178
00179
00180 vnl_decnum operator/(vnl_decnum const& r) const;
00181
00182
00183
00184 vnl_decnum operator%(vnl_decnum const& r) const;
00185
00186 inline vnl_decnum& operator+=(vnl_decnum const& r) { return *this = operator+(r); }
00187 inline vnl_decnum& operator-=(vnl_decnum const& r) { return *this = operator+(-r); }
00188 inline vnl_decnum& operator*=(vnl_decnum const& r) { return *this = operator*(r); }
00189 inline vnl_decnum& operator/=(vnl_decnum const& r) { return *this = operator/(r); }
00190 inline vnl_decnum& operator%=(vnl_decnum const& r) { return *this = operator%(r); }
00191
00192
00193
00194 inline vnl_decnum& operator=(vcl_string const& r) { return operator=(vnl_decnum(r)); }
00195 inline vnl_decnum& operator=(char const* r) { return operator=(vnl_decnum(vcl_string(r))); }
00196 inline vnl_decnum& operator=(unsigned long r) { return operator=(vnl_decnum(r)); }
00197 inline vnl_decnum& operator=(long r) { return operator=(vnl_decnum(r)); }
00198 inline vnl_decnum& operator=(unsigned int r) { return operator=(vnl_decnum(r)); }
00199 inline vnl_decnum& operator=(int r) { return operator=(vnl_decnum(r)); }
00200 inline vnl_decnum& operator=(double r) { return operator=(vnl_decnum(r)); }
00201
00202 inline vnl_decnum operator+(vcl_string const& r) const { return operator+(vnl_decnum(r)); }
00203 inline vnl_decnum operator+(char const* r) const { return operator+(vnl_decnum(vcl_string(r))); }
00204 inline vnl_decnum operator+(unsigned long r) const { return operator+(vnl_decnum(r)); }
00205 inline vnl_decnum operator+(long r) const { return operator+(vnl_decnum(r)); }
00206 inline vnl_decnum operator+(unsigned int r) const { return operator+(vnl_decnum(r)); }
00207 inline vnl_decnum operator+(int r) const { return operator+(vnl_decnum(r)); }
00208 inline vnl_decnum operator+(double r) const { return operator+(vnl_decnum(r)); }
00209
00210 inline vnl_decnum operator-(vcl_string const& r) const { return operator-(vnl_decnum(r)); }
00211 inline vnl_decnum operator-(char const* r) const { return operator-(vnl_decnum(vcl_string(r))); }
00212 inline vnl_decnum operator-(unsigned long r) const { return operator-(vnl_decnum(r)); }
00213 inline vnl_decnum operator-(long r) const { return operator+(vnl_decnum(-r)); }
00214 inline vnl_decnum operator-(unsigned int r) const { return operator-(vnl_decnum(r)); }
00215 inline vnl_decnum operator-(int r) const { return operator+(vnl_decnum(-r)); }
00216 inline vnl_decnum operator-(double r) const { return operator+(vnl_decnum(-r)); }
00217
00218 inline vnl_decnum operator*(vcl_string const& r) const { return operator*(vnl_decnum(r)); }
00219 inline vnl_decnum operator*(char const* r) const { return operator*(vnl_decnum(vcl_string(r))); }
00220 inline vnl_decnum operator*(unsigned long r) const { return operator*(vnl_decnum(r)); }
00221 inline vnl_decnum operator*(long r) const { return operator*(vnl_decnum(r)); }
00222 inline vnl_decnum operator*(unsigned int r) const { return operator*(vnl_decnum(r)); }
00223 inline vnl_decnum operator*(int r) const { return operator*(vnl_decnum(r)); }
00224 inline vnl_decnum operator*(double r) const { return operator*(vnl_decnum(r)); }
00225
00226 inline vnl_decnum operator/(vcl_string const& r) const { return operator/(vnl_decnum(r)); }
00227 inline vnl_decnum operator/(char const* r) const { return operator/(vnl_decnum(vcl_string(r))); }
00228 inline vnl_decnum operator/(unsigned long r) const { return operator/(vnl_decnum(r)); }
00229 inline vnl_decnum operator/(long r) const { return operator/(vnl_decnum(r)); }
00230 inline vnl_decnum operator/(unsigned int r) const { return operator/(vnl_decnum(r)); }
00231 inline vnl_decnum operator/(int r) const { return operator/(vnl_decnum(r)); }
00232 inline vnl_decnum operator/(double r) const { return operator/(vnl_decnum(r)); }
00233
00234 inline vnl_decnum operator%(vcl_string const& r) const { return operator%(vnl_decnum(r)); }
00235 inline vnl_decnum operator%(char const* r) const { return operator%(vnl_decnum(vcl_string(r))); }
00236 inline vnl_decnum operator%(unsigned long r) const { return operator%(vnl_decnum(r)); }
00237 inline vnl_decnum operator%(long r) const { return operator%(vnl_decnum(r)); }
00238 inline vnl_decnum operator%(unsigned int r) const { return operator%(vnl_decnum(r)); }
00239 inline vnl_decnum operator%(int r) const { return operator%(vnl_decnum(r)); }
00240 inline vnl_decnum operator%(double r) const { return operator%(vnl_decnum(r)); }
00241
00242 inline vnl_decnum& operator+=(vcl_string const& r) { return *this = operator+(vnl_decnum(r)); }
00243 inline vnl_decnum& operator-=(vcl_string const& r) { return *this = operator-(vnl_decnum(r)); }
00244 inline vnl_decnum& operator*=(vcl_string const& r) { return *this = operator*(vnl_decnum(r)); }
00245 inline vnl_decnum& operator/=(vcl_string const& r) { return *this = operator/(vnl_decnum(r)); }
00246 inline vnl_decnum& operator%=(vcl_string const& r) { return *this = operator%(vnl_decnum(r)); }
00247
00248 inline vnl_decnum& operator+=(char const* r) { return *this = operator+(vcl_string(r)); }
00249 inline vnl_decnum& operator-=(char const* r) { return *this = operator-(vcl_string(r)); }
00250 inline vnl_decnum& operator*=(char const* r) { return *this = operator*(vcl_string(r)); }
00251 inline vnl_decnum& operator/=(char const* r) { return *this = operator/(vcl_string(r)); }
00252 inline vnl_decnum& operator%=(char const* r) { return *this = operator%(vcl_string(r)); }
00253
00254 inline vnl_decnum& operator+=(unsigned long r) { return *this = operator+(vnl_decnum(r)); }
00255 inline vnl_decnum& operator-=(unsigned long r) { return *this = operator-(vnl_decnum(r)); }
00256 inline vnl_decnum& operator*=(unsigned long r) { return *this = operator*(vnl_decnum(r)); }
00257 inline vnl_decnum& operator/=(unsigned long r) { return *this = operator/(vnl_decnum(r)); }
00258 inline vnl_decnum& operator%=(unsigned long r) { return *this = operator%(vnl_decnum(r)); }
00259
00260 inline vnl_decnum& operator+=(long r) { return *this = operator+(vnl_decnum(r)); }
00261 inline vnl_decnum& operator-=(long r) { return *this = operator+(vnl_decnum(-r)); }
00262 inline vnl_decnum& operator*=(long r) { return *this = operator*(vnl_decnum(r)); }
00263 inline vnl_decnum& operator/=(long r) { return *this = operator/(vnl_decnum(r)); }
00264 inline vnl_decnum& operator%=(long r) { return *this = operator%(vnl_decnum(r)); }
00265
00266 inline vnl_decnum& operator+=(unsigned int r) { return *this = operator+(vnl_decnum(r)); }
00267 inline vnl_decnum& operator-=(unsigned int r) { return *this = operator-(vnl_decnum(r)); }
00268 inline vnl_decnum& operator*=(unsigned int r) { return *this = operator*(vnl_decnum(r)); }
00269 inline vnl_decnum& operator/=(unsigned int r) { return *this = operator/(vnl_decnum(r)); }
00270 inline vnl_decnum& operator%=(unsigned int r) { return *this = operator%(vnl_decnum(r)); }
00271
00272 inline vnl_decnum& operator+=(int r) { return *this = operator+(vnl_decnum(r)); }
00273 inline vnl_decnum& operator-=(int r) { return *this = operator+(vnl_decnum(-r)); }
00274 inline vnl_decnum& operator*=(int r) { return *this = operator*(vnl_decnum(r)); }
00275 inline vnl_decnum& operator/=(int r) { return *this = operator/(vnl_decnum(r)); }
00276 inline vnl_decnum& operator%=(int r) { return *this = operator%(vnl_decnum(r)); }
00277
00278 inline vnl_decnum& operator+=(double r) { return *this = operator+(vnl_decnum(r)); }
00279 inline vnl_decnum& operator-=(double r) { return *this = operator+(vnl_decnum(-r)); }
00280 inline vnl_decnum& operator*=(double r) { return *this = operator*(vnl_decnum(r)); }
00281 inline vnl_decnum& operator/=(double r) { return *this = operator/(vnl_decnum(r)); }
00282 inline vnl_decnum& operator%=(double r) { return *this = operator%(vnl_decnum(r)); }
00283
00284
00285 inline vnl_decnum& operator++() { return *this = operator+(1L); }
00286
00287 inline vnl_decnum& operator--() { return *this = operator-(1L); }
00288
00289 inline vnl_decnum operator++(int) { vnl_decnum b=(*this); operator++(); return b; }
00290
00291 inline vnl_decnum operator--(int) { vnl_decnum b=(*this); operator--(); return b; }
00292
00293 bool operator==(vnl_decnum const&) const;
00294 bool operator< (vnl_decnum const&) const;
00295 inline bool operator!=(vnl_decnum const& r) const { return !operator==(r); }
00296 inline bool operator> (vnl_decnum const& r) const { return r<(*this); }
00297 inline bool operator<=(vnl_decnum const& r) const { return !operator>(r); }
00298 inline bool operator>=(vnl_decnum const& r) const { return !operator<(r); }
00299
00300 inline bool operator==(vcl_string const& r) const { return operator==(vnl_decnum(r)); }
00301 inline bool operator< (vcl_string const& r) const { return operator< (vnl_decnum(r)); }
00302 inline bool operator!=(vcl_string const& r) const { return operator!=(vnl_decnum(r)); }
00303 inline bool operator> (vcl_string const& r) const { return operator> (vnl_decnum(r)); }
00304 inline bool operator<=(vcl_string const& r) const { return operator<=(vnl_decnum(r)); }
00305 inline bool operator>=(vcl_string const& r) const { return operator>=(vnl_decnum(r)); }
00306
00307 inline bool operator==(char const* r) const { return operator==(vcl_string(r)); }
00308 inline bool operator< (char const* r) const { return operator< (vcl_string(r)); }
00309 inline bool operator!=(char const* r) const { return operator!=(vcl_string(r)); }
00310 inline bool operator> (char const* r) const { return operator> (vcl_string(r)); }
00311 inline bool operator<=(char const* r) const { return operator<=(vcl_string(r)); }
00312 inline bool operator>=(char const* r) const { return operator>=(vcl_string(r)); }
00313
00314 inline bool operator==(unsigned long r) const { return operator==(vnl_decnum(r)); }
00315 inline bool operator< (unsigned long r) const { return operator< (vnl_decnum(r)); }
00316 inline bool operator!=(unsigned long r) const { return operator!=(vnl_decnum(r)); }
00317 inline bool operator> (unsigned long r) const { return operator> (vnl_decnum(r)); }
00318 inline bool operator<=(unsigned long r) const { return operator<=(vnl_decnum(r)); }
00319 inline bool operator>=(unsigned long r) const { return operator>=(vnl_decnum(r)); }
00320
00321 inline bool operator==(long r) const { return operator==(vnl_decnum(r)); }
00322 inline bool operator< (long r) const { return operator< (vnl_decnum(r)); }
00323 inline bool operator!=(long r) const { return operator!=(vnl_decnum(r)); }
00324 inline bool operator> (long r) const { return operator> (vnl_decnum(r)); }
00325 inline bool operator<=(long r) const { return operator<=(vnl_decnum(r)); }
00326 inline bool operator>=(long r) const { return operator>=(vnl_decnum(r)); }
00327
00328 inline bool operator==(unsigned int r) const { return operator==(vnl_decnum(r)); }
00329 inline bool operator< (unsigned int r) const { return operator< (vnl_decnum(r)); }
00330 inline bool operator!=(unsigned int r) const { return operator!=(vnl_decnum(r)); }
00331 inline bool operator> (unsigned int r) const { return operator> (vnl_decnum(r)); }
00332 inline bool operator<=(unsigned int r) const { return operator<=(vnl_decnum(r)); }
00333 inline bool operator>=(unsigned int r) const { return operator>=(vnl_decnum(r)); }
00334
00335 inline bool operator==(int r) const { return operator==(vnl_decnum(r)); }
00336 inline bool operator< (int r) const { return operator< (vnl_decnum(r)); }
00337 inline bool operator!=(int r) const { return operator!=(vnl_decnum(r)); }
00338 inline bool operator> (int r) const { return operator> (vnl_decnum(r)); }
00339 inline bool operator<=(int r) const { return operator<=(vnl_decnum(r)); }
00340 inline bool operator>=(int r) const { return operator>=(vnl_decnum(r)); }
00341
00342 inline bool operator==(double r) const { return operator==(vnl_decnum(r)); }
00343 inline bool operator< (double r) const { return operator< (vnl_decnum(r)); }
00344 inline bool operator!=(double r) const { return operator!=(vnl_decnum(r)); }
00345 inline bool operator> (double r) const { return operator> (vnl_decnum(r)); }
00346 inline bool operator<=(double r) const { return operator<=(vnl_decnum(r)); }
00347 inline bool operator>=(double r) const { return operator>=(vnl_decnum(r)); }
00348
00349 inline vnl_decnum abs() const { return sign_=='-' ? operator-() : *this; }
00350 inline vnl_decnum trunc() const { return exp_>=0L ? *this : vnl_decnum(sign_,data_.substr(0L,data_.length()+exp_),0L); }
00351 inline vnl_decnum roundup() const { return operator==(trunc()) ? *this : sign_=='-' ? trunc()-1 : trunc()+1; }
00352 inline vnl_decnum floor() const { return sign_=='-' ? roundup() : trunc(); }
00353 inline vnl_decnum ceil() const { return sign_=='-' ? trunc() : roundup(); }
00354 inline vnl_decnum pow(unsigned long p) const { return p==0L ? vnl_decnum(1L) : p==1L ? *this : pow(p/2)*pow((p+1)/2); }
00355
00356 private:
00357
00358
00359
00360 static vnl_decnum plus(vcl_string const&, vcl_string const&, long);
00361
00362
00363
00364
00365 static vnl_decnum minus(vcl_string const&, vcl_string const&, long);
00366
00367
00368
00369 static bool comp(vcl_string const&, vcl_string const&);
00370
00371 inline static vcl_string add_zeros(vcl_string const& source, unsigned long n)
00372 { vcl_string d = source; while (n--!=0) d.push_back('0'); return d; }
00373
00374
00375
00376 static vcl_string mult(vcl_string const&, char);
00377
00378
00379
00380
00381
00382
00383 static vcl_string div(vcl_string const&, vcl_string&);
00384 };
00385
00386
00387
00388 inline vcl_ostream& operator<<(vcl_ostream& s, vnl_decnum const& r)
00389 { return s << vcl_string(r); }
00390
00391
00392
00393 vcl_istream& operator>>(vcl_istream& s, vnl_decnum& r);
00394
00395 inline vnl_decnum ceil(vnl_decnum const& x) { return x.ceil(); }
00396 inline vnl_decnum floor(vnl_decnum const& x) { return x.floor(); }
00397 inline vnl_decnum pow(vnl_decnum const& x, unsigned long p) { return x.pow(p); }
00398
00399 inline vnl_decnum vnl_math_abs(vnl_decnum const& x) { return x.abs(); }
00400 inline vnl_decnum vnl_math_sqr(vnl_decnum const& x) { return x*x; }
00401 inline vnl_decnum vnl_math_cube(vnl_decnum const& x) { return x*x*x; }
00402 inline vnl_decnum vnl_math_squared_magnitude(vnl_decnum const& x) { return x*x; }
00403 inline bool vnl_math_isnan(vnl_decnum const& x) { return x.data() == "NaN"; }
00404 inline bool vnl_math_isfinite(vnl_decnum const& x) { return x.data() != "Inf" && x.data() != "NaN"; }
00405 inline vnl_decnum vnl_math_max(vnl_decnum const& x, vnl_decnum const& y) { return (x < y) ? y : x; }
00406 inline vnl_decnum vnl_math_min(vnl_decnum const& x, vnl_decnum const& y) { return (x < y) ? x : y; }
00407 inline int vnl_math_sgn(vnl_decnum x) { return x.sign()==' '?0:x.sign()=='+'?1:-1; }
00408 inline int vnl_math_sgn0(vnl_decnum x) { return x.sign()=='-'?-1:1; }
00409
00410 #endif // vnl_decnum_h_