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

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  real.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 "real.w"
  12.    by CTANGLE (Version 3.1 [km2c]) */
  13. #ifndef LEDA_REAL_H
  14. #define LEDA_REAL_H
  15. /*{Manpage {real} {} {Real Numbers} }*/
  16. #define DEBUG_REAL
  17. /*{Mdefinition
  18. An instance $x$ of the data type $real$ is an algebraic real number.
  19. There are many ways to construct a $real$:
  20. either by conversion from $double$, $integer$ or $rational$
  21. or by applying one of the arithmetic operators $+, -, *, /$ or $sqrt{ } $ to
  22. $real$ numbers.
  23. One may test the sign of a $real$ number or compare two $real$ numbers by
  24. any of the comparison relations $=, neq, < , leq, >$ and $geq$.
  25. The outcome of such a test is {em exact}.
  26. There is also a non--standard version of the sign function: the call
  27. $x.sign(integer q)$ computes the sign of $x$ under the precondition
  28. that $|x|leq 2^{-q}$ implies $x = 0$.
  29. This version of the sign function allows the user to assist the data type in the
  30. computation of the sign of $x$, cf. the example below.
  31. One can ask for $double$ approximations of a real number $x$.
  32. The calls $x.todouble()$ and $x.get_double_error()$ return $double$s $xnum$ and
  33. $xerr$ such that $|xnum - x |leq  |xnum |* xerr$.
  34. Note that $xerr = infty$ is possible.
  35. There are also several functions to compute more accurate approximations of
  36. $real$s.
  37. The call $x.get_precision()$ returns an $integer$ $xerr$ such that the internal
  38. approximation $x_num$ satisfies $|xnum - x |leq 2^{-xerr}$.
  39. The user may set a bound on $xerr$.
  40. More precisely, after the call $x.improve(integer q)$ the data type guarantees
  41. $xerr leq 2^{-q}$.
  42. }*/
  43. #include <LEDA/bigfloat.h>
  44. /* this is only a preliminary version of bigfloats */
  45. /* include "/KM/usr/burnikel/cweb/bigfloat/bigint.h" */
  46. #include <LEDA/rational.h>
  47. typedef rational bigrational;
  48. /* we use the name $bigrational$ instead of the LEDA name */
  49. typedef integer bigint;
  50. /* we use the name $bigint$ instead of the LEDA name */
  51. #include <LEDA/basic.h>
  52. extern const bigfloat zero;
  53. class real_rep;
  54. class real
  55. {
  56. real_rep*PTR;
  57. GenPtr copy() const;
  58. void   clear();
  59. public:
  60. /*{Mcreation x
  61. }*/
  62. real();
  63. /*{Mcreate
  64. introduces a variable var of type $real$ and initializes it to zero.
  65. }*/
  66. real(double y);
  67. /*{Mcreate
  68. introduces a variable var of type $real$ and initializes it to the 
  69. value of $y$.
  70. }*/
  71. real(int n);
  72. /*{Mcreate
  73. introduces a variable var of type $real$ and initializes it to the 
  74. value of $n$.
  75. }*/
  76. real(const integer& a);
  77. /*{Mcreate
  78. introduces a variable var of type $real$ and initializes it to the 
  79. value of $a$.
  80. }*/
  81. real(const bigfloat&);
  82. real(const rational& q);
  83. /*{Mcreate
  84. introduces a variable var of type $real$ and initializes it to the 
  85. value of $q$
  86. }*/
  87. real(const real&);
  88. real(real_rep&);
  89. ~real();
  90. real&operator= (const real&);
  91. /*{Moperations 1.5 5
  92. }*/
  93. /*{Mtext
  94. The arithmetic operations $+, -, *, /, +=,
  95. -=, *=, /=, -$(unary), the comparison operations $<, <=, >, 
  96. >=, ==, !=$ and the stream operations all are available.
  97. }*/
  98. friend real operator+(const real&,const real&);
  99. /*{Xbinopfunc addition
  100. }*/
  101. friend real operator-(const real&,const real&);
  102. /*{Xbinopfunc subtraction
  103. }*/
  104. friend real operator*(const real&,const real&);
  105. /*{Xbinopfunc multiplication
  106. }*/
  107. friend real operator/(const real&,const real&);
  108. /*{Xbinopfunc division
  109. }*/
  110. friend real operator-(const real&);
  111. /*{Xunop negation
  112. }*/
  113. friend real sqrt(const real&);
  114. /*{Mop squareroot operation.
  115. }*/
  116. friend real& operator+= (real&,const real&);
  117. /*{Xbinopfunc $var = var + y$
  118. }*/
  119. friend real& operator-= (real&,const real&);
  120. /*{Xbinopfunc $var = var - y$
  121. }*/
  122. friend real& operator*= (real&,const real&);
  123. /*{Xbinopfunc $var = var * y$
  124. }*/
  125. friend int operator<(const real&,const real&);
  126. /*{Xbinopfunc returns true if var is smaller than $y$
  127. }*/
  128. friend int operator<=(const real&,const real&);
  129. /*{Xbinopfunc returns true if var is smaller than or equal to $y$
  130. }*/
  131. friend int operator>(const real&,const real&);
  132. /*{Xbinopfunc returns true if var is greater than $y$
  133. }*/
  134. friend int operator>=(const real&,const real&);
  135. /*{Xbinopfunc returns true if var is greater than or equal to $y$
  136. }*/
  137. friend int operator==(const real&,const real&);
  138. /*{Xbinopfunc returns true if var is equal to $y$
  139. }*/
  140. friend int operator!=(const real&,const real&);
  141. /*{Xbinopfunc returns true if var is not equal to $y$
  142. }*/
  143. int sign();
  144. /*{Mop returns $-1$ if (the exact value of) var $< 0$, $1$ if var
  145.    $> 0$, and $0$ if var $= 0$.
  146. }*/
  147. int sign(const integer& a);
  148. /*{Mop as above. Precondition: if $|mbox{var} |leq  2^{-a}$
  149.    then $mbox{var} = 0$.
  150. }*/
  151. int sign(long);
  152. void improve(const integer& a);
  153. /*{Mop (re-)computes the approximation of var such that its final quality
  154.     is bounded by $a$, i.e., var.$get_precision() geq a$ after the
  155.     call var.$improve(a)$.
  156. }*/
  157. void improve(long);
  158. void compute_in(long k);
  159. /*{Mop (re-)computes the approximation of var; each numerical operation
  160.    is carried out with $k$ binary places.
  161. }*/
  162. void compute_up_to(long k);
  163. /*{Mop (re-)computes an approximation of var such that the error of the
  164.     approximation lies in the $k$-th binary place.
  165. }*/
  166. double todouble()const;
  167. /*{Mop returns the current double approximation of var.
  168. }*/
  169. bigfloat tobigfloat()const;
  170. double get_double_error()const;
  171. /*{Mop returns the quality of the current double approximation of var,
  172.     i.e., $|x- x.todouble() |leq
  173.     x.get_double_error() *  |x.todouble() |$.
  174. }*/
  175. integer get_precision()const;
  176. /*{Mop returns the quality of the current internal approximation $var.num$
  177.    of var, i.e., $|x - x.num |leq 2^{-x.get_precision()}$.\
  178. }*/
  179. friend ostream& operator<<(ostream& O, const real& x);
  180. /*{Xbinopfunc writes the best known approximation of the $real$
  181.     number $x$ to the output stream $O$.\
  182. }*/
  183. friend istream& operator>>(istream& I,real& x);
  184. /*{Xbinopfunc reads $real$ number $x$ from the input stream $I$
  185.    (in $double$ format).
  186. }*/
  187. #if !defined(__TEMPLATE_FUNCTIONS__)
  188. friend GenPtr  Create(const real*) { real x; return x.copy(); }
  189. friend void    Clear(real& y)      { y.clear();}
  190. friend GenPtr  Convert(real& y)    { return y.PTR; }
  191. friend GenPtr  Copy(const real& y) { return y.copy();}
  192. friend char*   Type_Name(const real*) { return "real"; }
  193. #endif
  194. };
  195. /*{Mtext
  196. smallskip
  197. {bf Non-member functions}
  198. smallskip }*/
  199. real fabs(real& x);
  200. /*{Mfunc absolute value of x.
  201. }*/
  202. real sq(const real& x);
  203. /*{Mfunc square of x.
  204. }*/
  205. real hypot(const real& x, const real& y);
  206. /*{Mfunc euclidean distance of vector (x,y) to the origin.
  207. }*/
  208. real powi(const real& x,int n);
  209. /*{Mfunc n-th power of x.
  210. }*/
  211. //  Functions necessary to use real as a type parameter in parameterized
  212. //  data types.
  213. inline void Print(const real& a, ostream & out) { out << a; }
  214. inline void Read(real& a, istream & in) { in >> a; }
  215. inline int compare(const real& x, const real& y)
  216. { return (x < y) ? -1 : ((x > y) ? 1 : 0); }
  217. /*{Mimplementation
  218. A name is represented by the expression which defines it and a $double$
  219. approximation $hat{x}$ together with a relative error bound
  220. $epsilon_{x}$.
  221. The arithmetic operators $+, -, *, /, sqrt{ }$ take constant time.
  222. When the sign of a name number needs to be determined, the data type first
  223. computes a number $q$, if not already given as an argument to $sign$, such that
  224. $|mbox{var} |leq  q$ implies $x = 0$.
  225. The bound $q$ is computed as described in cite{Mignotte:Buch}.
  226. The data type then computes an internal approximation $xnum$ for var with
  227. error bound $xerr leq q$.
  228. The sign of $xnum$ is then returned as the sign of var.
  229. Two shortcuts are used to speed up the computation of the sign.
  230. Firstly, if the $double$ approximation already suffices to determine the sign,
  231. then no further approximation is computed at all.
  232. Secondly, the internal approximation is first computed only with small
  233. precision.
  234. The precision is then doubled until either the sign can be decided (i.e.,
  235. if $xerr < |xnum |$) or the full precision $q$ is reached.\
  236. }*/
  237. /*{Mexample
  238. Examples can be found in  cite{BMS:ESA}.
  239. }*/
  240. #endif