ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
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?
#!/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
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.