Jump to content

Fixed-point arithmetic

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Michael Hardy (talk | contribs) at 23:07, 31 January 2004 (Moved material from fixed point.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

In computing, a fixed-point number representation is a real data type for a number that has a fixed number of digits after the decimal (or binary or hexadecimal) point. For example, a fixed-point number with 4 digits after the decimal point could be used to store numbers such as 1.3467, 281243.3234 and 0.1000, but would round 1.0301789 to 1.0302 and 0.0000654 to 0.0001.

Fixed-point can exactly represent decimal fractions while still employing the base 2 arithmetic efficient in most of today's computers. Most floating point representations in computers use base 2 values, which cannot exactly represent most fractions that are easily represented in base 10. For example, one-tenth (.1) and one-hundredth (.01) can only be represented approximately by floating point representations, while they can be represented exactly in fixed-point representations.

As long as the numeric value uses only the number of digits specified after the decimal point, fixed-point values can exactly represent all values up to its maximum value (determined by the number of bits in its representation). This is in contrast to floating-point representations, which include an automatically-managed exponent but cannot represent as many digits accurately (given the same number of bits in its representation).

A common use for fixed-point numbers is for storing monetary values, where the "inexact" values of floating-point numbers are often a liability. Fixed-point representations are also sometimes used if either the executing processor does not have any floating point unit (FPU) or if fixed-point provides an improved performance necessary for an application. Historically, fixed-point representations were the norm for decimal data types (for example, in PL/I or COBOL). The Ada programming language includes built-in support for both fixed-point and floating-point.

Many computer languages do not include built-in support for floating point values, because for most applications, floating-point representations are fast enough and accurate enough. Floating-point representations are more flexible than fixed-point representations, because they can handle a wider dynamic range. Floating-point representations are also slightly easier to use, because they do not require programmers to specify the number of digits after the decimal point.

However, if they are needed, fixed-point numbers can be implemented even in programming languages like C and C++ that do not include such support built-in.


Here are some last-gasp examples of fixed-point representations:

  1. GnuCash is an application for tracking money. It is written in C and switched from a floating-point representation of money to a fixed-point implementation as of version 1.6. This change was made to avoid the potential rounding errors of floating-point representations.
  2. Tremor is a software library that decodes the Ogg Vorbis audio format. Tremor uses fixed-point because many audio decoding hardware devices do not have an FPU (to save money) and audio decoding requires enough performance that a software implementation of floating-point on low-speed devices would not work well.

External Links: