File: rationalapprox.tex

package info (click to toggle)
sollya 8.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,540 kB
  • sloc: ansic: 124,655; yacc: 7,543; lex: 2,440; makefile: 888; cpp: 77
file content (63 lines) | stat: -rw-r--r-- 2,919 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
\subsection{rationalapprox}
\label{labrationalapprox}
\noindent Name: \textbf{rationalapprox}\\
\phantom{aaa}returns a fraction close to a given number.\\[0.2cm]
\noindent Library name:\\
\verb|   sollya_obj_t sollya_lib_rationalapprox(sollya_obj_t, sollya_obj_t)|\\[0.2cm]
\noindent Usage: 
\begin{center}
\textbf{rationalapprox}(\emph{x},\emph{n}) : (\textsf{constant}, \textsf{integer}) $\rightarrow$ \textsf{function}\\
\end{center}
Parameters: 
\begin{itemize}
\item \emph{x} is a number to approximate.
\item \emph{n} is a integer (representing a format).
\end{itemize}
\noindent Description: \begin{itemize}

\item \textbf{rationalapprox}(\emph{x},\emph{n}) returns a constant function of the form $a/b$ where $a$ and $b$ are
   integers. The value $a/b$ is an approximation of \emph{x}. The quality of this 
   approximation is determined by the parameter \emph{n} that indicates the number of
   correct bits that $a/b$ should have.

\item The command is not safe in the sense that it is not ensured that the error 
   between $a/b$ and \emph{x} is less than $2^{-n}$.

\item The following algorithm is used: \emph{x} is first rounded downwards and upwards to
   a format of \emph{n} bits, thus obtaining an interval $[x_l,\,x_u]$. This interval is then
   developed into a continued fraction as far as the representation is the same
   for every elements of $[x_l,\,x_u]$. The corresponding fraction is returned.

\item Since rational numbers are not a primitive object of \sollya, the fraction is
   returned as a constant function. This can be quite amazing, because \sollya
   immediately simplifies a constant function by evaluating it when the constant
   has to be displayed.
   To avoid this, you can use \textbf{print} (that displays the expression representing
   the constant and not the constant itself) or the commands \textbf{numerator} 
   and \textbf{denominator}.
\end{itemize}
\noindent Example 1: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> pi10 = rationalapprox(Pi,10);
> pi50 = rationalapprox(Pi,50);
> pi100 = rationalapprox(Pi,100);
> print( pi10, ": ", dirtysimplify(floor(-log2(abs(pi10-Pi)/Pi))), "bits." );
3.140625 :  11 bits.
> print( pi50, ": ", dirtysimplify(floor(-log2(abs(pi50-Pi)/Pi))), "bits." );
85563208 / 27235615 :  51 bits.
> print( pi100, ": ", dirtysimplify(floor(-log2(abs(pi100-Pi)/Pi))), "bits." );
4422001152019829 / 1407566683404023 :  100 bits.
\end{Verbatim}
\end{minipage}\end{center}
\noindent Example 2: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> a=0.1;
> b=rationalapprox(a,4);
> numerator(b); denominator(b);
1
10
> print(dirtysimplify(floor(-log2(abs((b-a)/a)))), "bits.");
166 bits.
\end{Verbatim}
\end{minipage}\end{center}
See also: \textbf{print} (\ref{labprint}), \textbf{numerator} (\ref{labnumerator}), \textbf{denominator} (\ref{labdenominator}), \textbf{rationalmode} (\ref{labrationalmode})