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

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