source: public/doc/gnu-c/Division-and-Remainder.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: 6.5 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>Division and Remainder (GNU C Language Manual)</title>
23
24<meta name="description" content="Division and Remainder (GNU C Language Manual)">
25<meta name="keywords" content="Division and Remainder (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="Arithmetic.html" rel="up" title="Arithmetic">
33<link href="Numeric-Comparisons.html" rel="next" title="Numeric Comparisons">
34<link href="Mixed-Mode.html" rel="prev" title="Mixed Mode">
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="Division-and-Remainder"></span><div class="header">
59<p>
60Next: <a href="Numeric-Comparisons.html" accesskey="n" rel="next">Numeric Comparisons</a>, Previous: <a href="Mixed-Mode.html" accesskey="p" rel="prev">Mixed Mode</a>, Up: <a href="Arithmetic.html" accesskey="u" rel="up">Arithmetic</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="Division-and-Remainder-1"></span><h3 class="section">6.5 Division and Remainder</h3>
64<span id="index-remainder-operator"></span>
65<span id="index-modulus"></span>
66<span id="index-operator_002c-remainder"></span>
67
68<p>Division of integers in C rounds the result to an integer. The result
69is always rounded towards zero.
70</p>
71<div class="example">
72<pre class="example"> 16 / 3 &rArr; 5
73-16 / 3 &rArr; -5
74 16 / -3 &rArr; -5
75-16 / -3 &rArr; 5
76</pre></div>
77
78<p>To get the corresponding remainder, use the &lsquo;<samp>%</samp>&rsquo; operator:
79</p>
80<div class="example">
81<pre class="example"> 16 % 3 &rArr; 1
82-16 % 3 &rArr; -1
83 16 % -3 &rArr; 1
84-16 % -3 &rArr; -1
85</pre></div>
86
87<p>&lsquo;<samp>%</samp>&rsquo; has the same operator precedence as &lsquo;<samp>/</samp>&rsquo; and &lsquo;<samp>*</samp>&rsquo;.
88</p>
89<p>From the rounded quotient and the remainder, you can reconstruct
90the dividend, like this:
91</p>
92<div class="example">
93<pre class="example">int
94original_dividend (int divisor, int quotient, int remainder)
95{
96 return divisor * quotient + remainder;
97}
98</pre></div>
99
100<p>To do unrounded division, use floating point. If only one operand is
101floating point, &lsquo;<samp>/</samp>&rsquo; converts the other operand to floating
102point.
103</p>
104<div class="example">
105<pre class="example">16.0 / 3 &rArr; 5.333333333333333
10616 / 3.0 &rArr; 5.333333333333333
10716.0 / 3.0 &rArr; 5.333333333333333
10816 / 3 &rArr; 5
109</pre></div>
110
111<p>The remainder operator &lsquo;<samp>%</samp>&rsquo; is not allowed for floating-point
112operands, because it is not needed. The concept of remainder makes
113sense for integers because the result of division of integers has to
114be an integer. For floating point, the result of division is a
115floating-point number, in other words a fraction, which will differ
116from the exact result only by a very small amount.
117</p>
118<p>There are functions in the standard C library to calculate remainders
119from integral-values division of floating-point numbers.
120See <a href="https://www.gnu.org/software/libc/manual/html_node/Remainder-Functions.html#Remainder-Functions">The GNU C Library</a> in <cite>The GNU C Library
121Reference Manual</cite>.
122</p>
123<p>Integer division overflows in one specific case: dividing the smallest
124negative value for the data type (see <a href="Maximum-and-Minimum-Values.html">Maximum and Minimum Values</a>)
125by -1. That&rsquo;s because the correct result, which is the
126corresponding positive number, does not fit (see <a href="Integer-Overflow.html">Integer Overflow</a>)
127in the same number of bits. On some computers now in use, this always
128causes a signal <code>SIGFPE</code> (see <a href="Signals.html">Signals</a>), the same behavior
129that the option <samp>-ftrapv</samp> specifies (see <a href="Signed-Overflow.html">Signed Overflow</a>).
130</p>
131<p>Division by zero leads to unpredictable results&mdash;depending on the
132type of computer, it might cause a signal <code>SIGFPE</code>, or it might
133produce a numeric result.
134</p>
135<span id="index-division-by-zero"></span>
136<span id="index-zero_002c-division-by"></span>
137<p><strong>Watch out:</strong> Make sure the program does not divide by zero. If
138you can&rsquo;t prove that the divisor is not zero, test whether it is zero,
139and skip the division if so.
140</p>
141<hr>
142<div class="header">
143<p>
144Next: <a href="Numeric-Comparisons.html" accesskey="n" rel="next">Numeric Comparisons</a>, Previous: <a href="Mixed-Mode.html" accesskey="p" rel="prev">Mixed Mode</a>, Up: <a href="Arithmetic.html" accesskey="u" rel="up">Arithmetic</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>
145</div>
146
147
148
149</body>
150</html>
Note: See TracBrowser for help on using the repository browser.