LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-02-2014, 11:35 PM   #1
pratikjajal
LQ Newbie
 
Registered: Oct 2013
Location: Rajkot, Gujarat, India
Posts: 3

Rep: Reputation: Disabled
How to perform mathematical operations on linux terminal?


Dear All,

Following is the command which is used to perform the mathematical operations on terminal:

Command:
echo "10.25*12" | bc -l

The answer will be 123.00 for the above mentioned command.

With the minor change in echo "Write here any mathematical operation", we can perform any mathematical operation.

Examples:
echo "25-12" | bc -l
echo "25+12" | bc -l
echo "25*12" | bc -l
echo "25/12" | bc -l
echo "25%12" | bc -l

If you need more help, please put your questions herewith this post.If you know more things about this share your ideas with me.

Regards,
Pratik
 
Old 01-03-2014, 03:26 AM   #2
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
In addition to the above, here are some bc related links that might come in handy:
- command-line calculations using bc
- Linux and Unix bc command


And two of my blog entries:
- Convert numeral systems
- Convert Metric <-> Imperial

Last edited by anon237; 01-03-2014 at 04:15 AM.
 
1 members found this post helpful.
Old 01-03-2014, 03:41 AM   #3
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
Another option:

perl -e "print sqrt(20736)";
perl -e "print 12**2"

Or use python as a calculator

python // open python prompt
import math // import math module
math.sqrt(144) // do your calculations...

(leave the prompt with ctrl-D)
 
Old 01-03-2014, 04:37 AM   #4
Soderlund
Member
 
Registered: Aug 2012
Posts: 185

Rep: Reputation: 81
Quote:
Originally Posted by druuna View Post
Interesting. I see you can also run it like a script instead of using bc files (though I don't know if it's any useful):

Code:
#!/usr/bin/bc -q
ibase=10
obase=2
for (;;)
{
        print "Decimal (0 to stop): "
        x = read()
        if (0 == read)
        {
                break;
        }
        print "Binary: "
        x
}
quit
 
Old 01-03-2014, 05:26 AM   #5
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@Soderlund: bc is a powerful tool which is often used in a simple form (echo some arithmetic operations and pipe it to bc).

I do see the usefulness of bc scripts, but I also tend to use echo to pipe it to bc, especially if the arithmetic that needs to be done is "simple".

I have this bash function in place:
Code:
# Function : cmToFi 
# Syntax   : cmToFi <centimeters> 
# 
# Convert centimeters to feet and inches 
function cmToFi() { 
  FEET=$( echo "scale=0;($1 * 0.3937) / 12" | bc ) 
  FEETTOT=$( echo "scale=2;($1 * 0.3937) / 12" | bc ) 
  INCHES=$( echo "scale=0;(($FEETTOT - $FEET) * 12 + 0.5)/1" | bc ) 
  echo "$FEET $INCHES" 
}
which could be rewritten as:
Code:
#!/usr/bin/bc -q

onefeet=0.3937

print "\nConvert centimeters to feet and inches\n"
print "- Centimeters: " ; cmin=read()

scale=0
xfeet=(cmin * onefeet)/12
scale=2
totfeet=(cmin * onefeet)/12
scale=0
xinches=((totfeet - xfeet) * 12 + 0.5)/1

print cmin
print " centimeters equals "
print xfeet
print " feet and "
print xinches
print " inches"
print "\n"
quit
If the arithmetic gets complicated it is probably better to use a bc script instead of echoing multiple statements to bc.

BTW: There's a typo in your example:
Code:
        x = read()
        if (0 == read)
        {
That should be:
Code:
        x = read()
        if (0 == x)
        {

Last edited by anon237; 01-03-2014 at 06:23 AM. Reason: Fixed rounding up/down
 
Old 01-25-2014, 03:03 AM   #6
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Rep: Reputation: 138Reputation: 138
You could use the bash simply:
e=$(($i +1)) /sthg like this

or a C program that allows you to do any mathematical operation. C is surely the best rather than use slow, buggy, python. Perl is probably on your distro, python maybe not. gcc is the core of Linux, so I recommend C or C++
 
Old 01-25-2014, 03:12 AM   #7
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by patrick295767 View Post
You could use the bash simply:
e=$(($i +1)) /sthg like this
That won't work.

The syntax is wrong and more importantly: Bash doesn't handle floating point very well, you'll end up with a rounded integer.
 
Old 01-25-2014, 04:24 AM   #8
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by druuna View Post
That won't work.

The syntax is wrong and more importantly: Bash doesn't handle floating point very well, you'll end up with a rounded integer.
Oh, man. This might be true. I forgot it, I dont use bash on regular basis any longer.

ok so it might be


Code:
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <string.h> // for strncpy  

// simple math calc: gcc -lm math.c  -o test-bin

int main(){
  printf(" Hello: %g \n" , 12 * 3.4 );
  return 0;
}
I include more in case you do slightly more stuffs.
 
Old 01-26-2014, 09:55 PM   #9
NeXuS_2006
Member
 
Registered: Dec 2003
Distribution: Fedora
Posts: 64

Rep: Reputation: 18
You'd rather write a C program, compile, and run it than use "slow, buggy python"?
 
Old 01-27-2014, 12:52 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,991

Rep: Reputation: 7889Reputation: 7889Reputation: 7889Reputation: 7889Reputation: 7889Reputation: 7889Reputation: 7889Reputation: 7889Reputation: 7889Reputation: 7889Reputation: 7889
To return back to the original question:
Quote:
How to perform mathematical operations on linux terminal?
there is another tool, dc:
It is a reverse-polish desk calculator which supports unlimited precision arithmetic.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] command line/tty/terminal delete/cut operations confusion (ctrl+u,ctrl+y etc) NeoLux Linux - Newbie 4 12-16-2012 11:33 AM
Can GUI operations be viewed real time within a terminal? Zaileion Linux - Newbie 2 10-17-2011 09:26 PM
howto record operations in log file from terminal drManhattan Linux - Newbie 4 07-27-2011 07:05 PM
how to perform floating point operations in linux kernel? raulapati Programming 1 04-07-2011 06:25 AM
operations of gnome-terminal chrismiceli Linux - Software 1 12-04-2002 10:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration