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

Gleitkommagenauigkeit, Rundung und Ausnahmen

Verstehen Sie die Darstellung nach IEEE 754, Rundungsmodi und wie FPU- und SSE-Einheiten Ausnahmen wie Überlauf, Unterlauf und ungültige Operationen signalisieren.

Gleitkommagenauigkeit, Rundung und Ausnahmen ist eine kostenlose Assembly Language & x86 Low-Level Systems Programming-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Assembly Language & x86 Low-Level Systems Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Assembly Language & x86 Low-Level Systems Programming-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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

Häufig gestellte Fragen

Ist die Lektion „Gleitkommagenauigkeit, Rundung und Ausnahmen“ kostenlos?

Ja — der vollständige Text von „Gleitkommagenauigkeit, Rundung und Ausnahmen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Assembly Language & x86 Low-Level Systems Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Assembly Language & x86 Low-Level Systems Programming-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Gleitkommagenauigkeit, Rundung und Ausnahmen“?

Verstehen Sie die Darstellung nach IEEE 754, Rundungsmodi und wie FPU- und SSE-Einheiten Ausnahmen wie Überlauf, Unterlauf und ungültige Operationen signalisieren. Du übst Assembly Language & x86 Low-Level Systems Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Assembly Language & x86 Low-Level Systems Programming zu starten?

Keine Vorkenntnisse erforderlich. Assembly Language & x86 Low-Level Systems Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Gleitkommagenauigkeit, Rundung und Ausnahmen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Assembly Language & x86 Low-Level Systems Programming-Lektion Code schreiben und ausführen?

Ja. Jede Assembly Language & x86 Low-Level Systems Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Grundlagen der x87-FPU-Programmierung
  2. Einführung in SSE/AVX-Instruktionssätze
  3. Code mit SIMD vektorisieren
  4. Gleitkommagenauigkeit, Rundung und Ausnahmen
← Zurück zu Assembly Language & x86 Low-Level Systems Programming