Sophie

Sophie

distrib > Mandriva > 9.1 > i586 > by-pkgid > 0195c26144d6a6d8437da6bf9d0aeff3 > files > 52

zsh-doc-4.1.0-0.dev5.4mdk.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                      "http://www.w3.org/TR/html40/loose.dtd">
<HTML>
<!-- Created on August, 9 2002 by texi2html 1.65 -->
<!--
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>
Send bugs and suggestions to <texi2html@mathematik.uni-kl.de>

-->
<HEAD>
<TITLE>zsh: Arithmetic Evaluation</TITLE>

<META NAME="description" CONTENT="zsh: Arithmetic Evaluation">
<META NAME="keywords" CONTENT="zsh: Arithmetic Evaluation">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.65">

</HEAD>

<BODY LANG="en" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">

<A NAME="SEC37"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_9.html#SEC36"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_11.html#SEC38"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_9.html#SEC34"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_11.html#SEC38"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_24.html#SEC195">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H1> 10. Arithmetic Evaluation </H1>
<!--docid::SEC37::-->
<A NAME="IDX114"></A>
<A NAME="IDX115"></A>
<A NAME="IDX116"></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>.  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 is always double precision.
<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 &quot;</TT><VAR>...</VAR><TT>&quot;</TT>'.  For example, the following statement
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>(( val = 2 + 1 ))
</pre></td></tr></table><P>

is equivalent to
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>let &quot;val = 2 + 1&quot;
</pre></td></tr></table><P>

both assigning the value 3 to the shell variable <TT>val</TT> and returning a
zero status.
</P>
<P>

<A NAME="IDX117"></A>
Integers can be in bases other than 10.
A leading `<TT>0x</TT>' or `<TT>0X</TT>' denotes hexadecimal.
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>

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>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>typeset -i 16 y
print $(( [#8] x = 32, y = 32 ))
print $x $y
</pre></td></tr></table><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>

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.
</P>
<P>

<A NAME="IDX118"></A>
<A NAME="IDX119"></A>
An arithmetic expression uses nearly the same syntax, precedence, and
associativity of expressions in C.
The following operators are supported (listed in decreasing order
of precedence):
</P>
<P>

</P>
<DL COMPACT>
<DT><TT>+ - ! ~ ++ --</TT>
<DD>unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement
<DT><TT>&lt;&lt; &gt;&gt;</TT>
<DD>bitwise shift left, right
<DT><TT>&amp;</TT>
<DD>bitwise AND
<DT><TT>^</TT>
<DD>bitwise XOR
<DT><TT>|</TT>
<DD>bitwise OR
<DT><TT>**</TT>
<DD>exponentiation
<DT><TT>* / %</TT>
<DD>multiplication, division, modulus (remainder)
<DT><TT>+ -</TT>
<DD>addition, subtraction
<DT><TT>&lt; &gt; &lt;= &gt;=</TT>
<DD>comparison
<DT><TT>== !=</TT>
<DD>equality and inequality
<DT><TT>&amp;&amp;</TT>
<DD>logical AND
<DT><TT>|| ^^</TT>
<DD>logical OR, XOR
<DT><TT>? :</TT>
<DD>ternary operator
<DT><TT>= += -= *= /= %= &amp;= ^= |= &lt;&lt;= &gt;&gt;= &amp;&amp;= ||= ^^= **=</TT>
<DD>assignment
<DT><TT>,</TT>
<DD>comma operator
</DL>
<P>

The operators `<TT>&amp;&amp;</TT>', `<TT>||</TT>', `<TT>&amp;&amp;=</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>

<A NAME="IDX120"></A>
<A NAME="IDX121"></A>
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 ASCII
value of this character and an expression of the form `<TT>#</TT><VAR>foo</VAR>'
gives the ASCII value of the first character of the value of the
parameter <VAR>foo</VAR>.  Note that this is different from the expression
`<TT>$#</TT><VAR>foo</VAR>', a standard parameter substitution which gives the
length of the parameter <VAR>foo</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>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>((val2 = val1 * 2))
</pre></td></tr></table><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="IDX122"></A>
<A NAME="IDX123"></A>
<A NAME="IDX124"></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="IDX125"></A>
<A NAME="IDX126"></A>
<A NAME="IDX127"></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. `<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>&amp;</TT>', `<TT>|</TT>', `<TT>^</TT>', `<TT>%</TT>', `<TT>&lt;&lt;</TT>', `<TT>&gt;&gt;</TT>' and their
equivalents with assignment) is given a floating point argument, it will be
silently rounded down to the next integer.
</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>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>for (( f = 0; f &lt; 1; f += 0.1 )); do
# use $f
done
</pre></td></tr></table><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.
<A NAME="Conditional Expressions"></A>
</P>
<P>

<HR SIZE="6">
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_9.html#SEC34"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_11.html#SEC38"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_24.html#SEC195">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="zsh_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<BR>
<FONT SIZE="-1">
This document was generated
by <I>Peter Stephenson</I> on <I>August, 9 2002</I>
using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html
"><I>texi2html</I></A>
</FONT>

</BODY>
</HTML>