6.9.1 Arithmetic & logical operators

Asymptote uses the standard binary arithmetic operators. However, when one integer is divided by another, both arguments are converted to real values before dividing and a real quotient is returned (since this is typically what is intended; otherwise one can use the function int quotient(int x, int y), which returns greatest integer less than or equal to x/y). In all other cases both operands are promoted to the same type, which will also be the type of the result:

+

addition

-

subtraction

*

multiplication

/

division

#

integer division; equivalent to quotient(x,y). Noting that the Python3 community adopted our comment symbol (//) for integer division, we decided to reciprocate and use their comment symbol for integer division in Asymptote!

%

modulo; the result always has the same sign as the divisor. In particular, this makes q*(p # q)+p % q == p for all integers p and nonzero integers q.

^

power; if the exponent (second argument) is an int, recursive multiplication is used; otherwise, logarithms and exponentials are used (** is a synonym for ^).

The usual boolean operators are also defined:

==

equals

!=

not equals

<

less than

<=

less than or equals

>=

greater than or equals

>

greater than

&&

and (with conditional evaluation of right-hand argument)

&

and

||

or (with conditional evaluation of right-hand argument)

|

or

^

xor

!

not

Asymptote also supports the C-like conditional syntax:

bool positive=(pi > 0) ? true : false;

The function T interp(T a, T b, real t) returns (1-t)*a+t*b for nonintegral built-in arithmetic types T. If a and b are pens, they are first promoted to the same color space.

Asymptote also defines bitwise functions int AND(int,int), int OR(int,int), int XOR(int,int), int NOT(int), int CLZ(int) (count leading zeros), int CTZ(int) (count trailing zeros), int popcount(int) (count bits populated by ones), and int bitreverse(int a, int bits) (reverse bits within a word of length bits).