Fixed-point arithmetic: Difference between revisions

Content deleted Content added
mNo edit summary
No edit summary
Line 3:
{{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. TheFixed-point bits to the left of the radix pointnumbers are magnitudeuseful bitsfor that represent integer values, the bits to the right of the radix point representrepresenting fractional values. in Eachnative fractionaltwo's bitcomplement representsformat an inverse power of 2. Thusif the firstexecuting fractionalprocessor bithas isno 1/2,[[floating thepoint]] secondunit is(FPU) 1/4,or theif third is 1/8 and sofixed-point on.provides
These 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 an
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: <math>\left({bm-1}\right)^2-1 + {bf^2-1 \over bf^2}\mbox{ where } bm \mbox{ is bits of magnitude and }bf \mbox{is bits of fraction}</math>.
For example, a 16 bit signed fixed-point binary number with 4 bits after the decimalradix 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 representrepresented valuesso uplong toas they fit within the maximum/minimumrange determined by the number of magnitude bits. This is in contrast to [[floating point|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