Salta al contenuto
Note per Studenti Fixed-Point and Floating-Point Numbers Report

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 FF fractional bits the represented number is the integer divided by 2F2^F. The resolution is 1/2F1/2^F. Range and precision are fixed.

Example. 8 bits of which 4 fractional: 0101,1000=5.50101{,}1000=5.5.

Example (sensor). A temperature sensor returns a 13-bit datum (sign, 88 integer bits, 44 fractional bits): resolution 1/24=0.0625 ∘C1/2^4=0.0625\ ^\circ\mathrm C. The datum 0 0001 1001 10100\,0001\,1001\,1010 has integer part 0001 1001=250001\,1001=25 and fractional part 10102=101010_2=10, i.e. 10/16=0.62510/16=0.625: 25.625 ∘C25.625\ ^\circ\mathrm C. To display it in decimal the fractional part must be remapped to 00-9999 (multiplying by 100/16=6.25100/16=6.25: 10⋅6.25=62.5≈6210\cdot6.25=62.5\approx62) 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, ± 1.m⋅2E\pm\,1.m\cdot2^{E}. 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: 1101.01=1.10101⋅231101.01=1.10101\cdot2^3; 0.00101=1.01⋅2−30.00101=1.01\cdot2^{-3}.

Example. 101.12=5.5=1.0112⋅22101.1_2=5.5=1.011_2\cdot2^2.

IEEE 754 format

Formula (value of an IEEE 754 number). x=(−1)S⋅1.M⋅2 E−biasx=(-1)^S\cdot1.M\cdot2^{\,E-\text{bias}}

Format Bits Sign SS Exponent EE Mantissa MM Bias Decimal digits
single precision 32 1 8 23 127 about 7
double precision 64 1 11 52 1023 about 16

Example. 42C8000016\mathrm{42C80000}_{16}: S=0S=0, E=133E=133, 1.M=1.100121.M=1.1001_2, so x=1.10012⋅2133−127=11001002=100x=1.1001_2\cdot2^{133-127}=1100100_2=100.

Decimal to IEEE 754 (single)

Example: −12.375-12.375.

  1. Sign: negative, S=1S=1.
  2. Binary: 12=110012=1100, 0.375=0.0110.375=0.011, so 1100.0111100.011.
  3. Normalize: 1.100011⋅231.100011\cdot2^3.
  4. Exponent: E=3+127=130=1000 0010E=3+127=130=1000\,0010.
  5. Mantissa: the bits after the point, padded to 23 with zeros: 1000 1100 0000 0000 0000 0001000\,1100\,0000\,0000\,0000\,000.

1⏟S 1000 0010⏟E 1000 1100 0000 0000 0000 000⏟M\underbrace{1}_{S}\ \underbrace{1000\,0010}_{E}\ \underbrace{1000\,1100\,0000\,0000\,0000\,000}_{M}

Grouping by 4 bits: 1100 0001 0100 0110 0000⋯=C1460000161100\,0001\,0100\,0110\,0000\dots=\text{C1460000}_{16}.

Second example, a small number: 0.15625=0.001012=1.01⋅2−30.15625=0.00101_2=1.01\cdot2^{-3}, so S=0S=0, E=124=0111 1100E=124=0111\,1100, M=0100…0M=0100\dots0: 3E20000016\text{3E200000}_{16}.

IEEE 754 to decimal

Example: 42C8000016\text{42C80000}_{16}. 0100 0010 1100 1000 0000⋯=0⏟S 1000 0101⏟E 1001 0000…⏟M0100\,0010\,1100\,1000\,0000\dots=\underbrace{0}_{S}\ \underbrace{1000\,0101}_{E}\ \underbrace{1001\,0000\dots}_{M}. E=133E=133, exponent 133−127=6133-127=6; value +1.10012⋅26=11001002=100+1.1001_2\cdot2^6=1100100_2=100.

Special values

Property (special values).

Exponent EE Mantissa MM Meaning
00 00 ±0\pm0
00 ≠0\ne0 denormalized: (−1)S⋅0.M⋅2−126(-1)^S\cdot0.M\cdot2^{-126}
1…2541\dots254 any normalized
255255 (all 1) 00 ±∞\pm\infty (e.g. 1/01/0, overflow)
255255 ≠0\ne0 NaN (e.g. 0/00/0, −1\sqrt{-1})

Example. In single precision 7F80000016\mathrm{7F800000}_{16} is +∞+\infty and 0000000016\mathrm{00000000}_{16} is +0+0.

Denormals fill the gap between 00 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: (2−2−23)⋅2127≈3.4⋅1038(2-2^{-23})\cdot2^{127}\approx3.4\cdot10^{38}.
  • Smallest positive normalized: 2−126≈1.18⋅10−382^{-126}\approx1.18\cdot10^{-38} (pattern 0080000016\mathrm{00800000}_{16}).
  • Spacing of consecutive floats with exponent ee: 2e−232^{e-23}. Near 1 it is 2−23≈1.2⋅10−72^{-23}\approx1.2\cdot10^{-7} (machine epsilondistance between 1 and the next float); near 224≈1.7⋅1072^{24}\approx1.7\cdot10^7 it is 2: from there on not all integers are representable.
  • Overflow: exponent too large gives ∞\infty. Underflow: too small gives a denormal or 0.

Rounding and operations

Common mistakes

  • Forgetting the bias (writing E=3E=3 instead of 130130) 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 S,E,MS,E,M and split into 8 groups of 4.

Versione ripasso

Fixed point

FF fractional bits: value = integer /2F/2^F, resolution 1/2F1/2^F. 0101,1000=5.50101{,}1000=5.5. Sensor: 13 bits (sign, 8 integer, 4 fractional), resolution 0.0625 ∘C0.0625\ ^\circ\mathrm C; 0 0001 1001 1010=25+10/16=25.625 ∘C0\,0001\,1001\,1010=25+10/16=25.625\ ^\circ\mathrm C. 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

±1.m⋅2E\pm1.m\cdot2^E: wide range, constant relative precision. Normalize: 1101.01=1.10101⋅231101.01=1.10101\cdot2^3.

x=(−1)S⋅1.M⋅2E−biasx=(-1)^S\cdot1.M\cdot2^{E-\text{bias}}

Format Bits SS EE MM 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 (−12.375-12.375): S=1S=1; 1100.011=1.100011⋅231100.011=1.100011\cdot2^3; E=130=1000 0010E=130=1000\,0010; M=1000 1100…M=1000\,1100\dots; result C146000016\text{C1460000}_{16}. 0.15625=1.01⋅2−30.15625=1.01\cdot2^{-3}: E=124E=124, 3E20000016\text{3E200000}_{16}.
  • From IEEE (42C8000016\text{42C80000}_{16}): S=0S=0, E=133E=133, M=1001…M=1001\dots: 1.10012⋅26=1001.1001_2\cdot2^{6}=100.

Special values

E=0,M=0E=0,M=0: ±0\pm0; E=0,M≠0E=0,M\ne0: denormal 0.M⋅2−1260.M\cdot2^{-126}; E=1…254E=1\dots254: normal; E=255,M=0E=255,M=0: ±∞\pm\infty; E=255,M≠0E=255,M\ne0: NaN. 7F80000016=+∞\mathrm{7F800000}_{16}=+\infty.

Range and precision (single)

Max (2−2−23)2127≈3.4⋅1038(2-2^{-23})2^{127}\approx3.4\cdot10^{38}; min normalized 2−126≈1.18⋅10−382^{-126}\approx1.18\cdot10^{-38}; spacing 2e−232^{e-23} (≈1.2⋅10−7\approx1.2\cdot10^{-7} near 1; 2 near 2242^{24}, so 224+1=2242^{24}+1=2^{24}). Overflow gives ∞\infty, underflow a denormal or 0.

Operations

Rounding to nearest, ties to even (0.1+0.2≠0.30.1+0.2\ne0.3). 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 S,E,MS,E,M.

Teoria collegata