Representing and Manipulating Floating Points
실수 숫자들이 컴퓨터 내에서 어떻게 표현되고 계산되는지
How to represent fractional values with finite number of bits?
정해진 bit 내에서 소수점 숫자를 표현?
Fractional Binary Numbers
Fractional Binary Numbers (2)
• Divide by 2 by shifting right
수 << 1
• Multiply by 2 by shifting left
Representable numbers
• Can only exactly represent numbers of the form x * 2^k
Ex. 5 + 3/4
• Other numbers have repeating bit representations
Ex. 1/3
1. Fixed-Point Representation (1)
p.q Fixed-point representation
Example: 17.14 fixed-point representation
- 1 bit for sign bit
- 17 bits for the integer part // 정수 부분
- 14 bits for the fractional part // 실수 부분
- An integer x represents the real number x / 2^14
- Maximum value: (2^31 - 1) / 2^14 ≒ 131071.999
Properties 특성
x, y: fixed-point number, n: integer, f = 1 << q라 가정
• Convert n to fixed point: n * f
5를 17.14 fixed point로 바꾸면 5 * (1 << 14)
000 ... 001010000..000
• Add x and y : x + y
• Subtract y from x : x - y
• Add x and n : x + n * f
• Multiply x by n : x * n
• Divide x by n : x / n
Pros
• Can use integer arithmetic to manipulate
Cons
• Cannot represent wide ranges of numbers
2. IEEE standard 754
Normalized Form : -2.34 × 10^56
반드시 1로 시작
소숫점 표현
- Numerical form
• MSB is sign bit
• exp field encodes E (Exponent)
• frac field encodes M (Mantissa)
• Single precision: 8 exp bits, 23 frac bits (32 bits total) // c에서 float형
• Double precision: 11 exp bits, 52 frac bits (64 bits total) // c에서 double형
1. Normalized Values
Condition: exp =/= 000…0 and exp =/= 111…1
Exponent coded as biased value
• E = Exp - Bias // unsigned로 음수값도 표현
• Exp : unsigned value denoted by exp // 2의보수체계처럼이 아닌 u로 해석
• Bias : Bias to represent a negative value
1-1 Single precision: 127 (Exp : 1~254, E : -126~127) // 255는 111111111로 사용 안 함
1-2 Double precision: 1023 (Exp : 1~2046, E : -1022~1023)
Significand coded with implied leading 1
• M = 1.xxx…x
- Minimum when 000…0 (M = 1.0)
- Maximum when 111…1 (M = 2.0 - ε)
Denormalized Values
Condition: exp = 000…0
Cases
• exp = 000…0, frac = 000…0
- 0 값 표현, +0 -0 있음
• exp = 000…0, frac =/= 000…0
- 0.0에 아주 가까운 수
Condition: exp = 111…1
Cases
• exp = 111…1, frac = 000…0
- Represents value + or ∞(infinity)
- Operation that overflows
- Both positive and negative
- e.g. 1.0/0.0 = -1.0/-0.0 = +∞, 1.0/-0.0 = -∞
• exp = 111…1, frac =/= 000…0
- Not-a-Number (NaN)
- Represents case when no numeric value can be determined
- e.g., sqrt(-1), ∞ - ∞, ∞ x 0, ...
Tiny FP Example
8bit
1 / 4 / 3
8-bit floating point representation
• The sign bit is in the most significant bit
• The next four bits are the exp, with a bias of 7
• The last three bits are the frac
IEEE와 동일
FP zero same as integer zero • All bits = 0
Can (almost) use unsigned integer comparison
• Must first compare sign bits • Must consider -0 = 0
• NaNs problematic
- Will be greater than any other values
• Otherwise OK
- Denormalized vs. normalized - Normalized vs. Infinity
'전공 > 시스템프로그래밍' 카테고리의 다른 글
두번째 과제 (0) | 2022.11.05 |
---|---|
[09] ASM2 : Control Flow (1) | 2022.10.28 |
[03-3] Integer(정수) / Representing and Manipulating Integers (0) | 2022.10.28 |
[05] BYTE ORDERING (0) | 2022.10.27 |
[08] ASM1 (0) | 2022.10.25 |