<HTML> <HEAD> <!-- This HTML file has been created by texi2html 1.51 from ProgrammersManual.texinfo on 4 March 1997 --> <TITLE>LambdaMOO Programmer's Manual - Arithmetic</TITLE> </HEAD> <BODY> Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_13.html">previous</A>, <A HREF="ProgrammersManual_15.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>. <P><HR><P> <H3><A NAME="SEC14" HREF="ProgrammersManual_toc.html#TOC14">Arithmetic Operators</A></H3> <P> All of the usual simple operations on numbers are available to MOO programs: </P> <PRE> + - * / % </PRE> <P> These are, in order, addition, subtraction, multiplication, division, and remainder. In the following table, the expressions on the left have the corresponding values on the right: </P> <PRE> 5 + 2 => 7 5 - 2 => 3 5 * 2 => 10 5 / 2 => 2 5.0 / 2.0 => 2.5 5 % 2 => 1 5.0 % 2.0 => 1.0 5 % -2 => 1 -5 % 2 => -1 -5 % -2 => -1 -(5 + 2) => -7 </PRE> <P> Note that integer division in MOO throws away the remainder and that the result of the remainder operator (<SAMP>`%'</SAMP>) has the same sign as the left-hand operand. Also, note that <SAMP>`-'</SAMP> can be used without a left-hand operand to negate a numeric expression. </P> <BLOCKQUOTE> <P> <EM>Fine point:</EM> Integers and floating-point numbers cannot be mixed in any particular use of these arithmetic operators; unlike some other programming languages, MOO does not automatically coerce integers into floating-point numbers. You can use the <CODE>tofloat()</CODE> function to perform an explicit conversion. </BLOCKQUOTE> <P> The <SAMP>`+'</SAMP> operator can also be used to append two strings. The expression </P> <PRE> "foo" + "bar" </PRE> <P> has the value </P> <PRE> "foobar" </PRE> <P> Unless both operands to an arithmetic operator are numbers of the same kind (or, for <SAMP>`+'</SAMP>, both strings), the error value <CODE>E_TYPE</CODE> is raised. If the right-hand operand for the division or remainder operators (<SAMP>`/'</SAMP> or <SAMP>`%'</SAMP>) is zero, the error value <CODE>E_DIV</CODE> is raised. </P> <P> MOO also supports the exponentiation operation, also known as "raising to a power," using the <SAMP>`^'</SAMP> operator: </P> <PRE> 3 ^ 4 => 81 3 ^ 4.5 error--> E_TYPE 3.5 ^ 4 => 150.0625 3.5 ^ 4.5 => 280.741230801382 </PRE> <P> Note that if the first operand is an integer, then the second operand must also be an integer. If the first operand is a floating-point number, then the second operand can be either kind of number. Although it is legal to raise an integer to a negative power, it is unlikely to be terribly useful. </P> <P><HR><P> Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_13.html">previous</A>, <A HREF="ProgrammersManual_15.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>. </BODY> </HTML>