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 11-30-2008, 06:11 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Rep: Reputation: 30
Using sqrt in a korn shell


I have a program I wrote for a class like a year ago that I am messing around with. It is really simple, just getting a number and doing the square and the square root and things like that. I know it worked before because I turned it in and I got an "A" but now it doesn't. I am using the latest ubuntu live cd that didn't come with a korn shell so I had to install pdksh which is a public domain version.

The line in the script is this
Code:
FINAL2=$((sqrt(FINAL1)))
and this is the error it is giving me
Code:
./korn.sh[43]: sqrt(FINAL1): unexpected `(
Can anyone help?

PS. Also, either I can't remember how to use the code tags or it isn't working in the preview post window. Am I just doing it wrong?

Last edited by snowman81; 12-01-2008 at 12:59 PM.
 
Old 11-30-2008, 06:48 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
awk
Code:
# awk 'BEGIN{print sqrt(4)}'
2
or using bc
Code:
 # echo "sqrt(4)" | bc
2
 
Old 12-01-2008, 06:30 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Building on ghostdog74's idea:
Code:
#!/usr/bin/env ksh

# @(#) s1       Demonstrate floating point in pdksh.
# pdksh does not support floating-point directly.

echo
export LC_ALL=C
echo "Environment: LC_ALL = $LC_ALL"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) bc
echo

isqrt()
{
echo "sqrt($1)" | bc
}

sqrt()
{
echo "scale=3 ; sqrt($1)" | bc
}

echo " Integer:"
for i in 2 7 16 20
do
  t=$( isqrt $i )
  printf " Integer: square root of %d is %d\n" $i $t
done

echo
echo " Float:"
for i in 2 7 16 20
do
  t=$( sqrt $i )
  printf " Float: square root of %f is %f\n" $i $t
done

exit 0
Producing:
Code:
% ./s1

Environment: LC_ALL = C
(Versions displayed with local utility "version")
Linux 2.6.11-x1
pdksh 5.2.14 99/07/13.2
bc 1.06

 Integer:
 Integer: square root of 2 is 1
 Integer: square root of 7 is 2
 Integer: square root of 16 is 4
 Integer: square root of 20 is 4

 Float:
 Float: square root of 2.000000 is 1.414000
 Float: square root of 7.000000 is 2.645000
 Float: square root of 16.000000 is 4.000000
 Float: square root of 20.000000 is 4.472000
Best wishes ... cheers, makyo
 
Old 12-01-2008, 12:51 PM   #4
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Original Poster
Rep: Reputation: 30
I probably should have mentioned that one of the original requirements was to not use an outside program like bc. But the function in awk does work and I didn't know you could do that. So it got fixed and I learned something new. Sweet.

testing code tag
Code:
 blah blah
So that's how you do it... I'm dumb.

Last edited by snowman81; 12-01-2008 at 12:58 PM.
 
Old 12-01-2008, 02:45 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by snowman81 View Post
I probably should have mentioned that one of the original requirements was to not use an outside program like bc. But the function in awk does work ...
Are you saying that you could not use bc because it is an outside program -- presumably external to the shell -- but that the use of awk is permissible? ... cheers, makyo
 
Old 12-01-2008, 02:48 PM   #6
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Original Poster
Rep: Reputation: 30
Hmmm, I suppose you're right. I honestly don't remember what he told me. Meh, I suppose it doesn't matter anymore since I'm not under any restrictions anymore. Thanks for the help.
 
  


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
korn shell variable help 4play Programming 9 11-23-2011 12:03 PM
How to parse in korn shell eddieboo Linux - Newbie 1 04-19-2008 07:06 AM
Arrays in Korn Shell...? vous Programming 1 03-26-2005 01:35 AM
Using a Korn shell in rc scripts desbyleo Solaris / OpenSolaris 3 11-12-2004 05:27 PM
Korn shell script Muzica Solaris / OpenSolaris 4 09-06-2004 12:47 PM

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

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