Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fractional exponent with bc

Tags:

exponent

sqrt

bc

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?

like image 820
Antony Hatchkins Avatar asked Apr 23 '13 08:04

Antony Hatchkins


People also ask

What is the rule of fractional exponent?

What is the Rule for Fractional Exponents? In the case of fractional exponents, the numerator is the power and the denominator is the root. This is the general rule of fractional exponents. We can write xm/n as n√(xm).


2 Answers

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.

like image 119
idealvin Avatar answered Sep 28 '22 04:09

idealvin


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.

like image 23
ShinTakezou Avatar answered Sep 28 '22 04:09

ShinTakezou