Wednesday, May 1, 2024
86
rated 0 times [  86] [ 0]  / answers: 1 / hits: 83630  / 2 Years ago, fri, july 15, 2022, 11:34:02

I am looking for a calculator which can do calculations in the terminal itself, without any other extra prefixes and suffixes.


For example: If I typed something like 10000-9000 in the terminal, the answer should come out as 1000.


Once again I am saying, I just need a quick calculator in the terminal, without any characters added. I know if I switch to Python, it can do that, but I don't want it in such a way.


More From » command-line

 Answers
1

Bash Arithmetic


Another possible solution is to add a simple function for Bash's built-in arithmetic. Put this in your .bashrc file to try it out:



=() {
echo "$(($@))"
}

So now, you don't even need $((...)) anymore, just = which seems natural enough.


Replacement


Another thing if you want to be even faster: you can make it replace p with + and x with *. This will work for that:


=() {
local IFS=' '
local calc="${*//p/+}"
calc="${calc//x/*}"
echo "$(($calc))"
}

= 5 x 5 # Returns 25
= 50p25 # Returns 75

Now you don't even need Shift anymore, the only thing is in front of arithmetic.


Hexadecimal output


Output can be displayed in both decimal and hexadecimal, if so desired. (Note: using x substitution will conflict with the 0x... hex syntax)


=() {
local answer="$(($@))"
printf '%d (%#x)
' "$answer" "$answer"
}

Example:


$ = 16 + 0x10
272 (0x110)

$ = 16**3 + 16**4
69632 (0x11000)

Using bc


If you want slightly more advanced calculations, you can pipe it to bc like so:


=() {
local IFS=' '
local calc="${*//p/+}"
calc="${calc//x/*}"
bc -l <<<"scale=10;$calc"
}

= 'sqrt(2)' # Returns 1.4142135623
= '4*a(1)' # Returns pi (3.1415926532)

The functions provided by bc are as follows (and can be found in man bc):


sqrt ( expression )
The value of the sqrt function is the square root of the expression.
If the expression is negative, a run time error is generated.

s (x) The sine of x, x is in radians.

c (x) The cosine of x, x is in radians.

a (x) The arctangent of x, arctangent returns radians.

l (x) The natural logarithm of x.

e (x) The exponential function of raising e to the value x.

j (n,x)
The Bessel function of integer order n of x.

It also supports if, for, while, and variables like a programming language. Though it may be better to write to a file if you wanted that.


Keep in mind that it will substitute p and x in function/variable names. It may be better to just remove the replacements.


Using gcalccmd


You can also make the function call gcalccmd (from gnome-calculator) like so:


=() {
local IFS=' '
local calc="$*"
# Uncomment the below for (p → +) and (x → *)
#calc="${calc//p/+}"
#calc="${calc//x/*}"
printf '%s
quit' "$calc" | gcalccmd | sed 's:^> ::g'
}

= 'sqrt(2)' # Returns 1.4142135623
= '4^4' # Returns 256

The available functions seem to be (taken straight from the source code), == denotes equivalent functions:


ln()
sqrt()
abs()
int()
frac()
sin()
cos()
tan()
sin⁻¹() == asin()
cos⁻¹() == acos()
tan⁻¹() == atan()
sinh()
cosh()
tanh()
sinh⁻¹() == asinh()
cosh⁻¹() == acosh()
tanh⁻¹() == atanh()
ones()
twos()

[#28345] Sunday, July 17, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tisglitter

Total Points: 307
Total Questions: 103
Total Answers: 119

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
tisglitter questions
Sun, Jan 9, 22, 16:18, 2 Years ago
Wed, Jun 1, 22, 18:03, 2 Years ago
Fri, Dec 10, 21, 14:31, 2 Years ago
;