-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFloatingPointSimulation.tex
73 lines (64 loc) · 2.98 KB
/
FloatingPointSimulation.tex
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
\ifx\wholebook\relax\else
\documentclass[twoside]{book}
\usepackage[active]{srcltx}
\usepackage[LY1]{fontenc}
\input{DhbDefinitions}
\begin{document}
\fi
\chapter{Decimal floating-point simulation}
\label{ch-fpSimul} The class {\tt DhbDecimalFloatingNumber} is
intended to demonstrate rounding problems with floating-point
number representation. It models the floating-point number
representation. The radix of the floating-point representation is
decimal to ease the reading of the results. It also allows people
to carry some of the operations by hand. This model is almost
equivalent to what is done inside a computer. One notable
difference is the absence of exponent offset. Another difference
is that there is no limit on the size of the exponent. Simple as
it is this model can be used to illustrate rounding problems to
beginners. This class is only intended for didactical purposes.
Instances of the class are created with the method {\tt new:}
supplying any number as argument. For example, $${\tt
DhbDecimalFloatingNumber new: 3.141592653589793238462643}$$
Arithmetic operations are performed as usual. For example, the
first expression of section \ref{sec-rounding} can be coded as:
\begin{codeExample}
\begin{verbatim}
| a b |
a := DhbDecimalFloatingNumber new: (2 raisedToInteger: 64).
b := DhbDecimalFloatingNumber new: 300.
a + b
\end{verbatim}
\end{codeExample}
The class has two instance variables:
\begin{description}
\item[{\tt mantissa}] contains the mantissa of the number normalized to a finite number of
digits,
\item[{\tt exponent}] contains the negative of the decimal exponent of the number.
\end{description}
The class has one class variable:
\begin{description}
\item[{\tt Digits}] contains the maximum number of digits kept in the mantissa of all instances.
\end{description}
The method {\tt normalize:} computes the mantissa and exponent of
the argument and stores it into the corresponding instance
variables. The method {\tt value} allows retrieving the value of
the number in numerical form. The method {\tt printOn:} allows to
visualize the representation of the number.
The number of decimal digits kept in the mantissa can be changed
by the user using the method {\tt setDigits:}. By default it is
set to 15, which corresponds roughly to the precision of a 64-bit
IEEE standard floating-point number.
The four arithmetic operations and the square root have been
defined. The mechanism of each operation is the same. First each
operand is converted to a fraction. Then, the operation is carried
using normal numbers. In the case of arithmetic operation the
result is exact since the use of Smalltalk fractions guaranties
absolute precision. Then, a new instance of the class {\tt
DhbDecimalFloatingNumber} is created from the result and is
returned. Rounding errors will occur during this step.
\begin{listing} Smalltalk code
simulating decimal floating number arithmetic \label{ls:testst}
\input{Smalltalk/NumericalPrecision/DhbDecimalFloatingNumber}
\end{listing}
\ifx\wholebook\relax\else\end{document}\fi