User Tools

Site Tools


floating_point_calculation

This is an old revision of the document!


Floating Point Calculation

This is an example represent how to compute Floating point in bash.

Details

If you are like me and you like to use the bash shell as much as possible, you have probably run into the situation where you wanted to have a calculator built into your bash shell. So with a 2 line script and an edit to your .bashrc file, your bash shell can become a useful basic calculator. No more firing up kcalc to do simple math. With the program bc and a very small bash script, you can now get those decimal places to show up.

The two line script.

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

Now save the script as bashcalc.sh

Now, you can do a couple of things at this point.

METHOD #1 If you're like me, you have a /home/crouse/Scripts directory (ok, so yours probably doesn't say crouse lol) and you put most of your scripts in there. The benefit of this is that it doesn't require any special privledges to accomplish.

mv bashcalc.sh /home/$USER/Scripts/bashcalc.sh
chmod a+x /home/$USER/Scripts/bashcalc.sh

Now that the script is in your script directory, you need an alias in the .bashrc file in order to make it work. Here is the alias in my .bashrc file for example:

alias calc='sh /home/crouse/scripts/bashcalc.sh'

METHOD #2 Alternatively, you can put the script into /usr/bin and there is no need to edit the .bashrc file as /usr/bin is in your bash shell's path. You at least need the ability to sudo or install as root to accomplish this method.

sudo cp bashcalc.sh /usr/bin/calc
sudo chmod 755 /usr/bin/calc

Once all of this text editing, moving, and assorted stuff is done, you can use calc and do floating point math with your bash shell!

Examples:

[crouse@localhost ~]$ calc 2.55*2.6666
6.7998
[crouse@localhost ~]$ calc 25.55/3.6666666
6.9681
[crouse@localhost ~]$ calc 25.2222+6.33333
31.55553
[crouse@localhost ~]$ calc 99-1.333333333
97.666666667

Notice that the division is only carried out to 4 places past the decimal point? That's a limitation of the bc command. You can override the 4 places to whatever you prefer by simply changing the line:

echo "scale=4; $1" | bc ;exit

The “scale=4” is what controls the amount of decimal places the division will calculate too. Just change the 4 to whatever length you prefer. Now you can do simple floating point math with just your bash shell.

Here is an adaptation of the first script that allows you to keep inputing calculations (kind of like a calculator) without having to restart the script every time:

FILE DOWNLOAD: http://www.bashscripts.org/downloads/Scripts/crouse/calc.sh

You can use the function above and extend it to create a sort of bash adding machine. It's a calculator that constantly runs in a bash shell.

You can edit the script to suit your needs, but it basically allows you to key in your problems, gives the results and waits for another problem to be input.

The default scale for division is to go 4 decimal places, this can be adjusted in the script to whatever length you wish to use. This was because I sometimes needed to enter in alot of individual calculations and was tired of typing the word calc :)

This could be extended even further and store individual calculations if you wanted it to, but that's beyond the scope of this article at the moment.

#!/bin/bash
 
#######################################################
#     Bash CALCulator 2
#######################################################
#
#
#    FILE: bashcalc2.sh
# VERSION: 1.1
#    DATE: 06-19-2006
#
#  AUTHOR: Crouse - Please visit bashscripts.org and usalug.org
#
#
########################################################
 
header ()
{
clear; echo "Bash Calculator - Enter a calculation and hit enter";
echo "---------------------------------------------------";
}
 
calc ()
{
header
while true
do read -p "" bashcalc;
       if [ "$bashcalc" = "quit" ]
         then
           exit
       fi
 
       if [ "$bashcalc" = "clear" ]
        then
          calc
       fi
 
       if [ "$bashcalc" = "help" ]
        then
          clear
          echo "Options include:"
          echo " help - This help file"
          echo " clear - Clears the screen"
          echo " quit - Quits the program"
          echo " "
          read -p  "Hit any key to continue" temp;
          calc
       fi
 
  echo "scale=4; ${bashcalc}" | bc ;
  echo "---------------------";
done
}
 
# Program run starts here
calc
 
exit 0

Ref. http://www.novell.com/coolsolutions/tools/17043.html

floating_point_calculation.1327680141.txt.gz · Last modified: 2020/08/10 02:30 (external edit)