Quake edits
This commit is contained in:
parent
3e3dbdbef5
commit
1e14f793bc
@ -6,10 +6,10 @@ In base 10, we interpret place value as follows:
|
||||
\begin{itemize}
|
||||
\item $0.1 = 10^{-1}$
|
||||
\item $0.03 = 3 \ \times 10^{-2}$
|
||||
\item $0.0008 = 8 \times 10^{-4}$
|
||||
\item $0.0208 = 2 \times 10^{-2} + 8 \times 10^{-4}$
|
||||
\end{itemize}
|
||||
|
||||
\footnotetext{this is a misnomer, but that's ok.}
|
||||
\footnotetext{\say{decimal} is a misnomer, but that's ok.}
|
||||
|
||||
\vspace{5mm}
|
||||
|
||||
@ -24,7 +24,7 @@ We can do the same in base 2:
|
||||
|
||||
\problem{}
|
||||
Rewrite the following binary decimals in base 10: \par
|
||||
\note{You may leave your answer as a fraction}
|
||||
\note{You may leave your answer as a fraction.}
|
||||
\begin{itemize}
|
||||
\item $\texttt{1011.101}$
|
||||
\item $\texttt{110.1101}$
|
||||
|
@ -1,4 +1,4 @@
|
||||
\section{Approximation}
|
||||
\section{Integers and Floats}
|
||||
|
||||
\generic{Observation:}
|
||||
For small values of $a$, $\log_2(1 + a)$ is approximately equal to $a$. \par
|
||||
@ -10,16 +10,23 @@ We'll add a \say{correction term} $\varepsilon$ to this approximation, so that $
|
||||
|
||||
TODO: why? Graphs.
|
||||
|
||||
\problem{}
|
||||
\problem{}<convert>
|
||||
Use the fact that $\log_2(1 + a) \approx a + \varepsilon$ to approximate $\log_2(x_f)$ in terms of $x_i$. \par
|
||||
|
||||
\vspace{5mm}
|
||||
|
||||
Namely, show that
|
||||
\begin{equation*}
|
||||
\log_2(x_f) ~=~ \frac{1}{2^{23}}(x_i) - 127 + \varepsilon
|
||||
\log_2(x_f) ~=~ \frac{x_i}{2^{23}} - 127 + \varepsilon
|
||||
\end{equation*}
|
||||
for some error term $\varepsilon$
|
||||
for some correction term term $\varepsilon$
|
||||
|
||||
\vspace{5mm}
|
||||
|
||||
Before you start, make sure you understand what we're trying to do: \par
|
||||
We're finding an expression for $\log_2(x_f)$ in terms of $x_i$ and an correction term $\varepsilon$. \par
|
||||
That is, we're finding a closed-form expression that connects the two interpretations \par
|
||||
(\texttt{float} and \texttt{int}) of the bit string $x$.
|
||||
|
||||
|
||||
\begin{solution}
|
||||
|
@ -32,7 +32,11 @@ If we rewrite this using notation we're familiar with, we get the following:
|
||||
\vspace{2mm}
|
||||
|
||||
Our goal in this section is to understand why this works: \par
|
||||
How does Quake approximate $\frac{1}{\sqrt{x}}$ by simply subtracting and dividing by two??
|
||||
\begin{itemize}
|
||||
\item How does Quake approximate $\frac{1}{\sqrt{x}}$ by simply subtracting and dividing by two?
|
||||
\item What's special about $6240089$?
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\problem{}
|
||||
Using basic log rules, rewrite $\log_2(1 / \sqrt{x})$ in terms of $\log_2(x)$.
|
||||
@ -45,39 +49,84 @@ Using basic log rules, rewrite $\log_2(1 / \sqrt{x})$ in terms of $\log_2(x)$.
|
||||
|
||||
|
||||
\vfill
|
||||
\pagebreak
|
||||
|
||||
\generic{Setup:}
|
||||
We are now ready to show that $\texttt{Q\_sqrt} \approx \frac{1}{\sqrt{n_f}}$. \par
|
||||
For convenience, let's call the bit string of the inverse square root $r$. \par
|
||||
In other words,
|
||||
\begin{equation*}
|
||||
r_f := \frac{1}{\sqrt{n_f}}
|
||||
\end{equation*}
|
||||
This is the value we want to approximate.
|
||||
|
||||
\problem{}
|
||||
Find the exact value of $\kappa$ in terms of $\varepsilon$. \par
|
||||
\note{Remember, $\varepsilon$ is the correction term in the approximation $\log_2(1 + a) = a + \varepsilon$.}
|
||||
\problem{}<finala>
|
||||
Find an approximation for $\log_2(r_f)$ in terms of $n_i$ and $\varepsilon$ \par
|
||||
\note{Remember, $\varepsilon$ is the correction constant in our approximation of $\log_2(1 + a)$.}
|
||||
|
||||
\begin{solution}
|
||||
Say $g_f = \frac{1}{\sqrt{n_f}}$ (i.e, $g_f$ is the value we want to compute). We then have:
|
||||
\begin{align*}
|
||||
\log_2(g_f)
|
||||
&=\log_2(\frac{1}{\sqrt{n_f}}) \\
|
||||
&=\frac{-1}{2}\log_2(n_f) \\
|
||||
&=\frac{-1}{2}\left( \frac{n_i}{2^{23}} + \varepsilon - 127 \right)
|
||||
\end{align*}
|
||||
But we also know that
|
||||
\begin{align*}
|
||||
\log_2(g_f)
|
||||
&=\frac{g_i}{2^{23}} + \varepsilon - 127
|
||||
\end{align*}
|
||||
So,
|
||||
\begin{align*}
|
||||
\frac{g_i}{2^{23}} + \varepsilon - 127
|
||||
&=\frac{-1}{2}\left( \frac{n_i}{2^{23}} + \varepsilon - 127 \right) \\
|
||||
\frac{g_i}{2^{23}}
|
||||
&=\frac{-1}{2}\left( \frac{n_i}{2^{23}} \right) + \frac{3}{2}(\varepsilon - 127) \\
|
||||
g_i
|
||||
&=\frac{-1}{2}\left(n_i \right) + 2^{23}\frac{3}{2}(\varepsilon - 127) \\
|
||||
&=2^{23}\frac{3}{2}(\varepsilon - 127) - \frac{n_i}{2}
|
||||
\end{align*}
|
||||
|
||||
and thus $\kappa = 2^{23}\frac{3}{2}(\varepsilon - 127)$.
|
||||
\begin{equation*}
|
||||
\log_2(r_f)
|
||||
~=~ \log_2(\frac{1}{\sqrt{n_f}})
|
||||
~=~ \frac{-1}{2}\log_2(n_f)
|
||||
~\approx~ \frac{-1}{2}\left( \frac{n_i}{2^{23}} + \varepsilon - 127 \right)
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}<finalb>
|
||||
Let's call the \say{magic number} in the code above $\kappa$, so that
|
||||
\begin{equation*}
|
||||
\texttt{Q\_sqrt}(n_f) = \kappa - (n_i \div 2)
|
||||
\end{equation*}
|
||||
Use problems \ref{num:convert} and \ref{num:finala} to show that $\texttt{Q\_sqrt}(n_f) \approx r_i$. \par
|
||||
|
||||
\begin{solution}
|
||||
From \ref{convert}, we know that
|
||||
\begin{equation*}
|
||||
\log_2(r_f) \approx \frac{r_i}{2^{23}} + \varepsilon - 127
|
||||
\end{equation*}
|
||||
|
||||
\note{
|
||||
Our approximation of $\log_2(1+a)$ uses a fixed correction constant, \par
|
||||
so the $\varepsilon$ here is equivalent to the $\varepsilon$ in \ref{finala}.
|
||||
}
|
||||
|
||||
Combining this with the result from \ref{finala}, we get:
|
||||
\begin{align*}
|
||||
\frac{r_i}{2^{23}} + \varepsilon - 127
|
||||
&~\approx~\frac{-1}{2}\left( \frac{n_i}{2^{23}} + \varepsilon - 127 \right) \\
|
||||
\frac{r_i}{2^{23}}
|
||||
&~\approx~\frac{-1}{2}\left( \frac{n_i}{2^{23}} \right) + \frac{3}{2}(\varepsilon - 127) \\
|
||||
r_i
|
||||
&~\approx~\frac{-1}{2}\left(n_i \right) + 2^{23}\frac{3}{2}(\varepsilon - 127)
|
||||
~=~ 2^{23}\frac{3}{2}(\varepsilon - 127) - \frac{n_i}{2}
|
||||
\end{align*}
|
||||
|
||||
\vspace{2mm}
|
||||
|
||||
This is exactly what we need! If we set $\kappa$ to $\frac{2^{24}}{3}(\varepsilon - 127)$, then
|
||||
\begin{equation*}
|
||||
r_i ~\approx~ \kappa - (n_i \div 2) ~=~ \texttt{Q\_sqrt}(n_f)
|
||||
\end{equation*}
|
||||
\end{solution}
|
||||
|
||||
\vfill
|
||||
|
||||
\problem{}
|
||||
What is the exact value of $\kappa$ in terms of $\varepsilon$? \par
|
||||
\hint{Look at \ref{finalb}. We already found it!}
|
||||
|
||||
\begin{solution}
|
||||
This problem makes sure our students see that
|
||||
$\kappa = \frac{2^{24}}{3}(\varepsilon - 127)$. \par
|
||||
See the soluton to \ref{finalb}.
|
||||
\end{solution}
|
||||
|
||||
\makeatletter
|
||||
\if@solutions\else
|
||||
\vspace{2cm}
|
||||
\fi\makeatother
|
||||
|
||||
\pagebreak
|
Loading…
x
Reference in New Issue
Block a user