Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there any other command line calculator that supports log, n! calculations ? At least bc can't do that, it told me parse error

It's best if I could use script, e.g echo '5!' | program

share|improve this question

4 Answers 4

up vote 6 down vote accepted

bc supports the natural logarithm if invoked with the -l flag. You can calculate the base-10 or base-2 log with it:

$ bc -l
...
l(100)/l(10)
2.00000000000000000000

l(256)/l(2)
8.00000000000000000007

I don't think there's a built-in factorial, but that's easy enough to write yourself:

$ bc
...
define fact_rec (n) { 
  if (n < 0) {
    print "oops";
    halt;
  }
  if (n < 2) return 1;
  return n*fact_rec(n-1);
}
fact_rec(5)
120

Or:

define fact_it (n) {
  if (n < 0) {
    print "oops";
    halt;
  }
  res = 1;
  for (; n > 1; n--) {
    res *= n;
  }
  return res;
}
fact_it(100)
93326215443944152681699238856266700490715968264381621468592963895217\
59999322991560894146397615651828625369792082722375825118521091686400\
0000000000000000000000
share|improve this answer
1  
checking for n==1 results in endless loop if you give it a negative number or zero. use n<=1 instead as in the bc man page. Really, though, it should return an error of negative numbers - according to en.wikipedia.org/wiki/… this is a known problem with using to recursion to calculate factorials. –  cas Jul 29 '12 at 8:49
    
this isn't meant as a study in the perfect implementation of factorial, just a simple demo to show that bc has functions, including recursive functions. –  Mat Jul 29 '12 at 8:53
    
true, but someone will someday read that example function and say "great, I was wondering how to do that" and use it. BTW, i'm about as far from being a maths geek as you can be and still be a computer geek...but since i was correcting an error I thought i'd better do a little research first :) –  cas Jul 29 '12 at 8:55
    
Ok, there you go. Correct recursive and iterative versions... –  Mat Jul 29 '12 at 9:01
    
not quite correct: 0!=1, just as 1!=1. At least, that's what wikipedia says. –  cas Jul 29 '12 at 9:03

Orpie is the calculator for calculator and command line geeks. It emulates a modern HP RPN calculator, which is of course the only true way to calculate.

If you are a calculator heretic, raised on TIs, Casios, and such, there are many RPN tutorials online with which you may begin your re-education. The Orpie manual will eventually be of some use to you, once you get the RPN Way of thinking down.

To compute 5! in Orpie, just type it as you'd write it: 5 !. You can press Enter between them to push 5 onto the stack first, but it isn't necessary.

To compute log10(5), type 5 Enter ' l o Enter. You do have to push the 5 onto the stack first in this case, since the next keystroke isn't an operator. That single quote character enters command abbreviation mode, which lets you start typing log10, which you can uniquely identify with just the first two characters. (l alone gets you the natural log function, ln instead.)

As with any RPN calculator, you can get really fast with Orpie, with a bit of practice.

Orpie is in the stock Ubuntu repos. FreeBSD includes it in Ports, as math/orpie. On OS X, you can install it through Homebrew. Third-party packages may be available for other OSes. Building from source may be a bit of a chore, since it is written in OCaml, and you probably don't have an OCaml development environment installed. Getting one set up isn't particularly hard, though.

share|improve this answer

There's always GNU Octave, the emacs of command-line calculators (for when you want a complete development environment and programming language built-in to your calculator, and thousands of optional add-ons)

or R if stats are more your thing.

I mostly just use bc -l to get the standard math library loaded...I even have bc aliased to bc -l. See Mat's answer for info on defining a factorial function....although the bc man page defines it as:

define f (x) {
  if (x <= 1) return (1);
  return (f(x-1) * x);
}

Checking for <=1 rather than == 1 prevents an endless loop if you happen to feed it a negative number (should be an error) or zero (valid).

share|improve this answer

If you have Mac Ports:

$ sudo /opt/local/bin/port install orpie
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.