File: Representation-of-floating-point-numbers.html

package info (click to toggle)
gsl-ref-html 2.3-1
  • links: PTS
  • area: non-free
  • in suites: bullseye, buster, sid
  • size: 6,876 kB
  • ctags: 4,574
  • sloc: makefile: 35
file content (249 lines) | stat: -rw-r--r-- 10,314 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 The GSL Team.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Free Software
Needs Free Documentation", the Front-Cover text being "A GNU Manual",
and with the Back-Cover Text being (a) (see below). A copy of the
license is included in the section entitled "GNU Free Documentation
License".

(a) The Back-Cover Text is: "You have the freedom to copy and modify this
GNU Manual." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Scientific Library &ndash; Reference Manual: Representation of floating point numbers</title>

<meta name="description" content="GNU Scientific Library &ndash; Reference Manual: Representation of floating point numbers">
<meta name="keywords" content="GNU Scientific Library &ndash; Reference Manual: Representation of floating point numbers">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Function-Index.html#Function-Index" rel="index" title="Function Index">
<link href="IEEE-floating_002dpoint-arithmetic.html#IEEE-floating_002dpoint-arithmetic" rel="up" title="IEEE floating-point arithmetic">
<link href="Setting-up-your-IEEE-environment.html#Setting-up-your-IEEE-environment" rel="next" title="Setting up your IEEE environment">
<link href="IEEE-floating_002dpoint-arithmetic.html#IEEE-floating_002dpoint-arithmetic" rel="previous" title="IEEE floating-point arithmetic">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Representation-of-floating-point-numbers"></a>
<div class="header">
<p>
Next: <a href="Setting-up-your-IEEE-environment.html#Setting-up-your-IEEE-environment" accesskey="n" rel="next">Setting up your IEEE environment</a>, Up: <a href="IEEE-floating_002dpoint-arithmetic.html#IEEE-floating_002dpoint-arithmetic" accesskey="u" rel="up">IEEE floating-point arithmetic</a> &nbsp; [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Representation-of-floating-point-numbers-1"></a>
<h3 class="section">45.1 Representation of floating point numbers</h3>
<a name="index-IEEE-format-for-floating-point-numbers"></a>
<a name="index-bias_002c-IEEE-format"></a>
<a name="index-exponent_002c-IEEE-format"></a>
<a name="index-sign-bit_002c-IEEE-format"></a>
<a name="index-mantissa_002c-IEEE-format"></a>
<p>The IEEE Standard for Binary Floating-Point Arithmetic defines binary
formats for single and double precision numbers.  Each number is composed
of three parts: a <em>sign bit</em> (<em>s</em>), an <em>exponent</em>
(<em>E</em>) and a <em>fraction</em> (<em>f</em>).  The numerical value of the
combination <em>(s,E,f)</em> is given by the following formula,
</p>
<div class="example">
<pre class="example">(-1)^s (1.fffff...) 2^E
</pre></div>

<a name="index-normalized-form_002c-IEEE-format"></a>
<a name="index-denormalized-form_002c-IEEE-format"></a>
<p>The sign bit is either zero or one.  The exponent ranges from a minimum value
<em>E_min</em> 
to a maximum value
<em>E_max</em> depending on the precision.  The exponent is converted to an 
unsigned number
<em>e</em>, known as the <em>biased exponent</em>, for storage by adding a
<em>bias</em> parameter,
<em>e = E + bias</em>.
The sequence <em>fffff...</em> represents the digits of the binary
fraction <em>f</em>.  The binary digits are stored in <em>normalized
form</em>, by adjusting the exponent to give a leading digit of <em>1</em>. 
Since the leading digit is always 1 for normalized numbers it is
assumed implicitly and does not have to be stored.
Numbers smaller than 
<em>2^(E_min)</em>
are be stored in <em>denormalized form</em> with a leading zero,
</p>
<div class="example">
<pre class="example">(-1)^s (0.fffff...) 2^(E_min)
</pre></div>

<a name="index-zero_002c-IEEE-format"></a>
<a name="index-infinity_002c-IEEE-format"></a>
<p>This allows gradual underflow down to 
<em>2^(E_min - p)</em> for <em>p</em> bits of precision. 
A zero is encoded with the special exponent of 
<em>2^(E_min - 1)</em> and infinities with the exponent of 
<em>2^(E_max + 1)</em>.
</p>
<a name="index-single-precision_002c-IEEE-format"></a>
<p>The format for single precision numbers uses 32 bits divided in the
following way,
</p>
<div class="smallexample">
<pre class="smallexample">seeeeeeeefffffffffffffffffffffff
    
s = sign bit, 1 bit
e = exponent, 8 bits  (E_min=-126, E_max=127, bias=127)
f = fraction, 23 bits
</pre></div>

<a name="index-double-precision_002c-IEEE-format"></a>
<p>The format for double precision numbers uses 64 bits divided in the
following way,
</p>
<div class="smallexample">
<pre class="smallexample">seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff

s = sign bit, 1 bit
e = exponent, 11 bits  (E_min=-1022, E_max=1023, bias=1023)
f = fraction, 52 bits
</pre></div>

<p>It is often useful to be able to investigate the behavior of a
calculation at the bit-level and the library provides functions for
printing the IEEE representations in a human-readable form.
</p>

<dl>
<dt><a name="index-gsl_005fieee_005ffprintf_005ffloat"></a>Function: <em>void</em> <strong>gsl_ieee_fprintf_float</strong> <em>(FILE * <var>stream</var>, const float * <var>x</var>)</em></dt>
<dt><a name="index-gsl_005fieee_005ffprintf_005fdouble"></a>Function: <em>void</em> <strong>gsl_ieee_fprintf_double</strong> <em>(FILE * <var>stream</var>, const double * <var>x</var>)</em></dt>
<dd><p>These functions output a formatted version of the IEEE floating-point
number pointed to by <var>x</var> to the stream <var>stream</var>. A pointer is
used to pass the number indirectly, to avoid any undesired promotion
from <code>float</code> to <code>double</code>.  The output takes one of the
following forms,
</p>
<dl compact="compact">
<dt><code>NaN</code></dt>
<dd><p>the Not-a-Number symbol
</p>
</dd>
<dt><code>Inf, -Inf</code></dt>
<dd><p>positive or negative infinity
</p>
</dd>
<dt><code>1.fffff...*2^E, -1.fffff...*2^E</code></dt>
<dd><p>a normalized floating point number
</p>
</dd>
<dt><code>0.fffff...*2^E, -0.fffff...*2^E</code></dt>
<dd><p>a denormalized floating point number
</p>
</dd>
<dt><code>0, -0</code></dt>
<dd><p>positive or negative zero
</p>
</dd>
</dl>

<p>The output can be used directly in GNU Emacs Calc mode by preceding it
with <code>2#</code> to indicate binary.
</p></dd></dl>

<dl>
<dt><a name="index-gsl_005fieee_005fprintf_005ffloat"></a>Function: <em>void</em> <strong>gsl_ieee_printf_float</strong> <em>(const float * <var>x</var>)</em></dt>
<dt><a name="index-gsl_005fieee_005fprintf_005fdouble"></a>Function: <em>void</em> <strong>gsl_ieee_printf_double</strong> <em>(const double * <var>x</var>)</em></dt>
<dd><p>These functions output a formatted version of the IEEE floating-point
number pointed to by <var>x</var> to the stream <code>stdout</code>.
</p></dd></dl>

<p>The following program demonstrates the use of the functions by printing
the single and double precision representations of the fraction
<em>1/3</em>.  For comparison the representation of the value promoted from
single to double precision is also printed.
</p>
<div class="example">
<pre class="verbatim">#include &lt;stdio.h&gt;
#include &lt;gsl/gsl_ieee_utils.h&gt;

int
main (void) 
{
  float f = 1.0/3.0;
  double d = 1.0/3.0;

  double fd = f; /* promote from float to double */
  
  printf (&quot; f=&quot;); gsl_ieee_printf_float(&amp;f); 
  printf (&quot;\n&quot;);

  printf (&quot;fd=&quot;); gsl_ieee_printf_double(&amp;fd); 
  printf (&quot;\n&quot;);

  printf (&quot; d=&quot;); gsl_ieee_printf_double(&amp;d); 
  printf (&quot;\n&quot;);

  return 0;
}
</pre></div>

<p>The binary representation of <em>1/3</em> is <em>0.01010101... </em>.  The
output below shows that the IEEE format normalizes this fraction to give
a leading digit of 1,
</p>
<div class="smallexample">
<pre class="smallexample"> f= 1.01010101010101010101011*2^-2
fd= 1.0101010101010101010101100000000000000000000000000000*2^-2
 d= 1.0101010101010101010101010101010101010101010101010101*2^-2
</pre></div>

<p>The output also shows that a single-precision number is promoted to
double-precision by adding zeros in the binary representation.
</p>

  





<hr>
<div class="header">
<p>
Next: <a href="Setting-up-your-IEEE-environment.html#Setting-up-your-IEEE-environment" accesskey="n" rel="next">Setting up your IEEE environment</a>, Up: <a href="IEEE-floating_002dpoint-arithmetic.html#IEEE-floating_002dpoint-arithmetic" accesskey="u" rel="up">IEEE floating-point arithmetic</a> &nbsp; [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>