Fixed point number
A fixed point number is a number that consists of a fixed number of digits . The position of the comma is fixed, hence the name.
The basic idea behind it is the information technology representation of a section of the rational numbers . This mapping to a data type of limited size , typically integers of different bit widths, requires a fixed number of digits for the part before and after the decimal point. Usually, according to the definition, the first digits are pre-decimal places and the remaining decimal places . In computer science, both decimal and binary fixed-point numbers are of great practical importance.
Advantages and disadvantages compared to a floating point number
Computing effort
The fixed position of the decimal point means that there is no computational effort compared to calculating with floating point numbers . If a binary fixed-point representation is selected in a computer program , the multiplications and divisions necessary for conversion and correction can also be replaced by fast shift operations. An example of an application that was specifically designed with fixed point arithmetic for reasons of computational effort is z. B. Fractint , a fractal generator . Since the mid-1990s, processors have had dedicated floating-point calculators, which make the calculation of fractals with floating-point numbers faster than that with integers.
Precision and Dynamics
Due to the exact representation, the value range of a fixed point number is smaller than the respective value range of a floating point number of the same (bit) length. For this, however, the exactness of the representation of a number in the entire value range is ensured, not always with floating point numbers (e.g. through absorption). An example of an application that requires precise arithmetic and therefore uses fixed point numbers is GnuCash .
Representation example: binary fixed point number
All binary fixed point numbers of length and places in front of the decimal point with :
n = 2 | n = 1 | n = 0 | ||||
Binary pattern | Binary | Decimal | Binary | Decimal | Binary | Decimal |
---|---|---|---|---|---|---|
00 | 00, | 0 | 0.0 | 0.0 | , 00 | 0.00 |
01 | 01, | 1 | 0.1 | 0.5 | , 01 | 0.25 |
10 | 10, | 2 | 1.0 | 1.0 | , 10 | 0.50 |
11 | 11, | 3 | 1.1 | 1.5 | , 11 | 0.75 |
Note that each of the four listed binary patterns stands for three different numbers, depending on where the comma is placed. Since the number of places in front of the decimal point is already fixed by definition, it is unnecessary to write or save the usual comma, i.e. H. the representation is always that of the "binary pattern" column.
Calculating with fixed point numbers
In the case of the calculation with fixed point numbers, the binary patterns are processed in the same way as in the case of the calculation with whole numbers . Fixed point arithmetic can therefore be performed by any digital processor that supports arithmetic operations on integers. However, there are a few rules that apply to the position of the comma before and after the arithmetic operation:
- With addition and subtraction, the position of the comma must be identical for all operands. If this is not the case, the operands must be adjusted accordingly using shift operations. The comma position of the result then corresponds to the comma position of the operands.
- In the case of multiplication, the number of places in front of the decimal point of the result corresponds to the sum of the numbers of places in front of the decimal point of all operands; the number of decimal places of the result also corresponds to the sum of the number of decimal places of all operands.
- A division or multiplication by a power corresponds to a bit shift operation of the binary pattern to the right or left by positions with the decimal point remaining the same. Alternatively, the operation can be seen as an imaginary shift of the comma.
Application examples
The following examples are based on a decimal fixed point representation with two decimal places. This is expressed by the factor 100, by which the original values were multiplied to get the fixed point representation.
In the fixed point representation:
- addition
- subtraction
- multiplication
- Here the result is not a fixed-point number based on the same criteria as the two factors; a correction is necessary (here: division by 100).
- division
- The result here is not a fixed-point number based on the same criteria as dividend and divisor; a correction is necessary (here: multiplication by 100).
- Example in binary 8-bit representation with 4 decimal places and shift operation, such as B. is used in computer architectures
Decimal:
Binary:
Calculation in the 8-bit fixed point representation with 4 decimal places:
Multiplication:
Result in the fixed point representation:
- ,
with the meaning
Incorrect representation of the decimal point, therefore the shift operation is necessary:
The result would be according to the 8-bit fixed point representation with 4 decimal places:
The result after the correction now corresponds to the expected result.
Problems
There can be some problems with representing a real number . In the following, the fixed-point number (based on the representation in a computer) has a length of and pre- and post-decimal places. Let the number of digits be - i.e. a binary fixed point number the length of one byte with an equal number of places before and after the decimal point. The subscript denotes the representation of the number: for a real number in the usual decimal representation and for such a fixed point number.
As you can see, only fixed-point numbers between and (with a resolution of ) can be represented with eight bits and four places before and after the decimal point . This small display area is also the decisive disadvantage compared to floating point numbers .
Furthermore, as with floating point numbers, rounding errors occur when converting the decimal, real numbers into a binary fixed point representation. can be represented in contrast to exactly. However, no matter how many decimal places, it cannot be represented as a sum of powers of two. To avoid these problems, a decimal fixed point representation can be used if necessary.
Nomenclature of the number format
The number format for fixed-point numbers with one decimal place is also called “Q1”, with two decimal places “Q2” etc. If the word length is several bits, the format can be specified even more precisely by specifying the places before the decimal point. The places before and after the decimal point are separated by a point. A fixed-point number with eight bits can be designated as “Q7.1” or “Q6.2”, for example.
Decimal fixed point number
The use of decimal fixed point numbers is particularly useful for amounts of money. For example, if a certain invoice amount is paid first with a down payment and later with a final payment, when floating-point numbers are used, rounding errors may result in a remaining amount or credit far below the smallest currency unit. Corresponding orders in a database would later be found by searching for orders that have not been fully paid (i.e. with the remaining amount not equal to 0), since the remaining amount is very small, but not 0.
In most programming languages and databases, there is a data type called Currency , Money or Decimal . It is based on a signed integer , usually 64 bits long. Allocation, addition and subtraction of two such data types with one another take place as with an integer, as well as multiplication of fixed-point numbers and integers and rounding-off division of a decimal fixed-point number by an integer. For other operations, which may also be lossy, the computer must consider the number of decimal places. This can be freely defined in some languages. If this is not the case, the data type usually has four decimal places and could informally be referred to as the ten-thousandth. Currency sub-units currently available in the world in addition to the widespread hundredths are fifths ( Mauritania and Madagascar ) and in the MENA region thousandths (see decimal currency ), which can thus be represented. The range of values of a 64-bit number with four decimal places is −922,337,203,685,477.5808 to 922,337,203,685,477.5807. That is around ten times the global economic output in dollars in 2017. A 32-bit number with four decimal places, for example called smallmoney (literally: small money) in T-SQL , allows numbers from −214,748.3648 to 214,748.3647.
See also
Web links
Individual evidence
- ↑ Noel Giffin: Fractint: A Little Code, Limitations of Integer Math (And How We Cope) . Nehee Enterprises. Archived from the original on May 19, 2012. Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. Retrieved July 9, 2012.
- ^ David Goldberg: What Every Computer Scientist Should Know About Floating-Point Arithmetic . In: ACM Computing Surveys . 23, 1991, pp. 5-48. doi : 10.1145 / 103162.103163 . Retrieved September 2, 2010.
- ↑ What's new in GnuCash 1.6? . gnucash.org. Retrieved September 3, 2010.