0Pricing
Assembly Language & x86 Low-Level Systems Programming · 강의

부동소수점 정밀도, 반올림 및 예외

IEEE 754 표현과 반올림 모드를 이해하고, FPU와 SSE 장치가 오버플로, 언더플로, 잘못된 연산과 같은 예외를 알리는 방식을 배웁니다.

부동소수점 정밀도, 반올림 및 예외은(는) CoddyKit의 무료 Assembly Language & x86 Low-Level Systems Programming 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Assembly Language & x86 Low-Level Systems Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

IEEE 754 Format

x86 floating-point follows the IEEE 754 standard. A number is stored as sign, exponent, and mantissa:

  • Single (32-bit): 1 + 8 + 23
  • Double (64-bit): 1 + 11 + 52
  • Extended (80-bit): used internally by the x87 FPU

Why Precision Matters

Most decimal fractions cannot be represented exactly in binary. For example 0.1 + 0.2 does not equal exactly 0.3. Accumulated rounding error is a core hazard in numerical code.

The Four Rounding Modes

IEEE 754 defines four rounding modes:

  • Round to nearest, ties to even (default)
  • Round toward negative infinity (floor)
  • Round toward positive infinity (ceil)
  • Round toward zero (truncate)

Controlling Rounding on x87

The x87 control word holds the rounding-control (RC) bits. You read it with fstcw, modify, then load with fldcw.

sub esp, 4
fstcw [esp]          ; store control word
or word [esp], 0x0C00 ; RC = 11 -> round toward zero
fldcw [esp]          ; load it back
add esp, 4

SSE MXCSR Register

SSE has its own control/status register, MXCSR. It holds rounding-control bits plus exception masks and flags. Manage it with ldmxcsr and stmxcsr.

sub rsp, 4
stmxcsr [rsp]        ; save MXCSR
ldmxcsr [rsp]        ; restore MXCSR
add rsp, 4

Floating-Point Exceptions

The standard defines six exceptions:

  • Invalid (e.g. 0/0, sqrt of negative)
  • Denormal operand
  • Divide by zero
  • Overflow
  • Underflow
  • Inexact (rounding occurred)

Masked vs Unmasked

Each exception can be masked. A masked exception produces a default result (like infinity or NaN) and sets a flag. An unmasked exception raises a CPU trap so your handler can intervene.

Special Values: NaN and Infinity

IEEE 754 reserves bit patterns for special values:

  • +Inf / -Inf from overflow or divide-by-zero
  • NaN (Not a Number) from invalid operations

Any comparison with NaN is false — even NaN == NaN.

Detecting NaN

Because NaN is unordered, you detect it with an unordered compare. In SSE, ucomiss sets the parity flag when an operand is NaN.

ucomiss xmm0, xmm0   ; compare value with itself
jp is_nan            ; PF set => NaN detected

A Runnable C Demonstration

This self-contained C program shows that floating-point addition is not exact and detects a NaN.

#include <stdio.h>
#include <math.h>
int main(void) {
    double a = 0.1 + 0.2;
    printf("0.1+0.2 = %.17f\n", a);
    printf("equals 0.3? %d\n", a == 0.3);
    double nan_val = 0.0 / 0.0;
    printf("isnan? %d\n", isnan(nan_val));
    return 0;
}

Practical Advice

To write robust floating-point code:

  • Never compare floats with ==; use an epsilon tolerance
  • Be aware of accumulation order in sums
  • Keep the default round-to-nearest unless you have a reason
  • Check status flags after risky operations

Quick Check

Test your floating-point knowledge.

Recap

You explored floating-point behavior:

  • IEEE 754 defines single, double, and 80-bit extended formats
  • Four rounding modes are configured via the x87 control word or MXCSR
  • Six exceptions can be masked (default result + flag) or unmasked (trap)
  • NaN and infinity are special values; NaN compares false to everything

자주 묻는 질문

“부동소수점 정밀도, 반올림 및 예외” 강의는 무료인가요?

네 — “부동소수점 정밀도, 반올림 및 예외” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Assembly Language & x86 Low-Level Systems Programming 강의 전체를 잠금 해제할 수 있습니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“부동소수점 정밀도, 반올림 및 예외”에서 뭘 배우나요?

IEEE 754 표현과 반올림 모드를 이해하고, FPU와 SSE 장치가 오버플로, 언더플로, 잘못된 연산과 같은 예외를 알리는 방식을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Assembly Language & x86 Low-Level Systems Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Assembly Language & x86 Low-Level Systems Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Assembly Language & x86 Low-Level Systems Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“부동소수점 정밀도, 반올림 및 예외” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Assembly Language & x86 Low-Level Systems Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Assembly Language & x86 Low-Level Systems Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. x87 FPU 프로그래밍 기초
  2. SSE/AVX 명령어 집합 소개
  3. SIMD를 사용한 코드 벡터화
  4. 부동소수점 정밀도, 반올림 및 예외
← Assembly Language & x86 Low-Level Systems Programming(으)로 돌아가기