Using fractional exponent with bc
Asked Answered
D

2

26

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?

Diaper answered 23/4, 2013 at 8:49 Comment(14)
The second argument of expr ^ expr must be an integer (scale=0). But 0.5 has scale = 1.Breebreech
Note bc has a native square-root function already: scale=5; sqrt(9)Cryo
If you're not tied to bc, you can use Genius as an alternative. It supports rational exponents out of the box: genius --exec='81^0.75'Affricate
" 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.Unceremonious
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?Nasty
@Nasty e is exp, l is log, -l is --mathlibDiaper
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.Nasty
@Nasty 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.Diaper
The base must also be an integer and cannot be a fraction in bc.Nasty
@Nasty Hmm, the base of what exactly?Diaper
given a^b, a must be int in bc I think.Nasty
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 resultNasty
"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.Diaper
Can I calculate basis e with fractional exponent in bc? e(0.5*l(e)) leads to Math error: overflow: number cannot fitNasty
T
11

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.

Tumbleweed answered 28/8, 2017 at 8:40 Comment(3)
This implementation has precision loss in case of pow(32, 0.2) for exampleChemism
Here is an addition to Alvin's answer using an env.var.Nasty
bc ~/.bcrc -l does not work it is the other way round bc -l ~/.bcrc.Nasty
T
5

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.

Tank answered 23/4, 2013 at 9:5 Comment(10)
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.Diaper
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).Tank
"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?Diaper
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. en.wikipedia.org/wiki/ExponentiationTank
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?Diaper
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 *.Tank
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.Diaper
let us continue this discussion in chatTank
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...Tank
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) nor in matlab (vpi?). Not sure about octave.Diaper

© 2022 - 2024 — McMap. All rights reserved.