I use the GNU Emacs Calc, an RPN calculator, exclusively unless I’m away from a computer; as a programming, scientific and general calculator. This is my cheat sheet for my oft-used functions. There’s also a printable, broad-coverage official reference card (PDF). True to Emacs’ documenting tradition, the manual documents all features extensively, with examples too.

Programming

Binary

  • Set binary radix: d 2
    • Prefix with O to display two’s complement as 2##1100 instead of -2#0100
      • Notice #-count for disambiguation
    • Other useful radices: 16, 0 and 8
  • Set word size (32 bits by default) before doing anything interesting: b w
    • Set negative word size if you want to work on signed numbers
      • Arithmetic -1320 >> 4 = 65530 (base = 16) instead of -83 (base = -16)
    • Manually clip values to $2^w$: b c
    • Set word size per-operation (except shifts/rotates) with prefix: C-u SIZE
      • Example: C-u 4 b n flips bits and clips to $2^4$
  • Enable leading zero display: d z
    • Useful to see the entire word
  • Enable digit grouping: d g
  • Input numbers in a particular radix: R#DIGITS e.g. 16#f00ba4
  • Most useful binary operations start with prefix b
    • Binary NOT: b n
    • Binary AND, OR, XOR, DIFF: b a, b o, b x, b d
      • Takes two operands from stack
      • DIFF is actually bits set in X but not in Y i.e. X & ~Y
    • Shift: b l, b r, b R, b L
      • Takes 1 or prefix as second operand; use top-of-stack with Hyperbolic prefix H
      • Uppercase does arithmetic shift; arithmetic left shift makes sense with negative arguments
    • Rotate: b t
      • Takes 1 or prefix as second operand; use top-of-stack with Hyperbolic prefix H
      • Use negative second operand for rotate-right

Misc

  • Integer divide quotient / reminder: \ / %
    • Note: floors (rounds → -∞) unlike the truncating C integer division (rounds → 0)
  • Floor, Truncate, Ceil, Round: F, I R, I F, R
  • Vector
    • (Euclidean) Length / l-2 norm: A
    • Dot / Cross products: * / V C
    • Determinant / Inverse / Transpose: V D / & / v t
    • Element-wise add (map): V M +
    • Sum elements (reduce): V R +
    • Pack / Unpack / Reverse: v p / v u / v v
  • Convert to new unit / base unit: u c / u b
    • Input with unit using algebraic input e.g. ' 60 km/hr
      • Temperature units: degC, degF, K
  • Prime numbers
    • Test prime: k p
    • Prime factorize: k f
    • Next/Previous prime: k n, I k n
  • Random number: k r
    • Between [0, M) or (M, 0] depending on sign(M); M is top-of-stack or prefix
    • Use 1. (mind the decimal point) to generate numbers between [0, 1)
    • Random again (same M): k a
  • Shuffle $\binom{n}{k}$: k h
    • Takes n and k from stack and returns a list
    • Shuffle top-of-stack list with prefix: C-u -1
  • Permutation (${}_n\mathrm{ P }_k$) and Combination (${}_n\mathrm{ C }_k$): H k c, k c
    • Factorial: !

Scientific

  • Get constants
KeyConstantNameValue
P$\pi$Pi3.14159265359
H P$e$Euler’s number2.71828182846
I P$\gamma$Euler’s constant0.577215664902
I H P$\phi$Golden ratio1.61803398875
  • Floating-point / Fixed-point / Scientific notation: d n / d f / d s
  • Set precision: p
  • Log to top-of-stack as base: B
    • Integer log to top-of-stack as base f I
    • $log_{10}$: H L
    • $ln$: L
  • Square root: Q
    • Integer square root: f Q
    • n-th root: I ^
  • Reciprocal: &
  • Convert to fraction/float: c F / c f
  • Change to radians/degrees: m r / m d
  • Calculate sin and cos: calc-sincos

General

  • Edit top-of-stack: backtick
  • Rotate stack: C-u 0 M-TAB