home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 2.ddi / INCLUDE.ZIP / BCD.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  8.4 KB  |  367 lines

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