# Scientific calculator

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
1,616pages on
this wiki
Tip 1235 Printable Monobook Previous Next

created 2006 · complexity basic · version 6.0

Here is how to define and use a calculator, using embedded Python.

These two lines go in your vimrc:

```:command! -nargs=+ Calc :py print <args>
:py from math import *
```

Now use it inside Vim:

```:Calc 2**10+5,2**16,2**128
1029 65536 340282366920938463463374607431768211456
:Calc sin(pi/2), log(10)
1.0 2.302585
```

You have to have Vim compiled with Python support, and have Python on your machine. Use `:version` to see which features are included in your Vim.

If you don't have Python support in your Vim, but do have a python command, use this instead

```command! -nargs=+ Calc :!python -c "from math import *; print <args>"
```

You also get complex numbers and other goodies. Google for Python and math. Here is an example from complex math:

```:py from cmath import *
:Calc exp(pi*1j) , " Euler famous identify e^i.pi = -1"
(-1+1.22460635382e-016j)

:Calc sum(range(1,100+1)), "Gauss' famous identity sum(1,100)"
5050
```

## ReferencesEdit

### dllEdit

These days, Vim Python support in Windows is usually via a dll, installed separately from Vim: `:version` shows `+python/dyn`. Go to http://python.org/ to get the Python installer.

To check whether you have Python software that Vim can use (either statically linked or dynamically findable), do `:echo has('python')` — 0 (zero) means "no", anything else (normally 1) means "yes".

• Exponentiation is pow(base,exponent), not `**`
• If you can lay hands on (or compile) a Vim patched with Bill McCarthy's additional floating-point functions (#7 in the list), you won't need to know trigonometric formulas for "missing" functions such as tan(), asin(), atan2() etc., and you'll be able to use `acos(-1)` rather than `4*atan(1)` for π, `exp(1)` rather than a literal value for e.