-
Notifications
You must be signed in to change notification settings - Fork 0
/
megafavnumber-challenge.html
285 lines (234 loc) · 7.79 KB
/
megafavnumber-challenge.html
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>An intrinsic limitation of numbers on computers - #MegaFavNumber</title>
<meta name="author" content="Mythreyi Ramesh"/>
<style type="text/css">
.underline { text-decoration: underline; }
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js/css/reveal.css"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js/css/theme/moon.css" id="theme"/>
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'https://cdn.jsdelivr.net/npm/reveal.js/css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="sec-title-slide">
<h2>An intrinsic limitation of numbers on computers - #MegaFavNumber</h2>
</section>
<aside class="notes">
<p>
My contribution to the #MegaFavNumber playlist.
</p>
</aside>
<section>
<section id="slide-orgfba443d">
<h2 id="orgfba443d">My MegaFavNumber</h2>
<aside class="notes">
<p>
Overall sentiment: we can’t solve finite precision, but we can work around it, and <code>binary64</code> does it well.
</p>
</aside>
<p>
\(9007199254740993\)
</p>
<p>
It is the <i>smallest</i> natural number that cannot be stored in the standard <code>double-precision</code> format.
</p>
</section>
</section>
<section>
<section id="slide-org583aba3">
<h2 id="org583aba3">What does <code>double</code> mean?</h2>
</section>
<section>
<p>
Computers usually store <i>binary</i> numbers.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Decimal</th>
<th scope="col" class="org-left">Binary</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>1</code></td>
<td class="org-left"><code>1</code></td>
</tr>
<tr>
<td class="org-left"><code>2</code></td>
<td class="org-left"><code>10</code></td>
</tr>
<tr>
<td class="org-left"><code>3</code></td>
<td class="org-left"><code>11</code></td>
</tr>
<tr>
<td class="org-left"><code>4</code></td>
<td class="org-left"><code>100</code></td>
</tr>
<tr>
<td class="org-left"><code>8</code></td>
<td class="org-left"><code>1000</code></td>
</tr>
<tr>
<td class="org-left"><code>16</code></td>
<td class="org-left"><code>10000</code></td>
</tr>
<tr>
<td class="org-left"><code>32</code></td>
<td class="org-left"><code>100000</code></td>
</tr>
</tbody>
</table>
</section>
<section>
<p>
The standard format for <code>double</code> is called <code>binary64</code> by IEEE.
</p>
<p>
Floating point numbers are stored as \(m \times 2^n\).
</p>
<ul>
<li>1 bit stores the sign of \(m\)</li>
<li>11 bits store \(n\)</li>
<li>52 bits store \(m\)</li>
</ul>
</section>
<section>
<p>
What happens when \(m\) can’t be stored in 52 bits?
</p>
</section>
</section>
<section>
<section id="slide-org4039dd4">
<h2 id="org4039dd4">Exploring the neighbourhood of \(2^{53}\)</h2>
</section>
<section>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #f8f8f2;">power</span> = <span style="color: #bd93f9; font-weight: bold;">53</span>
<span style="color: #ff79c6;">for</span> n <span style="color: #ff79c6;">in</span> <span style="color: #ffb86c;">range</span>(<span style="color: #bd93f9; font-weight: bold;">2</span>**power-<span style="color: #bd93f9; font-weight: bold;">5</span>,<span style="color: #bd93f9; font-weight: bold;">2</span>**power+<span style="color: #bd93f9; font-weight: bold;">5</span>):
<span style="color: #ff79c6;">print</span>(n,<span style="color: #f1fa8c;">"=="</span>,<span style="color: #ffb86c;">float</span>(n),end=<span style="color: #f1fa8c;">" => "</span>)
<span style="color: #ff79c6;">print</span>(n == <span style="color: #ffb86c;">float</span>(n))
</pre>
</div>
<pre class="example">
9007199254740987 == 9007199254740987.0 => True
9007199254740988 == 9007199254740988.0 => True
9007199254740989 == 9007199254740989.0 => True
9007199254740990 == 9007199254740990.0 => True
9007199254740991 == 9007199254740991.0 => True
9007199254740992 == 9007199254740992.0 => True
9007199254740993 == 9007199254740992.0 => False
9007199254740994 == 9007199254740994.0 => True
9007199254740995 == 9007199254740996.0 => False
9007199254740996 == 9007199254740996.0 => True
</pre>
</section>
<section>
<p>
\(9007199254740993 = 2^{53} + 1 = 1.00000000 \dots 01 \times 2^{53}\)
</p>
<p>
There are \(52\) zeros and the last digit (\(1\)) cannot be stored!
</p>
<p>
<code>float(9007199254740993) = 9007199254740992.0</code>
</p>
</section>
</section>
<section>
<section id="slide-org728b54b">
<h2 id="org728b54b">Takeaways</h2>
<p>
We only have finite precision no matter which format we store numbers in.
</p>
</section>
<section>
<p>
<i>Good thing</i>: numbers are well-represented for most practical purposes!
</p>
</section>
<section>
<p>
<i>Then, there is symbolic computing.</i>
</p>
</section>
</section>
<section>
<section id="slide-org876451f">
<h2 id="org876451f">Food For Thought</h2>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #ff79c6;">print</span>(<span style="color: #bd93f9; font-weight: bold;">0.1</span>+<span style="color: #bd93f9; font-weight: bold;">0.1</span>+<span style="color: #bd93f9; font-weight: bold;">0.1</span> == <span style="color: #bd93f9; font-weight: bold;">0.3</span>)
</pre>
</div>
<pre class="example">
False
</pre>
</section>
<section>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #ff79c6;">print</span>(<span style="color: #bd93f9; font-weight: bold;">0.1</span>+<span style="color: #bd93f9; font-weight: bold;">0.1</span>+<span style="color: #bd93f9; font-weight: bold;">0.1</span>)
<span style="color: #ff79c6;">print</span>(<span style="color: #bd93f9; font-weight: bold;">0.3</span>)
</pre>
</div>
<pre class="example">
0.30000000000000004
0.3
</pre>
<p>
How does finite precision explain this?
</p>
</section>
</section>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: false,
center: true,
slideNumber: 'c',
rollingLinks: false,
keyboard: true,
mouseWheel: false,
fragmentInURL: false,
hashOneBasedIndex: false,
pdfSeparateFragments: true,
overview: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'convex', // see README of reveal.js for options
transitionSpeed: 'default',
// Optional libraries used to extend reveal.js
dependencies: [
{ src: 'https://cdn.jsdelivr.net/npm/reveal.js/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'https://cdn.jsdelivr.net/npm/reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'https://cdn.jsdelivr.net/npm/reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'https://cdn.jsdelivr.net/npm/reveal.js/plugin/search/search.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'https://cdn.jsdelivr.net/npm/reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } }]
});
</script>
</body>
</html>