Tags

, , ,

Sure, you can use commandline python or ruby (irb); but you may use bc, the inbuilt arbitrary precision calculator, as well. A typical syntax would be:

$ echo "scale=4; 7/9" | bc

where scale=4 tells bc to print the result of 7 divided by 9 with 4 digits after the decimal point.

You may save the following script in a file say cal.bsh in the home directory


#!/bin/bash
echo "scale=4; $1" | bc ;exit

Then make it executable and use it to do the calculation

$ ~/cal.bsh 7/9

For more information: man bc
While you are at it, you may want to have a look at some built-in math functions there, e.g.


s(x) = sin(x)
c(x) = cos(x)
a(x) = arctan(x)
l(x) = Ln(x)
e(x) = exp(x)

These functions are preloaded using the -l flag. Moreover, when this flag is used the default scale is set to 20.

Advertisements