Compare commits

..

No commits in common. "ecb70b9ea896c869eb80998259abcc9454e41c96" and "e5d3fbee19d5211cc81c6cdeb350b0d2aded47d5" have entirely different histories.

4 changed files with 44 additions and 162 deletions

View File

@ -4,13 +4,7 @@
// All functions that maybe used by client code are listed here // All functions that maybe used by client code are listed here
#import "misc.typ": * #import "misc.typ": *
#import "object.typ": problem, definition, theorem, example, remark, generic #import "object.typ": problem, definition, theorem, example, remark, generic
#import "solution.typ": ( #import "solution.typ": if_solutions, if_no_solutions, solution
if_solutions,
if_no_solutions,
if_solutions_else,
solution,
instructornote,
)
/// Main handout wrapper. /// Main handout wrapper.

View File

@ -1,4 +1,4 @@
#import "misc.typ": ored, oteal #import "misc.typ": ored
/// If false, hide instructor info. /// If false, hide instructor info.
/// ///
@ -27,10 +27,6 @@
if not show_solutions { content } if not show_solutions { content }
} }
#let if_solutions_else(if_yes, if_no) = {
if show_solutions { if_yes } else { if_no }
}
#let solution(content) = { #let solution(content) = {
if_solutions( if_solutions(
align( align(
@ -58,32 +54,3 @@
), ),
) )
} }
#let instructornote(content) = {
let c = oteal
if_solutions(
align(
center,
stack(
block(
width: 100%,
breakable: false,
fill: c,
stroke: c + 2pt,
inset: 1.5mm,
align(left, text(fill: white, weight: "bold", [Instructor note:])),
),
block(
width: 100%,
height: auto,
breakable: false,
fill: c.lighten(80%).desaturate(10%),
stroke: c + 2pt,
inset: 3mm,
align(left, content),
),
),
),
)
}

View File

@ -5,141 +5,67 @@
= Integers and Floats = Integers and Floats
#generic("Observation:") #generic("Observation:")
For small values of $x$, $log_2(1 + x)$ is approximately equal to $x$. \ For small values of $a$, $log_2(1 + a)$ is approximately equal to $a$. \
Note that this equality is exact for $x = 0$ and $x = 1$, since $log_2(1) = 0$ and $log_2(2) = 1$. Note that this equality is exact for $a = 0$ and $a = 1$, since $log_2(1) = 0$ and $log_2(2) = 1$.
#v(5mm) #v(2mm)
We'll add the _correction term_ $epsilon$ to our approximation: $log_2(1 + a) approx a + epsilon$. \ We'll add a "correction term" $epsilon$ to this approximation, so that $log_2(1 + a) approx a + epsilon$.
This allows us to improve the average error of our linear approximation:
#table( #cetz.canvas({
stroke: none, import cetz.draw: *
align: center,
columns: (1fr, 1fr),
inset: 5mm,
[$log(1+x)$ and $x + 0$]
+ cetz.canvas({
import cetz.draw: *
let f1(x) = calc.log(calc.abs(x + 1), base: 2) let f1(x) = calc.log(calc.abs(x + 1), base: 2)
let f2(x) = x let f2(x) = x
// Set-up a thin axis style // Set-up a thin axis style
set-style(axes: (stroke: .5pt, tick: (stroke: .5pt))) set-style(axes: (stroke: .5pt, tick: (stroke: .5pt)))
plot.plot( plot.plot(
size: (7, 7), size: (8, 8),
x-tick-step: 0.2, x-tick-step: 0.2,
y-tick-step: 0.2, y-tick-step: 0.2,
y-min: 0, y-min: 0,
y-max: 1, y-max: 1,
x-min: 0, x-min: 0,
x-max: 1, x-max: 1,
legend: none, legend: none,
axis-style: "scientific-auto", axis-style: "scientific-auto",
x-label: none,
y-label: none,
{
let domain = (0, 1)
plot.add( {
f1, let domain = (0, 10)
domain: domain,
label: $log(1+x)$,
style: (stroke: ogrape),
)
plot.add( plot.add-fill-between(
f2, f1,
domain: domain, f2,
label: $x$, domain: domain,
style: (stroke: oblue), style: (stroke: none, fill: luma(75%)),
)
},
) )
})
+ [
Max error: 0.086 \
Average error: 0.0573
],
[$log(1+x)$ and $x + 0.045$]
+ cetz.canvas({
import cetz.draw: *
let f1(x) = calc.log(calc.abs(x + 1), base: 2) plot.add(
let f2(x) = x + 0.0450466 f1,
domain: domain,
// Set-up a thin axis style label: $log(1+x)$,
set-style(axes: (stroke: .5pt, tick: (stroke: .5pt))) style: (stroke: black),
plot.plot(
size: (7, 7),
x-tick-step: 0.2,
y-tick-step: 0.2,
y-min: 0,
y-max: 1,
x-min: 0,
x-max: 1,
legend: none,
axis-style: "scientific-auto",
x-label: none,
y-label: none,
{
let domain = (0, 1)
plot.add(
f1,
domain: domain,
label: $log(1+x)$,
style: (stroke: ogrape),
)
plot.add(
f2,
domain: domain,
label: $x$,
style: (stroke: oblue),
)
},
) )
}) plot.add(f2, domain: domain, label: $x$, style: (stroke: black))
+ [ },
Max error: 0.041 \ )
Average error: 0.0254 })
],
)
TODO: why? Graphs.
A suitiable value of $epsilon$ can be found using calculus or with computational trial-and-error. \
We won't bother with this---we'll simply leave the correction term as an opaque constant $epsilon$.
#v(1fr)
#note(
type: "Note",
[
"Average error" above is simply the area of the region between the two graphs:
$
integral_0^1 abs( #v(1mm) log(1+x) - (x+epsilon) #v(1mm))
$
Feel free to ignore this note, it isn't a critical part of this handout.
],
)
#pagebreak()
#problem(label: "convert") #problem(label: "convert")
Use the fact that $log_2(1 + a) approx a + epsilon$ to approximate $log_2(x_f)$ in terms of $x_i$. \ Use the fact that $log_2(1 + a) approx a + epsilon$ to approximate $log_2(x_f)$ in terms of $x_i$. \
#v(5mm)
Namely, show that Namely, show that
$ $
log_2(x_f) = (x_i) / (2^23) - 127 + epsilon log_2(x_f) = (x_i) / (2^23) - 127 + epsilon
$ $
for some correction term term $epsilon$ \
#note([ #note([
In other words, we're finding an expression for $x$ as a float In other words, we're finding an expression for $x$ as a float
in terms of $x$ as an int. in terms of $x$ as an int.

View File

@ -4,9 +4,6 @@
The following code is present in _Quake III Arena_ (1999): The following code is present in _Quake III Arena_ (1999):
#v(5mm)
```c ```c
float Q_rsqrt( float number ) { float Q_rsqrt( float number ) {
long i = * ( long * ) &number; long i = * ( long * ) &number;
@ -15,8 +12,6 @@ float Q_rsqrt( float number ) {
} }
``` ```
#v(5mm)
This code defines a function `Q_rsqrt` that consumes a float `number` and approximates its inverse square root. This code defines a function `Q_rsqrt` that consumes a float `number` and approximates its inverse square root.
If we rewrite this using notation we're familiar with, we get the following: If we rewrite this using notation we're familiar with, we get the following:
$ $
@ -117,7 +112,7 @@ What is the exact value of $kappa$ in terms of $epsilon$? \
#solution[ #solution[
This problem makes sure our students see that This problem makes sure our students see that
$kappa = 2^23 3/2 (127 - epsilon)$. \ $kappa = (2^24)/3(epsilon - 127)$. \
See the solution to @finalb. See the solution to @finalb.
] ]