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>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 | <!--
|
---|
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="Division-and-Remainder"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <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> [<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
|
---|
69 | is always rounded towards zero.
|
---|
70 | </p>
|
---|
71 | <div class="example">
|
---|
72 | <pre class="example"> 16 / 3 ⇒ 5
|
---|
73 | -16 / 3 ⇒ -5
|
---|
74 | 16 / -3 ⇒ -5
|
---|
75 | -16 / -3 ⇒ 5
|
---|
76 | </pre></div>
|
---|
77 |
|
---|
78 | <p>To get the corresponding remainder, use the ‘<samp>%</samp>’ operator:
|
---|
79 | </p>
|
---|
80 | <div class="example">
|
---|
81 | <pre class="example"> 16 % 3 ⇒ 1
|
---|
82 | -16 % 3 ⇒ -1
|
---|
83 | 16 % -3 ⇒ 1
|
---|
84 | -16 % -3 ⇒ -1
|
---|
85 | </pre></div>
|
---|
86 |
|
---|
87 | <p>‘<samp>%</samp>’ has the same operator precedence as ‘<samp>/</samp>’ and ‘<samp>*</samp>’.
|
---|
88 | </p>
|
---|
89 | <p>From the rounded quotient and the remainder, you can reconstruct
|
---|
90 | the dividend, like this:
|
---|
91 | </p>
|
---|
92 | <div class="example">
|
---|
93 | <pre class="example">int
|
---|
94 | original_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
|
---|
101 | floating point, ‘<samp>/</samp>’ converts the other operand to floating
|
---|
102 | point.
|
---|
103 | </p>
|
---|
104 | <div class="example">
|
---|
105 | <pre class="example">16.0 / 3 ⇒ 5.333333333333333
|
---|
106 | 16 / 3.0 ⇒ 5.333333333333333
|
---|
107 | 16.0 / 3.0 ⇒ 5.333333333333333
|
---|
108 | 16 / 3 ⇒ 5
|
---|
109 | </pre></div>
|
---|
110 |
|
---|
111 | <p>The remainder operator ‘<samp>%</samp>’ is not allowed for floating-point
|
---|
112 | operands, because it is not needed. The concept of remainder makes
|
---|
113 | sense for integers because the result of division of integers has to
|
---|
114 | be an integer. For floating point, the result of division is a
|
---|
115 | floating-point number, in other words a fraction, which will differ
|
---|
116 | from the exact result only by a very small amount.
|
---|
117 | </p>
|
---|
118 | <p>There are functions in the standard C library to calculate remainders
|
---|
119 | from integral-values division of floating-point numbers.
|
---|
120 | See <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
|
---|
121 | Reference Manual</cite>.
|
---|
122 | </p>
|
---|
123 | <p>Integer division overflows in one specific case: dividing the smallest
|
---|
124 | negative value for the data type (see <a href="Maximum-and-Minimum-Values.html">Maximum and Minimum Values</a>)
|
---|
125 | by -1. That’s because the correct result, which is the
|
---|
126 | corresponding positive number, does not fit (see <a href="Integer-Overflow.html">Integer Overflow</a>)
|
---|
127 | in the same number of bits. On some computers now in use, this always
|
---|
128 | causes a signal <code>SIGFPE</code> (see <a href="Signals.html">Signals</a>), the same behavior
|
---|
129 | that 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—depending on the
|
---|
132 | type of computer, it might cause a signal <code>SIGFPE</code>, or it might
|
---|
133 | produce 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
|
---|
138 | you can’t prove that the divisor is not zero, test whether it is zero,
|
---|
139 | and skip the division if so.
|
---|
140 | </p>
|
---|
141 | <hr>
|
---|
142 | <div class="header">
|
---|
143 | <p>
|
---|
144 | Next: <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> [<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>
|
---|