View Full Version : How to find square root of a number?
colau
August 18th, 2009, 04:38 AM
Hi,
How to find square root of a number in bash shell?
Is there any built-in sqrt function?
kaibob
August 18th, 2009, 05:12 AM
I don't know of any built-in square root function. I've just started to learn the bc utility. It will find the square root of a number as follows:
calc=$(echo "sqrt ( 10 )" | bc -l) ; echo $calc
Change the number 10 to whatever is applicable.
colau
August 18th, 2009, 05:27 AM
I don't know of any built-in square root function. I've just started to learn the bc utility. It will find the square root of a number as follows:
calc=$(echo "sqrt ( 10 )" | bc -l) ; echo $calc
Change the number 10 to whatever is applicable.
What are the other mathematical functions available in bash shell(like "sqrt")?
floor
ceil
pow
soltanis
August 18th, 2009, 05:30 AM
They're not available through bash, they're available through the bc (1) utility, a command line calculator. Look through its manpage for syntax + examples.
geirha
August 18th, 2009, 09:38 AM
Bash can only do integer arithmetics, not floating point, so you have to rely on an external program to do those types of calculations.
$ echo $((2+2))
4
$ echo $((5/2))
2
colau
August 18th, 2009, 12:16 PM
Bash can only do integer arithmetics, not floating point, so you have to rely on an external program to do those types of calculations.
$ echo $((2+2))
4
$ echo $((5/2))
2
What are those external programs?One is bc.
Arndt
August 18th, 2009, 12:26 PM
What are those external programs?One is bc.
For example:
$ python -c 'import math; print math.sqrt(3)'
1.73205080757
$ perl -e 'print sqrt(3), "\n"'
1.73205080756888
credobyte
August 18th, 2009, 01:49 PM
http://abs-guide.awardspace.com/mathc.php - give it a try :)
djbushido
August 18th, 2009, 02:51 PM
You could write simple C/C++ programs to "import <math.h>" and then perform a simple function and exit - like cos, sqrt, etc. But I like the idea of a python shell a bit more, personally.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.