Exploring Integer And Floating Point Math Options On Linux Command Line

Performing Basic Arithmetic with expr

The expr command in Linux provides a way to perform basic integer arithmetic operations like addition, subtraction, multiplication and division. It takes integer arguments and evaluates the result of the given expression. Some of the key things expr can be used for include:

  • Adding, subtracting, multiplying and dividing integers
  • Using the modulo operator to find the remainder of a division operation
  • Testing equality and comparison of integers

Here is a simple usage example of expr to add two integers:

$ expr 2 + 3
5

And to find the remainder of a division:

$ expr 10 % 3 
1

As expr works on integers, using non-integer arguments can lead to unexpected results. Overall, expr allows evaluating arithmetic, boolean and string expressions using command line arguments rather than a programming language. This makes it convenient for basic math within shell scripts and one-off calculations.

Adding, Subtracting, Multiplying and Dividing Integers

All the basic arithmetic operations – addition, subtraction, multiplication and division are supported by the expr command. Each operation must be specified by its own operator. Let’s look at some examples:

$ expr 3 + 5
8

$ expr 12 - 5 
7

$ expr 2 \* 4
8

$ expr 8 / 2
4

Note that the operators require escaping to prevent shell interpretation. As we can see, expr evaluates the arithmetic expression and outputs the computed result.

Modulo Operator for Finding Remainders

The modulo or remainder operator (%) can be used to find the leftover portion after division of one integer by another:

$ expr 15 % 4
3 

This computes 15 divided by 4 which leaves a remainder of 3. The % operator implements modulo arithmetic to compute remainders just like other languages.

Examples of Basic expr Usage

Let’s look at a few more examples of using expr:

$ expr 5 \* \( 2 + 3 \) 
25

Parentheses can group sub-expressions just like in other languages. This shows the order of operations being respected.

  
$ expr length Hello
5

The length keyword can find the length of a string.

$ expr 5 > 3   
1

Comparison operators like > and < work to test whether one integer is greater than or less than another, outputting 1 if the relation is true or 0 if false.

$ expr 5 = 5
1 

Equality testing with the = operator checks if two arguments are equal, again putting out 1 or 0.

This covers the basic integer arithmetic and comparison functionality provided by the humble expr command. Next we’ll look at floating point math capabilities.

Using bc for Floating Point and Advanced Math

For doing any kind of floating point arithmetic or advanced math, the bc command is what you would reach for on Linux. It implements a language for arbitrary precision numeric processing that includes:

  • Floating point math with full support for decimals
  • Powerful built-in math functions
  • Logical and comparison operators
  • Language constructs like loops and conditionals

In terms of math capabilities, bc allows:

  • Basic operators for addition, subtraction, multiplication and division
  • Exponential functions
  • Logarithms and roots
  • Trigonometric functions like sine, cosine etc
  • Setting precision and scale
  • Multi-line equations
  • Variable assignments

Let’s explore some bc basics.

Enabling bc with the -l Math Library

Even though bc offers lots of built-in math functions by default, the full set of capabilities need to enabled by turning on the math library using the -l option:

  
$ bc -l 
2^10
1024

Now features like exponents and more trig functions will work. Omitting -l leads to a lot less available.

Setting Scale for Precision

The default scale value determines how many decimal digits are printed from floating point operations. This can be set using the scale=N variable:

$ bc -l
scale=4
5/3
1.6667

Here the scale is adjusted to 4, so division shows 4 decimal places. The maximum scale available depends on the underlying architecture.

Basic Operators

The common arithmetic operators like +, -, * and / all work as expected in bc:

  
$ bc -l
12 + 5
17
72/8
9

Combined with setting scale, floating point math is straightforward:

scale=5
4.5/1.5 
3.00000

Exponentials and Logarithms

Exponentiation is done with the ^ operator, so 2^3=8. Logs are available via built-ins like l() and a few variations:

2^4
16
l(10)
2

Additional trigonometric functions round out bc’s math capabilities:

s(45)
.7071067812
c(45)
.7071067812

The article continues with 4000 more words across the remaining sections…

Leave a Reply

Your email address will not be published. Required fields are marked *