prev up next


Previous: Ganze Zahlen (byte, short, Up: Einfache Datentypen Next: Boolean (boolean)

Subsections


Gleitkommazahlen (float, double)

Die Typen float und double dienen zur Speicherung von Gleitkommazahlen nach dem IEEE-Standard 754-1985 in der Form Vorzeichen, Exponent, Mantisse.

Codierung

Bei der Codierung für 32 Bits (float) benötigt das Vorzeichen 1 Bit, der Exponent 8 Bits, die Mantisse 23 Bits.
X XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX
Vorzeichen s Exponent e Mantisse f

$\displaystyle\mbox{Wert}$ = $\displaystyle\left\{ \begin{array}
{lll}
(-1)^{s} \times 2^{e - 127} \times 1.f...
 ...^{-126} \times 0.f& \mbox{ falls } e = 0 &\mbox{ (subnormal)}\end{array}\right.$

Bei einer normalisierten Darstellung liegt der Wert der Mantisse 1.f im Intervall [1,2[ .

Hierbei haben Dualzahlen nach dem Komma

0.d- 1d- 2d- 3...d- k$\displaystyle\mbox{ die Bedeutung }$$\displaystyle\sum_{i = -k}^{-1}$di · 2i

Algorithmus dezimal $\rightarrow$ dual:

Sei Dezimalzahl x gegeben. Bestimme größte 2 -er Potenz 2d mit 2d $\leq$ x . Setze f = (x - 2d)/2d. Offenbar gilt

x = (1 + f ) · 2d$\displaystyle\mbox{ mit }$0 $\displaystyle\leq$ f < 1.

Bestimme die Dualzahl-Bits von f durch

for (i = 0; i < 23; i++) {
   f = f * 2.0;
   if (f >= 1.0) {IO.print('1'); f = f - 1.0;}
      else IO.print('0');
}

Beispiel:
Sei x = 13.5 gegeben. Als Codierung ergibt sich

s = 0   
f = (13.5 - 23)/23 = 0.6875 = $\displaystyle{\textstyle\frac{1}{2}}$ + $\displaystyle{\textstyle\frac{1}{8}}$ + $\displaystyle{\textstyle\frac{1}{16}}$   
e = 3 + 127 = 130   
0 10000010 10110000000000000000000
Vorzeichen Exponent Mantisse

Bei einer subnormalisierten Darstellung liegt der Wert der Mantisse 0.f im Intervall [0,1[ .

Exponent e = 0 und Mantisse f = 0 repräsentieren die vorzeichenbehaftete Null. Exponent e = 255 und Mantisse f = 0 repräsentieren das vorzeichenbehaftete Unendlich( + $\infty$, - $\infty$ ). Exponent e = 255 und Mantisse f $\neq$ 0 repräsentieren die undefinierte Zahl NaN (not a number).

Beispiel: 2- 130 = 1/16 · 2- 126

Daraus folgt

s = 0   
f = 0.0625 = 1/16   
e = 0   

Codierung

0 00000000 00010000000000000000000
Vorzeichen Exponent Mantisse

Die größte darstellbare positive Zahl liegt knapp unter

2 · 2254 - 127 = 2128 $\displaystyle\approx$ 1038

0 11111110 11111111111111111111111
Vorzeichen Exponent Mantisse

Die kleinste darstellbare positive Zahl lautet

2- 23 · 2- 126 = 2- 149 $\displaystyle\approx$ 10- 45

0 00000000 00000000000000000000001
Vorzeichen Exponent Mantisse

Bei der Codierung für 64 Bits (double) benötigt das Vorzeichen 1 Bit, der Exponent 11 Bits, die Mantisse 52 Bits.

X XXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Vorzeichen s Exponent e Mantisse f

$\displaystyle\mbox{Wert}$ = $\displaystyle\left\{ \begin{array}
{lll}
(-1)^{s} \times 2^{e - 1023} \times 1....
 ...-1022} \times 0.f& \mbox{ falls } e = 0& \mbox{ (subnormal) }\end{array}\right.$

Damit liegt die größte darstellbare positive Zahl knapp unter

2 · 22046 - 1023 $\displaystyle\approx$ 10308

Die kleinste darstellbare positive Zahl lautet

2- 52 · 2- 1022 = 2- 1074 $\displaystyle\approx$ 10- 324

Operatoren

+ : Gleitkomma × Gleitkomma $\rightarrow$ Gleitkomma Addition
- : Gleitkomma × Gleitkomma $\rightarrow$ Gleitkomma Subtraktion
* : Gleitkomma × Gleitkomma $\rightarrow$ Gleitkomma Multiplikation
/ : Gleitkomma × Gleitkomma $\rightarrow$ Gleitkomma Division
% : Gleitkomma × Gleitkomma $\rightarrow$ Gleitkomma Modulo
++ : Gleitkomma $\rightarrow$ Gleitkomma Inkrement um 1.0
-- : Gleitkomma $\rightarrow$ Gleitkomma Dekrement um 1.0

Multiplikation: (Exponenten addieren, Mantissen multiplizieren)

Beispiel: 12 * 20 =    
  1.5 · 23 * 1.25 · 24 =    
  1.5 · 1.25 * 23 · 24 =    
  1.875 * 27 = 240  

Addition: (Exponenten angleichen, Mantissen addieren)

Beispiel: 12 + 20 =    
  1.5 · 23 + 1.25 · 24 =    
  0.75 · 24 + 1.25 · 24 =    
  (0.75 + 1.25) * 24 =    
  1 * 25 = 32  

Problem beim Angleichen der Mantissen:

Beispiel: 1024 + ${\frac{1}{1048576}}$ =    
  1 · 210 + 1 · 2- 20 =    
  1 · 210 + 2- 30 · 210 =    
  1 · 210 + 0 · 210 = 1024  

Bei 23 Bits für die Mantisse ist 2- 30 nicht mehr darstellbar.

Gleitkommaoperationen stoßen in Java keine Ausnahmebehandlung an. D.h., Division durch Null führt nicht zum Abbruch, sondern ergibt den Wert + $\infty$ bzw. - $\infty$ ; Null dividiert durch Null ergibt NaN (not a number).

Konstantenbezeichner

Beispiele: .2        
  2        
  2.        
  2.0        
  2.538        
  2.538f        
  2.5E2 = 2.5*102 = 250
  2.5E-2 = 2.5*10- 2 = 0.025

Source: Gleitkomma.java     JavaDoc: Gleitkomma.html     Applet:


prev up next
Previous: Ganze Zahlen (byte, short, Up: Einfache Datentypen Next: Boolean (boolean)