LinuxQuestions.org
Help answer threads with 0 replies.
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-07-2021, 11:59 AM   #1
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,911

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Testers wanted for IQ4sh/iq - Calculator for CLI or scripts


I have a new project and need testers. iq is a calculator written to replace bc/awk, etc., in scripts. It includes functions for exp, pow, nrt and log, as well as the four basic operations. It also has a function 'tsst' for comparing decimal values with the same syntax as the shell 'test/[' builtins.

iq itself is written in the posix-shell language, with no external calls to other calculators. But don't let that stop you from trying it out -it does offer some surprising functionality and speed. I am using it as the base for neural-networks which functions quite well, and competes head-to-head with bc and awk. In fact, it beats them once you start looping a learning-routine in a NN a few times.

iq also can be used as a one-shot CLI calculator or sourced into your shell session.

You can get it here:
https://github.com/math-for-shell/iq4sh

The file 'iq' is the main calculator and the file 'iq+' contains a bunch of useful functions for trig, activation-functions for NN and more. Run iq with no arguments or: 'iq iqhelp' to see the main help page and get started.

Your comments, corrections, contributions and bug reports will be well-received, when they are well-intentioned.
 
Old 11-07-2021, 01:20 PM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,669
Blog Entries: 3

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
Hope you don't think that I am being too critical...

I downloaded both of them. Your code is hard to read because it is all squished together. That makes the code smaller, but hard to read. 1000 lines of bash all squished together. If people can't read your code easy, then they will ignore it. Potential for it to do something bad.

Instead of this
Code:
if [ $(( ${#Am} + ${#Bm} )) -lt 19 ] ; then
    while : ;do case $mrint in '0'*) mrint=${mrint#?*} ;; *) break ;; esac ;done # depad everything -even to null
    while : ;do case $mnint in '0'*) mnint=${mnint#?*} ;; *) break ;; esac ;done
    while : ;do case $mrfrac in *'0') mrfrac=${mrfrac%*?} ;; *) break ;; esac ;done
    while : ;do case $mnfrac in *'0') mnfrac=${mnfrac%*?} ;; *) break ;; esac ;done
This would read much better.
Code:
if [ $(( ${#Am} + ${#Bm} )) -lt 19 ] ; then
    while : ;do
        case $mrint in '0'
            *) mrint=${mrint#?*}
            *) break
            ;;
        esac
    done # depad everything -even to null
    
    while : ;do 
        case $mnint in '0'
            *) mnint=${mnint#?*}
            *) break
            ;;
        esac
    done
    
    while : ;do 
        case $mrfrac in 
            *'0') mrfrac=${mrfrac%*?}
            *) break
            ;;
        esac
    done
    
    while : ;do 
        case $mnfrac in 
            *'0') mnfrac=${mnfrac%*?}
            *) break
            ;;
        esac
    done
The 'help' is no help. I had to guess on how to use it. It does work after I guessed. You know how it works, no one else does.

Code:
bash ./iq add 3 + 3
6.0
bash ./iq mul 3 * 3
./iq: line 190: 3 * 1ut1: value too great for base (error token is "abc")
bash ./iq mul 3 x 3
9.0
Can't figure out how to subtract, needs better docs. A python shell is easier to use than this.

Also, a mod is going to come along and tell you to put this kind of thing in your blog.

Thanks for the effort.

Last edited by teckk; 11-07-2021 at 01:21 PM.
 
Old 11-07-2021, 01:28 PM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,034

Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
Can't figure out how to subtract, needs better docs.
Obviously, with add.
Code:
$ iq add 3 3
6.0
Code:
$ iq add 3 -3
0.0
Quote:
Originally Posted by teckk View Post
The 'help' is no help. I had to guess on how to use it.
Why?
Code:
$ iq
 IQ4sh/iq - Version:1.61803398 - Copyright 2021 Gilbert Ashley
 Precision calculator for bash, zsh, posh, dash or other posix shell.
 Available functions: tsst add mul div pow intpow epow nthrt logx exp
 Utility functions: cmp3w nthrt_solve fstpow simplfrac factorial isnum getE getP getPhi
 The 'div' function requires an operator (/,%,r). Otherwise they are optional.
 Most functions accept a scale option: '-sX', where X is the desired precision.
 Use 'iqhelp funcname' to show help for each function.
Code:
$ iq iqhelp add
 'add' adds and/or subtracts a series of 2 or more decimal numbers.
  example use: 'add 34.1 87.124 -23' or: 'add -s0 22.5 34.666 22.4'
  Operators and signs can be mixed: 234 + -4 - +5 -5 5 - -5 but never'--'.
  Scaling is optional and no truncation is done, unless requested.
BTW, I do prefer the compact OP's coding style where consecutive lines basically do the same with small variation. I guess it's a matter of taste. To give you an example of my own code with similar properties:
Code:
head=...${DIRSTACK[1]#$docpfx} hlen=${#head} tail=${PWD##*/} tlen=${#tail}
((hspace=(fspace=COLUMNS-p1len-extralen-p2len-nwidth-1)-tlen)) ||:
sep=...
if   (( hspace           >hlen     )); then sep=/          # head/tail
elif (( (hlen=hspace-4)  >3        )); then sep=.../       # h.../tail
elif (( (tlen=fspace-6)  >(hlen=0) )); then sfx=...        # ...t...
elif (( (tlen+=${#extra})>0        )); then sfx=... extra= # ...t...
# Given that $COLUMNS>60, the rest is extremely unlikely (>9999999 entries).
elif (( (tlen=0)         >fspace-3 )); then sfx=' [...eXit...]> ' swidth=.
fi
printf -v prompt "%.${hlen}s$sep%.${tlen}s$sfx%${swidth}s" \
  "${head//$'\n'/?}" "${tail//$'\n'/?}" "$prompt1$extra$prompt2"
Sometimes, it's better to factor out repeating parts into a separate function. But I don't feel like every repeating stanza however small deserves a function of itself.

Last edited by shruggy; 11-07-2021 at 02:27 PM.
 
1 members found this post helpful.
Old 11-07-2021, 03:20 PM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,911

Original Poster
Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
I've gone through many transformations in coding style over the years. Most of the functions do similar conditioning of input, but never exactly the same, so one validate_format_input function would only work for parts of the job. Calls to other functions are expensive, so kept to a minimum. Feel free to expand the white space as much as you like. I like it compact so that I can see the important parts of the function all at once. Thanks very much for trying it out and responding.

One thing you'll notice is that there not many if/else phrases. Since most handling involves several options, they are better handle with 'case' as a text-comparison. Also, there are no bashisms in there and no printf usage -simply because it fails when fed really long numbers. That means replacing these things with non-bash solutions. The thing I miss the most with posix-shell is the inabilty to read a variable/string one character at a time. The solutions are not pretty, but work under sveral different shells.

"./iq mul 3 * 3" If you read the help you'll see that '*' is not a valid operator -using it from the command line expands the listing of the contents of the current directory. Use 'x', '.', 'X' or no operator at all. The only time an operator is required is for 'div', since it also handles the 'mod' with the '%' operator, or 'r' as operator for the alternate mod format '16r0.26'. The 'add' function doesn't really need the operator if you enter signed numbers, although it will handle mixed input like: add 3 5 -5 + 3 + -3 - +3 or anything else you throw at it -except --number.

For a bit of fun, try running:
iq epow -e12 0.12 22
iq epow -s12 0.12 22
iq epow -S12 0.12 22
 
Old 11-08-2021, 02:02 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,201

Rep: Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822
there are some shellcheck warnings....
How fast is it? How is it better than bc? Would be nice to add some examples to the documentation.
 
Old 11-08-2021, 09:49 AM   #6
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,669
Blog Entries: 3

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
I tried it, it works fine. Didn't read the docs enough I guess.

Code:
bash ./iq mul 32.3 x 12
387.6

bash ./iq div 12 / 4
3.0

bash ./iq iqhelp pow 
 'pow' raises a decimal number to a simple, complex and/or negative exponent.
  Example usage: 'pow -s6 2.7182 -1.618' for:(2.7182^-1.618)
  Example usage: 'pow -s7 3.14 4.12' for:(3.14^4.12)
  'pow' is expensive -moderate scaling and short exponent fractions help.
  Exponent fractions more than 3 digits are not recommended, as response time
  increases greatly and accuracy diminishes.
  
bash ./iq pow 3 3
27.0

bash ./iq iqhelp exp
 'exp' - 'e^x' - raises Euler's number 'e' to a given power,
  within a limited range, where 0.00000000000001 < x < 3.6 
  Example usage: 'exp -s12 3.6' returns: '36.598234443677'
  Example usage: 'exp -s8 1.134' returns: '3.10806392'
  Accuracy is usually ~12 places, but less with some values of 'x'.
  'exp' uses the back-end functions 'e2x_M' and 'e2x_T' for calculation.
  
bash ./iq exp -s5 23
9744802823.99000
Quote:
How is it better than bc?
Not sure that it is, or better than a python shell. But It does work.
 
Old 11-08-2021, 09:59 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,911

Original Poster
Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
It'll beat bc to the start -unless bc is already cached. iq needs about 6ms to load. For a lot of things iq has a huge disadvantage, but it really shines where lots of calls are being made to external programs in scripts. Like bc, iq can work with very long numbers and not automatically start returning e-notation like awk does. iq does complex/negative powers which bc does not. Especially useful is 'tsst' and it's engine 'cmp3w' which does numeric comparisons of long int/dec numbers.

I will be improving the docs and providing more on github -your questions/comments help drive that. I used to use bc a lot to verify answers until iq got fancier. I change to mostly use the Casio/Keisan online calculator which gives answers up to 135 digits. Recently I even surpassed them with iq's nthrt function and with epow, mentioned above. I forget now what the input was for nthroot, but in the comments inside the epow function, there is a plea for someone to verify iq output from this:
epow -e6 0.0105 ^ 1000000000 = 1.174730e-1978810701
Thats 0.0105 ^1billion
I assume it is correct because keisan agreed with iq up to 10 or 100 million -before giving up- crashed my browser even.

Recently I was testing iq using ksh -it ran twice as fast as under dash (12X faster than bash). In an example 3-node neural-network, running head-to-head against bc/awk, iq (under dash) needed about 7-10 iterations of training before becoming faster than bc/awk. Since the functions stay in memory they avoid the external calls. When I tried iq under ksh, it actually won even in a single iteration. I can't recommend using ksh yet, though -it showed some behaviors that I hadn't accounted for -and possibly a bug in ksh. I will re-visit that later.

I know that the iq syntax varies from both normal calculators and from C function calls. Since it was mostly conceived to be used from scripts, but does not evaluate (grouped/nested) expressions, the syntax needed to be short
and as fast as possible to process. This is why all the inputs and operators need to be spaced -parameters passed to function are easy to work with and need no parsing like: fun(a,b) would. In the example 3-node network mentioned above, the calls to awk looked like this:
hiddensum1=`echo - | awk -v var1="$e" -v var2="$hidden1" '{print (1/(1+(var1 ^ -var2)))}'`
Thats a sigmoid/logistic function, which you can call directly from 'iq+' as 'sigmoid_real' or as 'sigmoid_tanh' in a much faster approximation. The 1st and 2nd derivatives are in there too, as well as a bunch of other NN-related functions. They are mostly trivial since the real work is done by the stuff in 'iq'. Having a look at them may illustrate more of the syntax used by iq.
 
Old 11-08-2021, 10:34 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,201

Rep: Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822
hard to believe it is faster than bc. need to make some controlled measurement for that.
Again, shellcheck reports some problems, for example useless variables. (probably false, but if you are speaking about speed, those are important)
I did not check, but probably
Code:
# instead of
[ "$tstret" = '<' ] && return 0 || return 1 
# this is slightly faster
[ "$tstret" = '<' ]; return $?
Also I think using $( ) and echo is expensive, so:
Code:
func() {
 .....
 echo $result }
var=$(func .... g..)
is slower than:
Code:
func() {
 .....
 RES=$result }
func .... g..;var=$RES
And yes, bash is slow.
 
Old 11-08-2021, 11:09 AM   #9
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,911

Original Poster
Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
func() {
.....
RES=$result }
func .... g..;var=$RES
assigning a variable takes longer than you think,probably. I've tested different ways of doing this and used what worked best. I find echoing results from a function works and reads well. Depending on functions being able to pass variable values back out is iffy. I used to write very sane looking variable declarations with local, but variable scoping is not at all consistent between shells. It was tough to eliminate namespace-pollution, though. You may notice that there are no recursive functions at all. You can nest functions somewhat like this:
$( fun1 $(fun2) ) and both add and mul work with a series of inputs.

About comparing with bc, I'm more concerned about accurate answers than beating some time. I was extremely surprised the first time I saw this actually beat bc and awk in a specific application. Note especially, that the pow function is very expensive and you will not want to wait for answers with an exponent using over 2-3 digits(fractional part).
Anyway, it never occurred to me that this would perform anywhere better than some compiled object where special hardware comes into play. I simply wanted a proof-of-concept that the shell could really do decimal maths, and that one could really create an neural-network with just the shell.

In order to deal with long numbers quickly, I used some unusual techniques -like chunking(cmp3w,add,mul) and, in epow with reduction of a problem to keep only the SigFigs needed to produce the desired precision. As it is, the big slowdown for exponentiation is figuring out how much precision is needed in the intermediate multiplications. epow solves this. intpow overcomes it, too, but with less certainty. At the same time, epow introduces what might be called 'vario-float' -you can have it carry out work at any scale you want and not have to lose any precision.

There on my github, you can also get 'bin4sh' which takes the same liberties and uses its' own free-form method to represent binary numbers -without any hard limits on size -no 32-bit, 64-bit formats there. bin4sh was an idea I had to possibly overcome the exponentiation bottle-neck. But, sorry, if string-length is the problem then representation in binary is not the answer -but the exercise was fun -it actually only uses the shell-native math for loop counters, etc. The actual 'math' is done as a text-problem -similar to hardwired circuits. And my rummaging around on 'Exploring Binary', laid the groundwork for handling pow effectively in decimal system.

I'm running-on, now, when I should be off improving my docs!
 
Old 11-08-2021, 11:32 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,201

Rep: Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822
Quote:
Originally Posted by gnashley View Post
func() {
.....
RES=$result }
func .... g..;var=$RES
assigning a variable takes longer than you think,probably. I've tested different ways of doing this and used what worked best. I find echoing results from a function works and reads well. Depending on functions being able to pass variable values back out is iffy.
Would be nice to see a really accurate measurement. Do not need to think. probably.
 
Old 11-08-2021, 12:27 PM   #11
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,911

Original Poster
Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Near the bottom of iq is a function called tue_nix which I use to measure startup latency like:
time iq tue_nix
When iq was just the '4 maths', it would load and execute that in 3ms - tue_nix is a jab at the German *Tue nichts* -do nothing. Early on I did a lot of experiments -where to start with- anything bash could do, dash did in 1/3 the time. zsh falls in between them and ksh runs twice as fast as dash. Referencing such an empty function takes about 1ms. When working with very long numbers 40-60 digits, I could see that assigning/shuffling variables was sometimes taking ~4ms. I was running 1000 iterations for comparison.

Were it not for my wish to implement a full pow function, iq would only be ~300 lines/code. Some of the functions have been recently rewritten. I'm no fan of one-liners anywhere, but if a section that is similar/duplicated in 3/4 of the functions and is 'tight', then I collapse it to a line or two, so that the 'meat' of the function is easily visible.

About speed, I had an idea once about pre-formatting the functions. The shell strips comments and regularizes white-space, line-endings, etc. With bash you can use declare(IIRC) a function to print out the contents of a function in that format. So, I ran all the functions through and printed them out and ran it that way. And that really went slow. Of course, the shell still goes through the same procedure and re-does the formatting at more expense because the lines of code are now 4-5 times what they were. And while comments get stripped out everywhere, the more there are the longer they take to process. I only recently added the help function -ugly thing that it is. Yes there are umpteen calls to 'echo' but I do have control of the formatting. I do like 'echo' though, hehe -count the shell builtins that iq calls.

Last edited by gnashley; 11-08-2021 at 12:28 PM.
 
Old 11-08-2021, 01:09 PM   #12
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,034

Rep: Reputation: Disabled
Quote:
Originally Posted by gnashley View Post
Yes there are umpteen calls to 'echo' but I do have control of the formatting.
You know that strings in the shell can be multi-line, don't you?
 
Old 11-08-2021, 01:16 PM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,201

Rep: Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822Reputation: 5822
Quote:
Originally Posted by gnashley View Post
Near the bottom of iq is a function called tue_nix which I use to measure startup latency like:
time iq tue_nix
When iq was just the '4 maths', it would load and execute that in 3ms - tue_nix is a jab at the German *Tue nichts* -do nothing. Early on I did a lot of experiments -where to start with- anything bash could do, dash did in 1/3 the time. zsh falls in between them and ksh runs twice as fast as dash. Referencing such an empty function takes about 1ms.
You can definitely measure the execution time of an empty function, but that will say nothing useful. It mostly depends on the cache, ram, load (amongst other things).
Measuring the time of the process creation is not related to the speed of that code at all.
Quote:
Originally Posted by gnashley View Post
When working with very long numbers 40-60 digits, I could see that assigning/shuffling variables was sometimes taking ~4ms. I was running 1000 iterations for comparison.
1000 iteration is probably not enough to measure anything
Code:
A="xxxxxxxx...xxx" (100 x chars, whatever)
f0 () 
{ 
    for i in {1..10000};
    do
        :;
    done
}

f1 () 
{ 
    for i in {1..10000};
    do
        a=$A;
        b=$a;  # you can measure double assignment too
    done
}
f2 () 
{ 
    for i in {1..10000};
    do
        a=$(echo $A);
    done
}

declare -f f0 f1 f2
time f0
real	0m0,041s
user	0m0,037s
sys	0m0,004s
time f1
real	0m0,035s
user	0m0,035s
sys	0m0,000s
time f2
real	0m3,120s
user	0m2,411s
sys	0m1,031s
using $( ) you will still have an assignment (you can't avoid that), just there is a huge overhead of $( echo ).
Quote:
Originally Posted by gnashley View Post
Were it not for my wish to implement a full pow function, iq would only be ~300 lines/code. Some of the functions have been recently rewritten. I'm no fan of one-liners anywhere, but if a section that is similar/duplicated in 3/4 of the functions and is 'tight', then I collapse it to a line or two, so that the 'meat' of the function is easily visible.

About speed, I had an idea once about pre-formatting the functions. The shell strips comments and regularizes white-space, line-endings, etc. With bash you can use declare(IIRC) a function to print out the contents of a function in that format. So, I ran all the functions through and printed them out and ran it that way. And that really went slow. Of course, the shell still goes through the same procedure and re-does the formatting at more expense because the lines of code are now 4-5 times what they were. And while comments get stripped out everywhere, the more there are the longer they take to process. I only recently added the help function -ugly thing that it is. Yes there are umpteen calls to 'echo' but I do have control of the formatting. I do like 'echo' though, hehe -count the shell builtins that iq calls.
So your code is not faster than bc, but probably, sometimes your shell was started quicker than bc. Anyway, we did not really expect that (an interpreted shell code cannot be faster than a compiled binary). And also I think this code can be made faster.

From the other hand this is still a good work.
 
Old 11-09-2021, 10:25 AM   #14
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,034

Rep: Reputation: Disabled
Quote:
Originally Posted by gnashley View Post
in the comments inside the epow function, there is a plea for someone to verify iq output from this:
epow -e6 0.0105 ^ 1000000000 = 1.174730e-1978810701
Yeah, that would be difficult. Does anybody know how to conjure bc/dc into printing the result in scientific notation? I mean it takes six lines on the screen for the exponent of 100 already:
Code:
$ dc -e'2147483647k0.0105 100^p'
.00000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000131501257\
846303455025597532093716748160656467299314561042155052377232536375957\
656844830279020202285498992380899128422453062472473774474555235555032\
17471616562842841313685227078167372383177280426025390625
And for 100000 I get almost 6000 lines of output (the result seems to be the same, but dc wraps lines at 80 characters while bc does it at 79)
Code:
$ dc -e'2147483647k0.0105 100000^p'|wc -l
5798
$ bc <<<'scale=2147483647;0.0105^100000'|wc -l
5883
$ dc -e'2147483647k0.0105 100000^p'|tail -2
412615969068304081823507830992242432013861197503956645959988236427307\
12890625
$ bc <<<'scale=2147483647;0.0105^100000'|tail -2
17688272204013324126159690683040818235078309922424320138611975039566\
4595998823642730712890625
Qalculate! played along up to ten millions, but then gave up
Code:
$ LC_ALL=C qalc 0.0105^10000000
0.0105^10000000 ≈ 9.788122156E-19788108
$ LC_ALL=C qalc 0.0105^100000000
warning: Floating point overflow
warning: Floating point underflow
0.0105^100000000 ≈ 2.974349007E132221929 / 2000.000000^100000000
Though it still correctly calculated the mantissa for 105^100000000:
Code:
$ LC_ALL=C qalc 105^100000000
105^100000000 ≈ 8.072235198E202118929
gawk -M held out until 100 millions
Code:
$ gawk -M 'BEGIN{print 0.0105^100000000}'
8.07224e-197881071
$ gawk -M 'BEGIN{print 0.0105^1000000000}'
0
I haven't tried gawk-mpfr, but would expect it to have the same limits as gawk -M.

Update. Yep, gawk-mpfr is the same, as expected. Linked against MPFR 3.1.6. The gawk -M above gives the same results with MPFR 4.0.2 as well.
Code:
$ gawk -l mpfr 'BEGIN{print mpfr_pow(0.0105,1000000000)}'
0.0000000000000000E-1
I tried Perl as well, but it's excruciatingly slow. When using the default Math::BigInt::Calc library, it gets almost unbearable already at the exponent of 100000.
Code:
$ time perl -Mbignum -le 'print 0.0105->bpow(100000,10)->bnstr'
8.509557825e-197882

real	0m41,720s
user	0m41,575s
sys	0m0,008s
FastCalc is more or less the same. GMP is better, but I lost my patience after 100 millions as well:
Code:
$ time perl -M"bignum lib=>'GMP'" -le 'print 0.0105->bpow(100000000,10)->bnstr'
8.072235198e-197881071

real	5m39,063s
user	5m36,866s
sys	0m1,130s
I didn't test Pari.

For Python, I haven't tried mpmath yet (done, see below). decimal chokes at one million already:
Code:
>>> from decimal import *
>>> getcontext().prec=10
>>> Decimal(0.0105)**100000
Decimal('8.509557825E-197882')
>>> Decimal(0.0105)**1000000
Decimal('0E-1000008')
Update 2. At last! mpmath passes the test with flying colors
Code:
$ python3 -c 'import mpmath;print(mpmath.mpf(0.0105)**1000000000)'
1.1747301080866e-1978810701

Last edited by shruggy; 11-10-2021 at 12:25 PM.
 
Old 11-10-2021, 08:56 AM   #15
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,911

Original Poster
Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Please get updated iq/iq+
All issues from yesterday should be fixed. I'll address the comments later.
 
  


Reply

Tags
ai, calculator, math, neural-network, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: Calculator N+ is an open source scientific calculator for your smartphone LXer Syndicated Linux News 0 11-27-2019 07:21 AM
70+ bug-fixes fro WindowMaker [testers wanted] gnashley Slackware 7 12-12-2009 09:38 AM
VirtualBox USB config test script; testers wanted catkin Linux - Virtualization and Cloud 42 10-23-2009 06:22 AM
LXer: SwapBoost v0.1alpha - early testers wanted LXer Syndicated Linux News 0 07-08-2007 10:46 PM
Beta testers wanted for new distro (IBLS) ico2 Linux - Distributions 4 12-31-2005 08:18 AM

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

All times are GMT -5. The time now is 04:25 AM.

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