source: public/doc/gnu-c/Exact-Floating-Constants.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 5.8 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Exact Floating Constants (GNU C Language Manual)</title>
23
24<meta name="description" content="Exact Floating Constants (GNU C Language Manual)">
25<meta name="keywords" content="Exact Floating Constants (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="Floating-Point-in-Depth.html" rel="up" title="Floating Point in Depth">
33<link href="Handling-Infinity.html" rel="next" title="Handling Infinity">
34<link href="Error-Recovery.html" rel="prev" title="Error Recovery">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Exact-Floating-Constants"></span><div class="header">
59<p>
60Next: <a href="Handling-Infinity.html" accesskey="n" rel="next">Handling Infinity</a>, Previous: <a href="Error-Recovery.html" accesskey="p" rel="prev">Error Recovery</a>, Up: <a href="Floating-Point-in-Depth.html" accesskey="u" rel="up">Floating Point in Depth</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="Exact-Floating_002dPoint-Constants"></span><h3 class="section">28.12 Exact Floating-Point Constants</h3>
64<span id="index-exact-specification-of-floating_002dpoint-constants"></span>
65<span id="index-floating_002dpoint-constants_002c-exact-specification-of"></span>
66
67<p>One of the frustrations that numerical programmers have suffered
68with since the dawn of digital computers is the inability to
69precisely specify numbers in their programs. On the early
70decimal machines, that was not an issue: you could write a
71constant <code>1e-30</code> and be confident of that exact value
72being used in floating-point operations. However, when the
73hardware works in a base other than 10, then human-specified
74numbers have to be converted to that base, and then converted
75back again at output time. The two base conversions are rarely
76exact, and unwanted rounding errors are introduced.
77</p>
78<span id="index-hexademical-floating_002dpoint-constants"></span>
79<p>As computers usually represent numbers in a base other than 10,
80numbers often must be converted to and from different bases, and
81rounding errors can occur during conversion. This problem is solved
82in C using hexademical floating-point constants. For example,
83<code>+0x1.fffffcp-1</code> is the number that is the IEEE 754 32-bit value
84closest to, but below, <code>1.0</code>. The significand is represented as a
85hexadecimal fraction, and the <em>power of two</em> is written in
86decimal following the exponent letter <code>p</code> (the traditional
87exponent letter <code>e</code> is not possible, because it is a hexadecimal
88digit).
89</p>
90<p>In <code>printf</code> and <code>scanf</code> and related functions, you can use
91the &lsquo;<samp>%a</samp>&rsquo; and &lsquo;<samp>%A</samp>&rsquo; format specifiers for writing and reading
92hexadecimal floating-point values. &lsquo;<samp>%a</samp>&rsquo; writes them with lower
93case letters and &lsquo;<samp>%A</samp>&rsquo; writes them with upper case letters. For
94instance, this code reproduces our sample number:
95</p>
96<div class="example">
97<pre class="example">printf (&quot;%a\n&quot;, 1.0 - pow (2.0, -23));
98 -| 0x1.fffffcp-1
99</pre></div>
100
101<p>The <code>strtod</code> family was similarly extended to recognize
102numbers in that new format.
103</p>
104<p>If you want to ensure exact data representation for transfer of
105floating-point numbers between C programs on different
106computers, then hexadecimal constants are an optimum choice.
107</p>
108
109<hr>
110<div class="header">
111<p>
112Next: <a href="Handling-Infinity.html" accesskey="n" rel="next">Handling Infinity</a>, Previous: <a href="Error-Recovery.html" accesskey="p" rel="prev">Error Recovery</a>, Up: <a href="Floating-Point-in-Depth.html" accesskey="u" rel="up">Floating Point in Depth</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
113</div>
114
115
116
117</body>
118</html>
Note: See TracBrowser for help on using the repository browser.