# 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. ${\ displaystyle n \ leq k}$${\ displaystyle m = kn}$

### 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 : ${\ displaystyle k = 2}$${\ displaystyle n}$${\ displaystyle n \ in \ {0,1,2 \}}$

Binary pattern Binary Decimal Binary Decimal Binary Decimal n = 2 n = 1 n = 0 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.${\ displaystyle 2 ^ {n}}$${\ displaystyle n}$

### 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:

${\ displaystyle 6 {,} 3 \ rightarrow 630_ {F}}$
${\ displaystyle 2 {,} 1 \ rightarrow 210_ {F}}$
${\ displaystyle 630_ {F} + 210_ {F} = 840_ {F}}$
${\ displaystyle 840_ {F} \ rightarrow 8 {,} 4}$
subtraction
${\ displaystyle 630_ {F} -210_ {F} = 420_ {F}}$
${\ displaystyle 420_ {F} \ rightarrow 4 {,} 2}$
multiplication
${\ displaystyle 630_ {F} \ times 210_ {F} = 132300_ {?}}$
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).
${\ displaystyle 132300/100 = 1323_ {F} \ rightarrow 13 {,} 23}$
division
${\ displaystyle {\ frac {630_ {F}} {210_ {F}}} = 3_ {?}}$
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).
${\ displaystyle 3 \ times 100 = 300_ {F} \ rightarrow 3 {,} 00}$
Example in binary 8-bit representation with 4 decimal places and shift operation, such as B. is used in computer architectures

Decimal:

${\ displaystyle 3 {,} 5_ {d} \ times {} 2 {,} 5_ {d} = 8 {,} 75_ {d}}$

Binary:

${\ displaystyle 0011 {,} 1000_ {b} \ times {} 0010 {,} 1000_ {b} = 0000 \, 1000 {,} 1100 \, 0000_ {b}}$

Calculation in the 8-bit fixed point representation with 4 decimal places:

${\ displaystyle 3 {,} 5_ {d} \ rightarrow 0011 \, 1000_ {b}}$
${\ displaystyle 2 {,} 5_ {d} \ rightarrow 0010 \, 1000_ {b}}$

Multiplication:

${\ displaystyle 0011 \, 1000_ {b} \ times {} 0010 \, 1000_ {b} = 0000 \, 1000 \, 1100 \, 0000_ {b}}$

Result in the fixed point representation:

${\ displaystyle 1100 \, 0000_ {b}}$,

with the meaning

${\ displaystyle 1100 {,} 0000_ {b} = 12_ {d}}$

Incorrect representation of the decimal point, therefore the shift operation is necessary:

${\ displaystyle 0011 {,} 1000_ {b} \ times {} 0010 {,} 1000_ {b} = 0000 \, 1000 {,} 1100 \, 0000_ {b}}$

The result would be according to the 8-bit fixed point representation with 4 decimal places:

${\ displaystyle 1000 {,} 1100_ {b} = 8 {,} 75_ {d}}$

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. ${\ displaystyle z}$${\ displaystyle k = 8}$${\ displaystyle n = m = 4}$${\ displaystyle \ {0.1 \}}$${\ displaystyle X_ {R}}$${\ displaystyle X_ {F}}$

• ${\ displaystyle 0_ {R} = 00000000_ {F}}$
• ${\ displaystyle 1_ {R} = 00010000_ {F}}$
• ${\ displaystyle 10_ {R} = 10100000_ {F}}$
• ${\ displaystyle 0 {,} 5_ {R} = 00001000_ {F}}$
• ${\ displaystyle 0 {,} 625_ {R} = 00001010_ {F}}$
• ${\ displaystyle 0 {,} 0625_ {R} = 00000001_ {F}}$
• ${\ displaystyle 15 {,} 9375_ {R} = 11111111_ {F}}$
• ${\ displaystyle 16_ {R}> 11111111_ {F}}$
• ${\ displaystyle 0 {,} 06_ ​​{R} <00000001_ {F}}$
• ${\ displaystyle 7 {,} 7_ {R} \ approx 01111011_ {F}}$

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 . ${\ displaystyle 0_ {R}}$${\ displaystyle 15 {,} 9375_ {R}}$${\ displaystyle 0 {,} 0625_ {R}}$

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. ${\ displaystyle 7_ {R} = 01110000_ {F}}$${\ displaystyle 0 {,} 7_ {R} \ approx 00001011_ {F}}$${\ displaystyle 0 {,} 7_ {R}}$

## 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.