Skip to content

Latest commit

 

History

History
251 lines (193 loc) · 5.86 KB

File metadata and controls

251 lines (193 loc) · 5.86 KB

bc An arbitrary precision calculator language

bc command is used for the command line calculator. It is similar to basic calculator by using which we can perform mathematical calculation. bc command supports the following operators:

  • Arithmetic Operators
  • Assignment Operator
  • Increment/Decrement Operators
  • Relational Operator
  • Logical or Boolean Operators
  • Math Functions
  • Conditional Statements
  • Iterative Statements

1. Arithmetic operators

bc supports addition (+), subtraction (-), multiplication (*), division (/), remainder (%) and rxponentiation (^) operators.

	$ echo "12+5" | bc
	17
	$ echo "12-5" | bc
	7
	$ echo "12*5" | bc
	60
	$ echo "19/5" | bc
	3
	$ echo "19%5" | bc
	4
	$ echo "2^5" | bc
	32

We can perform these operations on variables.

	$ a=5
	$ b=2
	
	$ echo "$a+$b" | bc
	7
	$ echo "$a-$b" | bc
	3
	$ echo "$a*$b" | bc
	10
	$ echo "$a/$b" | bc
	2
	$ echo "$a%$b" | bc
	1
	$ echo "$a^$b" | bc
	25
	

we can also store the result of above operations in variable.

  	$ res=$(echo "10*5" | bc)
	$ echo "$res"
 	50

2. Assignment operators

The list of assignments operators supported are:

  • var = value assign the vale to the variable
  • var += value similar to var = var + value
  • var -= value similar to var = var – value
  • var *= value similar to var = var * value
  • var /= value similar to var = var / value
  • var ^= value similar to var = var ^ value
  • var %= value similar to var = var % value
  # Assign 10 to the variable and print the value on the terminal.
  $ echo "var=10; var" | bc           
  10
  
  $ echo "var=10; var ^= 2; var" | bc
  100
 
  $ x=$(echo "var=500; var%=7; var" | bc)
  $ echo $x
  3
  

3. Increment/Decrement Operators

  • Pre increment operator (++var) Variable is increased first and then result of variable is stored.
  • Post Increment Operator (var++) Result of the variable is used first and then variable is incremented.
  • Pre Decrement operator (--var) Variable is decreased first and then result of variable is stored.
  • Post Decrement operator (var--) Result of the variable is used first and then variable is decremented.
	$ echo "var = 10; var--" | bc
	10
 
 	$ echo "var = 10; ++var" | bc
	11
	
	$ echo "var = 10; var++" | bc
	10
	
	$ echo "var = 10; --var" | bc
	9

4. Relational Operator

Relational operators are used to compare 2 numbers. If the comparison is true, then result is 1. Otherwise(false), returns 0. These operators are generally used in conditional statements like if. The list of relational operators supported in bc command are shown below:

  • expr1 < expr2 : Result is 1 if expr1 is strictly less than expr2.
  • expr1 <= expr2 : Result is 1 if expr1 is less than or equal to expr2.
  • expr1 > expr2 : Result is 1 if expr1 is strictly greater than expr2.
  • expr1 >= expr2 : Result is 1 if expr1 is greater than or equal to expr2.
  • expr1 == expr2 : Result is 1 if expr1 is equal to expr2.
  • expr1 != expr2 : Result is 1 if expr1 is not equal to expr2.
		$ echo "10 > 5" | bc
		1

		$ echo "10 == 5" | bc
		0

		$ echo "1 >= 1" | bc
		1

5. Logical or Boolean Operators

Logical operators are mostly used in conditional statements. The result of the logical operators is either 1(TRUE) or 0(FALSE).

  • expr1 && expr2: Result is 1 if both expressions are non-zero.
  • expr1 || expr2: Result is 1 if either expression is non-zero.
  • !expr : Result is 1 if expr is 0.
   $ echo "10 && 5" | bc
   1

   $ echo "0 || 0" | bc
   0

   $ echo "! 0" | bc
   1

6. Math Functions

  • use –l to import the standard math library.
  • sqrt(x) returns the square root of the number x. If the expression is negative, a run time error is generated.
  • length(x) returns the number of digits in x.
  • ibase and obase defines the conversion base for input and output numbers. The default for both input and output is base 10.
  • scale defines number of digits after the decimal point in the expression.
  • last is a variable that has the value of the last printed number.
		
   	$ echo "obase=2; 15" | bc –l
   	1111

   	$ echo "obase=8; 9" | bc –l
   	11

   	$ echo "ibase=2; 1111" | bc –l
   	15

   	$ echo "ibase=2; obase=8; 10" | bc –l
   	2

7. Conditional Statements

Conditional Statements are used to take decisions and execute statements based on these decisions. bc command supports the if condition.

	if(condition) {statements} else {statemnts}

To test a given number is even or odd:

	$ echo 'n=8; if(n%2==0) print "n is even\n" else print "n is odd\n" ' | bc
	n is even

	$ echo 'n=9; if(n%2==0) print "n is even\n" else print "n is odd\n" ' | bc
	n is odd
		

8. Iterative Statements

bc command supports the for loop and while loop for doing iterations.

		for(assignment; condition; updation)
		{
		      statements.....
		      .......
		      ........
		}

		OR

		while(condition)
		{
		      statements.....
		      .......
		      ........
		}
	$ echo "for(i=1; i<=5; i++) {i;}" | bc
	1
	2
	3
	4
	5
	
	$ echo "i=1;while(i<=5) {i; i+=1}" | bc
	1
	2
	3
	4
	5

Precedence and Associativity of the operators

The expression precedence from lowest to highest is as follows:

  • || operator, left associative
  • && operator, left associative
  • ! operator, nonassociative
  • Relational operators, left associative
  • Assignment operator, right associative
  • + and - operators, left associative
  • *, / and % operators, left associative
  • ^ operator, right associative
  • unary - operator, nonassociative
  • ++ and -- operators, nonassociative

References: