4 ms·
> I stole the calc bash function from Addy Osmani in 2012 and have used it daily since. Do yourself a favor # ~/.bashrc function py { python3 -c "from
by mgraczyk 4y ago
> I stole the calc bash function from Addy Osmani in 2012 and have used it daily since.
Do yourself a favor
# ~/.bashrc
function py {
python3 -c "from math import *; print($*)"
}
$ py 3 + 3
6
$ py "sqrt(7)"
2.6457513110645907
$ py pi / 5
0.6283185307179586
- mixmastamyk 4y agoThis is very neat at the CLI. However I always keep a terminal tab/pane with ptpython open. Add math, os, sys, datetime, work libs, etc. to your startup file (see below). With pip, it’s incredible. https://www.assertnotmagic.com/2018/06/30/python-startup-file/ https://www.assertnotmagic.com/2018/06/30/python-startup-fil...
- LanceH 4y agoI write out calculations in an emacs lisp buffer. This lets me adjust them, correct typos, or save the whole thing.
- noisy_boy 4y agoI often forget to quote * which results in multiplications not working when done using the function approach mentioned above. No such issues with the startup approach. Plus I can store the results in variables and play with them (anything more complicated is more suited for LibreOffice Calc).
- Siira 4y agoalias calc='noglob calc-actual-implementation' Disables globs for that particular command in Zsh.
- ImprovedSilence 4y agowhy not just use bc -l ?
- remram 4y ago(not OP, but I have a similar shortcut) Mostly because I am familiar with Python and I don't want to learn the syntax for the advanced expressions I will seldom use. c() s() etc instead of cos() sin() is not too bad, but if I have to do a specific rounding/truncation/modulo, I probably know how to do it in Python and I definitely don't know how to do it in bc syntax. Of course a similar shortcut can be defined for your language of choice, if that's JavaScript/Ruby/R/... then you probably want to use that.
- kretaceous 4y agoI have a similar one! m () { node -p "$*" } Not as elegant because it doesn't import all the Math functions but serves me for small calculations!