Here are the normal math operators that work on integers and floating point numbers:

+
(add) 

(subtract; or a negative number, e.g.int x = 5
) 
*
(multiply)
Here are somewhatunique math operators that work with integers:

/
(quotient:13 / 5
is 2 because 5 goes into 13 two times) 
%
(remainder:13 % 5
is 3 because the remainder of 5/13 is 2)
Note that /
works as you would expect with floating point numbers
(e.g. 1.2 / 5.6
is about 0.214).
Precedence works the same as you would expect: * / %
happen before + 
so (5 + 6 % 4  (3 + 4) /
2)
equals 4 as you would expect. You can use parentheses to
clarify the math.
Arithmetic shorthand
You can use the following shorthand for changing the values of variables. The shorthand form is shown, then the equivalent form is described in comments.
What’s the difference between x++
and ++x
(or x
and x
)? An
assignment expression actually has a value. So x = 5
turns into the
value 5 (and changes x
to equal 5). That makes the following
possible: y = x = 5
, which sets y
equal to the value x = 5
,
which is the value 5.
The difference is that x++
gives back the old value of x
(but also
increments x
), while ++x
increments x
then gives its value. So
if x
is 5, and we have y = x++
then y
is 5 and x
is 6. On the
other hand, if we have y = ++x
then y
is 6 and x
is 6.
Boolean operators
Variables of type bool
have the following special operators:
Mathy example
This example shows use of several mathematical functions and operators.
Here is the example again, but this time with a variable x
:
In these examples, various useful math functions were used. Here is a larger list. These all come from cmath
(which must be “included”).
pow(a, b)
– raisea
to the powerb
;a
andb
should have typedouble
exp(a)
– raise Euler’s number e to the powera
(adouble
)log(a)
– find the natural logarithm ofa
(adouble
)log10(a)
– find the “base 10” logarithm ofa
sqrt(a)
– find the square root ofa
(adouble
)sin(a)
– the result of sine(a) (a
is adouble
, in radians)cos(a)
,tan(a)
– obviousacos(a)
,asin(a)
– the arc cosine/sine ofa
; answer is in radians (a
is adouble
, as is the answer)
Conversion example
This code converts teaspoons to other units of measurement.