home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / INC.PAK / BCD.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  11KB  |  413 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 6.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. #if !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
  42. #pragma option -po-     // disable Object data calling convention
  43. #endif
  44. #endif
  45.  
  46. #pragma option -Vo-
  47.  
  48. #if defined(__STDC__)
  49. #pragma warn -nak
  50. #endif
  51.  
  52. #endif  /* !RC_INVOKED */
  53.  
  54.  
  55. #define _BcdMaxDecimals     5000
  56.  
  57. _CLASSDEF(bcd)
  58.  
  59. class _EXPCLASS bcd {
  60.  
  61. public:
  62.     // constructors
  63.     _RTLENTRY bcd();
  64.     _RTLENTRY bcd(int x);
  65.     _RTLENTRY bcd(unsigned int x);
  66.     _RTLENTRY bcd(long x);
  67.     _RTLENTRY bcd(unsigned long x);
  68.     _RTLENTRY bcd(double x, int decimals = _BcdMaxDecimals);
  69.     _RTLENTRY bcd(long double x, int decimals = _BcdMaxDecimals);
  70.  
  71.     // bcd manipulations
  72.     friend long double _RTLENTRY real(bcd _FAR &);   // Return the real part
  73.  
  74.     // Overloaded ANSI C math functions
  75.     friend bcd _RTLENTRY _EXPFUNC abs(bcd _FAR &);
  76.     friend bcd _RTLENTRY _EXPFUNC acos(bcd _FAR &);
  77.     friend bcd _RTLENTRY _EXPFUNC asin(bcd _FAR &);
  78.     friend bcd _RTLENTRY _EXPFUNC atan(bcd _FAR &);
  79.     friend bcd _RTLENTRY _EXPFUNC cos(bcd _FAR &);
  80.     friend bcd _RTLENTRY _EXPFUNC cosh(bcd _FAR &);
  81.     friend bcd _RTLENTRY _EXPFUNC exp(bcd _FAR &);
  82.     friend bcd _RTLENTRY _EXPFUNC log(bcd _FAR &);
  83.     friend bcd _RTLENTRY _EXPFUNC log10(bcd _FAR &);
  84.     friend bcd _RTLENTRY _EXPFUNC pow(bcd _FAR & base, bcd _FAR & expon);
  85.     friend bcd _RTLENTRY _EXPFUNC sin(bcd _FAR &);
  86.     friend bcd _RTLENTRY _EXPFUNC sinh(bcd _FAR &);
  87.     friend bcd _RTLENTRY _EXPFUNC sqrt(bcd _FAR &);
  88.     friend bcd _RTLENTRY _EXPFUNC tan(bcd _FAR &);
  89.     friend bcd _RTLENTRY _EXPFUNC tanh(bcd _FAR &);
  90.  
  91.     // Binary Operator Functions
  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-(long double, bcd _FAR &);
  97.     friend bcd _RTLENTRY _EXPFUNC operator-(bcd _FAR &, long double);
  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 bcd _RTLENTRY _EXPFUNC operator/(bcd _FAR &, bcd _FAR &);
  102.     friend bcd _RTLENTRY _EXPFUNC operator/(bcd _FAR &, long double);
  103.     friend bcd _RTLENTRY _EXPFUNC operator/(long double, 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.     friend int _RTLENTRY _EXPFUNC operator<=(bcd _FAR &, bcd _FAR &);
  108.     friend int _RTLENTRY _EXPFUNC operator>(bcd _FAR &, bcd _FAR &);
  109.     friend int _RTLENTRY _EXPFUNC operator<(bcd _FAR &, bcd _FAR &);
  110.     bcd _FAR & _RTLENTRY operator+=(bcd _FAR &);
  111.     bcd _FAR & _RTLENTRY operator+=(long double);
  112.     bcd _FAR & _RTLENTRY operator-=(bcd _FAR &);
  113.     bcd _FAR & _RTLENTRY operator-=(long double);
  114.     bcd _FAR & _RTLENTRY operator*=(bcd _FAR &);
  115.     bcd _FAR & _RTLENTRY operator*=(long double);
  116.     bcd _FAR & _RTLENTRY operator/=(bcd _FAR &);
  117.     bcd _FAR & _RTLENTRY operator/=(long double);
  118.     bcd  _RTLENTRY operator+();
  119.     bcd  _RTLENTRY operator-();
  120.  
  121. // Implementation
  122. private:
  123.     long mantissa[2];
  124.     int expo;
  125. };
  126.  
  127. // const bcd _bcd_zero(0);
  128.  
  129. enum _bcdexpo {
  130.     _ExpoZero,
  131.     _ExpoInf,
  132.     _ExpoNan
  133. };
  134.  
  135. extern "C"
  136. {
  137. #if defined(__FLAT__)
  138.     long double _RTLENTRY _EXPFUNC __bcd_tobinary (const bcd *p);
  139.     void        _RTLENTRY _EXPFUNC __bcd_todecimal(long double x, int decimals, bcd *p);
  140.     long double _RTLENTRY _EXPFUNC __bcd_log10    (bcd *p);
  141.     void        _RTLENTRY _EXPFUNC __bcd_pow10    (int n, bcd *p);
  142. #else
  143.     long double __pascal _EXPFUNC __bcd_tobinary(const bcd __far *p);
  144.     void        __pascal _EXPFUNC __bcd_todecimal(long double x, int decimals, bcd __far *p);
  145.     long double __pascal _EXPFUNC __bcd_log10(bcd __far *p);
  146.     void        __pascal _EXPFUNC __bcd_pow10(int n, bcd __far *p);
  147. #endif
  148. }
  149.  
  150. inline _RTLENTRY bcd::bcd()
  151. {
  152. /* if you want zero ...
  153.     mantissa[0] = 0;
  154.     mantissa[1] = 0;
  155.     expo = ExpoZero;
  156. */
  157. }
  158.  
  159. inline _RTLENTRY bcd::bcd(long double x, int decimals)
  160. {
  161.     __bcd_todecimal(x,decimals,this);
  162. }
  163.  
  164. inline _RTLENTRY bcd::bcd(double x, int decimals)
  165. {
  166.     __bcd_todecimal(x,decimals,this);
  167. }
  168.  
  169. inline _RTLENTRY bcd::bcd(int x)
  170. {
  171.     mantissa[0] = x;
  172.     mantissa[1] = x >= 0 ? 0 : -1;
  173.     expo = 0;
  174. }
  175.  
  176. inline _RTLENTRY bcd::bcd(unsigned int x)
  177. {
  178.     mantissa[0] = x;
  179.     mantissa[1] = 0;
  180.     expo = 0;
  181. }
  182.  
  183. inline _RTLENTRY bcd::bcd(long x)
  184. {
  185.     mantissa[0] = x;
  186.     mantissa[1] = x >= 0 ? 0 : -1;
  187.     expo = 0;
  188. }
  189.  
  190. inline _RTLENTRY bcd::bcd(unsigned long x)
  191. {
  192.     mantissa[0] = x;
  193.     mantissa[1] = 0;
  194.     expo = 0;
  195. }
  196.  
  197. inline long double _RTLENTRY real(bcd _FAR &z)
  198. {
  199.     return __bcd_tobinary(&z);
  200. }
  201.  
  202. // Definitions of compound-assignment operator member functions
  203.  
  204. inline bcd& _RTLENTRY bcd::operator+=(bcd _FAR &b)
  205. {
  206.     __bcd_todecimal(real(*this)+real(b),_BcdMaxDecimals,this);
  207.     return *this;
  208. }
  209.  
  210. inline bcd& _RTLENTRY bcd::operator+=(long double b)
  211. {
  212.     __bcd_todecimal(real(*this)+b,_BcdMaxDecimals,this);
  213.     return *this;
  214. }
  215.  
  216. inline bcd& _RTLENTRY bcd::operator-=(bcd _FAR &b)
  217. {
  218.     __bcd_todecimal(real(*this)-real(b),_BcdMaxDecimals,this);
  219.     return *this;
  220. }
  221.  
  222. inline bcd& _RTLENTRY bcd::operator-=(long double b)
  223. {
  224.     __bcd_todecimal(real(*this)-b,_BcdMaxDecimals,this);
  225.     return *this;
  226. }
  227.  
  228. inline bcd& _RTLENTRY bcd::operator*=(bcd _FAR &b)
  229. {
  230.     __bcd_todecimal(real(*this)*real(b),_BcdMaxDecimals,this);
  231.     return *this;
  232. }
  233.  
  234. inline bcd& _RTLENTRY bcd::operator*=(long double b)
  235. {
  236.     __bcd_todecimal(real(*this)*b,_BcdMaxDecimals,this);
  237.     return *this;
  238. }
  239.  
  240. inline bcd& _RTLENTRY bcd::operator/=(bcd _FAR &b)
  241. {
  242.     __bcd_todecimal(real(*this)/real(b),_BcdMaxDecimals,this);
  243.     return *this;
  244. }
  245.  
  246. inline bcd& _RTLENTRY bcd::operator/=(long double b)
  247. {
  248.     __bcd_todecimal(real(*this)/b,_BcdMaxDecimals,this);
  249.     return *this;
  250. }
  251.  
  252.  
  253. // Definitions of non-member binary operator functions
  254.  
  255. inline bcd _RTLENTRY operator+(bcd _FAR &a, bcd _FAR &b)
  256. {
  257.     return bcd(real(a) + real(b));
  258. }
  259.  
  260. inline bcd _RTLENTRY operator+(long double a, bcd _FAR &b)
  261. {
  262.     return bcd(a + real(b));
  263. }
  264.  
  265. inline bcd _RTLENTRY operator+(bcd _FAR &a, long double b)
  266. {
  267.     return bcd(real(a) + b);
  268. }
  269.  
  270. inline bcd _RTLENTRY operator-(bcd _FAR &a, bcd _FAR &b)
  271. {
  272.     return bcd(real(a) - real(b));
  273. }
  274.  
  275. inline bcd _RTLENTRY operator-(long double a, bcd _FAR &b)
  276. {
  277.     return bcd(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*(bcd _FAR &a, bcd _FAR &b)
  286. {
  287.     return bcd(real(a) * real(b));
  288. }
  289.  
  290. inline bcd _RTLENTRY operator*(bcd _FAR &a, long double b)
  291. {
  292.     return bcd(real(a) * 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, bcd  _FAR &b)
  301. {
  302.     return bcd(real(a) / real(b));
  303. }
  304.  
  305. inline bcd _RTLENTRY operator/(long double a, bcd _FAR &b)
  306. {
  307.     return bcd(a / real(b));
  308. }
  309.  
  310. inline bcd _RTLENTRY operator/(bcd _FAR &a, long double b)
  311. {
  312.     return bcd(real(a) / 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 int _RTLENTRY operator>(bcd _FAR &a, bcd _FAR &b)
  336. {
  337.     return real(a) > real(b);
  338. }
  339.  
  340. inline int _RTLENTRY operator<(bcd _FAR &a, bcd _FAR &b)
  341. {
  342.     return real(a) < real(b);
  343. }
  344.  
  345. inline bcd _RTLENTRY bcd::operator+()
  346. {
  347.     return *this;
  348. }
  349.  
  350. inline bcd _RTLENTRY bcd::operator-()
  351. {
  352. //  return bcd(-real(this));
  353.  
  354. // 1's comp
  355.     mantissa[0] = - ++ mantissa[0];
  356.     mantissa[1] = - ++ mantissa[1];
  357. // inc
  358.     if (++mantissa[0] == 0) ++mantissa[1];
  359.     return *this;
  360. }
  361.  
  362. inline bcd _RTLENTRY abs(bcd& a)   { return bcd(fabs(real(a)));}
  363. inline bcd _RTLENTRY acos(bcd& a)  { return bcd(acos(real(a)));}
  364. inline bcd _RTLENTRY asin(bcd& a)  { return bcd(asin(real(a)));}
  365. inline bcd _RTLENTRY atan(bcd& a)  { return bcd(atan(real(a)));}
  366. inline bcd _RTLENTRY cos(bcd& a)   { return bcd(cos(real(a)));}
  367. inline bcd _RTLENTRY cosh(bcd& a)  { return bcd(cosh(real(a)));}
  368. inline bcd _RTLENTRY exp(bcd& a)   { return bcd(exp(real(a)));}
  369. inline bcd _RTLENTRY log(bcd& a)   { return bcd(log(real(a)));}
  370. inline bcd _RTLENTRY log10(bcd& a) { return bcd(__bcd_log10(&a));}
  371. inline bcd _RTLENTRY sin(bcd& a)   { return bcd(sin(real(a)));}
  372. inline bcd _RTLENTRY sinh(bcd& a)  { return bcd(sinh(real(a)));}
  373. inline bcd _RTLENTRY sqrt(bcd& a)  { return bcd(sqrt(real(a)));}
  374. inline bcd _RTLENTRY tan(bcd& a)   { return bcd(tan(real(a)));}
  375. inline bcd _RTLENTRY tanh(bcd& a)  { return bcd(tanh(real(a)));}
  376.  
  377. inline bcd _RTLENTRY pow(bcd& a, bcd& b)   { return bcd(pow(real(a),real(b)));}
  378. inline void _RTLENTRY pow10(int n, bcd& a) { __bcd_pow10(n,&a);}
  379.  
  380. // bcd stream I/O
  381.  
  382. #if defined(__FLAT__)
  383. ostream _FAR & _RTLENTRY _EXPFUNC operator<<(ostream _FAR &, bcd _FAR &);
  384. istream _FAR & _RTLENTRY _EXPFUNC operator>>(istream _FAR &, bcd _FAR &);
  385. #else
  386. ostream _FAR & __pascal _EXPFUNC operator<<(ostream _FAR &, bcd _FAR &);
  387. istream _FAR & __pascal _EXPFUNC operator>>(istream _FAR &, bcd _FAR &);
  388. #endif
  389.  
  390.  
  391. #if !defined(RC_INVOKED)
  392.  
  393. #if defined(__STDC__)
  394. #pragma warn .nak
  395. #endif
  396.  
  397. #pragma option -Vo.
  398.  
  399. #if defined(__BCOPT__)
  400. #if !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
  401. #pragma option -po.     // restore Object data calling convention
  402. #endif
  403. #endif
  404.  
  405. #pragma option -a.
  406.  
  407. #endif  /* !RC_INVOKED */
  408.  
  409.  
  410. #endif  // __BCD_H
  411.  
  412.  
  413.