Microsoft MVP성태의 닷넷 이야기
닷넷: 2257. C# - float (단정도 실수) 저장소의 비트 구조 [링크 복사], [링크+제목 복사],
조회: 9706
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 8개 있습니다.)
.NET Framework: 539. C# - 부동 소수 계산 왜 이렇게 나오죠? (1)
; https://www.sysnet.pe.kr/2/0/10872

.NET Framework: 540. C# - 부동 소수 계산 왜 이렇게 나오죠? (2)
; https://www.sysnet.pe.kr/2/0/10873

.NET Framework: 608. double 값을 구할 때는 반드시 피연산자를 double로 형변환!
; https://www.sysnet.pe.kr/2/0/11055

개발 환경 구성: 440. C#, C++ - double의 Infinity, NaN 표현 방식
; https://www.sysnet.pe.kr/2/0/11896

기타: 85. 단정도/배정도 부동 소수점의 정밀도(Precision)에 따른 형변환 손실
; https://www.sysnet.pe.kr/2/0/13212

닷넷: 2257. C# - float (단정도 실수) 저장소의 비트 구조
; https://www.sysnet.pe.kr/2/0/13617

닷넷: 2258. C# - double (배정도 실수) 저장소의 비트 구조
; https://www.sysnet.pe.kr/2/0/13618

닷넷: 2259. C# - decimal 저장소의 비트 구조
; https://www.sysnet.pe.kr/2/0/13619




C# - float (단정도 실수) 저장소의 비트 구조

예전 글에서,

단정도/배정도 부동 소수점의 정밀도(Precision)에 따른 형변환 손실
; https://www.sysnet.pe.kr/2/0/13212

그림으로만 float (단정도 실수), double (배정도 실수)를 설명하고 지나갔는데요, 실제로 코드를 사용해서 이것을 들여다보겠습니다. ^^

우선, 단정도 실수의 분해는 다음의 그림에 따라,

[단정도 실수 - 그림 출처: https://ko.wikipedia.org/wiki/IEEE_754]
single_float_1.png

C# 7.0부터 리터럴에 "_" 밑줄 구분자를 임의의 위치에 추가할 수 있다는 점과 C# 7.2에 추가된 숫자 리터럴의 선행 밑줄을 통해 다음과 같은 표현으로 쉽게 분해할 수 있습니다.

namespace ConsoleApp1;

internal class Program
{
    static unsafe void Main(string[] args)
    {
        float f = -118.625f;

        Console.WriteLine($"{f}: sizeof(float): {sizeof(float)}");
        Console.WriteLine();

        byte* pFloat = (byte*)&f;
        PrintFloatFormat(pFloat);
    }

    private static unsafe void PrintFloatFormat(byte* pFloat)
    {
        uint data = *(uint*)pFloat;

        uint signBitMask  = 0b_1000_0000_0000_0000_0000_0000_0000_0000; // C# 7.2부터 컴파일 가능
        uint exponentMask = 0b_0111_1111_1000_0000_0000_0000_0000_0000;
        uint fractionMask = 0b_0000_0000_0111_1111_1111_1111_1111_1111;

        uint signBit = (data & signBitMask);
        uint exponentBits = (data & exponentMask);
        uint fractionBits = (data & fractionMask);

        Console.WriteLine(Convert.ToString((long)signBit, 2).PadLeft(32, '0').Separator(4, '_'));
        Console.WriteLine(Convert.ToString((long)exponentBits, 2).PadLeft(32, '0').Separator(4, '_'));
        Console.WriteLine(Convert.ToString((long)fractionBits, 2).PadLeft(32, '0').Separator(4, '_'));
    }
}

public static class StringExtension
{
    // ...[생략: 첨부 소스코드 참조]...
}

실행하면 다음과 같은 결과가 나오는데요,

-118.625: sizeof(float): 4

signBit:        1000_0000_0000_0000_0000_0000_0000_0000
exponentBits:   0100_0010_1000_0000_0000_0000_0000_0000
fractionBits:   0000_0000_0110_1101_0100_0000_0000_0000

왜 저런 결과가 나왔는지를 이해하기 위해서는 먼저 IEEE 754 표준에 따라 거치는 정규화 과정을 알아야 합니다. 즉, 위의 경우 "-118.625"는 다음과 같은 정규화 과정을 거칩니다.

2진수 변환)
-118.625 ==> 1110110.101

지수 표현)
1110110.101 ==> 1.110110101 * 26
    지수부: 6
    가수부: 1.110110101

그런데, 이상하군요? ^^ 위에서 지수 6은 2진수로 표현하면 0110인데, 어떻게 코드에서 출력한 exponentBits(100_0010_1), 즉 133이 되었을까요? 그것은 지수부를 표현하는 8비트를 절반 나누어 음의 지수와 양의 지수로 쓰기 때문입니다. 8비트니까, 0 ~ 255까지의 값을 표현할 수 있는데요, 중간인 127을 2의 0승으로 두고 그것보다 작으면 음, 크면 양의 제곱으로 처리를 하는 방식입니다. 따라서 여기서 지수는 6이므로 +127을 해서 133을 exponentBits에 저장한 것이고 그래서 100_0010_1 값이 나온 것입니다.

가수부의 처리도 재미있습니다. 위의 경우 보존해야 할 값은 1110110101이지만 정규화했을 때 언제나 앞자리 하나는 1이므로 (비트를 절약하기 위해) 그 부분은 절삭하고 (1)110110101의 110110101 값만 가수로 저장합니다.

대충 이해가 되시죠? ^^




그렇다면, 위와 같이 분해된 정보로부터 원래의 실숫값을 복원하는 것도 가능합니다.

우선, 가수부의 110110101에서 생략된 가장 상위의 1을 복원시켜줍니다.

1_1011_0101 ==> 11_1011_0101

그다음, 지수부의 133을 원래의 지수로 만들어줍니다. 이를 위해 (반대로) 127을 빼주면 됩니다.

6 = 133 - 127

이렇게 구한 값들을 통해 처음의 float 값으로 복원할 수 있습니다.

1.110110101 * 2E6

==> 1110110.101

10진수로 ==> 118.625

==> sign 비트 적용
-118.625

(118.625의 2진수 값이 실제로 1110110.101인지 진법 계산을 해보면 나오겠지만, 간편하게 온라인 진법 계산기를 사용해 확인할 수도 있습니다.)

이 과정을 코드로 표현하면 대충 다음과 같이 만들 수 있습니다. ^^

{
    bool minus = signBit != 0;
    uint exponents = exponentBits >> 23;

    // 삭제된 1을 복원하고,
    uint fractions = fractionBits | 0b_0000_0000_1000_0000_0000_0000_0000_0000;

    int shift = (int)exponents - 127;

    // (삭제된 1비트의 복원으로 9비트가 아닌) 8비트만 shift 시키면 원래의 가수로 변환
    fractions = fractions << 8;
    string mantissa = Convert.ToString(fractions, 2).TrimEnd('0');
    Console.WriteLine($"{(minus ? "-" : "")}{mantissa} * 2E{shift}");

    mantissa = Convert.ToString(fractions, 2).TrimEnd('0');
    mantissa = MarkDecimalPoint(mantissa, shift);
    Console.WriteLine($"{(minus ? "-" : "")}{mantissa}");

    decimal value = Recomposite(mantissa) * (minus ? -1 : 1);
    Console.WriteLine($"{value}, (float: {(float)value})");
}

private static decimal Recomposite(string mantissa)
{
    int pos = mantissa.IndexOf('.');

    string left = mantissa;
    string right = "";

    if (pos != -1)
    {
        left = mantissa[0..pos];
        right = mantissa[(pos + 1)..];
    }

    decimal integer = parseInteger(left);
    decimal decimalPart = parseDecimalPart(right);

    return integer + decimalPart;
}

private static decimal parseInteger(string left)
{
    decimal result = 0;
    decimal pow2 = 1;

    foreach (char ch in left.Reverse())
    {
        result = result + ((ch == '1') ? 1 : 0) * pow2;
        pow2 *= 2;
    }

    return result;
}

private static decimal parseDecimalPart(string right)
{
    decimal result = 0;
    decimal pow2 = 1m / 2m;

    foreach (char ch in right)
    {
        result = result + ((ch == '1') ? 1 : 0) * pow2;
        pow2 /= 2m;
    }

    return result;
}

private static string MarkDecimalPoint(string mantissa, int shift)
{
    if (shift >= 0)
    {
        shift++;
        mantissa = mantissa.PadRight(shift, '0');
    }
    else
    {
        string decimalPart = new string('0', -shift - 1);
        mantissa = "0." + decimalPart + mantissa;
        return mantissa;
    }

    if (mantissa.Length == shift)
    {
        return mantissa;
    }

    string left = mantissa[0..shift];
    string right = mantissa[shift..];

    return $"{left}.{right}";
}

이전 코드와 합쳐서 실행해 보면 이런 결과를 얻을 수 있습니다.

-118.625 (decimal: -118.625): sizeof(float): 4

signBit:        1000_0000_0000_0000_0000_0000_0000_0000
exponentBits:   0100_0010_1000_0000_0000_0000_0000_0000
fractionBits:   0000_0000_0110_1101_0100_0000_0000_0000

-1110110101 * 2E6
-1110110.101
-118.625, (float: -118.625)

잘 복원이 되었죠? ^^

(첨부 파일은 이 글의 예제 코드를 포함>합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 5/6/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 106  107  [108]  109  110  111  112  113  114  115  116  117  118  119  120  ...
NoWriterDateCnt.TitleFile(s)
11224정성태6/13/201718207.NET Framework: 661. Json.NET의 DeserializeObject 수행 시 속성 이름을 동적으로 바꾸는 방법파일 다운로드1
11223정성태6/12/201716839개발 환경 구성: 318. WCF Service Application과 WCFTestClient.exe
11222정성태6/10/201720565오류 유형: 399. WCF - A property with the name 'UriTemplateMatchResults' already exists.파일 다운로드1
11221정성태6/10/201717557오류 유형: 398. Fakes - Assembly 'Jennifer5.Fakes' with identity '[...].Fakes, [...]' uses '[...]' which has a higher version than referenced assembly '[...]' with identity '[...]'
11220정성태6/10/201722920.NET Framework: 660. Shallow Copy와 Deep Copy [1]파일 다운로드2
11219정성태6/7/201718237.NET Framework: 659. 닷넷 - TypeForwardedFrom / TypeForwardedTo 특성의 사용법
11218정성태6/1/201721068개발 환경 구성: 317. Hyper-V 내의 VM에서 다시 Hyper-V를 설치: Nested Virtualization
11217정성태6/1/201716943오류 유형: 397. initerrlog: Could not open error log file 'C:\...\MSSQL12.MSSQLSERVER\MSSQL\Log\ERRORLOG'
11216정성태6/1/201719051오류 유형: 396. Activation context generation failed
11215정성태6/1/201720005오류 유형: 395. 관리 콘솔을 실행하면 "This app has been blocked for your protection" 오류 발생 [1]
11214정성태6/1/201717708오류 유형: 394. MSDTC 서비스 시작 시 -1073737712(0xC0001010) 오류와 함께 종료되는 문제 [1]
11213정성태5/26/201722502오류 유형: 393. TFS - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
11212정성태5/26/201721838오류 유형: 392. Windows Server 2016에 KB4019472 업데이트가 실패하는 경우
11211정성태5/26/201720865오류 유형: 391. BeginInvoke에 전달한 람다 함수에 CS1660 에러가 발생하는 경우
11210정성태5/25/201721315기타: 65. ActiveX 없는 전자 메일에 사용된 "개인정보 보호를 위해 암호화된 보안메일"의 암호화 방법
11209정성태5/25/201768255Windows: 143. Windows 10의 Recovery 파티션을 삭제 및 새로 생성하는 방법 [16]
11208정성태5/25/201727983오류 유형: 390. diskpart의 set id 명령어에서 "The specified type is not in the correct format." 오류 발생
11207정성태5/24/201728311Windows: 142. Windows 10의 복구 콘솔로 부팅하는 방법
11206정성태5/24/201721595오류 유형: 389. DISM.exe - The specified image in the specified wim is already mounted for read/write access.
11205정성태5/24/201721275.NET Framework: 658. C#의 tail call 구현은? [1]
11204정성태5/22/201730813개발 환경 구성: 316. 간단하게 살펴보는 Docker for Windows [7]
11203정성태5/19/201718744오류 유형: 388. docker - Host does not exist: "default"
11202정성태5/19/201719810오류 유형: 387. WPF - There is no registered CultureInfo with the IetfLanguageTag 'ug'.
11201정성태5/16/201722577오류 유형: 386. WPF - .NET 3.5 이하에서 TextBox에 한글 입력 시 TextChanged 이벤트의 비정상 종료 문제 [1]파일 다운로드1
11200정성태5/16/201719375오류 유형: 385. WPF - 폰트가 없어 System.IO.FileNotFoundException 예외가 발생하는 경우
11199정성태5/16/201721183.NET Framework: 657. CultureInfo.GetCultures가 반환하는 값
... 106  107  [108]  109  110  111  112  113  114  115  116  117  118  119  120  ...