home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / INCLUDE.ZIP / BCD.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  9.0 KB  |  363 lines

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