Next: , Previous: References, Up: The spreadsheet

3.5.2 Formula syntax for Calc

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 variable org-calc-default-modes.

     p20           set the internal Calc calculation precision to 20 digits
     n3 s3 e2 f4   Normal, scientific, engineering, or fixed
                   format of the result of Calc passed back to Org.
                   Calc formatting is unlimited in precision as
                   long as the Calc calculation precision is greater.
     D R           angle modes: degrees, radians
     F S           fraction and symbolic modes
     N             interpret all fields as numbers, use 0 for non-numbers
     E             keep empty fields in ranges
     L             literal

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
     vmean($2..$7)        Compute column range mean, using vector function
     vmean($2..$7);EN     Same, but treat empty fields as 0
     taylor($3,x=7,2)     Taylor series of $3, at x=7, second degree

Calc also contains a complete set of logical operations. For example

     if($1<20,teen,string(""))  "teen" if age $1 less than 20, else empty

Note that you can also use two org-specific flags T and t for durations computations Durations and time values.


[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.