/usr/share/doc/zsh-common/html/Arithmetic-Evaluation.html is in zsh-doc 5.1.1-1ubuntu2.
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 419 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.0, 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.nocodebreak {white-space: nowrap}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: serif; 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"> << </a>]</td>
<td valign="middle" align="left">[<a href="Jobs-_0026-Signals.html#Signals" title="Previous section in reading order"> < </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"> > </a>]</td>
<td valign="middle" align="left">[<a href="Conditional-Expressions.html#Conditional-Expressions" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </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>
<a name="index-arithmetic-evaluation"></a>
<a name="index-evaluation_002c-arithmetic"></a>
<a name="index-let_002c-use-of"></a>
<p>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 ‘<tt>print - $(( 12345678901 ))</tt>’; if the
number appears unchanged, the precision is at least 8 bytes. Floating
point arithmetic always uses the ‘double’ 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 ‘<tt>((</tt>’, all the characters until a
matching ‘<tt>))</tt>’ are treated as a quoted expression and
arithmetic expansion performed as for an argument of <tt>let</tt>. More
precisely, ‘<tt>((</tt><var>...</var><tt>))</tt>’ is equivalent to
‘<tt>let "</tt><var>...</var><tt>"</tt>’. 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 "val = 2 + 1"
</pre></div>
<p>both assigning the value 3 to the shell variable <tt>val</tt> and returning a
zero status.
</p>
<a name="index-arithmetic-base"></a>
<a name="index-bases_002c-in-arithmetic"></a>
<p>Integers can be in bases other than 10.
A leading ‘<tt>0x</tt>’ or ‘<tt>0X</tt>’ denotes hexadecimal and a leading
‘<tt>0b</tt>’ or ‘<tt>0B</tt>’ binary.
Integers may also be of the form ‘<var>base</var><tt>#</tt><var>n</var>’,
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, ‘<tt>16#ff</tt>’ 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
‘<tt>[</tt><var>base</var><tt>]</tt><var>n</var>’ is also accepted.
</p>
<p>An integer expression or a base given in the form
‘<var>base</var><tt>#</tt><var>n</var>’ may contain underscores (‘<tt>_</tt>’) 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
‘<tt>[#</tt><var>base</var><tt>]</tt>’, for example ‘<tt>[#16]</tt>’. 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’s output arithmetic base as if the option ‘<tt>-i</tt> <var>base</var>’ 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 ‘<tt>8#40</tt>’, the rightmost value in the given output base, and
then ‘<tt>8#40 16#20</tt>’, 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 ‘<tt>0x1_0000_0000</tt>’.
</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 ‘<tt>3_162.277_660_168_379_5</tt>’ (the number of decimal places
shown may vary).
</p>
<a name="index-C_005fBASES_002c-use-of"></a>
<a name="index-OCTAL_005fZEROES_002c-use-of"></a>
<p>If the <tt>C_BASES</tt> option is set, hexadecimal numbers are output in the standard C
format, for example ‘<tt>0xFF</tt>’ instead of the usual ‘<tt>16#FF</tt>’. 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 ‘<tt>077</tt>’ instead of
‘<tt>8#77</tt>’. 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 ‘<tt>[#</tt><var>base</var><tt>]</tt>’ 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
‘<tt>[##16]</tt>’, 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>
<a name="index-arithmetic-operators"></a>
<a name="index-operators_002c-arithmetic"></a>
<p>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><< >></tt></dt>
<dd><p>bitwise shift left, right
</p></dd>
<dt><tt>&</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>< > <= >=</tt></dt>
<dd><p>comparison
</p></dd>
<dt><tt>== !=</tt></dt>
<dd><p>equality and inequality
</p></dd>
<dt><tt>&&</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>= += -= *= /= %= &= ^= |= <<= >>= &&= ||= ^^= **=</tt></dt>
<dd><p>assignment
</p></dd>
<dt><tt>,</tt></dt>
<dd><p>comma operator
</p></dd>
</dl>
<p>The operators ‘<tt>&&</tt>’, ‘<tt>||</tt>’, ‘<tt>&&=</tt>’, and ‘<tt>||=</tt>’ 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><< >></tt></dt>
<dd><p>bitwise shift left, right
</p></dd>
<dt><tt>< > <= >=</tt></dt>
<dd><p>comparison
</p></dd>
<dt><tt>== !=</tt></dt>
<dd><p>equality and inequality
</p></dd>
<dt><tt>&</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>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>= += -= *= /= %= &= ^= |= <<= >>= &&= ||= ^^= **=</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 ‘<tt>-3**2</tt>’ evaluates as ‘<tt>9</tt>’, not
‘<tt>-9</tt>’. Use parentheses where necessary: ‘<tt>-(3**2)</tt>’. This is
for compatibility with other shells.
</p>
<a name="index-mathematical-functions_002c-use-of"></a>
<a name="index-functions_002c-math_002c-use-of"></a>
<p>Mathematical functions can be called with the syntax
‘<var>func</var><tt>(</tt><var>args</var><tt>)</tt>’, 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 ‘<tt>##</tt><var>x</var>’ where <var>x</var> is any character
sequence such as ‘<tt>a</tt>’, ‘<tt>^A</tt>’, or ‘<tt>\M-\C-x</tt>’ gives the value of
this character and an expression of the form ‘<tt>#</tt><var>name</var>’ 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 ‘<tt>$#</tt><var>name</var>’, a standard
parameter substitution which gives the length of the parameter <var>name</var>.
‘<tt>#\</tt>’ is accepted instead of ‘<tt>##</tt>’, 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>
<a name="index-parameters_002c-floating-point"></a>
<a name="index-floating-point-parameters"></a>
<a name="index-float_002c-use-of"></a>
<p>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. ‘<tt>${</tt><var>float</var><tt>}</tt>’ uses the defined format, but
‘<tt>$((</tt><var>float</var><tt>))</tt>’ 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
(‘<tt>~</tt>’, ‘<tt>&</tt>’, ‘<tt>|</tt>’, ‘<tt>^</tt>’, ‘<tt>%</tt>’, ‘<tt><<</tt>’, ‘<tt>>></tt>’ 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 < 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 ‘<tt>f += 0.1</tt>’
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 ‘<tt>f = 0.0</tt>’.
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"> << </a>]</td>
<td valign="middle" align="left">[<a href="Conditional-Expressions.html#Conditional-Expressions" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </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>February 7, 2016</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>
|