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