24

bc, a Linux command-line calculator, is proficient enough to calculate

3^2
9

Even a negative exponent doesn't confuse it:

3^-2
0.11111

Yet it fails when it encounters

9^0.5
Runtime warning (func=(main), adr=8): non-zero scale in exponent

How could it be that bc can't handle this?

And what does the error message mean?


Yes, I've read this and the solution given there:

e(0.5*l(9))
2.99999999999999999998

And yes, it is no good because of precision loss and

A calculator is supposed to solve expressions. You are not supposed to make life easier for the calculator, it is supposed to be the other way around...


This feature was designed to encourage users to write their own functions. Making it a unique calculator that requires a user-defined function to calculate a square root.

It doesn't really bother me to write a function for tangents or cotangents as it looks pretty straightforward given s(x) and c(x). But in my opinion calculating a square root through a user-defined function is a bit too much.

Why anyone uses bc if there's Python out there? Speed?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • The second argument of expr ^ expr must be an integer (scale=0). But 0.5 has scale = 1. – hendrik Aug 12 '13 at 12:30
  • 1
    Note `bc` has a native square-root function already: `scale=5; sqrt(9)` – khaverim Apr 03 '17 at 18:25
  • If you're not tied to bc, you can use [Genius](https://www.jirka.org/genius.html) as an alternative. It supports rational exponents out of the box: `genius --exec='81^0.75'` – Matthias Braun Aug 08 '20 at 08:38
  • 2
    " no good because of precision loss ", yes and if you say: scale=3; and run e(0.5*l(9)) you get: 2.97 even worse... I expected the rounding to occur at the end, but that's wrong. – Martin T. Aug 20 '20 at 07:32
  • If I invoke the sqrt example, I do `echo "e(0.5*l(9))"|bc -l`. I need twice `l` here for the `mathlib` or is the first l for other? – Timo Jul 12 '22 at 11:08
  • 1
    @Timo `e` is exp, `l` is log, `-l` is --mathlib – Antony Hatchkins Jul 12 '22 at 11:47
  • What is the diff between `e(0.5*l(9))` and not using `e` as in `0.5*l(9)`? I see the result but I do not understand the diff. – Timo Aug 03 '22 at 09:58
  • 1
    @Timo Mathematically speaking, `exp(0.5*log(9))` is the same thing as `3^0.5` or `sqrt(3)`. If it is not obvious to you, revisit the properties of the logarithm function. – Antony Hatchkins Aug 03 '22 at 16:48
  • The base must also be an integer and cannot be a fraction in `bc`. – Timo Mar 27 '23 at 12:55
  • @Timo Hmm, the base of what exactly? – Antony Hatchkins Mar 28 '23 at 03:32
  • given `a^b`, `a` must be int in `bc` I think. – Timo Mar 30 '23 at 09:29
  • 1
    Regarding your comment from aug 3 2022, there is an error: `exp(0.5*log(9))=3^0.5`. It must be exp(0.5*log(9))=9^0.5. `3` is the result – Timo Mar 30 '23 at 09:31
  • 1
    "given a^b, a must be int in bc I think." this is wrong: 0.5^3 == 0.125. "exp(0.5*log(9))=9^0.5." this is right. – Antony Hatchkins Mar 31 '23 at 02:33

2 Answers2

9

In bc, b must be an integer in a ^ b. However you can add your own functions to bc like this:

create a file ~/.bcrc, add the following function to it:

define pow(a, b) {
    if (scale(b) == 0) {
        return a ^ b;
    }
    return e(b*l(a));
}

then you can start bc as follows:

bc ~/.bcrc -l

so you can use function pow to do such calculation.

See more here, you can add some more functions to bc.

idealvin
  • 178
  • 1
  • 5
4

bc is very basic and more complex functions not provided by the "math extension" must be implemented in the language itself: it has all you need to do it; in particular "power" is a common example even on wikipedia.

But you may be also interested in reading for example this answer here on SO.

Community
  • 1
  • 1
ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
  • 1
    I would agree with this had not bc have 'power' function already in there. It can calculate `3^2` and even `3^-2` flawlessly. Yet it answers with undecipherable error message when as simple a function as a square root is requested. – Antony Hatchkins Apr 23 '13 at 09:24
  • it has no the power function: it has the integer power function. The error when I ask for `9^0.11` in the bc I can use now is "exp not an integer" which makes it clearer. But "non-zero scale" means exactly the same, since you use `scale=0` to say bc you want "integer precision" (no decimal numbers). – ShinTakezou Apr 23 '13 at 09:40
  • 2
    "it has no the power function: it has the integer power function" makes no sense. Every single other function in bc accepts fractional arguments. Why make an exception for such a widely used function? – Antony Hatchkins Apr 23 '13 at 09:49
  • The other funcs you are talking about can be considered special (if you omit the `-l` switch you won't have them). About ''power'', the "basic" is that X^N is X repeated N times, so N must be integer for this interpretation to work. Extending it to real exponents is not trivial. Why `-l` does not "enhance" the `^`? Or there's no a pow func altogether with sqrt, s, c, ...? These are all other kind of question. https://en.wikipedia.org/wiki/Exponentiation – ShinTakezou Apr 23 '13 at 10:02
  • Yes, this makes sense. But the meaning of 'basic' is given from the point of view of the bc *author*, not from the point of view of bc *user*. I'm not really sure who needs such distinction except for the author. In fact I don't understand the sense of `-l` altogether. Some backwards compatibility issues? Is there any use case for bc without `-l`? – Antony Hatchkins Apr 23 '13 at 10:14
  • Simply I suppose you are using the wrong tool for your aim. I have "interpreted" Bc as a tool to experiment with implementing algo for computations, using arbitrary precision. When you do so, you rarely need more than `+, -, / and *`. – ShinTakezou Apr 23 '13 at 10:24
  • Yep, I was using it as a command-line calculator for a bunch of years. Is it better than, say, python or matlab in the use case you described? They at least have a debugger. – Antony Hatchkins Apr 23 '13 at 10:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28729/discussion-between-shintakezou-and-antony-hatchkins) – ShinTakezou Apr 23 '13 at 11:19
  • indeed I can't because of websense:) anyway: python and octave/matlab may be better but it depends on the context; they need more memory and more disk space to be installed, but rarely it's an issue... if you have both and you have installed them, use them instead to have the full power of math... – ShinTakezou Apr 23 '13 at 11:21
  • What I like about bc is that it launches instantly and it has all the history of the computations right in the console. But python is pretty good at those things as well. To come to think about it, it's not so convenient to operate arbitrary-precision neither in python ([Decimal](http://docs.python.org/2/library/decimal.html)) nor in matlab ([vpi](http://www.mathworks.com/matlabcentral/fileexchange/22725)?). Not sure about octave. – Antony Hatchkins Apr 23 '13 at 18:19