SlackwareThis Forum is for the discussion of Slackware Linux.
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 wrote a calculator in pure shell, mainly for use in scripts which require lots of math. But it can be used as a program, or sourced into your shell session. The basic calculator 'iq' provides the main functions for add/sub/mul/div and integer exponentiation. 'iq+' is an extended version which adds: pow, log/log2/ln, exp, and others.
Slackers will naturally want to have a good look at anything before using it, so I invite you to review the code for any reason. The design and coding-style may not be to your liking, but if you can get over them, you'll see that the body of programs consist entirely of functions, with a block of code at the end, which is only used when you run iq/iq+ as a program. Please keep comments, questions or suggestions about the coding style or design for another thread. It may look ad-hoc, but there is reasoning behind it.
I trust you guys to be sticklers for safety, so please tell me if you see anything loose or missing in the block of executable code. There are comments everywhere -I hope they are helpful.
If you decide to try the program, please run 'iq' first as it strictly enforces the syntax and vets every input. When you've understood the simple usage, then running iq+ will be easy. Please see the help pages.
When running iq+, the strict, expensive input validation is not used, so GIGO rules apply. Safety measures do still apply though.
I know some of you frequently use other shells, besides bash. iq/iq+ will run under: bash, zsh, posh, dash and ksh, but is less-tested under zsh/ksh. So any testing done with alternate shells is welcome. Also, there is the question whether iq might work under the ash version of Slackware or other less-known shells.
This not a duplicate thread, as I am asking here a question specific to Slackware users, and for their review of security matters -which they are quite good at. The thread has had a few views, and has survived a night without any nasty comments or distractions. Some of these guys have used my software before and I respect their thoughtful and considered insight.
I have been playing with iq for about 30 minutes. My hand held calculator has verified everything I've thrown at it. It took me a few minutes to get the syntax right for using bnpow(^), but even that one was verified each time. This is a neat piece of software well commented. Even a non-math person like me can use this.
I can't speak for the safety/security aspect, since my security needs are based on what I learn here. Like you, (from a different perspective) I trust these guys A LOT.
Great work, as I can see that this took some time.
On to iq+ in a few days. Like I said, I'm no math wiz. I'll be looking at running this as a program soon.
iq is designed to be used in scripts, but it needed to run like a program for developing and testing. It consists of inter-acting functions, so it can also be sourced into your shell session, where it stays out of the way until you need it. As a one-shot CLI calculator it compares well with bc or dc, and has, perhaps a more friendly syntax.
Code:
#Instead of:
echo "scale=6; 32.4*7.37813123" |bc
# this
iq mul -s6 32.4 x 7.37813123
Times for both are similar, but note that iq actually lets you control the scale. Unless you usually work with very long numbers, iq will hold its' own. Unlike compiled programs, iq has no hardware access, so longer numbers take longer to calculate -bc will deliver answers in about the same time for quite long numbers.
iq+, the advanced version, includes functionality which is lacking in bc:
I told you already, but I repeat it again: you measured the startup time of the shell (and compared to bc, dc and other software). That is completely pointless. Your code is still extremely slow and there are [a lot of] room for improvement (and actually I think you can never reach the speed of any binary tool within a shell). That means probably the shell will be started faster than bc, but the calculation itself will take longer, just you cannot measure it.
And actually there is no any slackware specific thing in this tool.
You really haven't tried my calculator, at all. Here's a 1-to1 which I am not ashamed of:
Code:
time echo - | awk -v var1="2.718281828459" -v var2="1.161815" '{print (1/(1+(var1 ^ -var2)))}'
0.761662
real 0m0.003s
time iq+ sigmoid_tanh -s6 1.161815
0.761662
real 0m0.017s
I wrote iq to replace that sigmoid calculation using awk, plus less complicated bc calls. The awk/bc calls were in a neural-network demo script. In about75 lines, it calls bc 40+ times and awk 4 times. I translated the same script to use iq. When run and under ksh, iq beats the bc/awk from the first iteration. Under dash it needs a couple of iterations to catch up. Under bash, both scripts do terribly and iq needs about 150 iterations till it catches up and passes the original script.
This thing really is capable of real work in reasonable times, whether you think so, or not. Even as a one-shot tool, can you tell the difference between an execution time of .003 seconds and .017 seconds? And compare the simplicity of the iq syntax.
And I see now, that Slackware now includes dash -which is great. iq consistently runs 3x faster under dash than bash and another 3x faster under ksh.
Last edited by gnashley; 01-16-2022 at 03:02 PM.
Reason: slack now has dash
Yes, and that is a very special case, where the general execution time with your code is better. That is not impossible, there are a lot of code out there which are extremely far from the optimal implementation, therefore you can easily improve. That does not necessarily mean your implementation is good or efficient, just better than the original one. And actually there was another thread here (LQ/slackware), where others stated they don't care if the execution time is 0.003s and/or 0.017s. (and you cannot measure reliably 0.003s this way).
And again, your speed does not depend on your code at all, but the shell you use - as you stated it earlier - basically your code is slow (compared to bc), but the running time of the computation is negligible compared to the time of creation of a new process (shell or bc or whatever).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.