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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-02-2014, 11:35 PM
|
#1
|
LQ Newbie
Registered: Oct 2013
Location: Rajkot, Gujarat, India
Posts: 3
Rep: 
|
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
|
|
|
01-03-2014, 03:41 AM
|
#3
|
Senior Member
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591
Rep: 
|
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)
|
|
|
01-03-2014, 04:37 AM
|
#4
|
Member
Registered: Aug 2012
Posts: 185
Rep:
|
Quote:
Originally Posted by druuna
|
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
|
|
|
01-03-2014, 05:26 AM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
@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
|
|
|
01-25-2014, 03:03 AM
|
#6
|
Member
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664
Rep: 
|
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++
|
|
|
01-25-2014, 03:12 AM
|
#7
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Quote:
Originally Posted by patrick295767
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.
|
|
|
01-25-2014, 04:24 AM
|
#8
|
Member
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664
Rep: 
|
Quote:
Originally Posted by druuna
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.
|
|
|
01-26-2014, 09:55 PM
|
#9
|
Member
Registered: Dec 2003
Distribution: Fedora
Posts: 64
Rep:
|
You'd rather write a C program, compile, and run it than use "slow, buggy python"?
|
|
|
01-27-2014, 12:52 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,991
|
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.
|
|
|
All times are GMT -5. The time now is 10:11 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|