This file is indexed.

/usr/share/doc/zsh-common/html/Arithmetic-Evaluation.html is in zsh-doc 5.1.1-1ubuntu2.3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>zsh: 11 Arithmetic Evaluation</title>

<meta name="description" content="zsh: 11 Arithmetic Evaluation">
<meta name="keywords" content="zsh: 11 Arithmetic Evaluation">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2any">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en">
<a name="Arithmetic-Evaluation"></a>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="Jobs-_0026-Signals.html#Jobs-_0026-Signals" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="Jobs-_0026-Signals.html#Signals" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="Conditional-Expressions.html#Conditional-Expressions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left">[<a href="Conditional-Expressions.html#Conditional-Expressions" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>

<a name="Arithmetic-Evaluation-1"></a>
<h1 class="chapter">11 Arithmetic Evaluation</h1>
<p><a name="index-arithmetic-evaluation"></a>
<a name="index-evaluation_002c-arithmetic"></a>
<a name="index-let_002c-use-of"></a>
The shell can perform integer and floating point arithmetic, either using
the builtin <tt>let</tt>, or via a substitution of the form
<tt>$((</tt><var>...</var><tt>))</tt>.  For
integers, the shell is usually compiled to use 8-byte precision where this
is available, otherwise precision is 4 bytes.  This can be tested, for
example, by giving the command &lsquo;<tt>print - $(( 12345678901 ))</tt>&rsquo;; if the
number appears unchanged, the precision is at least 8 bytes.  Floating
point arithmetic always uses the &lsquo;double&rsquo; type with whatever corresponding
precision is provided by the compiler and the library.
</p>
<p>The <tt>let</tt> builtin command takes arithmetic expressions as arguments; each
is evaluated separately.  Since many of the arithmetic operators, as well
as spaces, require quoting, an alternative form is provided: for any
command which begins with a &lsquo;<tt>((</tt>&rsquo;, all the characters until a
matching &lsquo;<tt>))</tt>&rsquo; are treated as a quoted expression and
arithmetic expansion performed as for an argument of <tt>let</tt>.  More
precisely, &lsquo;<tt>((</tt><var>...</var><tt>))</tt>&rsquo; is equivalent to
&lsquo;<tt>let &quot;</tt><var>...</var><tt>&quot;</tt>&rsquo;.  The return status is 0 if the arithmetic value
of the expression is non-zero, 1 if it is zero, and 2 if an error occurred.
</p>
<p>For example, the following statement
</p>
<div class="example">
<pre class="example">(( val = 2 + 1 ))
</pre></div>

<p>is equivalent to
</p>
<div class="example">
<pre class="example">let &quot;val = 2 + 1&quot;
</pre></div>

<p>both assigning the value 3 to the shell variable <tt>val</tt> and returning a
zero status.
</p>
<p><a name="index-arithmetic-base"></a>
<a name="index-bases_002c-in-arithmetic"></a>
Integers can be in bases other than 10.
A leading &lsquo;<tt>0x</tt>&rsquo; or &lsquo;<tt>0X</tt>&rsquo; denotes hexadecimal and a leading
&lsquo;<tt>0b</tt>&rsquo; or &lsquo;<tt>0B</tt>&rsquo; binary.
Integers may also be of the form &lsquo;<var>base</var><tt>#</tt><var>n</var>&rsquo;,
where <var>base</var> is a decimal number between two and thirty-six
representing the arithmetic base and <var>n</var>
is a number in that base (for example, &lsquo;<tt>16#ff</tt>&rsquo; is 255 in hexadecimal).
The <var>base</var><tt>#</tt> may also be omitted, in which case
base 10 is used.  For backwards compatibility the form
&lsquo;<tt>[</tt><var>base</var><tt>]</tt><var>n</var>&rsquo; is also accepted.
</p>
<p>An integer expression or a base given in the form
&lsquo;<var>base</var><tt>#</tt><var>n</var>&rsquo; may contain underscores (&lsquo;<tt>_</tt>&rsquo;) after the
leading digit for visual guidance; these are ignored in computation.
Examples are <tt>1_000_000</tt> or <tt>0xffff_ffff</tt> which are equivalent to
<tt>1000000</tt> and <tt>0xffffffff</tt> respectively.
</p>
<p>It is also possible to specify a base to be used for output in the form
&lsquo;<tt>[#</tt><var>base</var><tt>]</tt>&rsquo;, for example &lsquo;<tt>[#16]</tt>&rsquo;.  This is used when
outputting arithmetical substitutions or when assigning to scalar
parameters, but an explicitly defined integer or floating point parameter
will not be affected.  If an integer variable is implicitly defined by an
arithmetic expression, any base specified in this way will be set as the
variable&rsquo;s output arithmetic base as if the option &lsquo;<tt>-i</tt> <var>base</var>&rsquo; to
the <tt>typeset</tt> builtin had been used.  The expression has no precedence
and if it occurs more than once in a mathematical expression, the last
encountered is used.  For clarity it is recommended that it appear at the
beginning of an expression.  As an example:
</p>
<div class="example">
<pre class="example">typeset -i 16 y
print $(( [#8] x = 32, y = 32 ))
print $x $y
</pre></div>

<p>outputs first &lsquo;<tt>8#40</tt>&rsquo;, the rightmost value in the given output base, and
then &lsquo;<tt>8#40 16#20</tt>&rsquo;, because <tt>y</tt> has been explicitly declared to
have output base 16, while <tt>x</tt> (assuming it does not already exist) is
implicitly typed by the arithmetic evaluation, where it acquires the output
base 8.
</p>
<p>The <var>base</var> may be replaced or followed by an underscore, which may
itself be followed by a positive integer (if it is missing the value 3
is used).  This indicates that underscores should be inserted into the
output string, grouping the number for visual clarity.  The following
integer specifies the number of digits to group together.  For example:
</p>
<div class="example">
<pre class="example">setopt cbases
print $(( [#16_4] 65536 ** 2 ))
</pre></div>

<p>outputs &lsquo;<tt>0x1_0000_0000</tt>&rsquo;.
</p>
<p>The feature can be used with floating
point numbers, in which case the base must be omitted; grouping
is away from the decimal point.  For example,
</p>
<div class="example">
<pre class="example">zmodload zsh/mathfunc
print $(( [#_] sqrt(1e7) ))
</pre></div>

<p>outputs &lsquo;<tt>3_162.277_660_168_379_5</tt>&rsquo; (the number of decimal places
shown may vary).
</p>
<p><a name="index-C_005fBASES_002c-use-of"></a>
<a name="index-OCTAL_005fZEROES_002c-use-of"></a>
If the <tt>C_BASES</tt> option is set, hexadecimal numbers are output in the standard C
format, for example &lsquo;<tt>0xFF</tt>&rsquo; instead of the usual &lsquo;<tt>16#FF</tt>&rsquo;.  If the
option <tt>OCTAL_ZEROES</tt> is also set (it is not by default), octal numbers
will be treated similarly and hence appear as &lsquo;<tt>077</tt>&rsquo; instead of
&lsquo;<tt>8#77</tt>&rsquo;.  This option has no effect on the output of bases other than
hexadecimal and octal, and these formats are always understood on input.
</p>
<p>When an output base is specified using the &lsquo;<tt>[#</tt><var>base</var><tt>]</tt>&rsquo; syntax,
an appropriate base prefix will be output if necessary, so that the value
output is valid syntax for input.  If the <tt>#</tt> is doubled, for example
&lsquo;<tt>[##16]</tt>&rsquo;, then no base prefix is output.
</p>
<p>Floating point constants are recognized by the presence of a decimal point
or an exponent.  The decimal point may be the first character of the
constant, but the exponent character <tt>e</tt> or <tt>E</tt> may not, as it will be
taken for a parameter name.  All numeric parts (before and after the
decimal point and in the exponent) may contain underscores after the
leading digit for visual guidance; these are ignored in computation.
</p>
<p><a name="index-arithmetic-operators"></a>
<a name="index-operators_002c-arithmetic"></a>
An arithmetic expression uses nearly the same syntax and
associativity of expressions as in C.
</p>
<p>In the native mode of operation, the following operators are supported
(listed in decreasing order of precedence):
</p>
<dl compact="compact">
<dt><tt>+ - ! ~ ++ --</tt></dt>
<dd><p>unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement
</p></dd>
<dt><tt>&lt;&lt; &gt;&gt;</tt></dt>
<dd><p>bitwise shift left, right
</p></dd>
<dt><tt>&amp;</tt></dt>
<dd><p>bitwise AND
</p></dd>
<dt><tt>^</tt></dt>
<dd><p>bitwise XOR
</p></dd>
<dt><tt>|</tt></dt>
<dd><p>bitwise OR
</p></dd>
<dt><tt>**</tt></dt>
<dd><p>exponentiation
</p></dd>
<dt><tt>* / %</tt></dt>
<dd><p>multiplication, division, modulus (remainder)
</p></dd>
<dt><tt>+ -</tt></dt>
<dd><p>addition, subtraction
</p></dd>
<dt><tt>&lt; &gt; &lt;= &gt;=</tt></dt>
<dd><p>comparison
</p></dd>
<dt><tt>== !=</tt></dt>
<dd><p>equality and inequality
</p></dd>
<dt><tt>&amp;&amp;</tt></dt>
<dd><p>logical AND
</p></dd>
<dt><tt>|| ^^</tt></dt>
<dd><p>logical OR, XOR
</p></dd>
<dt><tt>? :</tt></dt>
<dd><p>ternary operator
</p></dd>
<dt><tt>= += -= *= /= %= &amp;= ^= |= &lt;&lt;= &gt;&gt;= &amp;&amp;= ||= ^^= **=</tt></dt>
<dd><p>assignment
</p></dd>
<dt><tt>,</tt></dt>
<dd><p>comma operator
</p></dd>
</dl>

<p>The operators &lsquo;<tt>&amp;&amp;</tt>&rsquo;, &lsquo;<tt>||</tt>&rsquo;, &lsquo;<tt>&amp;&amp;=</tt>&rsquo;, and &lsquo;<tt>||=</tt>&rsquo; are
short-circuiting, and only one of the latter two expressions in a ternary
operator is evaluated.  Note the precedence of the bitwise AND, OR,
and XOR operators.
</p>
<p>With the option <tt>C_PRECEDENCES</tt> the precedences (but no other
properties) of the operators are altered to be the same as those in
most other languages that support the relevant operators:
</p>
<dl compact="compact">
<dt><tt>+ - ! ~ ++ --</tt></dt>
<dd><p>unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement
</p></dd>
<dt><tt>**</tt></dt>
<dd><p>exponentiation
</p></dd>
<dt><tt>* / %</tt></dt>
<dd><p>multiplication, division, modulus (remainder)
</p></dd>
<dt><tt>+ -</tt></dt>
<dd><p>addition, subtraction
</p></dd>
<dt><tt>&lt;&lt; &gt;&gt;</tt></dt>
<dd><p>bitwise shift left, right
</p></dd>
<dt><tt>&lt; &gt; &lt;= &gt;=</tt></dt>
<dd><p>comparison
</p></dd>
<dt><tt>== !=</tt></dt>
<dd><p>equality and inequality
</p></dd>
<dt><tt>&amp;</tt></dt>
<dd><p>bitwise AND
</p></dd>
<dt><tt>^</tt></dt>
<dd><p>bitwise XOR
</p></dd>
<dt><tt>|</tt></dt>
<dd><p>bitwise OR
</p></dd>
<dt><tt>&amp;&amp;</tt></dt>
<dd><p>logical AND
</p></dd>
<dt><tt>^^</tt></dt>
<dd><p>logical XOR
</p></dd>
<dt><tt>||</tt></dt>
<dd><p>logical OR
</p></dd>
<dt><tt>? :</tt></dt>
<dd><p>ternary operator
</p></dd>
<dt><tt>= += -= *= /= %= &amp;= ^= |= &lt;&lt;= &gt;&gt;= &amp;&amp;= ||= ^^= **=</tt></dt>
<dd><p>assignment
</p></dd>
<dt><tt>,</tt></dt>
<dd><p>comma operator
</p></dd>
</dl>

<p>Note the precedence of exponentiation in both cases is below
that of unary operators, hence &lsquo;<tt>-3**2</tt>&rsquo; evaluates as &lsquo;<tt>9</tt>&rsquo;, not
&lsquo;<tt>-9</tt>&rsquo;.  Use parentheses where necessary: &lsquo;<tt>-(3**2)</tt>&rsquo;.  This is
for compatibility with other shells.
</p>
<p><a name="index-mathematical-functions_002c-use-of"></a>
<a name="index-functions_002c-math_002c-use-of"></a>
Mathematical functions can be called with the syntax
&lsquo;<var>func</var><tt>(</tt><var>args</var><tt>)</tt>&rsquo;, where the function decides
if the <var>args</var> is used as a string or a comma-separated list of
arithmetic expressions. The shell currently defines no mathematical
functions by default, but the module <tt>zsh/mathfunc</tt> may be loaded with
the <tt>zmodload</tt> builtin to provide standard floating point mathematical
functions.
</p>
<p>An expression of the form &lsquo;<tt>##</tt><var>x</var>&rsquo; where <var>x</var> is any character
sequence such as &lsquo;<tt>a</tt>&rsquo;, &lsquo;<tt>^A</tt>&rsquo;, or &lsquo;<tt>\M-\C-x</tt>&rsquo; gives the value of
this character and an expression of the form &lsquo;<tt>#</tt><var>name</var>&rsquo; gives the
value of the first character of the contents of the parameter <var>name</var>.
Character values are according to the character set used in the current
locale; for multibyte character handling the option <tt>MULTIBYTE</tt> must be
set.  Note that this form is different from &lsquo;<tt>$#</tt><var>name</var>&rsquo;, a standard
parameter substitution which gives the length of the parameter <var>name</var>.
&lsquo;<tt>#\</tt>&rsquo; is accepted instead of &lsquo;<tt>##</tt>&rsquo;, but its use is deprecated.
</p>
<p>Named parameters and subscripted arrays can be referenced by name within an
arithmetic expression without using the parameter expansion syntax.  For
example,
</p>
<div class="example">
<pre class="example">((val2 = val1 * 2))
</pre></div>

<p>assigns twice the value of <tt>$val1</tt> to the parameter named <tt>val2</tt>.
</p>
<p>An internal integer representation of a named parameter
can be specified with the <tt>integer</tt> builtin.
<a name="index-parameters_002c-integer"></a>
<a name="index-integer-parameters"></a>
<a name="index-integer_002c-use-of"></a>
Arithmetic evaluation is performed on the value of each
assignment to a named parameter declared integer
in this manner.  Assigning a floating point number to an integer results in
rounding down to the next integer.
</p>
<p><a name="index-parameters_002c-floating-point"></a>
<a name="index-floating-point-parameters"></a>
<a name="index-float_002c-use-of"></a>
Likewise, floating point numbers can be declared with the <tt>float</tt>
builtin; there are two types, differing only in their output format, as
described for the <tt>typeset</tt> builtin.  The output format can be bypassed
by using arithmetic substitution instead of the parameter substitution,
i.e. &lsquo;<tt>${</tt><var>float</var><tt>}</tt>&rsquo; uses the defined format, but
&lsquo;<tt>$((</tt><var>float</var><tt>))</tt>&rsquo; uses a generic floating point
format.
</p>
<p>Promotion of integer to floating point values is performed where
necessary.  In addition, if any operator which requires an integer
(&lsquo;<tt>~</tt>&rsquo;, &lsquo;<tt>&amp;</tt>&rsquo;, &lsquo;<tt>|</tt>&rsquo;, &lsquo;<tt>^</tt>&rsquo;, &lsquo;<tt>%</tt>&rsquo;, &lsquo;<tt>&lt;&lt;</tt>&rsquo;, &lsquo;<tt>&gt;&gt;</tt>&rsquo; and their
equivalents with assignment) is given a floating point argument, it will be
silently rounded down to the next integer.
</p>
<p>Users should beware that, in common with many other programming
languages but not software designed for calculation, the evaluation of
an expression in zsh is taken a term at a time and promotion of integers
to floating point does not occur in terms only containing integers.  A
typical result of this is that a division such as <tt>6/8</tt> is truncated,
in this being rounded down to 0.  The <tt>FORCE_FLOAT</tt> shell option can
be used in scripts or functions where floating point evaluation is
required throughout.
</p>
<p>Scalar variables can hold integer or floating point values at different
times; there is no memory of the numeric type in this case.
</p>
<p>If a variable is first assigned in a numeric context without previously
being declared, it will be implicitly typed as <tt>integer</tt> or <tt>float</tt> and
retain that type either until the type is explicitly changed or until the
end of the scope.  This can have unforeseen consequences.  For example, in
the loop
</p>
<div class="example">
<pre class="example">for (( f = 0; f &lt; 1; f += 0.1 )); do
# use $f
done
</pre></div>

<p>if <tt>f</tt> has not already been declared, the first assignment will cause it
to be created as an integer, and consequently the operation &lsquo;<tt>f += 0.1</tt>&rsquo;
will always cause the result to be truncated to zero, so that the loop will
fail.  A simple fix would be to turn the initialization into &lsquo;<tt>f = 0.0</tt>&rsquo;.
It is therefore best to declare numeric variables with explicit types.
</p><hr>
<table class="header" cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="Jobs-_0026-Signals.html#Jobs-_0026-Signals" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="Conditional-Expressions.html#Conditional-Expressions" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="index.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p><font size="-1">
  This document was generated on <em>September 10, 2018</em> using <a href="http://www.gnu.org/software/texinfo/"><em>texi2any</em></a>.
</font></p>

<font size="-1">Zsh version 5.1.1, released on September 11, 2015.</font>
</body>
</html>