On Feb 5, 11:29 pm, oversky <[email protected]> wrote: > http://vimrc-dissection.blogspot.com/2011/01/vim-inline-calculator-re... > > This inline calculator is very handy. > It doesn't required Python to be installed on the system. > And we can define variables. > > I am trying to improve it to use ^ as the power operator, like 2^3 = > 8, > I know vim has internal function pow(2,3) = 8. > But 2^3 is more natural to me as I use latex and matlab a lot. > > Following is the result of my modified version. > > 2^3 = 8.0 > 2.2^3.3 = 13.489469 > -2^3 = -8.0 > -2^4 = -16.0 > 2 ^ 3 = 8.0 > 2^3 - 3^2 = -1.0 > (-2)^3 = -8.0 > (-2)^4 = 16.0 > (-2.2)^(4) = 23.4256 > (22.3 ^ 3.2) = 20633.626351 > ((2^3)) = 8.0 > 2^3^4^0 = 1.0 > 2^ 3^ 4^5 = 1.152922e18 > 2^(2^2)^2 = 256.0 > > It handles simple equations very well. > However, I don't know how to make it deal with such equation: > > (2+3)^(5-3) > > because regexp can't find the matching bracket. > Is there a internal function acting like v_ib > > function! CalcLine(ln) > let tag_marker = "#" > let s = getline(a:ln) > > " remove old result if any > let x = substitute( s, '[[:space:]]*=.*', "", "" ) > " strip the tag, if any > let x1 = substitute( x, '[a-zA-Z0-9]\+'.tag_marker.'[[:space:]]*', > "", "" ) > " replace values by the tag > let x1 = substitute( x1, tag_marker.'\([a-zA-Z0-9]\+\)', > '\=CalcGetValue(submatch(1))', 'g' ) > > """"""""""""""""""""""""""""""""" modified section > """"""""""""""""""""""""""""""""""" > " replace base^exponent by power(base,exponent) > while matchstr( x1, '\^') == '^' > > let floating = '\d\+\%(\.\d\+\)\=' "define floating number pattern > let element = '\(\(([-+]\='.floating.')\)\|\('.floating.'\)\|\ > ((\=pow('.floating.','.floating.'))\=\)\)' > > let power = matchstr( x1, element.'\s*\^\s*'.element) > let base = matchstr( power, element.'\s*\^') > let base = substitute( base, '\s*\^', "", "") > let exponent = matchstr( power, '\^\s*'.element) > let exponent = substitute( exponent, '\^', "", "") > let y = printf('%f',eval( 'pow('.base.','.exponent.')' )) > let x1 = substitute( x1, power, y, "") > endwhile > > """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" > """"""""""""""""""" > > " evaluate > let v = 0 > try > let v = "".eval(x1) > catch /E806:/ > " VIM can't convert float to string automagically, apply printf("%g") > let v = "".eval('printf( "%g", '.x1.')') > endtry > " finish the job > call setline(a:ln, x.' = '.v) > endfunction
How is the project progressing? Bill -- You received this message from the "vim_use" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php
