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
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing 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>Rounding (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Rounding (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Rounding (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="Rounding-Issues.html" rel="next" title="Rounding Issues">
|
---|
34 | <link href="Exact-Floating_002dPoint.html" rel="prev" title="Exact Floating-Point">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Rounding"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Rounding-Issues.html" accesskey="n" rel="next">Rounding Issues</a>, Previous: <a href="Exact-Floating_002dPoint.html" accesskey="p" rel="prev">Exact Floating-Point</a>, Up: <a href="Floating-Point-in-Depth.html" accesskey="u" rel="up">Floating Point in Depth</a> [<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="Rounding-1"></span><h3 class="section">28.7 Rounding</h3>
|
---|
64 | <span id="index-rounding"></span>
|
---|
65 |
|
---|
66 | <p>When floating-point arithmetic produces a result that can’t fit
|
---|
67 | exactly in the significand of the type that’s in use, it has to
|
---|
68 | <em>round</em> the value. The basic arithmetic operations—addition,
|
---|
69 | subtraction, multiplication, division, and square root—always produce
|
---|
70 | a result that is equivalent to the exact, possibly infinite-precision
|
---|
71 | result rounded to storage precision according to the current rounding
|
---|
72 | rule.
|
---|
73 | </p>
|
---|
74 | <p>Rounding sets the <code>FE_INEXACT</code> exception flag (see <a href="Exception-Flags.html">Exception Flags</a>). This enables programs to determine that rounding has
|
---|
75 | occurred.
|
---|
76 | </p>
|
---|
77 | <p>Rounding consists of adjusting the exponent to bring the significand
|
---|
78 | back to the required base-point alignment, then applying the current
|
---|
79 | <em>rounding rule</em> to squeeze the significand into the fixed
|
---|
80 | available size.
|
---|
81 | </p>
|
---|
82 | <p>The current rule is selected at run time from four options. Here they
|
---|
83 | are:
|
---|
84 | </p>
|
---|
85 | <ul class="no-bullet">
|
---|
86 | <li>* <em>round-to-nearest</em>, with ties rounded to an even integer;
|
---|
87 |
|
---|
88 | </li><li>* <em>round-up</em>, towards <code>+Infinity</code>;
|
---|
89 |
|
---|
90 | </li><li>* <em>round-down</em>, towards <code>-Infinity</code>;
|
---|
91 |
|
---|
92 | </li><li>* <em>round-towards-zero</em>.
|
---|
93 | </li></ul>
|
---|
94 |
|
---|
95 | <p>Under those four rounding rules, a decimal value
|
---|
96 | <code>-1.2345</code> that is to be rounded to a four-digit result would
|
---|
97 | become <code>-1.234</code>, <code>-1.234</code>, <code>-1.235</code>, and
|
---|
98 | <code>-1.234</code>, respectively.
|
---|
99 | </p>
|
---|
100 | <p>The default rounding rule is <em>round-to-nearest</em>, because that has
|
---|
101 | the least bias, and produces the lowest average error. When the true
|
---|
102 | result lies exactly halfway between two representable machine numbers,
|
---|
103 | the result is rounded to the one that ends with an even digit.
|
---|
104 | </p>
|
---|
105 | <p>The <em>round-towards-zero</em> rule was common on many early computer
|
---|
106 | designs, because it is the easiest to implement: it just requires
|
---|
107 | silent truncation of all extra bits.
|
---|
108 | </p>
|
---|
109 | <p>The two other rules, <em>round-up</em> and <em>round-down</em>, are
|
---|
110 | essential for implementing <em>interval arithmetic</em>, whereby
|
---|
111 | each arithmetic operation produces lower and upper bounds that
|
---|
112 | are guaranteed to enclose the exact result.
|
---|
113 | </p>
|
---|
114 | <p>See <a href="Rounding-Control.html">Rounding Control</a>, for details on getting and setting the
|
---|
115 | current rounding mode.
|
---|
116 | </p>
|
---|
117 | <hr>
|
---|
118 | <div class="header">
|
---|
119 | <p>
|
---|
120 | Next: <a href="Rounding-Issues.html" accesskey="n" rel="next">Rounding Issues</a>, Previous: <a href="Exact-Floating_002dPoint.html" accesskey="p" rel="prev">Exact Floating-Point</a>, Up: <a href="Floating-Point-in-Depth.html" accesskey="u" rel="up">Floating Point in Depth</a> [<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>
|
---|
121 | </div>
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | </body>
|
---|
126 | </html>
|
---|