부동소수점

  • 다양한 방식으로 소수를 표현할 수 있어서 IEEE 표준을 정함. (IEEE 754-1985)
  • '부동소수점’의 '부동’은 不動이 아니라 'Floating’이다.

부동소수점 인코딩

  • 정규화된 표현: +1.xxxxtwo×2yyyy+1.xxx \dots x_{two} \times 2^{yyy \dots y}
  • 32-bit word를 3개의 필드로 나누면:
    | S | Exponent | Mantissa (fraction |
    31  30         23                   0
    
    • Sign (1 bit): 1 is negative, 0 positive
    • Exponent (8 bit): yy
    • Mantissa (23 bit): xx
  • Exponent 필드에는 biased notation을 사용한다:
    • 21,Exponent=1+127=128=1000 0000two2^1, \text{Exponent} = 1 + 127 = 128 = 1000 \ 0000_{two}
    • 2127,Exponent=127+127=254=1111 1110two2^{127}, \text{Exponent} = 127 + 127 = 254 = 1111 \ 1110_{two}
  • 그래서 정리하면: (1)S×(1.Mantissa)×2(Exponent127)(-1)^S \times (1.\text{Mantissa}) \times 2^{(\text{Exponent} - 127)}
    • 2003.0=0111 1101 0011two=(1)0×1.1111010011×2102003.0 = 0111 \ 1101 \ 0011_{two} = (-1)^0 \times 1.1111010011 \times 2^10
      • S=0S = 0
      • Exponent=E+Bias=10+127=137=1000 1001two\text{Exponent} = E + \text{Bias} = 10 + 127 = 137 = 1000 \ 1001_{two}
      • Mantissa=1111010011=111 1010 0110 0000 0000 0000two\text{Mantissa} = 1111010011 = 111 \ 1010 \ 0110 \ 0000 \ 0000 \ 0000_{two}
      • 2003.0=0 10001001 11110100110000000000000two2003.0 = 0 \ 10001001 \ 11110100110000000000000_{two}
  • 0을 표현할 수 없어서 Exponent와 Mantissa가 모두 0이면 0으로 특수하게 취급.

Binary FP to Decimal

0 | 0110 1000 | 101 0101 0100 0011 0100 0010
  • Sign: 00이므로 positive
  • Exponent: 0110 1000two=104ten0110 \ 1000_{two} = 104_{ten}
    • Bias adjustment: Exponent127=104127=23\text{Exponent} - 127 = 104 - 127 = -23
  • Mantissa: 1.101010101000011010000101.10101010100001101000010 (첫 자리가 1이므로 정규화된 표현)
1.10101010100001101000010=1+(1×21)+(0×22)+(1×23)+(0×24)+=1+21+23+25+27+29+214+215+217+222=1.0+0.666115 \begin{aligned} 1.10101010100001101000010 &= 1 + (1 \times 2^{-1}) + (0 \times 2^{-2}) + (1 \times 2^{-3}) + (0 \times 2^{-4}) + \dots \\ &=1 + 2^{-1} + 2^{-3} + 2^{-5} + 2^{-7} + 2^{-9} + 2^{-14} + 2^{-15} + 2^{-17} + 2^{-22} \\ &=1.0 + 0.666115 \end{aligned}
Mantissa×2Bias=1.666115ten×2231.986×107 \text{Mantissa} \times 2^{Bias} = 1.666115_{ten} \times 2^{-23} \approx 1.986 \times 10^{-7}

부동소수점 오류

  • 무한 소수가 되는 2진 분수를 정확히 표현하지 않고 10진수 근삿값으로 표현한다.[1]
  • 따라서 실제 값과 오차가 발생한다. 실수는 동등 연산(==)으로 비교하면 안된다.
  • round() 등의 함수로 미리 소수를 가공한 뒤 연산하자.

참고자료

이 문서를 인용한 문서


  1. 정겨울, “파이썬에서 부동소수점 오차 해결하기”, 2020. ↩︎