# Numeric Operators

#### Numeric Functions

The familiar mathematical operators + - * / operate on numeric expressions in the expected way.

Note that + and - have different meanings when applied to sets, and that + has yet another meaning (concatenation) when applied to strings.

#### Mathematical Operators

In all the following, the N argument is a number.

abs(N) computes the absolute value of is argument.

atan(N) computes arctangent, and returns its result in radians.

ceil(N) returns the first integer that is larger than N.

degrees(N) converts radians to degrees.

exp(N) returns the exponential of N, en.

floor(N) returns the first integer that is smaller than N.

log(N) computes the natural logarithm of its argument, which should be a number, a numeric attribute, or an expression that can be interpreted as a number.

mod(N,dividend) computes the remainder of A divided by B.

pow(N,K) raises N to the power of K.

radians(degrees) converts its argument, in degrees, to radians.

rand() returns a pseudo-random number between 0 and 1.

round(N) rounds the value of its argument to the nearest integer.

sin(N), cos(N), and tan(N) are the familiar trigonometric functions, taking arguments in radians.

sqrt(N) computes the square root of its argument.

#### sum(), sum_if, avg and avg_if()

In the following operators, the parameter expression may be an attribute reference, a literal number, or an expression.

sum(group,expression) computes the sum of a series of expressions. For example,

> `sum(child,$WordCount)`

computes the total word count of all the children of a note. 'group' may be any group designator {children,descendants,sibling,ancestor,all}. In addition, 'group' may take a single argument that designates a particular note other than this; for example

> `sum(children(/agents/books),$TextLength)`

sum_if(group,condition,expression) computes the sum of the values for expression for each item in the group, ignoring any notes for which if condition is not true. For example:

> `sum_if(children, $Checked, $Price)`

totals the prices of all the note's children which have been checked.

avg(group,expression) and avg_if(group,condition,expression) compute the mean of the values for expression for each item in the group. For example:

> `avg_if(group,condition,expression)`

computes the sum or the mean of a value computed in each note for a group that meets the designated condition.

> `sum_if(child,$Checked,$Qty*$Price)`

> `avg_if(child,$Checked,$Qty*$Price)`

compute the total price and the average price of checked items that are children of this note.

#### Converting Numbers to Strings

[Number].format(decimal_places[, width]) converts a number to a string. decimal_places specified the number of digits to be displayed after the decimal point, and may be zero to display an integer. If the optional width is specified, additional spaces will be added to the left of the number to pad the result to a minimum width. For example:

`$Price.format(2)`

converts the number $Price to a string, rounding to two decimal points.

If the argument to format is “l”, the number is formatted using local numeric formatting conventions. For example, commas are inserted before thousands and millions in the US.

If the argument to format is “$”, the number is formatted using local currency formatting conventions. For example, the number 1.5 would be formatted as $1.50” in the US, but “£1.50” in the UK.

If the argument to format is “$0”, the number is formatted using local currency and rounded to the nearest whole unit. For example, 1000.4 would be formatted as “$1000” in the US.