If you are a Linux, Free / Open BSD or other free OS user, there is a possibility that the command line interface is not alien to you. In this case, you can use the command shell for simple arithmetic operations. To do this, you do not need to install additional programs, everything is already in the basic set of the operating system. They allow you to qualitatively replace the usual calculator on the accountant's table.
Arithmetic operations with integers in bash
will look like this:
$((expression)) $(( n1+n2 )) $(( n1/n2 )) $(( n1*n2 )) $(( n1-n2 ))
For example:
$ echo $((15+25)) $ 40
On the bash
man
page , in the ARITHMETIC EVALUATION
section, you can familiarize yourself with the priority of executing operator actions. And, by the way, you can get the same result using the expr
command, instead of using double brackets in output commands.
$ expr 15 + 25 $ 40
Integer expressions are nice, but not enough even for a calculator. Fortunately, there is bc
in the set - C-like interactive interpreter. We will not waste time on addition and subtraction, we proceed immediately to more interesting activities.
$ echo 7^7 |bc 823543
This is a better calculator, since it allows you to get any number of numbers in the fractional part using the scale
variable. Beware fake proc versions of bc
, since they support only 99 decimal places!
$ echo 'scale=30;sqrt(2)' | bc 1.414213562373095048801688724209
2 more important variables: ibase
and obase
indicate the basis of incoming and outgoing numbers.
$ echo 'ibase=16;obase=A;FF' | bc 255
Here, by the way, there is an ambush. Look at these two examples. It seems to be trying to do the same, but the result is different. The whole point is that in the first example ibase=2
, but obase=10
takes the value 2 due to the fact that ibase
determines from the base obase
and 10 becomes equal to 2. To break this circle, you need to use hex.
$ echo 'ibase=2;obase=10;10' | bc 10 $ echo 'ibase=2;obase=A;10' | bc 2
In case of multiple exponentiation of a number, it is important to place brackets as necessary, because bc
is right associative and the result may not be the one you expected.
$ echo '4^4^4' |bc 13407807929942597099574024998205846127479365820592393377723561443721\ 76403007354697680187429816690342769003185818648605085375388281194656\ 9946433649006084096 $ echo '(4^4)^4' |bc 4294967296
In addition to these arts, bc
also has an interactive mode in which beeps and spoils everything does everything the same, but directly, without a pipeline. The -q
switch is needed to suppress a foul greeting.
$ bc -q 4^4^4 13407807929942597099574024998205846127479365820592393377723561443721\ 76403007354697680187429816690342769003185818648605085375388281194656\ 9946433649006084096 quit
Many moons ago at one of the popular English-speaking forum , now deceased, offered a brilliantly simple way to heat the percentages and calculate its speed in parrots.
time echo "scale=5000; 4*a(1)" | bc -l -q
We load the math library with the -l
option to bc
and ask to output the number π with an accuracy of 5000 decimal places. My calculation result on Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz
:
real 0m24.507s user 0m24.490s sys 0m0.000s
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.
In bc
you can, if you really need to, define functions and run scripts. The function definition has the following syntax:
define name ( parameters ) { newline auto_list statement_list }
The conditional operators if
and else
defined, the latter is not necessary to use, as well as the for
and while
headers. On Wikipedia, you can view a list of mathematical operators and compare with those in C. And this is how the calculation of the Fibonacci numbers in bc
.
#!/usr/bin/bc -q define fibo(n) { if (x <= 2) return n; a = 0; b = 1; for (i = 1; i < n; i++) { c = a+b; a = b; b = c; } return c; } fibo(1000) quit
As the PL bc
did not take off, however, it is more than good as a desktop calculator.
I have not often been able to use awk
, so every time I am surprised to discover new features of this program. If you need to calculate logarithms or sines, do not rush to panic, man awk
will help you. That's how we got the square root.
awk 'BEGIN{print sqrt(196)}' 14
But we already logarithm the number π, 5000 characters of which we have already calculated using bc
.
awk 'BEGIN{print log(3.141592653589793238462643383279502884197169399375105820974944592307)}' 1.14473
Read full list of features
atan2(y, x) Return the arctangent of y/x in radians. cos(expr) Return the cosine of expr, which is in radians. exp(expr) The exponential function. int(expr) Truncate to integer. log(expr) The natural logarithm function. rand() Return a random number N, between 0 and 1, such that 0 ≤ N < 1. sin(expr) Return the sine of expr, which is in radians. sqrt(expr) Return the square root of expr. srand([expr]) Use expr as the new seed for the random number generator. If no expr is provided, use the time of day. Return the previous seed for the random number generator.
Sometimes, I want to trust fate and send all three letters - awk. Actually, this is an example from the book of O'Reilly , which imitates the throwing of a coin, producing 2 different events. to drink or not to drink with equal probability.
#!/bin/bash ans=`awk -vmin=0 -vmax=1 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'` if [ $ans -eq 0 ]; then echo "no" else echo "yes" fi
In our mathematical workshop. There are a lot of different programs and if the topic takes off, we will try to go to the first and highest league of open source math software.
I. Replacing a calculator
bash
arithmetic substitutions.GNU bc
program GNU bc
.awk
.Ii. Tables
OpenOffice / LibreOffice Calc
.KSpread
.Gnumeric
.GNU Oleo
and others.Iii. Specialized math programs, student level +
Ocatve
.Scilab
.Maxima
.R
Sage
.Iv. Programming languages, math libraries and environments
Ansi C
, libraries math.h, complex.h, GSL
and other comrades.Java Scientific Library
Python
, SciPy, NumPy, Sympy
and other companions.COBOL
.Fortran
.Intel Math Kernel Library (Intel MKL)
AMD Accelerated Parallel Processing Math (APPLM)
AMD Core Math Library (ACML)
The list, of course, is not complete, so I apologize in advance if you have not indicated someone’s favorite math package or PL. The latter group is a truly spilled sea of diverse and useful software.
And here is the promised meskills along with the answer to the question from the picture. Source of
diff -u <(seq -f '%03.0f' 0 999) <((bc <<<'scale = 3009; 1 / 998001' | tr -d '\\\n'; echo) | sed s/.// | fold -3)
Source: https://habr.com/ru/post/310566/
All Articles