Fixed-Point and Floating-Point Numbers
In questa pagina 7
In questa pagina 6
Fixed point
Definition (fixed point). The binary point sits at a position fixed in advance: with fractional bits the represented number is the integer divided by . The resolution is . Range and precision are fixed.
Example. 8 bits of which 4 fractional: .
Example (sensor). A temperature sensor returns a 13-bit datum (sign, integer bits, fractional bits): resolution . The datum has integer part and fractional part , i.e. : . To display it in decimal the fractional part must be remapped to - (multiplying by : ) and then converted to BCD.
Negative values use two's complement (Signed Integer RepresentationsSigned integers on n bits: sign-magnitude, one's complement, two's complement and excess K, with representable ranges, computing the opposite, sign extension and a 4-bit comparison table.Signed Integer Representations →); additions and subtractions are the ordinary integer ones (Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic →, AddersHalf adder and full adder, generate-propagate-delete decomposition, the ripple-carry adder and its linear worst-case delay, the adder-subtractor, and the fast structures carry-lookahead, carry-bypass, carry-select and square-root carry-select with their delay formulas and optimal block sizes.Adders →). Simple, but range and precision are fixed.
Floating point
Definition (floating point, normalization). Scientific notation in base 2, . With the same number of bits it covers very small and very large values, with constant relative precision. A number is normalized by moving the point until a single 1 remains on its left: ; .
Example. .
IEEE 754 format
Formula (value of an IEEE 754 number).
Format Bits Sign Exponent Mantissa Bias Decimal digits single precision 32 1 8 23 127 about 7 double precision 64 1 11 52 1023 about 16
Example. : , , , so .
- The exponent is in excess 127 (Signed Integer RepresentationsSigned integers on n bits: sign-magnitude, one's complement, two's complement and excess K, with representable ranges, computing the opposite, sign extension and a 4-bit comparison table.Signed Integer Representations →): two positive floats compare as unsigned integers.
- The 1 before the point is not stored (hidden bit): the mantissa effectively has 24 bits of precision.
- Sign and magnitude are separate: and differ only in .
Decimal to IEEE 754 (single)
Example: .
- Sign: negative, .
- Binary: , , so .
- Normalize: .
- Exponent: .
- Mantissa: the bits after the point, padded to 23 with zeros: .
Grouping by 4 bits: .
Second example, a small number: , so , , : .
IEEE 754 to decimal
Example: . . , exponent ; value .
Special values
Property (special values).
Exponent Mantissa Meaning denormalized: any normalized (all 1) (e.g. , overflow) NaN (e.g. , )
Example. In single precision is and is .
Denormals fill the gap between and the smallest normalized number (gradual underflowvalues decrease toward zero losing precision one bit at a time instead of jumping suddenly to zero).
Range and precision (single)
Formula (largest and smallest normalized, spacing).
- Largest normalized: .
- Smallest positive normalized: (pattern ).
- Spacing of consecutive floats with exponent : . Near 1 it is (machine epsilondistance between 1 and the next float); near it is 2: from there on not all integers are representable.
- Overflow: exponent too large gives . Underflow: too small gives a denormal or 0.
Rounding and operations
- Extra bits are rounded; the default rounding is to nearest, ties to even. has a periodic mantissa and is rounded, so exactly.
- Addition: align the smaller exponent to the larger by shifting its mantissa right, add the mantissas, renormalize and round. With very different exponents the small number is lost: in single precision .
- Multiplication: multiply the mantissas and add the exponents (removing one bias: ).
- Addition is not associative: may differ from .
- In hardware the floating-point unit performs exponent alignment, mantissa addition, normalization and rounding (Arithmetic Logic UnitRole of the ALU, its inputs and the N Z C V flags, the 1-bit ALU built from AND, OR, a full adder and a multiplexer, the N-bit ALU with subtraction, NOR, set-less-than, zero and overflow detection, comparators, bus sharing with tri-state outputs, and the other computation units.Arithmetic Logic Unit →); in ARM it is a coprocessor with its own registers (VFP/NEON).
Common mistakes
- Forgetting the bias (writing instead of ) or applying it with the wrong sign when decoding.
- Including the hidden 1 in the 23 mantissa bits.
- Grouping hexadecimal starting from the mantissa instead of the sign bit: the 32 bits are taken in the order and split into 8 groups of 4.
Versione ripasso
Fixed point
fractional bits: value = integer , resolution . . Sensor: 13 bits (sign, 8 integer, 4 fractional), resolution ; . Negatives in two's complement (Signed Integer RepresentationsSigned integers on n bits: sign-magnitude, one's complement, two's complement and excess K, with representable ranges, computing the opposite, sign extension and a 4-bit comparison table.Signed Integer Representations →); add and subtract as integers (Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic →). Fixed range and precision.
Floating point
: wide range, constant relative precision. Normalize: .
| Format | Bits | Bias | |||
|---|---|---|---|---|---|
| single | 32 | 1 | 8 | 23 | 127 |
| double | 64 | 1 | 11 | 52 | 1023 |
Exponent in excess 127 (floats compare as unsigned); hidden bit: 24 bits of precision.
Conversions
- To IEEE (): ; ; ; ; result . : , .
- From IEEE (): , , : .
Special values
: ; : denormal ; : normal; : ; : NaN. .
Range and precision (single)
Max ; min normalized ; spacing ( near 1; 2 near , so ). Overflow gives , underflow a denormal or 0.
Operations
Rounding to nearest, ties to even (). Add: align exponents, add mantissas, renormalize, round. Multiply: multiply mantissas, add exponents minus 127. Not associative. FPU: Arithmetic Logic UnitRole of the ALU, its inputs and the N Z C V flags, the 1-bit ALU built from AND, OR, a full adder and a multiplexer, the N-bit ALU with subtraction, NOR, set-less-than, zero and overflow detection, comparators, bus sharing with tri-state outputs, and the other computation units.Arithmetic Logic Unit →.
Common mistakes: forgetting the bias; counting the hidden 1 in the 23 bits; hex grouping must follow the order .