home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / bcd.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  10.0 KB  |  374 lines

  1. /*  bcd.h
  2.  
  3.     BCD Number Library - Include File
  4.     class bcd:  declarations for decimal numbers.
  5.  
  6. */
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 10.0
  10.  *
  11.  *      Copyright (c) 1987, 2000 by Inprise Corporation
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16. /* $Revision:   9.5  $ */
  17.  
  18.  
  19. #ifndef __BCD_H
  20. #define __BCD_H
  21.  
  22. #ifndef __cplusplus
  23. #error Must use C++ for the type bcd.
  24. #endif
  25.  
  26. #ifndef ___STDDEF_H
  27. #include <_stddef.h>
  28. #endif
  29.  
  30. #if !defined(__MATH_H)
  31. #include <math.h>
  32. #endif
  33.  
  34. #include <iosfwd>
  35.  
  36. #if !defined(RC_INVOKED)
  37.  
  38. #pragma pack(push, 1)
  39.  
  40. #if defined(__STDC__)
  41. #pragma warn -nak
  42. #endif
  43.  
  44. #endif  /* !RC_INVOKED */
  45.  
  46.  
  47. #define _BcdMaxDecimals     5000
  48.  
  49. class _EXPCLASS bcd {
  50.  
  51. public:
  52.     // constructors
  53.     _RTLENTRY bcd();
  54.     _RTLENTRY bcd(int x);
  55.     _RTLENTRY bcd(unsigned int x);
  56.     _RTLENTRY bcd(long x);
  57.     _RTLENTRY bcd(unsigned long x);
  58.     _RTLENTRY bcd(double x, int decimals = _BcdMaxDecimals);
  59.     _RTLENTRY bcd(long double x, int decimals = _BcdMaxDecimals);
  60.  
  61.     // bcd manipulations
  62.     friend inline long double _RTLENTRY real(const bcd  &);   // Return the real part
  63.  
  64.     // Overloaded ANSI C math functions
  65.     friend inline bcd _RTLENTRY _EXPFUNC abs(const bcd  &);
  66.     friend inline bcd _RTLENTRY _EXPFUNC acos(const bcd  &);
  67.     friend inline bcd _RTLENTRY _EXPFUNC asin(const bcd  &);
  68.     friend inline bcd _RTLENTRY _EXPFUNC atan(const bcd  &);
  69.     friend inline bcd _RTLENTRY _EXPFUNC cos(const bcd  &);
  70.     friend inline bcd _RTLENTRY _EXPFUNC cosh(const bcd  &);
  71.     friend inline bcd _RTLENTRY _EXPFUNC exp(const bcd  &);
  72.     friend inline bcd _RTLENTRY _EXPFUNC log(const bcd  &);
  73.     friend inline bcd _RTLENTRY _EXPFUNC log10(const bcd  &);
  74.     friend inline bcd _RTLENTRY _EXPFUNC pow(const bcd  & base, const bcd  & expon);
  75.     friend inline bcd _RTLENTRY _EXPFUNC sin(const bcd  &);
  76.     friend inline bcd _RTLENTRY _EXPFUNC sinh(const bcd  &);
  77.     friend inline bcd _RTLENTRY _EXPFUNC sqrt(const bcd  &);
  78.     friend inline bcd _RTLENTRY _EXPFUNC tan(const bcd  &);
  79.     friend inline bcd _RTLENTRY _EXPFUNC tanh(const bcd  &);
  80.  
  81.     // Binary Operator Functions
  82.     friend inline bcd _RTLENTRY _EXPFUNC operator+(const bcd  &, const bcd  &);
  83.     friend inline bcd _RTLENTRY _EXPFUNC operator+(long double, const bcd  &);
  84.     friend inline bcd _RTLENTRY _EXPFUNC operator+(const bcd  &, long double);
  85.     friend inline bcd _RTLENTRY _EXPFUNC operator-(const bcd  &, const bcd  &);
  86.     friend inline bcd _RTLENTRY _EXPFUNC operator-(long double, const bcd  &);
  87.     friend inline bcd _RTLENTRY _EXPFUNC operator-(const bcd  &, long double);
  88.     friend inline bcd _RTLENTRY _EXPFUNC operator*(const bcd  &, const bcd  &);
  89.     friend inline bcd _RTLENTRY _EXPFUNC operator*(const bcd  &, long double);
  90.     friend inline bcd _RTLENTRY _EXPFUNC operator*(long double, const bcd  &);
  91.     friend inline bcd _RTLENTRY _EXPFUNC operator/(const bcd  &, const bcd  &);
  92.     friend inline bcd _RTLENTRY _EXPFUNC operator/(const bcd  &, long double);
  93.     friend inline bcd _RTLENTRY _EXPFUNC operator/(long double, const bcd  &);
  94.     friend inline int _RTLENTRY _EXPFUNC operator==(const bcd  &, const bcd  &);
  95.     friend inline int _RTLENTRY _EXPFUNC operator!=(const bcd  &, const bcd  &);
  96.     friend inline int _RTLENTRY _EXPFUNC operator>=(const bcd  &, const bcd  &);
  97.     friend inline int _RTLENTRY _EXPFUNC operator<=(const bcd  &, const bcd  &);
  98.     friend inline int _RTLENTRY _EXPFUNC operator>(const bcd  &, const bcd  &);
  99.     friend inline int _RTLENTRY _EXPFUNC operator<(const bcd  &, const bcd  &);
  100.     bcd  & _RTLENTRY operator+=(const bcd  &);
  101.     bcd  & _RTLENTRY operator+=(long double);
  102.     bcd  & _RTLENTRY operator-=(const bcd  &);
  103.     bcd  & _RTLENTRY operator-=(long double);
  104.     bcd  & _RTLENTRY operator*=(const bcd  &);
  105.     bcd  & _RTLENTRY operator*=(long double);
  106.     bcd  & _RTLENTRY operator/=(const bcd  &);
  107.     bcd  & _RTLENTRY operator/=(long double);
  108.     bcd  _RTLENTRY operator+();
  109.     bcd  _RTLENTRY operator-();
  110.  
  111. // Implementation
  112. private:
  113.     long mantissa[2];
  114.     int expo;
  115. };
  116.  
  117. // const bcd _bcd_zero(0);
  118.  
  119. enum _bcdexpo {
  120.     _ExpoZero,
  121.     _ExpoInf,
  122.     _ExpoNan
  123. };
  124.  
  125. extern "C"
  126. {
  127.     long double _RTLENTRY _EXPFUNC __bcd_tobinary (const bcd *p);
  128.     void        _RTLENTRY _EXPFUNC __bcd_todecimal(long double x, int decimals, bcd *p);
  129.     long double _RTLENTRY _EXPFUNC __bcd_log10    (const bcd *p);
  130.     void        _RTLENTRY _EXPFUNC __bcd_pow10    (int n, bcd *p);
  131. }
  132.  
  133. inline _RTLENTRY bcd::bcd()
  134. {
  135. /* if you want zero ...
  136.     mantissa[0] = 0;
  137.     mantissa[1] = 0;
  138.     expo = ExpoZero;
  139. */
  140. }
  141.  
  142. inline _RTLENTRY bcd::bcd(long double x, int decimals)
  143. {
  144.     __bcd_todecimal(x,decimals,this);
  145. }
  146.  
  147. inline _RTLENTRY bcd::bcd(double x, int decimals)
  148. {
  149.     __bcd_todecimal(x,decimals,this);
  150. }
  151.  
  152. inline _RTLENTRY bcd::bcd(int x)
  153. {
  154.     mantissa[0] = x;
  155.     mantissa[1] = x >= 0 ? 0 : -1;
  156.     expo = 0;
  157. }
  158.  
  159. inline _RTLENTRY bcd::bcd(unsigned int x)
  160. {
  161.     mantissa[0] = x;
  162.     mantissa[1] = 0;
  163.     expo = 0;
  164. }
  165.  
  166. inline _RTLENTRY bcd::bcd(long x)
  167. {
  168.     mantissa[0] = x;
  169.     mantissa[1] = x >= 0 ? 0 : -1;
  170.     expo = 0;
  171. }
  172.  
  173. inline _RTLENTRY bcd::bcd(unsigned long x)
  174. {
  175.     mantissa[0] = x;
  176.     mantissa[1] = 0;
  177.     expo = 0;
  178. }
  179.  
  180. inline long double _RTLENTRY real(const bcd  &z)
  181. {
  182.     return __bcd_tobinary(&z);
  183. }
  184.  
  185. // Definitions of compound-assignment operator member functions
  186.  
  187. inline bcd& _RTLENTRY bcd::operator+=(const bcd  &b)
  188. {
  189.     __bcd_todecimal(real(*this)+real(b),_BcdMaxDecimals,this);
  190.     return *this;
  191. }
  192.  
  193. inline bcd& _RTLENTRY bcd::operator+=(long double b)
  194. {
  195.     __bcd_todecimal(real(*this)+b,_BcdMaxDecimals,this);
  196.     return *this;
  197. }
  198.  
  199. inline bcd& _RTLENTRY bcd::operator-=(const bcd  &b)
  200. {
  201.     __bcd_todecimal(real(*this)-real(b),_BcdMaxDecimals,this);
  202.     return *this;
  203. }
  204.  
  205. inline bcd& _RTLENTRY bcd::operator-=(long double b)
  206. {
  207.     __bcd_todecimal(real(*this)-b,_BcdMaxDecimals,this);
  208.     return *this;
  209. }
  210.  
  211. inline bcd& _RTLENTRY bcd::operator*=(const bcd  &b)
  212. {
  213.     __bcd_todecimal(real(*this)*real(b),_BcdMaxDecimals,this);
  214.     return *this;
  215. }
  216.  
  217. inline bcd& _RTLENTRY bcd::operator*=(long double b)
  218. {
  219.     __bcd_todecimal(real(*this)*b,_BcdMaxDecimals,this);
  220.     return *this;
  221. }
  222.  
  223. inline bcd& _RTLENTRY bcd::operator/=(const bcd  &b)
  224. {
  225.     __bcd_todecimal(real(*this)/real(b),_BcdMaxDecimals,this);
  226.     return *this;
  227. }
  228.  
  229. inline bcd& _RTLENTRY bcd::operator/=(long double b)
  230. {
  231.     __bcd_todecimal(real(*this)/b,_BcdMaxDecimals,this);
  232.     return *this;
  233. }
  234.  
  235.  
  236. // Definitions of non-member binary operator functions
  237.  
  238. inline bcd _RTLENTRY operator+(const bcd  &a, const bcd  &b)
  239. {
  240.     return bcd(real(a) + real(b));
  241. }
  242.  
  243. inline bcd _RTLENTRY operator+(long double a, const bcd  &b)
  244. {
  245.     return bcd(a + real(b));
  246. }
  247.  
  248. inline bcd _RTLENTRY operator+(const bcd  &a, long double b)
  249. {
  250.     return bcd(real(a) + b);
  251. }
  252.  
  253. inline bcd _RTLENTRY operator-(const bcd  &a, const bcd  &b)
  254. {
  255.     return bcd(real(a) - real(b));
  256. }
  257.  
  258. inline bcd _RTLENTRY operator-(long double a, const bcd  &b)
  259. {
  260.     return bcd(a - real(b));
  261. }
  262.  
  263. inline bcd _RTLENTRY operator-(const bcd  &a, long double b)
  264. {
  265.     return bcd(real(a) - b);
  266. }
  267.  
  268. inline bcd _RTLENTRY operator*(const bcd  &a, const bcd  &b)
  269. {
  270.     return bcd(real(a) * real(b));
  271. }
  272.  
  273. inline bcd _RTLENTRY operator*(const bcd  &a, long double b)
  274. {
  275.     return bcd(real(a) * b);
  276. }
  277.  
  278. inline bcd _RTLENTRY operator*(long double a, const bcd  &b)
  279. {
  280.     return bcd(a * real(b));
  281. }
  282.  
  283. inline bcd _RTLENTRY operator/(const bcd  &a, const bcd   &b)
  284. {
  285.     return bcd(real(a) / real(b));
  286. }
  287.  
  288. inline bcd _RTLENTRY operator/(long double a, const bcd  &b)
  289. {
  290.     return bcd(a / real(b));
  291. }
  292.  
  293. inline bcd _RTLENTRY operator/(const bcd  &a, long double b)
  294. {
  295.     return bcd(real(a) / b);
  296. }
  297.  
  298. inline int _RTLENTRY operator==(const bcd  &a, const bcd  &b)
  299. {
  300.     return real(a) == real(b);
  301. }
  302.  
  303. inline int _RTLENTRY operator!=(const bcd  &a, const bcd  &b)
  304. {
  305.     return real(a) != real(b);
  306. }
  307.  
  308. inline int _RTLENTRY operator>=(const bcd  &a, const bcd  &b)
  309. {
  310.     return real(a) >= real(b);
  311. }
  312.  
  313. inline int _RTLENTRY operator<=(const bcd  &a, const bcd  &b)
  314. {
  315.     return real(a) <= real(b);
  316. }
  317.  
  318. inline int _RTLENTRY operator>(const bcd  &a, const bcd  &b)
  319. {
  320.     return real(a) > real(b);
  321. }
  322.  
  323. inline int _RTLENTRY operator<(const bcd  &a, const bcd  &b)
  324. {
  325.     return real(a) < real(b);
  326. }
  327.  
  328. inline bcd _RTLENTRY bcd::operator+()
  329. {
  330.     return *this;
  331. }
  332.  
  333. inline bcd _RTLENTRY bcd::operator-()
  334. {
  335.     return bcd(-real(*this));
  336. }
  337.  
  338. inline bcd _RTLENTRY abs(const bcd& a)   { return bcd(fabs(real(a)));}
  339. inline bcd _RTLENTRY acos(const bcd& a)  { return bcd(acos(real(a)));}
  340. inline bcd _RTLENTRY asin(const bcd& a)  { return bcd(asin(real(a)));}
  341. inline bcd _RTLENTRY atan(const bcd& a)  { return bcd(atan(real(a)));}
  342. inline bcd _RTLENTRY cos(const bcd& a)   { return bcd(cos(real(a)));}
  343. inline bcd _RTLENTRY cosh(const bcd& a)  { return bcd(cosh(real(a)));}
  344. inline bcd _RTLENTRY exp(const bcd& a)   { return bcd(exp(real(a)));}
  345. inline bcd _RTLENTRY log(const bcd& a)   { return bcd(log(real(a)));}
  346. inline bcd _RTLENTRY log10(const bcd& a) { return bcd(__bcd_log10(&a));}
  347. inline bcd _RTLENTRY sin(const bcd& a)   { return bcd(sin(real(a)));}
  348. inline bcd _RTLENTRY sinh(const bcd& a)  { return bcd(sinh(real(a)));}
  349. inline bcd _RTLENTRY sqrt(const bcd& a)  { return bcd(sqrt(real(a)));}
  350. inline bcd _RTLENTRY tan(const bcd& a)   { return bcd(tan(real(a)));}
  351. inline bcd _RTLENTRY tanh(const bcd& a)  { return bcd(tanh(real(a)));}
  352.  
  353. inline bcd _RTLENTRY pow(const bcd& a, const bcd& b)   { return bcd(pow(real(a),real(b)));}
  354. inline void _RTLENTRY pow10(int n, bcd& a) { __bcd_pow10(n,&a);}
  355.  
  356. // bcd stream I/O
  357.  
  358. std::ostream  & _RTLENTRY _EXPFUNC operator<<(std::ostream  &, const bcd  &);
  359. std::istream  & _RTLENTRY _EXPFUNC operator>>(std::istream  &, bcd  &);
  360.  
  361.  
  362. #if !defined(RC_INVOKED)
  363.  
  364. #if defined(__STDC__)
  365. #pragma warn .nak
  366. #endif
  367.  
  368. /* restore default packing */
  369. #pragma pack(pop)
  370.  
  371. #endif  /* !RC_INVOKED */
  372.  
  373. #endif  // __BCD_H
  374.