Precisione, arrotondamento ed eccezioni in virgola mobile
Comprenda la rappresentazione IEEE 754, le modalità di arrotondamento e il modo in cui le unità FPU e SSE segnalano eccezioni come overflow, underflow e operazioni non valide.
Precisione, arrotondamento ed eccezioni in virgola mobile è una lezione Assembly Language & x86 Low-Level Systems Programming gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Assembly Language & x86 Low-Level Systems Programming, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Assembly Language & x86 Low-Level Systems Programming include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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, 4SSE 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, 4Floating-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 detectedA 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
Domande Frequenti
La lezione «Precisione, arrotondamento ed eccezioni in virgola mobile» è gratuita?
Sì — il testo completo di «Precisione, arrotondamento ed eccezioni in virgola mobile» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Assembly Language & x86 Low-Level Systems Programming, passa a CoddyKit PRO. Il corso Assembly Language & x86 Low-Level Systems Programming include 4 lezioni in totale.
Cosa imparerò in «Precisione, arrotondamento ed eccezioni in virgola mobile»?
Comprenda la rappresentazione IEEE 754, le modalità di arrotondamento e il modo in cui le unità FPU e SSE segnalano eccezioni come overflow, underflow e operazioni non valide. Eserciti Assembly Language & x86 Low-Level Systems Programming con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Assembly Language & x86 Low-Level Systems Programming?
Non è richiesta alcuna esperienza precedente. Assembly Language & x86 Low-Level Systems Programming su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Precisione, arrotondamento ed eccezioni in virgola mobile»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Assembly Language & x86 Low-Level Systems Programming?
Sì. Ogni lezione Assembly Language & x86 Low-Level Systems Programming include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Nozioni di base sulla programmazione della FPU x87
- Introduzione ai set di istruzioni SSE/AVX
- Vettorizzazione del codice con SIMD
- Precisione, arrotondamento ed eccezioni in virgola mobile