Article 5ABBP [ANNNOUNCE] bin4sh - a command-line binary calculator/convertor written in posix-shell

[ANNNOUNCE] bin4sh - a command-line binary calculator/convertor written in posix-shell

by
gnashley
from LinuxQuestions.org on (#5ABBP)
There's a bit of a story behind this, but on this Most Propitious Day, all the ducks lined up and are ready to march. The date, version number, file size and input limit were all screaming to be released. Something only a Slacker could understand.

Here's a comment block from the attached shell script, which highlights the most unusual feature of bin4sh:
Code:# Most other bin_to_dec convertors will output -1234.386962890625 when you input: -10011010010.011000110001
# bin4sh returns: -1234.387, which matches the original input to dec_to_bin. My son, who is a Math Whiz,
# points out that -1234.386962890625 is technically 'correct'. I say: bin4sh's bin_to_dec returns the same
# degree of precision or irrationality as the original decimal input. It does this by smart-rounding the
# fraction at the furthest-right decimal place where accuracy is possible. The last digit of the
# output is not actually rounded, it is the last digit which is guaranteed to be correct. This feature
# allows bin4sh's bin_to_dec to output answers which look correct to the math layman. Using the 'raw' option
# allows the Math Whiz to have the 'correct' output. To each, his own.This program is little brother to a more extensive project -fp4sh- which I started this Spring during the first Corona lockdown. Typically, I had started out to program neural networks in bash. I only found one partial, buggy implementation on-line. A 75 line bash script which called the 'bc' calculator about 35 times, and 'awk' to implement the sigmoid function and its' 1st derivative -nearly essential for any NN.

I thought I'd -hehe- quickly put together some shell functions to perform fixed-precision math, floating-point or something to at least replace bc with. I couldn't then dream that I'd even implement the sigmoid function and many others before I finished. And that calculator would actually arbitrary precision to 10,000 places or more. And that it would use absolutely no external programs to do it in ~1000 lines of code.

Hold on, I'm getting there. Near the end when I had gotten exponentiation working fully, with negative and fractional exponents, I could see that exponents with more than 2-digit fractions were going to be extremely slow -like the inverse of the speed of light. I wondered if I might implement exponentiation in binary, where I could leverage the power of bit-shifting.

bin4sh is the result, although the goal of doing exponents in binary is still not done, and surely will be no faster. My math credentials are next to NULL -2 years of algebra in High School 45+ years ago. No Geometry or other. I really jogged my brain and learned a bunch along the way. And where fp4sh was a slog-fight, writing bin4sh has been great fun.

bin4sh is possibly still a CodeName, so feel free to suggest something. I've thought of (M)ath_leet, perhaps more correctly |M|ath_leet, math_1337 or bin_1337 -keeping in mind that in German 'bin' means 'I am'.
Attached Files
txt.gifbin4sh_1.337.sh.txt (35.5 KB)
latest?d=yIl2AUoC8zA latest?i=3HgKM3Yctc0:Qwy6VnP3lKA:F7zBnMy latest?i=3HgKM3Yctc0:Qwy6VnP3lKA:V_sGLiP latest?d=qj6IDK7rITs latest?i=3HgKM3Yctc0:Qwy6VnP3lKA:gIN9vFw3HgKM3Yctc0
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments