Fixed-point arithmetic: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 1: | Line 1: | ||
{{dablink|This article is about a form of limited-precision arithmetic in computing. For the fixed points of a mathematical function, see [[fixed point (mathematics)]].}} |
{{dablink|This article is about a form of limited-precision arithmetic in computing. For the fixed points of a mathematical function, see [[fixed point (mathematics)]].}} |
||
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 (binary or radix) point. Fixed-point numbers are useful for representing fractional values in native two's complement format if the executing processor has no [[floating point]] unit (FPU) or if fixed-point provides |
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 (binary or radix) point. Fixed-point numbers are useful for representing fractional values in native two's complement format if the executing processor has no [[floating point]] unit (FPU) or if fixed-point provides improved performance or accuracy. Most low-cost embedded processors do not have an [[FPU]]. |
||
improved performance. Most low cost embedded processors do not have an FPU. |
|||
The bits to the left of the radix point are magnitude bits that represent integer values, the bits to the right of the radix point represent fractional values. Each fractional bit represents an inverse power of 2. Thus the first fractional bit is 1/2, the second is 1/4, the third is 1/8 and so on. For signed fixed point numbers in [[two's complement]] format, the upper bound is given by |
The bits to the left of the radix point are magnitude bits that represent integer values, the bits to the right of the radix point represent fractional values. Each fractional bit represents an inverse power of 2. Thus the first fractional bit is 1/2, the second is 1/4, the third is 1/8 and so on. For signed fixed point numbers in [[two's complement]] format, the upper bound is given by |
Revision as of 23:46, 29 January 2006
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 (binary or radix) point. Fixed-point numbers are useful for representing fractional values in native two's complement format if the executing processor has no floating point unit (FPU) or if fixed-point provides improved performance or accuracy. Most low-cost embedded processors do not have an FPU.
The bits to the left of the radix point are magnitude bits that represent integer values, the bits to the right of the radix point represent fractional values. Each fractional bit represents an inverse power of 2. Thus the first fractional bit is 1/2, the second is 1/4, the third is 1/8 and so on. For signed fixed point numbers in two's complement format, the upper bound is given by
where m is bits of magnitude and f is bits of fraction.
For example, a 16 bit signed fixed-point binary number with 4 bits after the radix point yields 12 magnitude bits and 4 fractional bits. It can represent numbers between 2047.9375 and -2048. The asymmetry between upper and lower bounds is due to the two's complement notation. A 16 bit unsigned fixed-point binary number with 4 fractional bits ranges between 4095.9375 and 0. Fixed point numbers can represent fractional powers of two exactly, but, like floating point numbers, cannot exactly represent fractional powers of 10. If exact fractional powers of ten are desired, then binary-coded decimal (BCD) format should be used. However, BCD does not make as efficient use of bits as two's complement notation, nor is it as computationally fast.
For example, one-tenth (.1) and one-hundredth (.01) can be represented only approximately by two's complement fixed point or floating point representations, while they can be represented exactly in BCD representations.
Integer fixed-point values are aways exactly represented so long as they fit within the range determined by the magnitude bits. This is in contrast to floating-point representations, which have separate exponent and value fields which can allow specification of integer values larger than the number of bits in the value field, causing the represented value to lose precision.
A common use for fixed-point BCD numbers is for storing monetary values, where the inexact values of floating-point numbers are often a liability. 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.
Very few computer languages include built-in support for fixed point values, because for most applications, floating-point representations are fast enough and accurate enough. Floating-point representations are easier than fixed-point representations, because they can handle a wider dynamic range and do not require programmers to specify the number of digits after the radix 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.
Examples still in common use
- 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.
- Tremor and Toast are software libraries that decode the Ogg Vorbis and GSM Full Rate audio formats respectively. These codecs use fixed-point arithmetic 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 produce output in real time.
- All 3D graphics engines on Sony's original PlayStation and Nintendo's Game Boy Advance and Nintendo DS video game systems use fixed-point arithmetic for the same reason as Tremor and Toast: to gain throughput on an architecture without an FPU.
- Fractint represents numbers as Q3:29 fixed-point numbers[1].
- VisSim supports a fixed-point block set to allow simulation and automatic code generation of fixed-point operations. Both word size and radix point can be specified on an operator basis.x