LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2008, 02:41 AM   #1
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Rep: Reputation: 0
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:
sqrt($number)

Second method:

Quote:
awk '{print sqrt($number)}'

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.
 
Old 08-04-2008, 02:44 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
How about:

echo sqrt "($number)" | bc
 
Old 08-04-2008, 02:58 AM   #3
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Original Poster
Rep: Reputation: 0
thanks heaps mate!
 
Old 08-04-2008, 03:54 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by skuz_ball View Post
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:




Second method:




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.
you need BEGIN in you awk script since there's no file input
Code:
awk 'BEGIN{print sqrt(400)}'
# awk 'BEGIN{print 400**0.5}'
next time when it doesn't work, don't just say it doesn't work. Describe any errors or strange phenomena clearly.
 
Old 08-04-2008, 04:18 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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)
result2=$(echo "sqrt(3)" | bc -l)
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk convert column matrix to square matrix? johnpaulodonnell Programming 4 04-30-2008 01:45 PM
Square root in python? raskol Programming 4 04-06-2008 07:56 PM
Does the square root of -1 exist? Poll! 144419855310001 General 60 11-06-2006 02:54 AM
square root problems - script Dimitris Programming 4 09-02-2003 10:05 PM
root square / two solutions - one variable Dimitris Programming 5 08-28-2003 02:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:44 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