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