LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 01-09-2022, 01:55 PM   #1
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Precision calculator for scripting and CLI


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.

You can get iq and iq+ here:
https://github.com/math-for-shell/iq4sh

Last edited by gnashley; 01-09-2022 at 01:58 PM.
 
Old 01-10-2022, 04:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
duplicate of https://www.linuxquestions.org/quest...ts-4175703227/
 
Old 01-10-2022, 06:05 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Original Poster
Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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.
 
3 members found this post helpful.
Old 01-10-2022, 05:33 PM   #4
mlangdn
Senior Member
 
Registered: Mar 2005
Location: Kentucky
Distribution: Slackware64-current
Posts: 1,845

Rep: Reputation: 452Reputation: 452Reputation: 452Reputation: 452Reputation: 452
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.

Thanks again!
 
2 members found this post helpful.
Old 01-11-2022, 08:35 AM   #5
slackerDude
Member
 
Registered: Jan 2016
Posts: 157

Rep: Reputation: Disabled
What are its advantages over dc, for example? I've been using dc for decades to do simple calculations from a terminal, and it's everywhere..

Not saying there aren't advantages, just trying to understand, without learning the whole thing.
 
Old 01-14-2022, 02:48 PM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Original Poster
Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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:
Code:
echo "scale=8; 2.718281828^2" |bc
7.389056096
iq exp -s8 2
7.38905609
# but:
echo "scale=8; 2.718281828^2.357" |bc
Runtime warning (func=(main), adr=27): non-zero scale in exponent
7.389056096
iq+_1.73.sh exp -s8 2.357
10.55922620
iq+_1.73.sh pow -s8 2.718281828 2.357
10.55922620
iq is not yet everywhere, but it could be -all it needs is a shell.
 
3 members found this post helpful.
Old 01-15-2022, 03:07 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by gnashley View Post
Times for both are similar,
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.
 
Old 01-16-2022, 02:52 PM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Original Poster
Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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
 
1 members found this post helpful.
Old 01-17-2022, 01:35 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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).
 
  


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
Testers wanted for IQ4sh/iq - Calculator for CLI or scripts gnashley Programming 30 01-15-2022 05:16 AM
LXer: Calculator N+ is an open source scientific calculator for your smartphone LXer Syndicated Linux News 0 11-27-2019 06:21 AM
LXer: Cli.Fyi – A Tool To Quickly Retrieve Information About eMails, IP Addresses, URLs And Lots More From The CLI Or Browser LXer Syndicated Linux News 0 03-15-2018 03:03 PM
Linux elemental cli calculator bc. stf92 General 6 01-27-2014 11:15 PM
Saving CLI data in the CLI Tons of Fun Linux - General 4 04-03-2007 05:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

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