Next: Formula syntax for Lisp, Previous: References, Up: The spreadsheet
A formula can be any algebraic expression understood by the Emacs Calc
package. Note that calc has the non-standard convention that ‘/’
has lower precedence than ‘*’, so that ‘a/b*c’ is interpreted as
‘a/(b*c)’. Before evaluation by calc-eval
(see calc-eval), variable substitution takes place according to the
rules described above.
The range vectors can be directly fed into the Calc vector functions
like ‘vmean’ and ‘vsum’.
A formula can contain an optional mode string after a semicolon. This
string consists of flags to influence Calc and other modes during
execution. By default, Org uses the standard Calc modes (precision
12, angular units degrees, fraction and symbolic modes off). The display
format, however, has been changed to (float 8)
to keep tables
compact. The default settings can be configured using the option
org-calc-default-modes
.
List of modes:
p20
n3
, s3
, e2
, f4
D
, R
F
, S
T
, t
E
N
L
Unless you use large integer numbers or high-precision-calculation and -display for floating point numbers you may alternatively provide a ‘printf’ format specifier to reformat the Calc result after it has been passed back to Org instead of letting Calc already do the formatting1. A few examples:
$1+$2 Sum of first and second field $1+$2;%.2f Same, format result to two decimals exp($2)+exp($1) Math functions can be used $0;%.1f Reformat current cell to 1 decimal ($3-32)*5/9 Degrees F -> C conversion $c/$1/$cm Hz -> cm conversion, using constants.el tan($1);Dp3s1 Compute in degrees, precision 3, display SCI 1 sin($1);Dp3%.1e Same, but use printf specifier for display taylor($3,x=7,2) Taylor series of $3, at x=7, second degree
Calc also contains a complete set of logical operations, (see Logical Operations). For example
if($1 < 20, teen, string(""))
if("$1" == "nan" || "$2" == "nan", string(""), $1 + $2); E f-1
if(typeof(vmean($1..$7)) == 12, string(""), vmean($1..$7); E
if("$1..$7" == "[]", string(""), vmean($1..$7))
vmean($1..$7); EN
You can add your own Calc functions defined in Emacs Lisp with defmath
and use them in formula syntax for Calc.
[1] The ‘printf’ reformatting is limited in precision because the value passed to it is converted into an ‘integer’ or ‘double’. The ‘integer’ is limited in size by truncating the signed value to 32 bits. The ‘double’ is limited in precision to 64 bits overall which leaves approximately 16 significant decimal digits.