Safekipedia

Fixed-point arithmetic

Adapted from Wikipedia · Adventurer experience

In computing, fixed-point is a way to show fractional numbers. It keeps a set number of digits after the decimal point. For example, money is often shown with two digits after the decimal for cents, like $1.25 for one dollar and twenty-five cents. This method can also be used for other units.

Fixed-point numbers are shown in the same number base as whole numbers. They use negative powers of that base. The most common bases are decimal (base 10) and binary (base 2). This means the number is always a multiple of a small unit.

Fixed-point arithmetic was used in old mechanical calculators. Today, it is used in special cases. These include low-cost embedded systems, microprocessors, and microcontrollers. It is also useful in areas like image, video, and digital signal processing. It helps in accounting for money and in situations where avoiding rounding errors is important.

Representation

A fixed-point way to show numbers that are not whole is to use whole numbers that stand for parts of the real number. For example, the number 1.23 can be saved as the whole number 123, but we know it really means 1.23 because we agree to always divide by 100. This lets computers do math with these numbers using the same tools they use for whole numbers.

Negative numbers can also be shown this way. Computers can keep track of the sign using a special bit. For example, a pattern of bits might mean a negative number, and the rest of the bits tell us how big the number is, but we still divide by the same agreed-upon amount to get the real value.

Usually, a computer program will use the same way of showing these parts for all numbers of a certain type. This choice is made by the person writing the program, based on how exact they need the numbers to be and how big or small the numbers might get.

Example fixed-point rep­re­sen­ta­tion with scaling 1/100
Value
represented
Internal
representation
0.000
0.550
0.9999
2200
−14.1−1410
314.16031416
Parameters of some 16-bit signed binary fixed-point formats
fSδVminVmax
−31/2−3 = 84−262144262136
01/20 = 10.5−32768+32767
51/25 = 1/32−1024.00000+1023.96875
141/214 = 1/16384−2.00000000000000+1.99993896484375
151/215 = 1/32768−1.000000000000000+0.999969482421875
161/216 = 1/65536−0.5000000000000000+0.4999847412109375
201/220 = 1/1048576−0.03125000000000000000+0.03124904632568359375

Applications

Fixed-point numbers are often used for storing money because they help avoid rounding problems. For example, the money management app GnuCash started using fixed-point numbers instead of floating-point numbers in version 1.6.

Binary fixed-point numbers were commonly used from the late 1960s to the 1980s in tasks that needed a lot of math, like flight simulation and nuclear power plant control systems. They are still used today in many DSP applications and special microprocessors. They are also used for calculations with angles, like binary angular measurement, and in tools like the STM32G4 series CORDIC co-processors and for compressing JPEG images using discrete cosine transform algorithms.

Electronic devices such as electricity meters and digital clocks use special math formulas to correct errors caused by things like temperature changes. These formulas, created using polynomial regression, can be calculated more precisely and quickly with fixed-point numbers, even on simple computers. This method, using Horner's method, helps keep the results accurate by reducing how often rounding needs to happen.

Operations

Adding or subtracting two numbers with the same size part is easy: just add or subtract the whole numbers, and the size part stays the same. If the numbers have different size parts, they need to be made the same size first.

Multiplying two numbers means multiplying the whole numbers and then multiplying their size parts together. For example, multiplying 0.123 (which is 123 with a size part of 1/1000) by 2.5 (which is 25 with a size part of 1/10) gives 3075 with a size part of 1/10000, which is 0.3075.

Dividing one number by another means dividing their whole numbers and then dividing their size parts. For example, dividing 34.56 (which is 3456 with a size part of 1/100) by 1.234 (which is 1234 with a size part of 1/1000) gives about 30 with a size part of 10.

Changing the size part of a number often needs to be done in fixed-point math. This can happen when storing a number, preparing it for addition or subtraction, or after multiplying or dividing. Sometimes, the number needs to be made more precise or fit into a smaller space.

Converting between different size parts means multiplying or dividing the whole number by the right amount. For example, changing 1.23 (123 with size part 1/100) to a size part of 1/1000 means multiplying by 10, giving 1230 with size part 1/1000.

Converting a number to fixed-point from floating-point means multiplying by the size part and rounding. Converting from fixed-point to floating-point means dividing by the size part.

Hardware support

Most computers do not have special tools to work with fixed-point numbers. But they often have quick commands that can shift numbers left or right. This helps change the size of numbers without changing their sign.

Some older computers, like the IBM 1620 and the Burroughs B3500, used a special way to store numbers called binary-coded decimal. Some small computers still use this today. These computers can change number sizes using shifts or moving data in memory.

Certain special computers for signal processing can work directly with fixed-point numbers. They might have commands to multiply numbers and adjust their size right away. If a computer does not have this feature, a programmer needs to store the result in a bigger space and adjust the size themselves.

Overflow happens when a calculation result is too big to fit in the space set for it. This can change the sign and size of the number in big ways. Some computers can mark when overflow happens or stop the work, but it is often better to choose the right sizes for numbers before doing the work to avoid overflow.

Computer language support

Some older programming languages, like PL/I, COBOL, Ada, JOVIAL, and Coral 66, have special ways to handle fixed-point numbers. These languages let you store numbers with a set number of digits after the decimal point. The computer changes the numbers automatically when you do math or move them.

Many newer languages, such as FORTRAN, C, and C++, do not have built-in support for fixed-point numbers. This is because modern computers work well with floating-point numbers. But programmers can still create fixed-point operations by adjusting the numbers themselves. Databases and SQL also support fixed-point numbers for storing values like money. In 2008, a plan was made to add fixed-point support to the C language. The GNU Compiler Collection (GCC) includes support for this plan.

Detailed examples

When we multiply numbers that have a fixed number of decimal places, we can treat them like whole numbers first and then adjust the decimal places at the end. For example, multiplying 10.500 by 1.050 gives 11.025000. To do this with whole numbers, we move the decimal points three places to the right, turning 10.500 into 10500 and 1.050 into 1050. Multiplying these gives 11,025,000. Moving the decimal point back six places gives the answer 11.025000.

We can also do this using binary numbers, which computers use. For instance, to multiply 1.2 by 5.6 in binary with a fixed number of bits for fractions, we first convert these numbers into whole numbers by multiplying by 216. This gives us two large whole numbers. Multiplying these and then dividing by 216 gives the final result.

Notations

Different ways are used to describe how numbers are stored in fixed-point formats. For example, the Q notation from Texas Instruments uses Q_f_ to show how many parts of a number are fractions. Q15 means a number with 15 fraction parts.

Other systems like ARM, COBOL, and Ada programming language have their own special ways to write these formats. These notations help programmers see how numbers are stored and used in their programs.

Software application examples

Some software programs use fixed-point arithmetic to work with numbers that have parts smaller than a whole. For example, the TrueType font format uses this method to handle certain numbers precisely. Many older video games, like those for the PlayStation and Sega Saturn, also used fixed-point arithmetic because their systems did not have special hardware for handling decimal numbers.

Other programs, such as the TeX typesetting software and audio codecs like Tremor and WavPack, use fixed-point arithmetic to keep calculations accurate. Some tools for working with sensors and embedded systems, like the Nest Labs Utilities library and OpenGL ES 1.x, also use this method. Even calculators like dc and bc can be set to track a fixed number of decimal places.

Related articles

This article is a child-friendly adaptation of the Wikipedia article on Fixed-point arithmetic, available under CC BY-SA 4.0.