Chapter 2 Using Python as a Calculator
2.7 The Underlying MechanismΒΆ
Floating point arithmatic can be inaccurate in calculators; the same happens in Python.
In the above example, the expression is evaluated to an imprecise number. The main reason is limitation of memory space. A floating point number can contain an infinite amount of information, if there is an infinite number of digits after the decimal point. However, the amount of information that can be processed or stored by a calculator or a computer is finite. As a result, floating point numbers cannot be stored to an arbitrary precision, and floating point operators cannot be infinitely precise. The error that results from the imprecise operations is called rounding off error.
At this point it is useful to know a little about the underlying mechanism of Python, so that deeper understanding can be gained on facts such as rounding off errors, which enables more solid programs to be developed.
The basic architecture of a computer is shown in Fig. 1.2 previously. On the bottom of the figure, the main hardware components are shown, which include the CPU, memory and devices. Among the three main components, the CPU is the most important; it carries out computer instructions, including arithmetic operations. The typical way in which arithmetic operations are executed is: the CPU takes the operands from the memory, evaluates the result by applying the operator on them, and then stores the result back into the memory. The memory can be regarded as a long array of information storage units. Each unit is capable of storing a certain amount of information, and the index of its location in the long array is referred to as its memory address. Devices are the channel through which computers are connected to the physical world. Keyboards, mouses, displays, speakers and microphones are a few commonly-used devices. An important device is the hard disk, on which the OS defines a file system for external storage of information.
© Copyright 2024 GS Ng.