It's imprecise and much more clunky to write. bc exists for decades already and has arbitrary precision.
Show HN: Shellmath: Floating-point arithmetic directly in bash
11–19 of 19 posts
Re: Show HN: Shellmath: Floating-point arithmetic directly in bash
#12Why should I use `_shellmath_divide 1 $zero_factorial` over `bc 1 / $zero_factorial`? It's imprecise and much more clunky to write. bc exists for decades already and has arbitrary precision.
Also, `bc 1 / 2` causes an error (at least in bash 3.2 using bc version 1.06) because file 1 doesn't exist, so there's also that for a reason
Re: Show HN: Shellmath: Floating-point arithmetic directly in bash
#13Why should I use `_shellmath_divide 1 $zero_factorial` over `bc 1 / $zero_factorial`? It's imprecise and much more clunky to write. bc exists for decades already and has arbitrary precision.
I think shellmath exists because it could be done, rather than because it needed to be done. Also, `bc 1 / 2` causes an error (at least in bash 3.2 using bc version 1.06) because file 1 doesn't exist, so there's also that for a reason
Re: Show HN: Shellmath: Floating-point arithmetic directly in bash
#14Re: Show HN: Shellmath: Floating-point arithmetic directly in bash
#15Hello, all! I'm proud to present shellmath, a floating-point calculator written entirely in bash. Shellmath does not call out to any external programs -- calculators, text manipulators, or anything else -- and achieves execution speeds competitive with GNU awk and bc. You can do floating-point math directly in the shell, and now you have a ready-made family of routines for doing so! For skeptics ;-) I've included a d…
This is awful¹, I love it! Thumbing through the code made me wonder what the zsh(my normal shell) implementation feels like, and I think it is fair to say your code is easier to wrap your head around than the documentation for floats in zsh. FWIW - and given that you've only shown Windows timings - on my Linux box hyperfine shows: Summary './faster_e_demo.sh 15' ran 1.78 ± 0.16 times faster than './slower_e_demo.sh 1…
Re: Show HN: Shellmath: Floating-point arithmetic directly in bash
#16I guess I don't expect my shell to do much, I reach for numpy or something when I want fancier math.
Not to knock, but what's the use case? I think I prefer bc - "10/(200.7-73.6)" is easy to look at, I don't mind the extra dependency for that.
Actually on that note what I do need to find is a proper calculator on Android, what I have found are computer algebra systems with arcane learning curves. Its surprising how bad the default calc is and nobody had made a popular good alt...
Re: Show HN: Shellmath: Floating-point arithmetic directly in bash
#17> Ad astra per aspera. And yet reliance on Bash extensions rather than doing it in pure POSIX shell. ;) A quick skim suggests the biggest convenience is using local variables to make recursion easier. But it's fairly easy to implement push and pop routines to turn global variables into a stack. You could even name push "local" and keep much the same syntax. Better would be an indirect call function which saves and re…
Hmm that sounds fair, but if you can't do something simple like get bc why would you expect to be able to get this?
Re: Show HN: Shellmath: Floating-point arithmetic directly in bash
#18> Ad astra per aspera. And yet reliance on Bash extensions rather than doing it in pure POSIX shell. ;) A quick skim suggests the biggest convenience is using local variables to make recursion easier. But it's fairly easy to implement push and pop routines to turn global variables into a stack. You could even name push "local" and keep much the same syntax. Better would be an indirect call function which saves and re…
Re: Show HN: Shellmath: Floating-point arithmetic directly in bash
#19Very nice writeup! Related: I got a couple mails about raytracers in pure bash. It looks like they use fixed point arithmetic in bash rather than floating point, which works well: http://www.oilshell.org/blog/2021/01/audio-and-graphics.html... https://github.com/aneeshdurg/bash-raytracer https://www.bouledef.eu/~tleguern/articles/shell-fixedpoint/