source: public/doc/gnu-c/Rounding.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.7 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>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<!--
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="Rounding"></span><div class="header">
59<p>
60Next: <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> &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="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&rsquo;t fit
67exactly in the significand of the type that&rsquo;s in use, it has to
68<em>round</em> the value. The basic arithmetic operations&mdash;addition,
69subtraction, multiplication, division, and square root&mdash;always produce
70a result that is equivalent to the exact, possibly infinite-precision
71result rounded to storage precision according to the current rounding
72rule.
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
75occurred.
76</p>
77<p>Rounding consists of adjusting the exponent to bring the significand
78back to the required base-point alignment, then applying the current
79<em>rounding rule</em> to squeeze the significand into the fixed
80available size.
81</p>
82<p>The current rule is selected at run time from four options. Here they
83are:
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
97become <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
101the least bias, and produces the lowest average error. When the true
102result lies exactly halfway between two representable machine numbers,
103the 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
106designs, because it is the easiest to implement: it just requires
107silent truncation of all extra bits.
108</p>
109<p>The two other rules, <em>round-up</em> and <em>round-down</em>, are
110essential for implementing <em>interval arithmetic</em>, whereby
111each arithmetic operation produces lower and upper bounds that
112are 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
115current rounding mode.
116</p>
117<hr>
118<div class="header">
119<p>
120Next: <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> &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>
121</div>
122
123
124
125</body>
126</html>
Note: See TracBrowser for help on using the repository browser.