bigfloat.h
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 9k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  bigfloat.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. /* This file has been automatically generated from "bigfloat.w"
  12.    by CTANGLE (Version 3.1 [km2c]) */
  13. /*{manual\
  14. declare |bigfloat|
  15. }*/
  16. #ifndef BIGFLOAT_H
  17. #define BIGFLOAT_H
  18. #include <LEDA/integer.h>
  19. /* include "/KM/usr/burnikel/cweb/bigfloat/integer.h" */
  20. /* we use the LEDA data type integer to for th representaion of the bigfloats */
  21. enum rounding_modes {
  22.   TO_NEAREST, TO_ZERO, TO_P_INF, TO_N_INF, TO_INF, EXACT, DEFAULT
  23. };
  24. class bigfloat {
  25.   integer significant, exponent;
  26.   long precision;
  27.  public:
  28. /*{manual\
  29. creation
  30. a) create x  (|double| d)
  31. b) create x  (|integer| I)
  32. c) create x  (|long| I)
  33. d) create x  (|int| I)
  34. e) create x  (|integer| significant,|integer| exponent)
  35. f) create x  ()
  36. introduces a variable var of type name and initializes it to a given
  37. value (zero in variant e).
  38. }*/
  39.    bigfloat(const integer &, const integer &);
  40.    bigfloat(double = 0);
  41.    bigfloat(const integer &);
  42.    bigfloat(long);
  43.    bigfloat(int);
  44.    bigfloat(const bigfloat &);
  45.    ~bigfloat(void) {
  46.   }
  47.    bigfloat & operator = (const bigfloat &);
  48. /*{manual\
  49. operations 2 4.5
  50. }*/
  51.   double todouble(void) const;
  52. /*{manual\
  53. op double  todouble { }
  54.       {converts the bigfloat into a double value and returns it }
  55. }*/
  56.   integer tointeger(rounding_modes = TO_ZERO) const;
  57. /*{manual\
  58. op double  tointeger {rmode}
  59.       {converts the bigfloat into a integer value and returns it. If rmode
  60.        is ommitted tointeger uses default round mode. }
  61. }*/
  62.   void set_precision(long, rounding_modes = DEFAULT);
  63. /*{manual\
  64. op void set_precision {prec}
  65.       {sets the internal significant's precision to prec
  66.        and rounds the significant to that value if the old
  67.        precision was bigger else it shifts it to the wanted length}
  68. }*/
  69.   long get_precision(void);
  70. /*{manual\
  71. op long get_precision { }
  72.       {returns the current precision of the bigfloat }
  73. }*/
  74.   bigfloat round(long = 0, rounding_modes = DEFAULT);
  75. /*{manual\
  76. op |bigfloat| round {len,rmode}
  77.       {rounds the bigfloat to length len and uses a specific
  78.        round mode rmode or the default one. round returns
  79.        the rounded bigfloat}
  80. }*/
  81.   integer get_exponent() const {
  82.     return exponent;
  83.   }
  84. /*{manual\
  85. op |integer| get_exponent { }
  86.       {returns the exponent}
  87. }*/
  88.   integer get_significant() const {
  89.     return significant;
  90.   }
  91. /*{manual\
  92. op |integer| get_significant { }
  93.       {returns the significant}
  94. }*/
  95.   friend bigfloat add(const bigfloat &, const bigfloat &, long = -1, rounding_modes = DEFAULT);
  96. /*{manual\
  97. func |bigfloat| add {x,y,prec,rmode}
  98.       {returns $x+y$ computed to the precision prec and rounded in
  99.        mode rmode}
  100. }*/
  101.   friend bigfloat sub(const bigfloat &, const bigfloat &, long = -1, rounding_modes = DEFAULT);
  102. /*{manual\
  103. func |bigfloat| sub {x,y,prec,rmode}
  104.       {returns $x-y$ computed to the precision prec and rounded in
  105.        mode rmode}
  106. }*/
  107.   friend bigfloat mul(const bigfloat &, const bigfloat &, long = -1, rounding_modes = DEFAULT);
  108. /*{manual\
  109. func |bigfloat| mul {x,y,prec,rmode}
  110.       {returns $x*y$ computed to the precision prec and rounded in
  111.        mode rmode}
  112. }*/
  113.   friend bigfloat div(const bigfloat &, const bigfloat &, long = -1, rounding_modes = DEFAULT);
  114. /*{manual\
  115. func |bigfloat| div {x,y,prec,rmode}
  116.       {returns $x/y$ computed to the precision prec and rounded in
  117.        mode rmode}
  118. }*/
  119.   friend bigfloat sqrt(const bigfloat &, long = -1, rounding_modes = DEFAULT);
  120. /*{manual\
  121. func |bigfloat| sqrt {x,prec,rmode}
  122.       {returns $sqrt{x}$ computed to the precision prec and rounded in
  123.        mode rmode}
  124. }*/
  125.   friend bigfloat operator + (const bigfloat &, const bigfloat &);
  126. /*{manual\
  127. binop |bigfloat| + {y}
  128.       { returns add(var,y) and uses so default values for precision and
  129.         round mode}
  130. }*/
  131.   friend bigfloat operator - (const bigfloat &, const bigfloat &);
  132. /*{manual\
  133. binop |bigfloat| - {y}
  134.       { returns sub(var,y) }
  135. }*/
  136.   friend bigfloat operator *(const bigfloat &, const bigfloat &);
  137. /*{manual\
  138. binop |bigfloat| * {y}
  139.       { returns mul(var,y) }
  140. }*/
  141.   friend bigfloat operator / (const bigfloat &, const bigfloat &);
  142. /*{manual\
  143. binop |bigfloat| / {y}
  144.       { returns div(var,y) }
  145. }*/
  146.   friend bigfloat operator - (const bigfloat &);
  147. /*{manual\
  148. unop |bigfloat| -
  149.       { returns $-x$ }
  150. }*/
  151.   friend bool operator == (const bigfloat &, const bigfloat &);
  152. /*{manual\
  153. binop bool == {y}
  154.       { returns true if var is equal to y }
  155. }*/
  156.   friend bool operator != (const bigfloat &, const bigfloat &);
  157. /*{manual\
  158. binop bool != {y}
  159.       { returns true if var is not equal to y }
  160. }*/
  161.   friend bool operator > (const bigfloat &, const bigfloat &);
  162. /*{manual\
  163. binop bool > {y}
  164.       { returns true if var is greater than y }
  165. }*/
  166.   friend bool operator >= (const bigfloat &, const bigfloat &);
  167. /*{manual\
  168. binop bool >= {y}
  169.       { returns true if var is greater than or equal to y }
  170. }*/
  171.   friend bool operator < (const bigfloat &, const bigfloat &);
  172. /*{manual\
  173. binop bool < {y}
  174.       { returns true if var is smaller than y }
  175. }*/
  176.   friend bool operator <= (const bigfloat &, const bigfloat &);
  177. /*{manual\
  178. binop bool <= {y}
  179.       { returns true if var is smaller than or equal to y }
  180. }*/
  181.   friend bool isNaN(const bigfloat &);
  182. /*{manual\
  183. func bool isNaN {x}
  184.       { returns true if x is a NaN}
  185. }*/
  186.   friend bool isnInf(const bigfloat &);
  187. /*{manual\
  188. func bool isnInf {x}
  189.       { returns true if x is $-infty$}
  190. }*/
  191.   friend bool ispInf(const bigfloat &);
  192. /*{manual\
  193. func bool ispInf {x}
  194.       { returns true if x is $+infty$}
  195. }*/
  196.   friend bool isnZero(const bigfloat &);
  197. /*{manual\
  198. func bool isnZero {x}
  199.       { returns true if x is $-0$}
  200. }*/
  201.   friend bool ispZero(const bigfloat &);
  202. /*{manual\
  203. func bool ispZero {x}
  204.       { returns true if x is $+0$}
  205. }*/
  206.   friend bool isZero(const bigfloat &);
  207. /*{manual\
  208. func bool isZero {x}
  209.       { returns true if x is $pm 0$}
  210. }*/
  211.   friend bool isInf(const bigfloat &);
  212. /*{manual\
  213. func bool isInf {x}
  214.       { returns true if x is $pm infty$}
  215. }*/
  216. /*{manual\
  217. implementation
  218. A type is a floating point number of arbitrary precision.type s are
  219. represented by a significant and an exponent which are |integers|.
  220. They are defined on addition, subtraction, multiplication, division
  221. and square root. Furthermore the user is given some comparison functions on
  222. type s. The user is also provided with a couple of mathematical functions.
  223. In addition to that the user is given a rounding function that performs
  224. an exact rounding in different modes. The rounding modes conform to IEEE
  225. standard.
  226. }*/
  227.   friend integer ceil(const bigfloat & b);
  228. /*{manual\
  229. func |integer| ceil { }
  230.       { rounds var up to the next integer value}
  231. }*/
  232.   friend integer floor(const bigfloat & b);
  233. /*{manual\
  234. func |integer| floor { }
  235.       { rounds var down to the next integer value}
  236. }*/
  237.   friend bigfloat pow2(const integer &);
  238. /*{manual\
  239. func |bigfloat| pow2 { y }
  240.       { returns $2^y$}
  241. }*/
  242.   friend long sign(const bigfloat &);
  243. /*{manual\
  244. func long sign { }
  245.       { if var is negative sign return -1, if it is zero it returns zero else
  246.        it returns 1}
  247. }*/
  248.   friend long non_zero_sign(const bigfloat &);
  249. /*{manual\
  250. func long non_zero_sign { y }
  251.       { returns a sign either 1 or -1 as values. This function is needed
  252.         for special cases in which a signed zero can appear.}
  253. }*/
  254.   friend bigfloat fabs(const bigfloat &);
  255. /*{manual\
  256. func |bigfloat| fabs { }
  257.       { returns $|var|$}
  258. }*/
  259.   friend bigfloat outofchar(char *);
  260. /*{manual\
  261. func |bigfloat| outofchar { string }
  262.       { this routine converts a string of the format $pm 0.bbldots bbEex$ or
  263.         $pm d.dddldots$, where $bin [0,1]$ and $din [0,9]$into |bigfloat|,
  264.         to |bigfloat| format}
  265. }*/
  266.   friend ostream & operator << (ostream &, const bigfloat &);
  267. /*{manual\
  268. strop ostream& << {O}
  269.     { writes |real| number name to the output stream $O$ }
  270. }*/
  271.   friend istream & operator >> (istream &, bigfloat &);
  272. /*{manual\
  273. strop ostream& >> {I}
  274.     { reads |real| number name from the output stream $I$ (in |double|
  275.       format) }
  276. }*/
  277.   void print_contents(void) const;
  278. };
  279. #endif
  280. const long BIN_OUT = 0;
  281. const long DEC_OUT = 1;
  282. const long MAX_PREC = -1;
  283. const long negative = 0;
  284. const long positive = 1;
  285. const long bin_maxlen = 10000;
  286. const bigfloat NaN = bigfloat(0, 1026); /* the representation of NaN is does */
  287. /* not conform to IEEE because there */
  288. /* are no maximum exponents */
  289. const bigfloat pInf = bigfloat(0, 1024);
  290. const bigfloat nInf = bigfloat(0, 1025);
  291. const bigfloat pZero = bigfloat(0, -1023);
  292. const bigfloat nZero = bigfloat(0, -1024);
  293. /* globale Variablen */
  294. extern long global_prec;
  295. /* extern long output; */
  296. extern rounding_modes round_mode;
  297. extern const double Infinity;
  298. /* globale Funktionen */
  299. void set_glob_prec(long);
  300. void set_round_mode(rounding_modes = TO_NEAREST);