Skip to content

Commit 31a9ce6

Browse files
committed
added float math infinity and near-zero
1 parent 7ce3e9f commit 31a9ce6

File tree

10 files changed

+125
-8
lines changed

10 files changed

+125
-8
lines changed

bibliography/bibliography.bib

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ @string { l_usa_hoboken
190190
@string { l_usa_ithaca = "{{Ithaca}, {NY}, {USA}}" }
191191
@string { l_usa_new_york = "{{New York}, {NY}, {USA}}" }
192192
@string { l_usa_piscataway = "{{Piscataway}, {NJ}, {USA}}" }
193+
@string { l_usa_redwood_shores = "{{Redwood Shores}, {CA}, {USA}}" }
193194
@string { l_usa_reston = "{{Reston}, {VA}, {USA}}" }
194195
@string { l_usa_san_francisco = "{{San Francisco}, {CA}, {USA}}" }
195196
@string { l_usa_savannah = "{{Savannah}, {GA}, {USA}}" }
@@ -211,6 +212,7 @@ @string { p_iso
211212
@string { p_iso_iec = "{International Organization for Standardization~{(ISO)} / International Electrotechnical Commission~{(IEC)}}" }
212213
@string { p_informs = "{The Institute for Operations Research and the Management Sciences~({INFORMS})}" }
213214
@string { p_mit_press = "{{MIT} Press}" }
215+
@string { p_oracle = "{Oracle Corporation}" }
214216
@string { p_oreilly = "{{O'Reilly} Media, Inc.}" }
215217
@string { p_packt = "{Packt Publishing Ltd}" }
216218
@string { p_pearson_education = "Pearson Education, Inc." }
@@ -240,6 +242,7 @@ @string { pa_ieee_ny
240242
@string { pa_informs = l_usa_catonsville }
241243
@string { pa_microsoft_press = pa_pearson_education }
242244
@string { pa_mit_press = l_usa_cambridge }
245+
@string { pa_oracle = l_usa_redwood_shores }
243246
@string { pa_oreilly = l_usa_sebastopol }
244247
@string { pa_packt = l_uk_birmingham }
245248
@string { pa_pearson_education = l_usa_hoboken }
@@ -615,6 +618,22 @@ @article{N1939TTOP
615618
doi = {10.2307/2302515}
616619
}
617620

621+
@inbook{O2024CD,
622+
title = {Class Double},
623+
crossref = {O2024JPSEJDKV2AS},
624+
url = {https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Double.html},
625+
urldate = {2024-07-07},
626+
}
627+
628+
@book{O2024JPSEJDKV2AS,
629+
title = {\pgls{Java}\textsuperscript{\textregistered} Platform, Standard Edition \& \pgls{Java} Development Kit Version~22 {API} Specification},
630+
publisher = p_oracle,
631+
address = pa_oracle,
632+
date = {2024-04-09},
633+
url = {https://docs.oracle.com/en/java/javase/22/docs/api},
634+
urldate = {2024-07-07},
635+
}
636+
618637
@article{PRGWSUdVLFTEKPKFBV2016TSRFTAOGAG,
619638
author = a_perez_riverol_yasset # and # a_gatto_laurent # and # a_wang_rui # and # a_sachsenberg_timo # and # a_uszkoreit_julian # and # a_da_veiga_leprevost_felipe # and # a_fufezan_christian # and # a_ternent_tobias # and # a_eglen_stephen_j # and # a_katz_daniel_s # and # a_pollard_tom_j # and # a_konovalov_alexander # and # a_flight_robert_m # and # a_blin_kai # and # a_vizcaino_juan_antonio,
620639
title = {Ten Simple Rules for Taking Advantage of \git\ and \github},

styles/colors.sty

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
%%
4343
\RequirePackage{pagecolor}%
4444
\colorlet{titlepage-background}{hfuu-red}%
45-
\colorlet{titlepage-title}{hfuu-orange}%
46-
\colorlet{titlepage-text}{hfuu-orange!50!white}%
45+
\definecolor{titlepage-text}{HTML}{D6BDBD}%
46+
%\colorlet{titlepage-title}{hfuu-orange}%
47+
\colorlet{titlepage-title}{titlepage-text}%
4748
%
4849
%%
4950
%% Definition and Definition-Like Environments

text/main/basics/simpleDataTypesAndOperations/float/float.tex

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
You can jump right to the next section.}%
3838
%
3939
The first part, the so-called \pgls{significand} or \pgls{mantissa}, consists of 52~bits, represents the digits of the number.
40-
52~bits can represent $52\log_2 10\approx 15$~decimal digits, meaning that we can represent numbers to a precision of about 15~digits.
40+
52~bits can represent $52\log_2 10\approx 15$~to 16~decimal digits, meaning that we can represent numbers to a precision of about 15~digits.
4141
If we would just use 52~bits, then this would limit us to represent numbers maybe from~$0$ to~$2^{52}-1$ at a resolution of~$1$.
4242
Of course, we could also choose some other resolution, say~$0.001$.
4343
In this case, we could represent numbers from~$0$ to $0.001*(2^{52}-1)$ and the smallest non-zero number would be~$0.001$ instead of~$1$.
@@ -143,10 +143,10 @@
143143
\centering%
144144
\begin{tabular}{r@{}r@{{\color{orange}\textbf{e}}}l@{~~$\equiv$~~}r@{}l}%
145145
\multicolumn{2}{c}{$\beta$}&$\gamma$&\multicolumn{2}{c}{$\alpha$}\\\hline%
146-
&\texttt{A.BCDEFG}{\dots}&\texttt{\color{red}{+}}\texttt{HIJ}&&$\texttt{A.BCDEFG}{\dots}*10^{HIJ}$\\
147-
&\texttt{A.BCDEFG}{\dots}&\texttt{\color{red}{-}}\texttt{HIJ}&&$\texttt{A.BCDEFG}{\dots}*10^{{\color{red}{-}}HIJ}$\\
148-
{\color{blue}{-}}&\texttt{A.BCDEFG}{\dots}&\texttt{\color{red}{+}}\texttt{HIJ}&${\color{blue}{-}}$&$\texttt{A.BCDEFG}{\dots}*10^{HIJ}$\\
149-
{\color{blue}{-}}&\texttt{A.BCDEFG}{\dots}&\texttt{\color{red}{-}}\texttt{HIJ}&${\color{blue}{-}}$&$\texttt{A.BCDEFG}{\dots}*10^{{\color{red}{-}}HIJ}$%
146+
&\texttt{A.BCDEFG}{\dots}&\texttt{\color{red}{+}}\texttt{HIJ}&&$\texttt{A.BCDEFG}{\dots}*{\color{orange}10}^{HIJ}$\\
147+
&\texttt{A.BCDEFG}{\dots}&\texttt{\color{red}{-}}\texttt{HIJ}&&$\texttt{A.BCDEFG}{\dots}*{\color{orange}10}^{{\color{red}{-}}HIJ}$\\
148+
{\color{blue}{-}}&\texttt{A.BCDEFG}{\dots}&\texttt{\color{red}{+}}\texttt{HIJ}&${\color{blue}{-}}$&$\texttt{A.BCDEFG}{\dots}*{\color{orange}10}^{HIJ}$\\
149+
{\color{blue}{-}}&\texttt{A.BCDEFG}{\dots}&\texttt{\color{red}{-}}\texttt{HIJ}&${\color{blue}{-}}$&$\texttt{A.BCDEFG}{\dots}*{\color{orange}10}^{{\color{red}{-}}HIJ}$%
150150
\end{tabular}%
151151
%
152152
\caption{The structure of the scientific notation for floating point numbers in \python, which represent a value~$\alpha$ in the form~$\beta*10^{\gamma}$.}%
@@ -212,5 +212,97 @@
212212
Similarly, \pythonil{0.023e-20} becomes \pythonil{2.3e-22}.
213213
\endhsection%
214214
%
215+
\hsection{Limits and Special Floating Point Values: Infinity and \inQuotes{Not a Number}}%
216+
%
217+
\begin{figure}%
218+
\centering%
219+
\includegraphics[width=0.7\linewidth]{\currentDir/floatMathInConsoleZero}%
220+
\caption{What happens with very small floating point numbers in \python?}%
221+
\label{fig:floatMathInConsoleZero}%
222+
\end{figure}%
223+
%
224+
We already learned that the floating point type \pythonil{float} can represent both very small and very large numbers.
225+
But we also know that it is internally stored as chunk of 64~bits.
226+
So its range must be somehow finite.
227+
What happens if we exceed this finite range?
228+
229+
First the easy part:
230+
Now, we said that \python\ can store very small numbers, like $10^{-300}$ in \pythonil{float} data types.
231+
But how small really?
232+
Finding this out from the documentation is actually not so easy.
233+
Luckily, \pgls{Java} uses the same standard for its class \pythonil{Double}.
234+
In its documentation~\cite{O2024CD}, we find that the minimum value is~$2^{-1074}$, which is approximately~$4.940\decSep656\decSep458\decSep412\decSep465\decSep44*10^{-324}$.
235+
So we would expect the smallest possible floating point value in \python\ to also be in this range.
236+
237+
In \cref{fig:floatMathInConsoleInf}, we take a look what happens if we approach this number.
238+
We use the scientific notation and begin to print the number~\pythonil{1e-323} (which is~$10^{-323}$).
239+
This number is correctly represented as \pythonil{float} and shows up in the console exactly as we wrote it.
240+
However, if we go a bit smaller and enter \pythonil{9e-324}, which is~$9*10^{-324}=0.9*10^{-323}$, we find that it again shows up in the console as \pythonil{1e-323}.
241+
This is because the number is already subnormal~\cite{IEEE2019ISFFPA,H1997IS7FPN}, i.e., uses only a few of the significand bits.
242+
The full precision of 15~digits is no longer available at this small scale.
243+
Thus \pythonil{9e-324} and \pythonil{1e-323} map to the same \pythonil{float}.
244+
Converting this \pythonil{float} to text yields \pythonil{'1e-323'}.
245+
The same happens for \pythonil{8e-324}, which also maps to \pythonil{1e-323}.
246+
\pythonil{7e-324}, however, is the same as \pythonil{5e-324}.
247+
Matter of fact, \pythonil{6e-324}, \pythonil{5e-324}, \pythonil{4e-324}, and \pythonil{3e-324} all map to this same \pythonil{float}.
248+
249+
It turns out that this number is already the smallest \pythonil{float} that can be represented:
250+
\pythonil{2e-324} simply becomes \pythonil{0.0}.
251+
This value is simply too small to be represented as a 64~bit / double precision IEEE~Standard 754 floating point number~\cite{IEEE2019ISFFPA,H1997IS7FPN}.
252+
The text \pythonil{2e-324} that we enter into the \python\ console will therefore be translated to the \pythonil{float}~\pythonil{0.0}.
253+
The comparison \pythonil{2e-324 == 0.0} therefore results in \pythonil{True}, while \pythonil{3e-324 == 0.0} is still \pythonil{False}.
254+
So we learned what happens if we try to define very small floating point numbers:
255+
They become zero.
256+
257+
\begin{figure}%
258+
\centering%
259+
\includegraphics[width=0.7\linewidth]{\currentDir/floatMathInConsoleInf}%
260+
\caption{What happens with very large floating point numbers in \python?}%
261+
\label{fig:floatMathInConsoleInf}%
262+
\end{figure}%
263+
264+
But what happens to numbers that are too big for the available range?
265+
Again, according to the nice documentation of \pgls{Java}, the maximum 64~bit double precision floating point number value is~$(2-2^{-52})*2^{1023}\approx1.797\decSep693\decSep134\decSep862\decSep315\decSep708\dots*10^{308}$.
266+
We can enter this value as \pythonil{1.7976931348623157e+308} and it indeed prints correctly in \cref{fig:floatMathInConsoleInf}.
267+
If we step it up a little bit and enter \pythonil{1.7976931348623158e+308}, due to the limited precision, we again get \pythonil{1.7976931348623157e+308}.
268+
However, if we try entering \pythonil{1.7976931348623159e+308} into the \python\ console, something strange happens:
269+
The output is \pythonil{inf}.
270+
\pythonil{-1.7976931348623159e+308} gives us \pythonil{-inf}.
271+
Multiplying this value by two, i.e., computing \pythonil{-1.7976931348623157e+308 * 2}, still yields \pythonil{-inf}.
272+
\pythonil{inf} stands for \inQuotes{infinity} or~$\infty$.
273+
However, it \emph{actually} means \emph{too big to represent as \pythonil{float}}.
274+
If we enter numbers that are too big or exceed the valid range of \pythonil{float} by multiplication, addition, subtraction, or division, we simply get~\pythonil{inf}.
275+
This does not actually mean \inQuotes{mathematical infinity,} because, while \pythonil{-1.7976931348623159e+308} is very big, it is not infinitely big.
276+
It simply means that the number is too big to put it into a 64~bit \pythonil{float}.
277+
278+
Actually, the \pythonil{int} type can represent larger numbers easily.includegraphics
279+
\pythonil{1.7976931348623157e+308} is equivalent to \pythonil{17_976_931_348_623_157 * 10 ** 292}.
280+
This prints as a number with many zeros.
281+
Multiplying it with \pythonil{1.0} yields a \pythonil{float} with value \pythonil{1.7976931348623157e+308}, exactly as expected.
282+
If we try a number ten times as large, i.e., \pythonil{17_976_931_348_623_157 * 10 ** 293}, this is no problem with the \pythonil{int}.
283+
But we can no longer convert it to a \pythonil{float} by multiplying with~\pythonil{1.0}.
284+
Trying to do that with a ten times larger number, i.e., computing \pythonil{17_976_931_348_623_157 * 10 ** 293 * 1.0} leads to an exception:
285+
The output is \pythonil{OverflowError: int too large to convert to float}.
286+
An exception terminates the current flow of execution and signals an error.
287+
We will learn later what exceptions actually are and how to handle them properly.
288+
289+
The important thing to realize is that an overflow of a \pythonil{float} may either lead to the \pythonil{inf} value or to an error that stops your computation from continuing.
290+
As another example, let us again import the natural logarithm function \pythonil{log} and the Euler's constant~\pythonil{e} from the \pythonil{math} module by doing \pythonil{from math import e, log}.
291+
We now can compute the natural logrithm from the largest possible \pythonil{float} via \pythonil{log(1.7976931348623157e+308)}.
292+
We get \pythonil{709.782712893384}.
293+
Raising~$e$ to this power by doing \pythonil{e ** 709.782712893384} leads to the slightly smaller number~\pythonil{1.7976931348622053e+308} due to the limited precision of the \pythonil{float} type.
294+
However, if we try to raise~$e$ to a slightly larger power, and, for example, try to do \pythonil{e ** 709.782712893385}, we again face an \pythonil{OverflowError}.
295+
296+
We can also try to divide~1 by the largest \pythonil{float} and do \pythonil{1 / 1.7976931348623157e+308}.
297+
The result is the very small number \pythonil{5.562684646268003e-309}.
298+
However, if we divide~1 by \pythonil{1.7976931348623159e+308}, we get~\pythonil{0.0}.
299+
The reason is that \pythonil{1.7976931348623159e+308} becomes \pythonil{inf} and \pythonil{1 / inf} is~\pythonil{0}.
300+
301+
Finally, \pythonil{inf} also exists as constant in the \pythonil{math} module.
302+
We can import it via \pythonil{from math import inf}.
303+
And indeed, since the text \pythonil{1.7976931348623159e+308} is parsed to a \pythonil{float} value of \pythonil{inf}, we find that \pythonil{1.7976931348623159e+308 == inf} yields~\pythonil{True}.%
304+
%
305+
\endhsection%
306+
%
215307
\endhsection%
216308
%
Binary file not shown.
Binary file not shown.

text/title/hfuu_logo.pdf

10.1 KB
Binary file not shown.

text/title/hfuu_logo.svg

Lines changed: 1 addition & 0 deletions
Loading

text/title/tang_weisi.pdf

3.52 KB
Binary file not shown.

text/title/tang_weisi.svg

Lines changed: 1 addition & 0 deletions
Loading

text/title/titlepage.tex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
{\color{titlepage-title}\resizebox{0.9\linewidth}{!}{\bfseries\thetitle}}%
1212
\\[2cm]%
1313
%
14-
\resizebox{0.5\linewidth}{!}{\theauthor}%
14+
\resizebox{0.5\linewidth}{!}{\theauthor~\raisebox{0.7pt}{(\raisebox{-0.9pt}{\includegraphics[height=1.7ex]{text/title/tang_weisi}})}}%
1515
\\[2cm]%
1616
%
1717
\resizebox{0.275\linewidth}{!}{\thedate}%
1818
\\[2cm]%
1919
%
20+
\includegraphics[width=3.5cm]{text/title/hfuu_logo}%
21+
\\[2cm]%
22+
%
2023
\begin{abstract}\input{text/title/abstract.tex}\end{abstract}%
2124
}%
2225
%

0 commit comments

Comments
 (0)