Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(12 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1235
 
|id=1235
 
|previous=1234
 
|previous=1234
 
|next=1237
 
|next=1237
|created=May 20, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
|author=Anon
+
|author=
|version=n/a
+
|version=6.0
 
|rating=145/49
 
|rating=145/49
|category1=
+
|category1=Python
 
|category2=
 
|category2=
 
}}
 
}}
 
Here is how to define and use a calculator, using embedded Python.
 
Here is how to define and use a calculator, using embedded Python.
   
These two lines go in your vimrc:
+
These two lines go in your [[vimrc]]:
 
<pre>
 
<pre>
:command! -nargs=+ Calc :py print &lt;args&gt;
+
:command! -nargs=+ Calc :py print <args>
 
:py from math import *
 
:py from math import *
 
</pre>
 
</pre>
   
 
Now use it inside Vim:
 
Now use it inside Vim:
 
 
<pre>
 
<pre>
:Calc 2**10+5,2**16
+
:Calc 2**10+5,2**16,2**128
  +
1029 65536 340282366920938463463374607431768211456
1029 65536
 
 
 
:Calc sin(pi/2), log(10)
 
:Calc sin(pi/2), log(10)
 
1.0 2.302585
 
1.0 2.302585
 
</pre>
 
</pre>
   
 
You have to have Vim compiled with Python support, and have Python on your machine[[#dll|†]]. Use <code>:version</code> to see which features are included in your Vim.
You also get complex numbers and other goodies. Google for Python and math.
 
   
 
If you don't have Python support in your Vim, but do have a python command, use this instead
==Comments==
 
 
<pre>
Here is an example from complex math
 
 
command! -nargs=+ Calc :!python -c "from math import *; print <args>"
 
</pre>
   
 
You also get complex numbers and other goodies. Google for Python and math. Here is an example from complex math:
 
<pre>
 
<pre>
:from cmath import *
+
:py from cmath import *
 
:Calc exp(pi*1j) , " Euler famous identify e^i.pi = -1"
 
:Calc exp(pi*1j) , " Euler famous identify e^i.pi = -1"
 
(-1+1.22460635382e-016j)
 
(-1+1.22460635382e-016j)
   
:Calc reduce(lambda a,b:a+b,range(1,100+1)), "Gauss' famous identity sum(1,100)"
+
:Calc sum(range(1,100+1)), "Gauss' famous identity sum(1,100)"
 
5050
 
5050
 
</pre>
 
</pre>
   
  +
==References==
See references:
 
http://docs.python.org/lib/module-cmath.html
+
*http://docs.python.org/lib/module-cmath.html
http://mathworld.wolfram.com/EulerFormula.html
+
*http://mathworld.wolfram.com/EulerFormula.html
   
  +
===dll===
----
 
  +
These days, Vim Python support in Windows is usually via a dll, installed separately from Vim: <code>:version</code> shows <code>+python/dyn</code>. Go to http://python.org/ to get the Python installer.
You have to have Vim compiled with Python support. Use <tt>:version</tt> to see which features are included in your Vim.
 
   
  +
To check whether you have Python software that Vim can use (either statically linked or dynamically findable), do <code>:echo has('python')</code> — 0 (zero) means "no", anything else (normally 1) means "yes".
----
 
If you don't have Python compiled into your Vim, use this instead
 
<pre>
 
command! -nargs=+ Calc :r! python -c "from math import *; print &lt;args&gt;"
 
</pre>
 
   
 
==Comments==
----
 
  +
If your Vim (7.2 or later) is compiled with {{help|+float|prefix=no}}, you can do scientific calculations in Vim even without Python.
  +
* Use the numeric operators (except %) and floating point functions defined by Vim, see
  +
** {{help|expr5}}
  +
** {{help|expr6}}
  +
** {{help|expr7}}
  +
** {{help|function-list}} and scroll down to <code>Floating point computation</code>
  +
* Exponentiation is {{help|pow()|pow(|prefix=no}}base,exponent), not <code>**</code>
  +
* {{help|:echo|prefix=no}} gives you 6-digit precision by default, use {{help|printf()|prefix=no}} for user-specified precision, up to about 15 significant digits on an Intel PC ''(I don't know how many on a Mac PPC or an an IBM System-z).''
  +
* If you can lay hands on (or compile) a Vim patched with [http://groups.google.com/group/vim_dev/web/vim-patches 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 <code>acos(-1)</code> rather than <code>4*atan(1)</code> for ''π'', <code>exp(1)</code> rather than a literal value for ''e''.
  +
:— [[User:Tonymec|Tonymec]] 03:27, May 5, 2010 (UTC)
  +
:*These extra floating point functions will be part of Vim 7.3. They were included in the pre-release on [http://code.google.com/p/vim/source/detail?r=a8afba7027ae49fa057927ffc00cbdf76a76a4ba May 21, 2010]. [[User:JamesVega|JamesVega]] 18:53, May 21, 2010 (UTC)

Latest revision as of 06:15, 13 July 2012

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

References[]

dll[]

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

Comments[]

If your Vim (7.2 or later) is compiled with +float, you can do scientific calculations in Vim even without Python.

  • Use the numeric operators (except %) and floating point functions defined by Vim, see
  • Exponentiation is pow(base,exponent), not **
  • :echo gives you 6-digit precision by default, use printf() for user-specified precision, up to about 15 significant digits on an Intel PC (I don't know how many on a Mac PPC or an an IBM System-z).
  • 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.
Tonymec 03:27, May 5, 2010 (UTC)
  • These extra floating point functions will be part of Vim 7.3. They were included in the pre-release on May 21, 2010. JamesVega 18:53, May 21, 2010 (UTC)