square root of number using sqrt and/or awk
Hi all,
I'm trying to get the square root of a number and have used a couple of methods but neither has worked for me. First method: Quote:
Second method: Quote:
I have read somewhere that I must include <math.h> but am not exactly sure of what this is referring to and haven't seen this in any of the example scripts I have looked at. I am trying to write this using korn shell. Any help would be greatly appreciated. |
How about:
echo sqrt "($number)" | bc |
thanks heaps mate!
|
Quote:
Code:
awk 'BEGIN{print sqrt(400)}' |
The math.h thing is for when you are writing a program in C. If you are using shell script, you will need to use some external program, as most shells do not include a built-in square root command.
bc is probably the best bet here as it is a very small program (so it is quicker to invoke than other programs), and it can give you a lot of precision, or as little as you want. bc reads from standard input, so you can echo an expression to evaluate like these examples: Code:
result1=$(echo "3 + 5" | bc -l) |
All times are GMT -5. The time now is 11:08 AM. |