Hacker News new | past | comments | ask | show | jobs | submit login

> 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



This 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-fil...


I 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).


alias calc='noglob calc-actual-implementation'

Disables globs for that particular command in Zsh.


I write out calculations in an emacs lisp buffer. This lets me adjust them, correct typos, or save the whole thing.


I 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!


why not just use bc -l ?


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: